blob: 07201341a86174df948941b681289f1393ade710 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
421 }
422 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
423 {
424 // Allow mappings defined with ":lmap".
425 --no_mapping;
426 --allow_keys;
427 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100428 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000429 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 langmap_active = TRUE;
432 }
433#ifdef HAVE_INPUT_METHOD
434 save_smd = p_smd;
435 p_smd = FALSE; // Don't let the IM code show the mode here
436 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
437 im_set_active(TRUE);
438#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000440 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100441 MAY_WANT_TO_LOG_THIS;
442
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 // Disable bracketed paste and modifyOtherKeys here, we won't
444 // recognize the escape sequences with 'esckeys' off.
445 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000446 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448
449 *cp = plain_vgetc();
450
Bram Moolenaar24959102022-05-07 20:01:16 +0100451 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100453 MAY_WANT_TO_LOG_THIS;
454
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000456 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000457 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 }
459
460 if (langmap_active)
461 {
462 // Undo the decrement done above
463 ++no_mapping;
464 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100465 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000466 }
467#ifdef HAVE_INPUT_METHOD
468 if (lang)
469 {
470 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
471 im_save_status(&curbuf->b_p_iminsert);
472 im_set_active(FALSE);
473 }
474 p_smd = save_smd;
475#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100476 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000477 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478
479 if (!lit)
480 {
481#ifdef FEAT_DIGRAPHS
482 // Typing CTRL-K gets a digraph.
483 if (*cp == Ctrl_K
484 && ((nv_cmds[idx].cmd_flags & NV_LANG)
485 || cp == &cap->extra_char)
486 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
487 {
488 c = get_digraph(FALSE);
489 if (c > 0)
490 {
491 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000492 // Guessing how to update showcmd here...
493 del_from_showcmd(3);
494 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 }
496 }
497#endif
498
499 // adjust chars > 127, except after "tTfFr" commands
500 LANGMAP_ADJUST(*cp, !lang);
501#ifdef FEAT_RIGHTLEFT
502 // adjust Hebrew mapped char
503 if (p_hkmap && lang && KeyTyped)
504 *cp = hkmap(*cp);
505#endif
506 }
507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000508 // When the next character is CTRL-\ a following CTRL-N means the
509 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000510 if (cp == &cap->extra_char
511 && cap->nchar == Ctrl_BSL
512 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
513 {
514 cap->cmdchar = Ctrl_BSL;
515 cap->nchar = cap->extra_char;
516 idx = find_command(cap->cmdchar);
517 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000518 else if ((cap->nchar == 'n' || cap->nchar == 'N')
519 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
546 // When getting a text character and the next character is a
547 // multi-byte character, it could be a composing character.
548 // However, don't wait for it to arrive. Also, do enable mapping,
549 // because if it's put back with vungetc() it's too late to apply
550 // mapping.
551 --no_mapping;
552 while (enc_utf8 && lang && (c = vpeekc()) > 0
553 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
554 {
555 c = plain_vgetc();
556 if (!utf_iscomposing(c))
557 {
558 vungetc(c); // it wasn't, put it back
559 break;
560 }
561 else if (cap->ncharC1 == 0)
562 cap->ncharC1 = c;
563 else
564 cap->ncharC2 = c;
565 }
566 ++no_mapping;
567 }
568 --no_mapping;
569 --allow_keys;
570
571 return idx;
572}
573
574/*
575 * Returns TRUE if after processing a normal mode command, need to wait for a
576 * moment when a message is displayed that will be overwritten by the mode
577 * message.
578 */
579 static int
580normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
581{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000582 // In Visual mode and with "^O" in Insert mode, a short message will be
583 // overwritten by the mode message. Wait a bit, until a key is hit.
584 // In Visual mode, it's more important to keep the Visual area updated
585 // than keeping a message (e.g. from a /pat search).
586 // Only do this if the command was typed, not from a mapping.
587 // Don't wait when emsg_silent is non-zero.
588 // Also wait a bit after an error message, e.g. for "^O:".
589 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000590 return ( ((p_smd
591 && msg_silent == 0
592 && (restart_edit != 0
593 || (VIsual_active
594 && old_pos->lnum == curwin->w_cursor.lnum
595 && old_pos->col == curwin->w_cursor.col)
596 )
597 && (clear_cmdline
598 || redraw_cmdline)
599 && (msg_didout || (msg_didany && msg_scroll))
600 && !msg_nowait
601 && KeyTyped)
602 || (restart_edit != 0
603 && !VIsual_active
604 && (msg_scroll
605 || emsg_on_display)))
606 && cap->oap->regname == 0
607 && !(cap->retval & CA_COMMAND_BUSY)
608 && stuff_empty()
609 && typebuf_typed()
610 && emsg_silent == 0
611 && !in_assert_fails
612 && !did_wait_return
613 && cap->oap->op_type == OP_NOP);
614}
615
616/*
617 * After processing a normal mode command, wait for a moment when a message is
618 * displayed that will be overwritten by the mode message.
619 */
620 static void
621normal_cmd_wait_for_msg(void)
622{
623 int save_State = State;
624
625 // Draw the cursor with the right shape here
626 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100627 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000628
629 // If need to redraw, and there is a "keep_msg", redraw before the
630 // delay
631 if (must_redraw && keep_msg != NULL && !emsg_on_display)
632 {
633 char_u *kmsg;
634
635 kmsg = keep_msg;
636 keep_msg = NULL;
637 // Showmode() will clear keep_msg, but we want to use it anyway.
638 // First update w_topline.
639 setcursor();
640 update_screen(0);
641 // now reset it, otherwise it's put in the history again
642 keep_msg = kmsg;
643
644 kmsg = vim_strsave(keep_msg);
645 if (kmsg != NULL)
646 {
647 msg_attr((char *)kmsg, keep_msg_attr);
648 vim_free(kmsg);
649 }
650 }
651 setcursor();
652#ifdef CURSOR_SHAPE
653 ui_cursor_shape(); // may show different cursor shape
654#endif
655 cursor_on();
656 out_flush();
657 if (msg_scroll || emsg_on_display)
658 ui_delay(1003L, TRUE); // wait at least one second
659 ui_delay(3003L, FALSE); // wait up to three seconds
660 State = save_State;
661
662 msg_scroll = FALSE;
663 emsg_on_display = FALSE;
664}
665
666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * Execute a command in Normal mode.
668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670normal_cmd(
671 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000678 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 static int old_mapped_len = 0;
682 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000683 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200684 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaara80faa82020-04-12 19:37:17 +0200686 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // Use a count remembered from before entering an operator. After typing
690 // "3d" we return from normal_cmd() and come back here, the "3" is
691 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ca.opcount = opcount;
693
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000694 // If there is an operator pending, then the command we take this time
695 // will terminate it. Finish_op tells us to finish the operation before
696 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef CURSOR_SHAPE
698 c = finish_op;
699#endif
700 finish_op = (oap->op_type != OP_NOP);
701#ifdef CURSOR_SHAPE
702 if (finish_op != c)
703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705# ifdef FEAT_MOUSESHAPE
706 update_mouseshape(-1);
707# endif
708 }
709#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100710 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // When not finishing an operator and no register name typed, reset the
713 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000717#ifdef FEAT_EVAL
718 set_prevcount = TRUE;
719#endif
720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // Restore counts from before receiving K_CURSORHOLD. This means after
723 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
724 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000725 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
726 {
727 ca.opcount = oap->prev_opcount;
728 ca.count0 = oap->prev_count0;
729 oap->prev_opcount = 0;
730 oap->prev_count0 = 0;
731 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar24959102022-05-07 20:01:16 +0100735 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#endif
739
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100740#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100741 // Set v:count here, when called from main() and not a stuffed
742 // command, so that v:count can be used in an expression mapping
743 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100744 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100745 set_vcount_ca(&ca, &set_prevcount);
746#endif
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
749 * Get the command character from the user.
750 */
751 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100752 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000754 // If a mapping was started in Visual or Select mode, remember the length
755 // of the mapping. This is used below to not return to Insert mode for as
756 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (restart_edit == 0)
758 old_mapped_len = 0;
759 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000760 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 old_mapped_len = typebuf_maplen();
762
763 if (c == NUL)
764 c = K_ZERO;
765
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000766 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (VIsual_active
768 && VIsual_select
769 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
770 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000771 int len;
772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100773 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
774 // 'insertmode' is set) fake a "d"elete command, Insert mode will
775 // restart automatically.
776 // Insert the typed character in the typeahead buffer, so that it can
777 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000778 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
779
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000780 // When recording and gotchars() was called the character will be
781 // recorded again, remove the previous recording.
782 if (KeyTyped)
783 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000784
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000785 if (restart_edit != 0)
786 c = 'd';
787 else
788 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 msg_nowait = TRUE; // don't delay going to insert mode
790 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000793 // If the window was made so small that nothing shows, make it at least one
794 // line and one column when typing a command.
795 if (KeyTyped && !KeyStuffed)
796 win_ensure_size();
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000800 // Get the command count
801 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
802 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000804 // Find the command character in the table of commands.
805 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (ctrl_w)
807 {
808 ca.nchar = c;
809 ca.cmdchar = Ctrl_W;
810 }
811 else
812 ca.cmdchar = c;
813 idx = find_command(ca.cmdchar);
814 if (idx < 0)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
818 goto normal_end;
819 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000820
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100822 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000823 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000825 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (VIsual_active)
827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (km_stopsel
830 && (nv_cmds[idx].cmd_flags & NV_STS)
831 && !(mod_mask & MOD_MASK_SHIFT))
832 {
833 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100834 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (km_startsel)
839 {
840 if (nv_cmds[idx].cmd_flags & NV_SS)
841 {
842 unshift_special(&ca);
843 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 if (idx < 0)
845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000847 clearopbeep(oap);
848 goto normal_end;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
852 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857#ifdef FEAT_RIGHTLEFT
858 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
859 && (nv_cmds[idx].cmd_flags & NV_RL))
860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 // Invert horizontal movements and operations. Only when typed by the
862 // user directly, not when the result of a mapping or "x" translated
863 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 switch (ca.cmdchar)
865 {
866 case 'l': ca.cmdchar = 'h'; break;
867 case K_RIGHT: ca.cmdchar = K_LEFT; break;
868 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
869 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
870 case 'h': ca.cmdchar = 'l'; break;
871 case K_LEFT: ca.cmdchar = K_RIGHT; break;
872 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
873 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
874 case '>': ca.cmdchar = '<'; break;
875 case '<': ca.cmdchar = '>'; break;
876 }
877 idx = find_command(ca.cmdchar);
878 }
879#endif
880
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000881 // Get additional characters if we need them.
882 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
883 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000885 // Flush the showcmd characters onto the screen so we can see them while
886 // the command is being executed. Only do this when the shown command was
887 // actually displayed, otherwise this will slow down a lot when executing
888 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (need_flushbuf)
890 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100891
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000892 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200893 {
894 if (ex_normal_busy)
895 did_cursorhold = save_did_cursorhold;
896 else
897 did_cursorhold = FALSE;
898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
zeertzjqf86dea82023-03-05 21:15:06 +0000902 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
904 clearop(oap);
905 if (restart_edit == 0 && goto_im())
906 restart_edit = 'a';
907 goto normal_end;
908 }
909
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000910 if (ca.cmdchar != K_IGNORE)
911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000913 msg_col = 0;
914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100916 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When 'keymodel' contains "startsel" some keys start Select/Visual
919 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (!VIsual_active && km_startsel)
921 {
922 if (nv_cmds[idx].cmd_flags & NV_SS)
923 {
924 start_selection();
925 unshift_special(&ca);
926 idx = find_command(ca.cmdchar);
927 }
928 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
929 && (mod_mask & MOD_MASK_SHIFT))
930 {
931 start_selection();
932 mod_mask &= ~MOD_MASK_SHIFT;
933 }
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000936 // Execute the command!
937 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ca.arg = nv_cmds[idx].cmd_arg;
939 (nv_cmds[idx].cmd_func)(&ca);
940
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000941 // If we didn't start or finish an operator, reset oap->regname, unless we
942 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (!finish_op
944 && !oap->op_type
945 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
946 {
947 clearop(oap);
948#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200949 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951 }
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // Get the length of mapped chars again after typing a count, second
954 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000955 if (old_mapped_len > 0)
956 old_mapped_len = typebuf_maplen();
957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000958 // If an operation is pending, handle it. But not for K_IGNORE or
959 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200960 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100961 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000963 // Wait for a moment when a message is displayed that will be overwritten
964 // by the mode message.
965 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
966 normal_cmd_wait_for_msg();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969normal_end:
970
971 msg_nowait = FALSE;
972
Bram Moolenaarcc613032020-06-07 21:31:18 +0200973#ifdef FEAT_EVAL
974 if (finish_op)
975 reset_reg_var();
976#endif
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000979 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980#endif
zeertzjq73916ba2023-04-26 16:50:19 +0100981 if (oap->op_type == OP_NOP)
982 {
983 // Reset finish_op, in case it was set
984 finish_op = FALSE;
985 may_trigger_modechanged();
986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100988 // Redraw the cursor with another shape, if we were in Operator-pending
989 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +0000990 if (prev_finish_op || ca.cmdchar == 'r'
991 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100993 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994# ifdef FEAT_MOUSESHAPE
995 update_mouseshape(-1);
996# endif
997 }
998#endif
999
Bram Moolenaara983fe92008-07-31 20:04:27 +00001000 if (oap->op_type == OP_NOP && oap->regname == 0
zeertzjq73916ba2023-04-26 16:50:19 +01001001 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001004 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 vim_free(ca.searchbuf);
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (has_mbyte)
1008 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (curwin->w_p_scb && toplevel)
1011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 do_check_scrollbind(TRUE);
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar860cae12010-06-05 23:22:07 +02001016 if (curwin->w_p_crb && toplevel)
1017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001019 do_check_cursorbind();
1020 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001021
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001022#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001023 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001024 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001025 restart_edit = 0;
1026#endif
1027
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001028 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1029 // if still inside a mapping that started in Visual mode).
1030 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1033 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && !(ca.retval & CA_COMMAND_BUSY)
1035 && stuff_empty()
1036 && oap->regname == 0)
1037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (restart_VIsual_select == 1)
1039 {
1040 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001041 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 showmode();
1043 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001044 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001046 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 (void)edit(restart_edit, FALSE, 1L);
1048 }
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (restart_VIsual_select == 2)
1051 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 opcount = ca.opcount;
1055}
1056
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001057#ifdef FEAT_EVAL
1058/*
1059 * Set v:count and v:count1 according to "cap".
1060 * Set v:prevcount only when "set_prevcount" is TRUE.
1061 */
1062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064{
1065 long count = cap->count0;
1066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068 if (cap->opcount != 0)
1069 count = cap->opcount * (count == 0 ? 1 : count);
1070 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001072}
1073#endif
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001076 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * if not.
1078 */
1079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 static int did_check = FALSE;
1083
1084 if (full_screen)
1085 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001086 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001087 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 did_check = TRUE;
1089 }
1090}
1091
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001092#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1093/*
1094 * Call yank_do_autocmd() for "regname".
1095 */
1096 static void
1097call_yank_do_autocmd(int regname)
1098{
1099 oparg_T oa;
1100 yankreg_T *reg;
1101
1102 clear_oparg(&oa);
1103 oa.regname = regname;
1104 oa.op_type = OP_YANK;
1105 oa.is_VIsual = TRUE;
1106 reg = get_register(regname, TRUE);
1107 yank_do_autocmd(&oa, reg);
1108 free_register(reg);
1109}
1110#endif
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001113 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114 * This function or the next should ALWAYS be called to end Visual mode, except
1115 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
1117 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001118end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001119{
1120 end_visual_mode_keep_button();
1121 reset_held_button();
1122}
1123
1124 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001125end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001128 // If we are using the clipboard, then remember what was selected in case
1129 // we need to paste it somewhere while we still own the selection.
1130 // Only do this when the clipboard is already owned. Don't want to grab
1131 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (clip_star.available && clip_star.owned)
1133 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001134
1135# if defined(FEAT_EVAL)
1136 // Emit a TextYankPost for the automatic copy of the selection into the
1137 // star and/or plus register.
1138 if (has_textyankpost())
1139 {
1140 if (clip_isautosel_star())
1141 call_yank_do_autocmd('*');
1142 if (clip_isautosel_plus())
1143 call_yank_do_autocmd('+');
1144 }
1145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#endif
1147
1148 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 setmouse();
1150 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001152 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001153 curbuf->b_visual.vi_mode = VIsual_mode;
1154 curbuf->b_visual.vi_start = VIsual;
1155 curbuf->b_visual.vi_end = curwin->w_cursor;
1156 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#ifdef FEAT_EVAL
1158 curbuf->b_visual_mode_eval = VIsual_mode;
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (!virtual_active())
1161 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001162 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001164 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001165 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166}
1167
1168/*
1169 * Reset VIsual_active and VIsual_reselect.
1170 */
1171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001172reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
1174 if (VIsual_active)
1175 {
1176 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001177 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 VIsual_reselect = FALSE;
1180}
1181
1182/*
1183 * Reset VIsual_active and VIsual_reselect if it's set.
1184 */
1185 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001186reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 if (VIsual_active)
1189 {
1190 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001191 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 VIsual_reselect = FALSE;
1193 }
1194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001196 void
1197restore_visual_mode(void)
1198{
1199 if (VIsual_mode_orig != NUL)
1200 {
1201 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1202 VIsual_mode_orig = NUL;
1203 }
1204}
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206/*
1207 * Check for a balloon-eval special item to include when searching for an
1208 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1209 * Returns TRUE if the character at "*ptr" should be included.
1210 * "dir" is FORWARD or BACKWARD, the direction of searching.
1211 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1212 * "bnp" points to a counter for square brackets.
1213 */
1214 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001215find_is_eval_item(
1216 char_u *ptr,
1217 int *colp,
1218 int *bnp,
1219 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001221 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1223 ++*bnp;
1224 if (*bnp > 0)
1225 {
1226 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1227 --*bnp;
1228 return TRUE;
1229 }
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (*ptr == '.')
1233 return TRUE;
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1237 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1238 {
1239 *colp += dir;
1240 return TRUE;
1241 }
1242 return FALSE;
1243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245/*
1246 * Find the identifier under or to the right of the cursor.
1247 * "find_type" can have one of three values:
1248 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001249 * FIND_STRING: find any non-white text
1250 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001251 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 *
1253 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001254 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * This doesn't match the real Vi but I like it a little better and it
1258 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * When FIND_IDENT isn't defined, we backup until a blank.
1261 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001262 * Returns the length of the text, or zero if no text is found.
1263 * If text is found, a pointer to the text is put in "*text". This
1264 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 */
1266 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001270 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273/*
1274 * Like find_ident_under_cursor(), but for any window and any position.
1275 * However: Uses 'iskeyword' from the current window!.
1276 */
1277 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001278find_ident_at_pos(
1279 win_T *wp,
1280 linenr_T lnum,
1281 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001282 char_u **text,
1283 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001284 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001287 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 int this_class = 0;
1290 int prev_class;
1291 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001292 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001294 // if i == 0: try to find an identifier
1295 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1297 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1298 {
1299 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001300 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 */
1302 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (has_mbyte)
1304 {
1305 while (ptr[col] != NUL)
1306 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001307 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 this_class = mb_get_class(ptr + col);
1311 if (this_class != 0 && (i == 1 || this_class != 1))
1312 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001313 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001318 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 )
1321 ++col;
1322
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001327 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (has_mbyte)
1330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1333 this_class = mb_get_class((char_u *)"a");
1334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001336 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1339 prev_class = mb_get_class(ptr + prevcol);
1340 if (this_class != prev_class
1341 && (i == 0
1342 || prev_class == 0
1343 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 && (!(find_type & FIND_EVAL)
1345 || prevcol == 0
1346 || !find_is_eval_item(ptr + prevcol, &prevcol,
1347 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 )
1349 break;
1350 col = prevcol;
1351 }
1352
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001353 // If we don't want just any old text, or we've found an
1354 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (this_class > 2)
1356 this_class = 2;
1357 if (!(find_type & FIND_STRING) || this_class == 2)
1358 break;
1359 }
1360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 while (col > 0
1363 && ((i == 0
1364 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001365 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 && (!(find_type & FIND_IDENT)
1367 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 || ((find_type & FIND_EVAL)
1369 && col > 1
1370 && find_is_eval_item(ptr + col - 1, &col,
1371 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ))
1373 --col;
1374
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001375 // If we don't want just any old text, or we've found an
1376 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1378 break;
1379 }
1380 }
1381
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001382 if (ptr[col] == NUL || (i == 0
1383 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 if ((find_type & FIND_NOERROR) == 0)
1387 {
1388 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001389 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001390 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001391 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 return 0;
1394 }
1395 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001396 *text = ptr;
1397 if (textcol != NULL)
1398 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001401 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 bn = 0;
1404 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (has_mbyte)
1407 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001408 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 this_class = mb_get_class(ptr);
1410 while (ptr[col] != NUL
1411 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1412 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 || ((find_type & FIND_EVAL)
1414 && col <= (int)startcol
1415 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001417 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001421 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 || ((find_type & FIND_EVAL)
1423 && col <= (int)startcol
1424 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 return col;
1429}
1430
1431/*
1432 * Prepare for redo of a normal command.
1433 */
1434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001435prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 prep_redo(cap->oap->regname, cap->count0,
1438 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1439}
1440
1441/*
1442 * Prepare for redo of any command.
1443 * Note that only the last argument can be a multi-byte char.
1444 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001446prep_redo(
1447 int regname,
1448 long num,
1449 int cmd1,
1450 int cmd2,
1451 int cmd3,
1452 int cmd4,
1453 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001455 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1456}
1457
1458/*
1459 * Prepare for redo of any command with extra count after "cmd2".
1460 */
1461 void
1462prep_redo_num2(
1463 int regname,
1464 long num1,
1465 int cmd1,
1466 int cmd2,
1467 long num2,
1468 int cmd3,
1469 int cmd4,
1470 int cmd5)
1471{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001473
1474#ifdef FEAT_EVAL
1475 // Put info about a mapping in the redo buffer, so that "." will use the
1476 // same script context.
1477 may_add_last_used_map_to_redobuff();
1478#endif
1479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001480 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 AppendCharToRedobuff('"');
1483 AppendCharToRedobuff(regname);
1484 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001485 if (num1 != 0)
1486 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (cmd1 != NUL)
1488 AppendCharToRedobuff(cmd1);
1489 if (cmd2 != NUL)
1490 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001491 if (num2 != 0)
1492 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 if (cmd3 != NUL)
1494 AppendCharToRedobuff(cmd3);
1495 if (cmd4 != NUL)
1496 AppendCharToRedobuff(cmd4);
1497 if (cmd5 != NUL)
1498 AppendCharToRedobuff(cmd5);
1499}
1500
1501/*
zeertzjq73916ba2023-04-26 16:50:19 +01001502 * Check for operator active and clear it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 *
zeertzjq73916ba2023-04-26 16:50:19 +01001504 * Beep and return TRUE if an operator was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
1506 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001507checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 if (oap->op_type == OP_NOP)
1510 return FALSE;
1511 clearopbeep(oap);
1512 return TRUE;
1513}
1514
1515/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001516 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 *
zeertzjq73916ba2023-04-26 16:50:19 +01001518 * Beep and return TRUE if an operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 */
1520 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001521checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001523 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 return FALSE;
1525 clearopbeep(oap);
1526 return TRUE;
1527}
1528
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001529 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001530clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 oap->op_type = OP_NOP;
1533 oap->regname = 0;
1534 oap->motion_force = NUL;
1535 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001536 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537}
1538
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001539 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001540clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 clearop(oap);
1543 beep_flush();
1544}
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546/*
1547 * Remove the shift modifier from a special key.
1548 */
1549 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001550unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 switch (cap->cmdchar)
1553 {
1554 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1555 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1556 case K_S_UP: cap->cmdchar = K_UP; break;
1557 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1558 case K_S_HOME: cap->cmdchar = K_HOME; break;
1559 case K_S_END: cap->cmdchar = K_END; break;
1560 }
1561 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001564/*
1565 * If the mode is currently displayed clear the command line or update the
1566 * command displayed.
1567 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001569may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001570{
1571 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001572 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001573 else
1574 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001575}
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577/*
1578 * Routines for displaying a partly typed command
1579 */
1580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582static int showcmd_is_clear = TRUE;
1583static int showcmd_visual = FALSE;
1584
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001585static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001588clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 if (!p_sc)
1591 return;
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (VIsual_active && !char_avail())
1594 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001595 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 long lines;
1597 colnr_T leftcol, rightcol;
1598 linenr_T top, bot;
1599
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001600 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001601 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 top = VIsual.lnum;
1604 bot = curwin->w_cursor.lnum;
1605 }
1606 else
1607 {
1608 top = curwin->w_cursor.lnum;
1609 bot = VIsual.lnum;
1610 }
1611# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001612 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001613 (void)hasFolding(top, &top, NULL);
1614 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615# endif
1616 lines = bot - top + 1;
1617
1618 if (VIsual_mode == Ctrl_V)
1619 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001620# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001622 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001625 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001626 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001629# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001630 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001631 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1634 (long)(rightcol - leftcol + 1));
1635 }
1636 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1637 sprintf((char *)showcmd_buf, "%ld", lines);
1638 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001639 {
1640 char_u *s, *e;
1641 int l;
1642 int bytes = 0;
1643 int chars = 0;
1644
1645 if (cursor_bot)
1646 {
1647 s = ml_get_pos(&VIsual);
1648 e = ml_get_cursor();
1649 }
1650 else
1651 {
1652 s = ml_get_cursor();
1653 e = ml_get_pos(&VIsual);
1654 }
1655 while ((*p_sel != 'e') ? s <= e : s < e)
1656 {
1657 l = (*mb_ptr2len)(s);
1658 if (l == 0)
1659 {
1660 ++bytes;
1661 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001662 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001663 }
1664 bytes += l;
1665 ++chars;
1666 s += l;
1667 }
1668 if (bytes == chars)
1669 sprintf((char *)showcmd_buf, "%d", chars);
1670 else
1671 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1672 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001673 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 showcmd_visual = TRUE;
1675 }
1676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
1678 showcmd_buf[0] = NUL;
1679 showcmd_visual = FALSE;
1680
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001681 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (showcmd_is_clear)
1683 return;
1684 }
1685
1686 display_showcmd();
1687}
1688
1689/*
1690 * Add 'c' to string of shown command chars.
1691 * Return TRUE if output has been written (and setcursor() has been called).
1692 */
1693 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001694add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 char_u *p;
1697 int old_len;
1698 int extra_len;
1699 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 int i;
1701 static int ignore[] =
1702 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001703#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1705 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001706#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001707 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001708 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1710 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001711 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001713 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 0
1715 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar09df3122006-01-23 22:23:09 +00001717 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 return FALSE;
1719
1720 if (showcmd_visual)
1721 {
1722 showcmd_buf[0] = NUL;
1723 showcmd_visual = FALSE;
1724 }
1725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001726 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (IS_SPECIAL(c))
1728 for (i = 0; ignore[i] != 0; ++i)
1729 if (ignore[i] == c)
1730 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001733 if (*p == ' ')
1734 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 old_len = (int)STRLEN(showcmd_buf);
1736 extra_len = (int)STRLEN(p);
1737 overflow = old_len + extra_len - SHOWCMD_COLS;
1738 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001739 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1740 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 STRCAT(showcmd_buf, p);
1742
1743 if (char_avail())
1744 return FALSE;
1745
1746 display_showcmd();
1747
1748 return TRUE;
1749}
1750
1751 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001752add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 if (!add_to_showcmd(c))
1755 setcursor();
1756}
1757
1758/*
1759 * Delete 'len' characters from the end of the shown command.
1760 */
1761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001762del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 int old_len;
1765
1766 if (!p_sc)
1767 return;
1768
1769 old_len = (int)STRLEN(showcmd_buf);
1770 if (len > old_len)
1771 len = old_len;
1772 showcmd_buf[old_len - len] = NUL;
1773
1774 if (!char_avail())
1775 display_showcmd();
1776}
1777
1778/*
1779 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1780 * something and there is a partial mapping.
1781 */
1782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001783push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 if (p_sc)
1786 STRCPY(old_showcmd_buf, showcmd_buf);
1787}
1788
1789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001790pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
1792 if (!p_sc)
1793 return;
1794
1795 STRCPY(showcmd_buf, old_showcmd_buf);
1796
1797 display_showcmd();
1798}
1799
1800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001801display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
Luuk van Baalba936f62022-12-15 13:15:39 +00001803 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Luuk van Baalba936f62022-12-15 13:15:39 +00001805 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 cursor_off();
1807
Luuk van Baalba936f62022-12-15 13:15:39 +00001808 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001809 {
1810 if (showcmd_is_clear)
1811 curwin->w_redr_status = TRUE;
1812 else
1813 win_redr_status(curwin, FALSE);
1814 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001815 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001816 {
1817 if (showcmd_is_clear)
1818 redraw_tabline = TRUE;
1819 else
1820 draw_tabline();
1821 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001822 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001824 if (!showcmd_is_clear)
1825 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Luuk van Baalba936f62022-12-15 13:15:39 +00001827 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1828 // spaces
1829 screen_puts((char_u *)" " + len,
1830 (int)Rows - 1, sc_col + len, 0);
1831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
1837 * When "check" is FALSE, prepare for commands that scroll the window.
1838 * When "check" is TRUE, take care of scroll-binding after the window has
1839 * scrolled. Called from normal_cmd() and edit().
1840 */
1841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001842do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
1844 static win_T *old_curwin = NULL;
1845 static linenr_T old_topline = 0;
1846#ifdef FEAT_DIFF
1847 static int old_topfill = 0;
1848#endif
1849 static buf_T *old_buf = NULL;
1850 static colnr_T old_leftcol = 0;
1851
1852 if (check && curwin->w_p_scb)
1853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001854 // If a ":syncbind" command was just used, don't scroll, only reset
1855 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (did_syncbind)
1857 did_syncbind = FALSE;
1858 else if (curwin == old_curwin)
1859 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001860 // Synchronize other windows, as necessary according to
1861 // 'scrollbind'. Don't do this after an ":edit" command, except
1862 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if ((curwin->w_buffer == old_buf
1864#ifdef FEAT_DIFF
1865 || curwin->w_p_diff
1866#endif
1867 )
1868 && (curwin->w_topline != old_topline
1869#ifdef FEAT_DIFF
1870 || curwin->w_topfill != old_topfill
1871#endif
1872 || curwin->w_leftcol != old_leftcol))
1873 {
1874 check_scrollbind(curwin->w_topline - old_topline,
1875 (long)(curwin->w_leftcol - old_leftcol));
1876 }
1877 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001878 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001880 // When switching between windows, make sure that the relative
1881 // vertical offset is valid for the new window. The relative
1882 // offset is invalid whenever another 'scrollbind' window has
1883 // scrolled to a point that would force the current window to
1884 // scroll past the beginning or end of its buffer. When the
1885 // resync is performed, some of the other 'scrollbind' windows may
1886 // need to jump so that the current window's relative position is
1887 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1889 }
1890 curwin->w_scbind_pos = curwin->w_topline;
1891 }
1892
1893 old_curwin = curwin;
1894 old_topline = curwin->w_topline;
1895#ifdef FEAT_DIFF
1896 old_topfill = curwin->w_topfill;
1897#endif
1898 old_buf = curwin->w_buffer;
1899 old_leftcol = curwin->w_leftcol;
1900}
1901
1902/*
1903 * Synchronize any windows that have "scrollbind" set, based on the
1904 * number of rows by which the current window has changed
1905 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1906 */
1907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001908check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 int want_ver;
1911 int want_hor;
1912 win_T *old_curwin = curwin;
1913 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int old_VIsual_select = VIsual_select;
1915 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 colnr_T tgt_leftcol = curwin->w_leftcol;
1917 long topline;
1918 long y;
1919
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001920 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1922#ifdef FEAT_DIFF
1923 want_ver |= old_curwin->w_p_diff;
1924#endif
1925 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1926
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001927 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001929 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001932 // skip original window and windows with 'noscrollbind'
1933 if (curwin == old_curwin || !curwin->w_p_scb)
1934 continue;
1935
1936 // do the vertical scroll
1937 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001940 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
zeertzjq101d57b2022-07-31 18:34:32 +01001942 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
zeertzjq101d57b2022-07-31 18:34:32 +01001944 else
1945#endif
1946 {
1947 curwin->w_scbind_pos += topline_diff;
1948 topline = curwin->w_scbind_pos;
1949 if (topline > curbuf->b_ml.ml_line_count)
1950 topline = curbuf->b_ml.ml_line_count;
1951 if (topline < 1)
1952 topline = 1;
1953
1954 y = topline - curwin->w_topline;
1955 if (y > 0)
1956 scrollup(y, FALSE);
1957 else
1958 scrolldown(-y, FALSE);
1959 }
1960
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001961 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001962 cursor_correct();
1963 curwin->w_redr_status = TRUE;
1964 }
1965
1966 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001967 if (want_hor)
1968 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001971 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 VIsual_select = old_VIsual_select;
1973 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 curwin = old_curwin;
1975 curbuf = old_curbuf;
1976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978/*
1979 * Command character that's ignored.
1980 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001981 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001984nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001986 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987}
1988
1989/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001990 * Command character that doesn't do anything, but unlike nv_ignore() does
1991 * start edit(). Used for "startinsert" executed while starting up.
1992 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001995{
1996}
1997
1998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 * Command character doesn't exist.
2000 */
2001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002002nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 clearopbeep(cap->oap);
2005}
2006
2007/*
2008 * <Help> and <F1> commands.
2009 */
2010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 if (!checkclearopq(cap->oap))
2014 ex_help(NULL);
2015}
2016
2017/*
2018 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2019 */
2020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002023#ifdef FEAT_JOB_CHANNEL
2024 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2025 clearopbeep(cap->oap);
2026 else
2027#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002028 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002029 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002030 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002031 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2032 op_addsub(cap->oap, cap->count1, cap->arg);
2033 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002034 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002035 else if (VIsual_active)
2036 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002037 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002038 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039}
2040
2041/*
2042 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2043 */
2044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002047 if (checkclearop(cap->oap))
2048 return;
2049
2050 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002051 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002052 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2053 if (cap->arg == BACKWARD)
2054 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002055 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002056 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002057 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002058 else
2059 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060}
2061
2062/*
2063 * Implementation of "gd" and "gD" command.
2064 */
2065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002066nv_gd(
2067 oparg_T *oap,
2068 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002069 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
2071 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 char_u *ptr;
2073
Bram Moolenaard9d30582005-05-18 22:10:28 +00002074 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002075 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002076 == FAIL)
2077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002079 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002080 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002081
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002082#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002083 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2084 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002085#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002086 // clear any search statistics
2087 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2088 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002089}
2090
2091/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002092 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2093 * otherwise.
2094 */
2095 static int
2096is_ident(char_u *line, int offset)
2097{
2098 int i;
2099 int incomment = FALSE;
2100 int instring = 0;
2101 int prev = 0;
2102
2103 for (i = 0; i < offset && line[i] != NUL; i++)
2104 {
2105 if (instring != 0)
2106 {
2107 if (prev != '\\' && line[i] == instring)
2108 instring = 0;
2109 }
2110 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2111 {
2112 instring = line[i];
2113 }
2114 else
2115 {
2116 if (incomment)
2117 {
2118 if (prev == '*' && line[i] == '/')
2119 incomment = FALSE;
2120 }
2121 else if (prev == '/' && line[i] == '*')
2122 {
2123 incomment = TRUE;
2124 }
2125 else if (prev == '/' && line[i] == '/')
2126 {
2127 return FALSE;
2128 }
2129 }
2130
2131 prev = line[i];
2132 }
2133
2134 return incomment == FALSE && instring == 0;
2135}
2136
2137/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002138 * Search for variable declaration of "ptr[len]".
2139 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2140 * current file ("gD").
2141 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002142 * Return FAIL when not found.
2143 */
2144 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002145find_decl(
2146 char_u *ptr,
2147 int len,
2148 int locally,
2149 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002150 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002151{
2152 char_u *pat;
2153 pos_T old_pos;
2154 pos_T par_pos;
2155 pos_T found_pos;
2156 int t;
2157 int save_p_ws;
2158 int save_p_scs;
2159 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002160 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002161 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002162 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002163
2164 if ((pat = alloc(len + 7)) == NULL)
2165 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002167 // Put "\V" before the pattern to avoid that the special meaning of "."
2168 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002169 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2170 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 old_pos = curwin->w_cursor;
2172 save_p_ws = p_ws;
2173 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002174 p_ws = FALSE; // don't wrap around end of file now
2175 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002177 // With "gD" go to line 1.
2178 // With "gd" Search back for the start of the current function, then go
2179 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002180 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002182 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002184 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186 else
2187 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002188 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2190 --curwin->w_cursor.lnum;
2191 }
2192 curwin->w_cursor.col = 0;
2193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002195 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002196 for (;;)
2197 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002198 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002199 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002200 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002202
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002203 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002204 {
2205 pos_T *pos;
2206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 // Check that the block the match is in doesn't end before the
2208 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002209 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2210 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2211 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002212 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002213 // There can't be a useful match before the end of this block.
2214 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002215 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002216 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002217 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002218 }
2219
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 if (t == FAIL)
2221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 if (found_pos.lnum != 0)
2224 {
2225 curwin->w_cursor = found_pos;
2226 t = OK;
2227 }
2228 break;
2229 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002230 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002233 ++curwin->w_cursor.lnum;
2234 curwin->w_cursor.col = 0;
2235 continue;
2236 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002237 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // If the current position is not a valid identifier and a previous
2240 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002241 if (!valid && found_pos.lnum != 0)
2242 {
2243 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002244 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002245 }
2246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002247 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002248 if (valid && !locally)
2249 break;
2250 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002251 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002252 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002253 if (found_pos.lnum != 0)
2254 curwin->w_cursor = found_pos;
2255 break;
2256 }
2257
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 // For finding a local variable and the match is before the "{" or
2259 // inside a comment, continue searching. For K&R style function
2260 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002261 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002262 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002263 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002264 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002265 // Remove SEARCH_START from flags to avoid getting stuck at one
2266 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002267 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002269
2270 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 curwin->w_cursor = old_pos;
2274 }
2275 else
2276 {
2277 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002278 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 reset_search_dir();
2280 }
2281
2282 vim_free(pat);
2283 p_ws = save_p_ws;
2284 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002285
2286 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287}
2288
2289/*
2290 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2291 * lines rather than lines in the file.
2292 * 'dist' must be positive.
2293 *
2294 * Return OK if able to move cursor, FAIL otherwise.
2295 */
2296 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002299 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 int retval = OK;
2301 int atend = FALSE;
2302 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 int col_off1; // margin offset for first screen line
2304 int col_off2; // margin offset for wrapped screen line
2305 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002306 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002309 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 col_off1 = curwin_col_off();
2312 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002313 width1 = curwin->w_width - col_off1;
2314 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002315 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002316 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002319 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002320 // Instead of sticking at the last character of the buffer line we
2321 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (curwin->w_curswant == MAXCOL)
2323 {
2324 atend = TRUE;
2325 validate_virtcol();
2326 if (width1 <= 0)
2327 curwin->w_curswant = 0;
2328 else
2329 {
2330 curwin->w_curswant = width1 - 1;
2331 if (curwin->w_virtcol > curwin->w_curswant)
2332 curwin->w_curswant += ((curwin->w_virtcol
2333 - curwin->w_curswant - 1) / width2 + 1) * width2;
2334 }
2335 }
2336 else
2337 {
2338 if (linelen > width1)
2339 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2340 else
2341 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002342 if (curwin->w_curswant >= (colnr_T)n)
2343 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345
2346 while (dist--)
2347 {
2348 if (dir == BACKWARD)
2349 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002350 if ((long)curwin->w_curswant >= width1
2351#ifdef FEAT_FOLDING
2352 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2353#endif
2354 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002355 // Move back within the line. This can give a negative value
2356 // for w_curswant if width1 < width2 (with cpoptions+=n),
2357 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 curwin->w_curswant -= width2;
2359 else
2360 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002361 // to previous line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002362 if (!cursor_up_inner(curwin, 1))
Bram Moolenaare71996b2021-01-21 17:03:07 +01002363 {
2364 retval = FAIL;
2365 break;
2366 }
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002367 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (linelen > width1)
2369 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2370 + 1) * width2;
2371 }
2372 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002373 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375 if (linelen > width1)
2376 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2377 else
2378 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002379 if (curwin->w_curswant + width2 < (colnr_T)n
2380#ifdef FEAT_FOLDING
2381 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2382#endif
2383 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002384 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 curwin->w_curswant += width2;
2386 else
2387 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // to next line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002389 if (!cursor_down_inner(curwin, 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 retval = FAIL;
2392 break;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002395 // Check if the cursor has moved below the number display
2396 // when width1 < width2 (with cpoptions+=n). Subtract width2
2397 // to get a negative value for w_curswant, which will get
2398 // clipped to column 0.
2399 if (curwin->w_curswant >= width1)
2400 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002401 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 }
2404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002407 if (virtual_active() && atend)
2408 coladvance(MAXCOL);
2409 else
2410 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2413 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002414 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002415 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002416
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002417 // Check for landing on a character that got split at the end of the
2418 // last line. We want to advance a screenline, not end up in the same
2419 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002421 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002422#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002423 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2424 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002425#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002426
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002427 c = (*mb_ptr2char)(ml_get_cursor());
2428 if (dir == FORWARD && virtcol < curwin->w_curswant
2429 && (curwin->w_curswant <= (colnr_T)width1)
2430 && !vim_isprintc(c) && c > 255)
2431 oneright();
2432
Bram Moolenaar773b1582014-08-29 14:20:51 +02002433 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 && (curwin->w_curswant < (colnr_T)width1
2435 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2436 : ((curwin->w_curswant - width1) % width2
2437 > (colnr_T)width2 / 2)))
2438 --curwin->w_cursor.col;
2439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002443 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 return retval;
2446}
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448/*
2449 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2450 * cap->arg must be TRUE for CTRL-E.
2451 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002453nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 if (!checkclearop(cap->oap))
2456 scroll_redraw(cap->arg, cap->count1);
2457}
2458
2459/*
2460 * Scroll "count" lines up or down, and redraw.
2461 */
2462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002466 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifdef FEAT_DIFF
2468 int prev_topfill = curwin->w_topfill;
2469#endif
2470 linenr_T prev_lnum = curwin->w_cursor.lnum;
2471
2472 if (up)
2473 scrollup(count, TRUE);
2474 else
2475 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002476 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002478 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2479 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 cursor_correct();
2481 check_cursor_moved(curwin);
2482 curwin->w_valid |= VALID_TOPLINE;
2483
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002484 // If moved back to where we were, at least move the cursor, otherwise
2485 // we get stuck at one position. Don't move the cursor up if the
2486 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002488 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489#ifdef FEAT_DIFF
2490 && curwin->w_topfill == prev_topfill
2491#endif
2492 )
2493 {
2494 if (up)
2495 {
2496 if (curwin->w_cursor.lnum > prev_lnum
2497 || cursor_down(1L, FALSE) == FAIL)
2498 break;
2499 }
2500 else
2501 {
2502 if (curwin->w_cursor.lnum < prev_lnum
2503 || prev_topline == 1L
2504 || cursor_up(1L, FALSE) == FAIL)
2505 break;
2506 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002507 // Mark w_topline as valid, otherwise the screen jumps back at the
2508 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 check_cursor_moved(curwin);
2510 curwin->w_valid |= VALID_TOPLINE;
2511 }
2512 }
2513 if (curwin->w_cursor.lnum != prev_lnum)
2514 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002515 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002519 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2520 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2521 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002522 */
2523 static int
2524nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2525{
2526 int nchar = *nchar_arg;
2527 long n;
2528
2529 // "z123{nchar}": edit the count before obtaining {nchar}
2530 if (checkclearop(cap->oap))
2531 return FALSE;
2532 n = nchar - '0';
2533
2534 for (;;)
2535 {
2536#ifdef USE_ON_FLY_SCROLL
2537 dont_scroll = TRUE; // disallow scrolling here
2538#endif
2539 ++no_mapping;
2540 ++allow_keys; // no mapping for nchar, but allow key codes
2541 nchar = plain_vgetc();
2542 LANGMAP_ADJUST(nchar, TRUE);
2543 --no_mapping;
2544 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002545 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002547 if (nchar == K_DEL || nchar == K_KDEL)
2548 n /= 10;
2549 else if (VIM_ISDIGIT(nchar))
2550 n = n * 10 + (nchar - '0');
2551 else if (nchar == CAR)
2552 {
2553#ifdef FEAT_GUI
2554 need_mouse_correct = TRUE;
2555#endif
2556 win_setheight((int)n);
2557 break;
2558 }
2559 else if (nchar == 'l'
2560 || nchar == 'h'
2561 || nchar == K_LEFT
2562 || nchar == K_RIGHT)
2563 {
2564 cap->count1 = n ? n * cap->count1 : cap->count1;
2565 *nchar_arg = nchar;
2566 return TRUE;
2567 }
2568 else
2569 {
2570 clearopbeep(cap->oap);
2571 break;
2572 }
2573 }
2574 cap->oap->op_type = OP_NOP;
2575 return FALSE;
2576}
2577
2578#ifdef FEAT_SPELL
2579/*
2580 * "zug" and "zuw": undo "zg" and "zw"
2581 * "zg": add good word to word list
2582 * "zw": add wrong word to word list
2583 * "zG": add good word to temp word list
2584 * "zW": add wrong word to temp word list
2585 */
2586 static int
2587nv_zg_zw(cmdarg_T *cap, int nchar)
2588{
2589 char_u *ptr = NULL;
2590 int len;
2591 int undo = FALSE;
2592
2593 if (nchar == 'u')
2594 {
2595 ++no_mapping;
2596 ++allow_keys; // no mapping for nchar, but allow key codes
2597 nchar = plain_vgetc();
2598 LANGMAP_ADJUST(nchar, TRUE);
2599 --no_mapping;
2600 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002601 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002602
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002603 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2604 {
2605 clearopbeep(cap->oap);
2606 return OK;
2607 }
2608 undo = TRUE;
2609 }
2610
2611 if (checkclearop(cap->oap))
2612 return OK;
2613 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2614 return FAIL;
2615 if (ptr == NULL)
2616 {
2617 pos_T pos = curwin->w_cursor;
2618
2619 // Find bad word under the cursor. When 'spell' is
2620 // off this fails and find_ident_under_cursor() is
2621 // used below.
2622 emsg_off++;
2623 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2624 emsg_off--;
2625 if (len != 0 && curwin->w_cursor.col <= pos.col)
2626 ptr = ml_get_pos(&curwin->w_cursor);
2627 curwin->w_cursor = pos;
2628 }
2629
2630 if (ptr == NULL
2631 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2632 return FAIL;
2633 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2634 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2635 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2636
2637 return OK;
2638}
2639#endif
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Commands that start with "z".
2643 */
2644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002645nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 long n;
2648 colnr_T col;
2649 int nchar = cap->nchar;
2650#ifdef FEAT_FOLDING
2651 long old_fdl = curwin->w_p_fdl;
2652 int old_fen = curwin->w_p_fen;
2653#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002654 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002656 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (
2660#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002661 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2662 // and "zC" only in Visual mode. "zj" and "zk" are motion
2663 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 cap->nchar != 'f' && cap->nchar != 'F'
2665 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2666 && cap->nchar != 'j' && cap->nchar != 'k'
2667 &&
2668#endif
2669 checkclearop(cap->oap))
2670 return;
2671
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002672 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2673 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2675 && cap->count0
2676 && cap->count0 != curwin->w_cursor.lnum)
2677 {
2678 setpcmark();
2679 if (cap->count0 > curbuf->b_ml.ml_line_count)
2680 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2681 else
2682 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002683 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685
2686 switch (nchar)
2687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002688 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 case '+':
2690 if (cap->count0 == 0)
2691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // No count given: put cursor at the line below screen
2693 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2696 else
2697 curwin->w_cursor.lnum = curwin->w_botline;
2698 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 case NL:
2701 case CAR:
2702 case K_KENTER:
2703 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002707 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002708 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 break;
2710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002715 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002716 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002717 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 break;
2719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // "z^", "z-" and "zb": put cursor at bottom of screen
2721 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2722 // when <count> is at bottom of window, and puts that one at
2723 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (cap->count0 != 0)
2725 {
2726 scroll_cursor_bot(0, TRUE);
2727 curwin->w_cursor.lnum = curwin->w_topline;
2728 }
2729 else if (curwin->w_topline == 1)
2730 curwin->w_cursor.lnum = 1;
2731 else
2732 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 case '-':
2735 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002739 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002740 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
2742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 case 'h':
2750 case K_LEFT:
2751 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002752 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2753 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 break;
2755
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002756 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002760 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 case 'l':
2762 case K_RIGHT:
2763 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002764 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 break;
2766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 case 's': if (!curwin->w_p_wrap)
2769 {
2770#ifdef FEAT_FOLDING
2771 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
2774#endif
2775 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002776 if ((long)col > siso)
2777 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else
2779 col = 0;
2780 if (curwin->w_leftcol != col)
2781 {
2782 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002783 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 }
2786 break;
2787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002788 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 case 'e': if (!curwin->w_p_wrap)
2790 {
2791#ifdef FEAT_FOLDING
2792 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else
2795#endif
2796 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002797 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002798 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 col = 0;
2800 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002801 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (curwin->w_leftcol != col)
2803 {
2804 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002805 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 }
2808 break;
2809
Christian Brabandt2fa93842021-05-30 22:17:25 +02002810 // "zp", "zP" in block mode put without addind trailing spaces
2811 case 'P':
2812 case 'p': nv_put(cap);
2813 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002814 // "zy" Yank without trailing spaces
2815 case 'y': nv_operator(cap);
2816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 // "zF": create fold command
2819 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case 'F':
2821 case 'f': if (foldManualAllowed(TRUE))
2822 {
2823 cap->nchar = 'f';
2824 nv_operator(cap);
2825 curwin->w_p_fen = TRUE;
2826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2829 {
2830 nv_operator(cap);
2831 finish_op = TRUE;
2832 }
2833 }
2834 else
2835 clearopbeep(cap->oap);
2836 break;
2837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // "zd": delete fold at cursor
2839 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 case 'd':
2841 case 'D': if (foldManualAllowed(FALSE))
2842 {
2843 if (VIsual_active)
2844 nv_operator(cap);
2845 else
2846 deleteFold(curwin->w_cursor.lnum,
2847 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2848 }
2849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case 'E': if (foldmethodIsManual(curwin))
2853 {
2854 clearFolding(curwin);
2855 changed_window_setting();
2856 }
2857 else if (foldmethodIsMarker(curwin))
2858 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2859 TRUE, FALSE);
2860 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002861 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 break;
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case 'n': curwin->w_p_fen = FALSE;
2866 break;
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 case 'N': curwin->w_p_fen = TRUE;
2870 break;
2871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2874 break;
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2878 openFold(curwin->w_cursor.lnum, cap->count1);
2879 else
2880 {
2881 closeFold(curwin->w_cursor.lnum, cap->count1);
2882 curwin->w_p_fen = TRUE;
2883 }
2884 break;
2885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2888 openFoldRecurse(curwin->w_cursor.lnum);
2889 else
2890 {
2891 closeFoldRecurse(curwin->w_cursor.lnum);
2892 curwin->w_p_fen = TRUE;
2893 }
2894 break;
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 case 'o': if (VIsual_active)
2898 nv_operator(cap);
2899 else
2900 openFold(curwin->w_cursor.lnum, cap->count1);
2901 break;
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 case 'O': if (VIsual_active)
2905 nv_operator(cap);
2906 else
2907 openFoldRecurse(curwin->w_cursor.lnum);
2908 break;
2909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case 'c': if (VIsual_active)
2912 nv_operator(cap);
2913 else
2914 closeFold(curwin->w_cursor.lnum, cap->count1);
2915 curwin->w_p_fen = TRUE;
2916 break;
2917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 case 'C': if (VIsual_active)
2920 nv_operator(cap);
2921 else
2922 closeFoldRecurse(curwin->w_cursor.lnum);
2923 curwin->w_p_fen = TRUE;
2924 break;
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 case 'v': foldOpenCursor();
2928 break;
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 curwin->w_foldinvalid = TRUE; // recompute folds
2933 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 foldOpenCursor();
2935 break;
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 curwin->w_foldinvalid = TRUE; // recompute folds
2940 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002945 {
2946 curwin->w_p_fdl -= cap->count1;
2947 if (curwin->w_p_fdl < 0)
2948 curwin->w_p_fdl = 0;
2949 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 curwin->w_p_fen = TRUE;
2952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 curwin->w_p_fen = TRUE;
2958 break;
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002961 case 'r': curwin->w_p_fdl += cap->count1;
2962 {
2963 int d = getDeepestNesting();
2964
2965 if (curwin->w_p_fdl >= d)
2966 curwin->w_p_fdl = d;
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 break;
2969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 break;
2974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 case 'j': // "zj" move to next fold downwards
2976 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2978 cap->count1) == FAIL)
2979 clearopbeep(cap->oap);
2980 break;
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002984#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 case 'g': // "zg": add good word to word list
2987 case 'w': // "zw": add wrong word to word list
2988 case 'G': // "zG": add good word to temp word list
2989 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002990 if (nv_zg_zw(cap, nchar) == FAIL)
2991 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002992 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002995 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002996 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002997 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 default: clearopbeep(cap->oap);
3001 }
3002
3003#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (old_fen != curwin->w_p_fen)
3006 {
3007# ifdef FEAT_DIFF
3008 win_T *wp;
3009
3010 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 FOR_ALL_WINDOWS(wp)
3014 {
3015 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3016 {
3017 wp->w_p_fen = curwin->w_p_fen;
3018 changed_window_setting_win(wp);
3019 }
3020 }
3021 }
3022# endif
3023 changed_window_setting();
3024 }
3025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003026 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (old_fdl != curwin->w_p_fdl)
3028 newFoldLevel();
3029#endif
3030}
3031
3032#ifdef FEAT_GUI
3033/*
3034 * Vertical scrollbar movement.
3035 */
3036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003037nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 if (cap->oap->op_type != OP_NOP)
3040 clearopbeep(cap->oap);
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 gui_do_scroll();
3044}
3045
3046/*
3047 * Horizontal scrollbar movement.
3048 */
3049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050nv_hor_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
Christopher Plewright44c22092022-11-15 17:43:36 +00003056 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057}
3058#endif
3059
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003060#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003061/*
3062 * Click in GUI tab.
3063 */
3064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003065nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003066{
3067 if (cap->oap->op_type != OP_NOP)
3068 clearopbeep(cap->oap);
3069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003071 goto_tabpage(current_tab);
3072}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003073
3074/*
3075 * Selected item in tab line menu.
3076 */
3077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +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 Moolenaara226a6d2006-02-26 23:59:20 +00003084 handle_tabmenu();
3085}
3086
3087/*
3088 * Handle selecting an item of the GUI tab line menu.
3089 * Used in Normal and Insert mode.
3090 */
3091 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003092handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003093{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003094 switch (current_tabmenu)
3095 {
3096 case TABLINE_MENU_CLOSE:
3097 if (current_tab == 0)
3098 do_cmdline_cmd((char_u *)"tabclose");
3099 else
3100 {
3101 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3102 current_tab);
3103 do_cmdline_cmd(IObuff);
3104 }
3105 break;
3106
3107 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003108 if (current_tab == 0)
3109 do_cmdline_cmd((char_u *)"$tabnew");
3110 else
3111 {
3112 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3113 current_tab - 1);
3114 do_cmdline_cmd(IObuff);
3115 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003116 break;
3117
3118 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003119 if (current_tab == 0)
3120 do_cmdline_cmd((char_u *)"browse $tabnew");
3121 else
3122 {
3123 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3124 current_tab - 1);
3125 do_cmdline_cmd(IObuff);
3126 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003127 break;
3128 }
3129}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003130#endif
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132/*
3133 * "Q" command.
3134 */
3135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003136nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003138 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003140 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003141 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 do_exmode(FALSE);
3143}
3144
3145/*
3146 * Handle a ":" command.
3147 */
3148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003151 int old_p_im;
3152 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003153 int is_cmdkey = cap->cmdchar == K_COMMAND
3154 || cap->cmdchar == K_SCRIPT_COMMAND;
3155 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar957cf672020-11-12 14:21:06 +01003157 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003159 nv_operator(cap);
3160 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003162
3163 if (cap->oap->op_type != OP_NOP)
3164 {
3165 // Using ":" as a movement is characterwise exclusive.
3166 cap->oap->motion_type = MCHAR;
3167 cap->oap->inclusive = FALSE;
3168 }
3169 else if (cap->count0 && !is_cmdkey)
3170 {
3171 // translate "count:" into ":.,.+(count - 1)"
3172 stuffcharReadbuff('.');
3173 if (cap->count0 > 1)
3174 {
3175 stuffReadbuff((char_u *)",.+");
3176 stuffnumReadbuff((long)cap->count0 - 1L);
3177 }
3178 }
3179
3180 // When typing, don't type below an old message
3181 if (KeyTyped)
3182 compute_cmdrow();
3183
3184 old_p_im = p_im;
3185
3186 // get a command line and execute it
3187 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3188 if (is_cmdkey)
3189 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3190 else
3191 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3192
3193 // If 'insertmode' changed, enter or exit Insert mode
3194 if (p_im != old_p_im)
3195 {
3196 if (p_im)
3197 restart_edit = 'i';
3198 else
3199 restart_edit = 0;
3200 }
3201
3202 if (cmd_result == FAIL)
3203 // The Ex command failed, do not execute the operator.
3204 clearop(cap->oap);
3205 else if (cap->oap->op_type != OP_NOP
3206 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3207 || cap->oap->start.col >
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003208 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003209 || did_emsg
3210 ))
3211 // The start of the operator has become invalid by the Ex command.
3212 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213}
3214
3215/*
3216 * Handle CTRL-G command.
3217 */
3218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003219nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
3223 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003224 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 showmode();
3226 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003227 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003228 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 fileinfo((int)cap->count0, FALSE, TRUE);
3230}
3231
3232/*
3233 * Handle CTRL-H <Backspace> command.
3234 */
3235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (VIsual_active && VIsual_select)
3239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 v_visop(cap);
3242 }
3243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 nv_left(cap);
3245}
3246
3247/*
3248 * CTRL-L: clear screen and redraw.
3249 */
3250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003251nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003253 if (checkclearop(cap->oap))
3254 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003255
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003256#ifdef FEAT_SYN_HL
3257 // Clear all syntax states to force resyncing.
3258 syn_stack_free_all(curwin->w_s);
3259# ifdef FEAT_RELTIME
3260 {
3261 win_T *wp;
3262
3263 FOR_ALL_WINDOWS(wp)
3264 wp->w_s->b_syn_slow = FALSE;
3265 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003268 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003269#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3270# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003271 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003272# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003273 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275}
3276
3277/*
3278 * CTRL-O: In Select mode: switch to Visual mode for one command.
3279 * Otherwise: Go to older pcmark.
3280 */
3281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003282nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (VIsual_active && VIsual_select)
3285 {
3286 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003287 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003289 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 cap->count1 = -cap->count1;
3294 nv_pcmark(cap);
3295 }
3296}
3297
3298/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003299 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3300 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
3302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003303nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 if (!checkclearopq(cap->oap))
3306 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3307 GETF_SETMARK|GETF_ALT, FALSE);
3308}
3309
3310/*
3311 * "Z" commands.
3312 */
3313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003316 if (checkclearopq(cap->oap))
3317 return;
3318
3319 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003321 // "ZZ": equivalent to ":x".
3322 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003326 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 break;
3328
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003329 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
3334 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3335 */
3336 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003337do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 oparg_T oa;
3340 cmdarg_T ca;
3341
3342 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003343 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 ca.oap = &oa;
3345 ca.cmdchar = c1;
3346 ca.nchar = c2;
3347 nv_ident(&ca);
3348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003351 * 'K' normal-mode command. Get the command to lookup the keyword under the
3352 * cursor.
3353 */
3354 static int
3355nv_K_getcmd(
3356 cmdarg_T *cap,
3357 char_u *kp,
3358 int kp_help,
3359 int kp_ex,
3360 char_u **ptr_arg,
3361 int n,
3362 char_u *buf,
3363 unsigned buflen)
3364{
3365 char_u *ptr = *ptr_arg;
3366 int isman;
3367 int isman_s;
3368
3369 if (kp_help)
3370 {
3371 // in the help buffer
3372 STRCPY(buf, "he! ");
3373 return n;
3374 }
3375
3376 if (kp_ex)
3377 {
3378 // 'keywordprog' is an ex command
3379 if (cap->count0 != 0)
3380 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3381 else
3382 STRCPY(buf, kp);
3383 STRCAT(buf, " ");
3384 return n;
3385 }
3386
3387 // An external command will probably use an argument starting
3388 // with "-" as an option. To avoid trouble we skip the "-".
3389 while (*ptr == '-' && n > 0)
3390 {
3391 ++ptr;
3392 --n;
3393 }
3394 if (n == 0)
3395 {
3396 // found dashes only
3397 emsg(_(e_no_identifier_under_cursor));
3398 vim_free(buf);
3399 *ptr_arg = ptr;
3400 return 0;
3401 }
3402
3403 // When a count is given, turn it into a range. Is this
3404 // really what we want?
3405 isman = (STRCMP(kp, "man") == 0);
3406 isman_s = (STRCMP(kp, "man -s") == 0);
3407 if (cap->count0 != 0 && !(isman || isman_s))
3408 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3409
3410 STRCAT(buf, "! ");
3411 if (cap->count0 == 0 && isman_s)
3412 STRCAT(buf, "man");
3413 else
3414 STRCAT(buf, kp);
3415 STRCAT(buf, " ");
3416 if (cap->count0 != 0 && (isman || isman_s))
3417 {
3418 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3419 STRCAT(buf, " ");
3420 }
3421
3422 *ptr_arg = ptr;
3423 return n;
3424}
3425
3426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * Handle the commands that use the word under the cursor.
3428 * [g] CTRL-] :ta to current identifier
3429 * [g] 'K' run program for current identifier
3430 * [g] '*' / to current identifier or string
3431 * [g] '#' ? to current identifier or string
3432 * g ']' :tselect for current identifier
3433 */
3434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003435nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 char_u *ptr = NULL;
3438 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003439 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003440 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 char_u *kp; // value of 'keywordprg'
3443 int kp_help; // 'keywordprg' is ":he"
3444 int kp_ex; // 'keywordprg' starts with ":"
3445 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003448 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003451 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 cmdchar = cap->nchar;
3454 g_cmd = TRUE;
3455 }
3456 else
3457 {
3458 cmdchar = cap->cmdchar;
3459 g_cmd = FALSE;
3460 }
3461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 cmdchar = '#';
3464
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003465 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3469 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (checkclearopq(cap->oap))
3471 return;
3472 }
3473
3474 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3475 (cmdchar == '*' || cmdchar == '#')
3476 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3477 {
3478 clearop(cap->oap);
3479 return;
3480 }
3481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // Allocate buffer to put the command in. Inserting backslashes can
3483 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3484 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3486 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3487 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003488 if (kp_help && *skipwhite(ptr) == NUL)
3489 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003490 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003491 return;
3492 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003493 kp_ex = (*kp == ':');
3494 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3495 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (buf == NULL)
3497 return;
3498 buf[0] = NUL;
3499
3500 switch (cmdchar)
3501 {
3502 case '*':
3503 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003504 // Put cursor at start of word, makes search skip the word
3505 // under the cursor.
3506 // Call setpcmark() first, so "*``" puts the cursor back where
3507 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 setpcmark();
3509 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3510
3511 if (!g_cmd && vim_iswordp(ptr))
3512 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003513 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
3515
3516 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003517 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3518 if (n == 0)
3519 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 break;
3521
3522 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003523 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#ifdef FEAT_CSCOPE
3525 if (p_cst)
3526 STRCPY(buf, "cstag ");
3527 else
3528#endif
3529 STRCPY(buf, "ts ");
3530 break;
3531
3532 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003533 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (curbuf->b_help)
3535 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003537 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 if (g_cmd)
3539 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003540 else if (cap->count0 == 0)
3541 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003542 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003543 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003547 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003548 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003550 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003551 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003552 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003553 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003554 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003556 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003557 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003560 vim_free(buf);
3561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003563 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003564 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003565 {
3566 vim_free(buf);
3567 vim_free(p);
3568 return;
3569 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003570 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 STRCAT(buf, p);
3572 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003574 else
3575 {
3576 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003577 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003579 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003580 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003581 {
3582 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003584 aux_ptr = (char_u *)"";
3585 else
3586 aux_ptr = (char_u *)"\\|\"\n[";
3587 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003588 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 aux_ptr = (char_u *)"\\|\"\n*?[";
3590
3591 p = buf + STRLEN(buf);
3592 while (n-- > 0)
3593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003595 if (vim_strchr(aux_ptr, *ptr) != NULL)
3596 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // When current byte is a part of multibyte character, copy all
3598 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003599 if (has_mbyte)
3600 {
3601 int i;
3602 int len = (*mb_ptr2len)(ptr) - 1;
3603
3604 for (i = 0; i < len && n >= 1; ++i, --n)
3605 *p++ = *ptr++;
3606 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 *p++ = *ptr++;
3608 }
3609 *p = NUL;
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003612 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (cmdchar == '*' || cmdchar == '#')
3614 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003615 if (!g_cmd && (has_mbyte
3616 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3617 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003619
3620 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003621 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003623
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003624 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003627 {
3628 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003630 g_tag_at_cursor = FALSE;
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 vim_free(buf);
3634}
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636/*
3637 * Get visually selected text, within one line only.
3638 * Returns FAIL if more than one line selected.
3639 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003641get_visual_text(
3642 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 char_u **pp, // return: start of selected text
3644 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 if (VIsual_mode != 'V')
3647 unadjust_for_sel();
3648 if (VIsual.lnum != curwin->w_cursor.lnum)
3649 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003650 if (cap != NULL)
3651 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return FAIL;
3653 }
3654 if (VIsual_mode == 'V')
3655 {
3656 *pp = ml_get_curline();
3657 *lenp = (int)STRLEN(*pp);
3658 }
3659 else
3660 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003661 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 *pp = ml_get_pos(&curwin->w_cursor);
3664 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3665 }
3666 else
3667 {
3668 *pp = ml_get_pos(&VIsual);
3669 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3670 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003671 if (**pp == NUL)
3672 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003673 if (*lenp > 0)
3674 {
3675 if (has_mbyte)
3676 // Correct the length to include all bytes of the last
3677 // character.
3678 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3679 else if ((*pp)[*lenp - 1] == NUL)
3680 // Do not include a trailing NUL.
3681 *lenp -= 1;
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 reset_VIsual_and_resel();
3685 return OK;
3686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
3688/*
3689 * CTRL-T: backwards in tag stack
3690 */
3691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003692nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 if (!checkclearopq(cap->oap))
3695 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3696}
3697
3698/*
3699 * Handle scrolling command 'H', 'L' and 'M'.
3700 */
3701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003702nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 int used = 0;
3705 long n;
3706#ifdef FEAT_FOLDING
3707 linenr_T lnum;
3708#endif
3709 int half;
3710
3711 cap->oap->motion_type = MLINE;
3712 setpcmark();
3713
3714 if (cap->cmdchar == 'L')
3715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 curwin->w_cursor.lnum = curwin->w_botline - 1;
3718 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3719 curwin->w_cursor.lnum = 1;
3720 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003721 {
3722#ifdef FEAT_FOLDING
3723 if (hasAnyFolding(curwin))
3724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003725 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003726 for (n = cap->count1 - 1; n > 0
3727 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3728 {
3729 (void)hasFolding(curwin->w_cursor.lnum,
3730 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003731 if (curwin->w_cursor.lnum > curwin->w_topline)
3732 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003733 }
3734 }
3735 else
3736#endif
3737 curwin->w_cursor.lnum -= cap->count1 - 1;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 else
3741 {
3742 if (cap->cmdchar == 'M')
3743 {
3744#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003745 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 used -= diff_check_fill(curwin, curwin->w_topline)
3747 - curwin->w_topfill;
3748#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003749 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3751 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3752 {
3753#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // Count half he number of filler lines to be "below this
3755 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3757 + n) / 2 >= half)
3758 {
3759 --n;
3760 break;
3761 }
3762#endif
3763 used += plines(curwin->w_topline + n);
3764 if (used >= half)
3765 break;
3766#ifdef FEAT_FOLDING
3767 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3768 n = lnum - curwin->w_topline;
3769#endif
3770 }
3771 if (n > 0 && used > curwin->w_height)
3772 --n;
3773 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003774 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003777#ifdef FEAT_FOLDING
3778 if (hasAnyFolding(curwin))
3779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003780 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003781 lnum = curwin->w_topline;
3782 while (n-- > 0 && lnum < curwin->w_botline - 1)
3783 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003784 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003785 ++lnum;
3786 }
3787 n = lnum - curwin->w_topline;
3788 }
3789#endif
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 curwin->w_cursor.lnum = curwin->w_topline + n;
3792 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3793 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3794 }
3795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003796 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003797 if (cap->oap->op_type == OP_NOP)
3798 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 beginline(BL_SOL | BL_FIX);
3800}
3801
3802/*
3803 * Cursor right commands.
3804 */
3805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003806nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003809 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003814 if (mod_mask & MOD_MASK_CTRL)
3815 cap->arg = TRUE;
3816 nv_wordcmd(cap);
3817 return;
3818 }
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 cap->oap->motion_type = MCHAR;
3821 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003822 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003824 // In virtual edit mode, there's no such thing as "past_line", as lines
3825 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003827 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 for (n = cap->count1; n > 0; --n)
3830 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003831 if ((!past_line && oneright() == FAIL)
3832 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003833 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003835 // <Space> wraps to next line if 'whichwrap' has 's'.
3836 // 'l' wraps to next line if 'whichwrap' has 'l'.
3837 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if ( ((cap->cmdchar == ' '
3839 && vim_strchr(p_ww, 's') != NULL)
3840 || (cap->cmdchar == 'l'
3841 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003842 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 && vim_strchr(p_ww, '>') != NULL))
3844 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003846 // When deleting we also count the NL as a character.
3847 // Set cap->oap->inclusive when last char in the line is
3848 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003849 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003851 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 cap->oap->inclusive = TRUE;
3853 else
3854 {
3855 ++curwin->w_cursor.lnum;
3856 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 curwin->w_set_curswant = TRUE;
3859 cap->oap->inclusive = FALSE;
3860 }
3861 continue;
3862 }
3863 if (cap->oap->op_type == OP_NOP)
3864 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003865 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (n == cap->count1)
3867 beep_flush();
3868 }
3869 else
3870 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003871 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 cap->oap->inclusive = TRUE;
3873 }
3874 break;
3875 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003876 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (virtual_active())
3880 oneright();
3881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003884 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 ++curwin->w_cursor.col;
3887 }
3888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890#ifdef FEAT_FOLDING
3891 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3892 && cap->oap->op_type == OP_NOP)
3893 foldOpenCursor();
3894#endif
3895}
3896
3897/*
3898 * Cursor left commands.
3899 *
3900 * Returns TRUE when operator end should not be adjusted.
3901 */
3902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003903nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 long n;
3906
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003907 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003909 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003910 if (mod_mask & MOD_MASK_CTRL)
3911 cap->arg = 1;
3912 nv_bck_word(cap);
3913 return;
3914 }
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 cap->oap->motion_type = MCHAR;
3917 cap->oap->inclusive = FALSE;
3918 for (n = cap->count1; n > 0; --n)
3919 {
3920 if (oneleft() == FAIL)
3921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3923 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3924 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if ( (((cap->cmdchar == K_BS
3926 || cap->cmdchar == Ctrl_H)
3927 && vim_strchr(p_ww, 'b') != NULL)
3928 || (cap->cmdchar == 'h'
3929 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003930 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 && vim_strchr(p_ww, '<') != NULL))
3932 && curwin->w_cursor.lnum > 1)
3933 {
3934 --(curwin->w_cursor.lnum);
3935 coladvance((colnr_T)MAXCOL);
3936 curwin->w_set_curswant = TRUE;
3937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 // When the NL before the first char has to be deleted we
3939 // put the cursor on the NUL after the previous line.
3940 // This is a very special case, be careful!
3941 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if ( (cap->oap->op_type == OP_DELETE
3943 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003944 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003946 char_u *cp = ml_get_cursor();
3947
3948 if (*cp != NUL)
3949 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003950 if (has_mbyte)
3951 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3952 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003953 ++curwin->w_cursor.col;
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 cap->retval |= CA_NO_ADJ_OP_END;
3956 }
3957 continue;
3958 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003959 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3961 beep_flush();
3962 break;
3963 }
3964 }
3965#ifdef FEAT_FOLDING
3966 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3967 && cap->oap->op_type == OP_NOP)
3968 foldOpenCursor();
3969#endif
3970}
3971
3972/*
3973 * Cursor up commands.
3974 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3975 */
3976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003977nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003979 if (mod_mask & MOD_MASK_SHIFT)
3980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003981 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 cap->arg = BACKWARD;
3983 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003984 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003985 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003986
3987 cap->oap->motion_type = MLINE;
3988 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3989 clearopbeep(cap->oap);
3990 else if (cap->arg)
3991 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993
3994/*
3995 * Cursor down commands.
3996 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3997 */
3998 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003999nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 if (mod_mask & MOD_MASK_SHIFT)
4002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004 cap->arg = FORWARD;
4005 nv_page(cap);
4006 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004007#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004008 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004009 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4010 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004015 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 cmdwin_result = CAR;
4017 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004018#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004020 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4021 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4022 {
4023 invoke_prompt_callback();
4024 if (restart_edit == 0)
4025 restart_edit = 'a';
4026 }
4027 else
4028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 cap->oap->motion_type = MLINE;
4031 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4032 clearopbeep(cap->oap);
4033 else if (cap->arg)
4034 beginline(BL_WHITE | BL_FIX);
4035 }
4036 }
4037}
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039/*
4040 * Grab the file name under the cursor and edit it.
4041 */
4042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004043nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004046 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004048 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004050
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004051#ifdef FEAT_PROP_POPUP
4052 if (ERROR_IF_TERM_POPUP_WINDOW)
4053 return;
4054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004056 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 if (ptr != NULL)
4059 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004060 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004061 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004062 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004064 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004065 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004066 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004067 {
4068 curwin->w_cursor.lnum = lnum;
4069 check_cursor_lnum();
4070 beginline(BL_SOL | BL_FIX);
4071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 vim_free(ptr);
4073 }
4074 else
4075 clearop(cap->oap);
4076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078/*
4079 * <End> command: to end of current line or last line.
4080 */
4081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004084 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004086 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004088 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090 nv_dollar(cap);
4091}
4092
4093/*
4094 * Handle the "$" command.
4095 */
4096 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004097nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 cap->oap->motion_type = MCHAR;
4100 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // In virtual mode when off the edge of a line and an operator
4102 // is pending (whew!) keep the cursor where it is.
4103 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (!virtual_active() || gchar_cursor() != NUL
4105 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (cursor_down((long)(cap->count1 - 1),
4108 cap->oap->op_type == OP_NOP) == FAIL)
4109 clearopbeep(cap->oap);
4110#ifdef FEAT_FOLDING
4111 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4112 foldOpenCursor();
4113#endif
4114}
4115
4116/*
4117 * Implementation of '?' and '/' commands.
4118 * If cap->arg is TRUE don't set PC mark.
4119 */
4120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004124 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004128 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 cap->cmdchar = 'g';
4130 cap->nchar = '?';
4131 nv_operator(cap);
4132 return;
4133 }
4134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004135 // When using 'incsearch' the cursor may be moved to set a different search
4136 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004137 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 if (cap->searchbuf == NULL)
4140 {
4141 clearop(oap);
4142 return;
4143 }
4144
Bram Moolenaar46539112015-02-17 15:43:57 +01004145 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004146 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004147 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
LemonBoya4399382022-04-09 21:04:08 +01004150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151/*
4152 * Handle "N" and "n" commands.
4153 * cap->arg is SEARCH_REV for "N", 0 for "n".
4154 */
4155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004156nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004158 pos_T old = curwin->w_cursor;
4159 int wrapped = FALSE;
4160 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004161
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004162 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004164 // Avoid getting stuck on the current cursor position, which can
4165 // happen when an offset is given and the cursor is on the last char
4166 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004167 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004168 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 cap->count1 -= 1;
4170 }
LemonBoya4399382022-04-09 21:04:08 +01004171
4172#ifdef FEAT_SEARCH_EXTRA
4173 // Redraw the window to refresh the highlighted matches.
4174 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004175 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177}
4178
4179/*
4180 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4181 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004182 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004184 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185normal_search(
4186 cmdarg_T *cap,
4187 int dir,
4188 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004189 int opt, // extra flags for do_search()
4190 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004193 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004194#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004195 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 cap->oap->motion_type = MCHAR;
4199 cap->oap->inclusive = FALSE;
4200 cap->oap->use_reg_one = TRUE;
4201 curwin->w_set_curswant = TRUE;
4202
Bram Moolenaara80faa82020-04-12 19:37:17 +02004203 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004204 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004205 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4206 if (wrapped != NULL)
4207 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (i == 0)
4209 clearop(cap->oap);
4210 else
4211 {
4212 if (i == 2)
4213 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#ifdef FEAT_FOLDING
4216 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4217 foldOpenCursor();
4218#endif
4219 }
LemonBoya4399382022-04-09 21:04:08 +01004220#ifdef FEAT_SEARCH_EXTRA
4221 // Redraw the window to refresh the highlighted matches.
4222 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004223 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // "/$" will put the cursor after the end of the line, may need to
4227 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004229 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
4232/*
4233 * Character search commands.
4234 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4235 * ',' and FALSE for ';'.
4236 * cap->nchar is NUL for ',' and ';' (repeat the search)
4237 */
4238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004239nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
4241 int t_cmd;
4242
4243 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4244 t_cmd = TRUE;
4245 else
4246 t_cmd = FALSE;
4247
4248 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004251 clearopbeep(cap->oap);
4252 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004254
4255 curwin->w_set_curswant = TRUE;
4256 // Include a Tab for "tx" and for "dfx".
4257 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4258 && (t_cmd || cap->oap->op_type != OP_NOP))
4259 {
4260 colnr_T scol, ecol;
4261
4262 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4263 curwin->w_cursor.coladd = ecol - scol;
4264 }
4265 else
4266 curwin->w_cursor.coladd = 0;
4267 adjust_for_sel(cap);
4268#ifdef FEAT_FOLDING
4269 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4270 foldOpenCursor();
4271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272}
4273
4274/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004275 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4276 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4277 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4278 * "[m" or "]m" search for prev/next start of (Java) method.
4279 * "[M" or "]M" search for prev/next end of (Java) method.
4280 */
4281 static void
4282nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4283{
4284 pos_T new_pos = {0, 0, 0};
4285 pos_T *pos = NULL; // init for GCC
4286 pos_T prev_pos;
4287 long n;
4288 int findc;
4289 int c;
4290
4291 if (cap->nchar == '*')
4292 cap->nchar = '/';
4293 prev_pos.lnum = 0;
4294 if (cap->nchar == 'm' || cap->nchar == 'M')
4295 {
4296 if (cap->cmdchar == '[')
4297 findc = '{';
4298 else
4299 findc = '}';
4300 n = 9999;
4301 }
4302 else
4303 {
4304 findc = cap->nchar;
4305 n = cap->count1;
4306 }
4307 for ( ; n > 0; --n)
4308 {
4309 if ((pos = findmatchlimit(cap->oap, findc,
4310 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4311 {
4312 if (new_pos.lnum == 0) // nothing found
4313 {
4314 if (cap->nchar != 'm' && cap->nchar != 'M')
4315 clearopbeep(cap->oap);
4316 }
4317 else
4318 pos = &new_pos; // use last one found
4319 break;
4320 }
4321 prev_pos = new_pos;
4322 curwin->w_cursor = *pos;
4323 new_pos = *pos;
4324 }
4325 curwin->w_cursor = *old_pos;
4326
4327 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4328 // brought us to the match for "[m" and "]M" when inside a method.
4329 // Try finding the '{' or '}' we want to be at.
4330 // Also repeat for the given count.
4331 if (cap->nchar == 'm' || cap->nchar == 'M')
4332 {
4333 // norm is TRUE for "]M" and "[m"
4334 int norm = ((findc == '{') == (cap->nchar == 'm'));
4335
4336 n = cap->count1;
4337 // found a match: we were inside a method
4338 if (prev_pos.lnum != 0)
4339 {
4340 pos = &prev_pos;
4341 curwin->w_cursor = prev_pos;
4342 if (norm)
4343 --n;
4344 }
4345 else
4346 pos = NULL;
4347 while (n > 0)
4348 {
4349 for (;;)
4350 {
4351 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4352 {
4353 // if not found anything, that's an error
4354 if (pos == NULL)
4355 clearopbeep(cap->oap);
4356 n = 0;
4357 break;
4358 }
4359 c = gchar_cursor();
4360 if (c == '{' || c == '}')
4361 {
4362 // Must have found end/start of class: use it.
4363 // Or found the place to be at.
4364 if ((c == findc && norm) || (n == 1 && !norm))
4365 {
4366 new_pos = curwin->w_cursor;
4367 pos = &new_pos;
4368 n = 0;
4369 }
4370 // if no match found at all, we started outside of the
4371 // class and we're inside now. Just go on.
4372 else if (new_pos.lnum == 0)
4373 {
4374 new_pos = curwin->w_cursor;
4375 pos = &new_pos;
4376 }
4377 // found start/end of other method: go to match
4378 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004379 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4380 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004381 n = 0;
4382 else
4383 curwin->w_cursor = *pos;
4384 break;
4385 }
4386 }
4387 --n;
4388 }
4389 curwin->w_cursor = *old_pos;
4390 if (pos == NULL && new_pos.lnum != 0)
4391 clearopbeep(cap->oap);
4392 }
4393 if (pos != NULL)
4394 {
4395 setpcmark();
4396 curwin->w_cursor = *pos;
4397 curwin->w_set_curswant = TRUE;
4398#ifdef FEAT_FOLDING
4399 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4400 && cap->oap->op_type == OP_NOP)
4401 foldOpenCursor();
4402#endif
4403 }
4404}
4405
4406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 * "[" and "]" commands.
4408 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4409 */
4410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004411nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004414 pos_T *pos = NULL; // init for GCC
4415 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 int flag;
4417 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 cap->oap->motion_type = MCHAR;
4420 cap->oap->inclusive = FALSE;
4421 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004422 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004424 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (cap->nchar == 'f')
4426 nv_gotofile(cap);
4427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004430 // Find the occurrence(s) of the identifier or define under cursor
4431 // in current and included files or jump to the first occurrence.
4432 //
4433 // search list jump
4434 // fwd bwd fwd bwd fwd bwd
4435 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4436 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004437 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 char_u *ptr;
4440 int len;
4441
4442 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4443 clearop(cap->oap);
4444 else
4445 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004446 // Make a copy, if the line was changed it will be freed.
4447 ptr = vim_strnsave(ptr, len);
4448 if (ptr == NULL)
4449 return;
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 find_pattern_in_path(ptr, 0, len, TRUE,
4452 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4453 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4454 cap->count1,
4455 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4456 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4457 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4458 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004459 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 curwin->w_set_curswant = TRUE;
4461 }
4462 }
4463 else
4464#endif
4465
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004466 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4467 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4468 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4469 // "[m" or "]m" search for prev/next start of (Java) method.
4470 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 if ( (cap->cmdchar == '['
4472 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4473 || (cap->cmdchar == ']'
4474 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004475 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004477 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else if (cap->nchar == '[' || cap->nchar == ']')
4479 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004480 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 flag = '{';
4482 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004483 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004486 // Imitate strange Vi behaviour: When using "]]" with an operator
4487 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004488 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 (cap->oap->op_type != OP_NOP
4490 && cap->arg == FORWARD && flag == '{')))
4491 clearopbeep(cap->oap);
4492 else
4493 {
4494 if (cap->oap->op_type == OP_NOP)
4495 beginline(BL_WHITE | BL_FIX);
4496#ifdef FEAT_FOLDING
4497 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4498 foldOpenCursor();
4499#endif
4500 }
4501 }
4502
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004503 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else if (cap->nchar == 'p' || cap->nchar == 'P')
4505 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004506 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004509 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 else if (cap->nchar == '\'' || cap->nchar == '`')
4511 {
4512 pos = &curwin->w_cursor;
4513 for (n = cap->count1; n > 0; --n)
4514 {
4515 prev_pos = *pos;
4516 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4517 cap->nchar == '\'');
4518 if (pos == NULL)
4519 break;
4520 }
4521 if (pos == NULL)
4522 pos = &prev_pos;
4523 nv_cursormark(cap, cap->nchar == '\'', pos);
4524 }
4525
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004526 // [ or ] followed by a middle mouse click: put selected text with
4527 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004528 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 (void)do_mouse(cap->oap, cap->nchar,
4531 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4532 cap->count1, PUT_FIXINDENT);
4533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004536 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else if (cap->nchar == 'z')
4538 {
4539 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4540 cap->count1) == FAIL)
4541 clearopbeep(cap->oap);
4542 }
4543#endif
4544
4545#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004546 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else if (cap->nchar == 'c')
4548 {
4549 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4550 cap->count1) == FAIL)
4551 clearopbeep(cap->oap);
4552 }
4553#endif
4554
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004555#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004556 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004557 else if (cap->nchar == 's' || cap->nchar == 'S')
4558 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004559 setpcmark();
4560 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004561 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4562 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004563 {
4564 clearopbeep(cap->oap);
4565 break;
4566 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004567 else
4568 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004569# ifdef FEAT_FOLDING
4570 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4571 foldOpenCursor();
4572# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 }
4574#endif
4575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004576 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 clearopbeep(cap->oap);
4579}
4580
4581/*
4582 * Handle Normal mode "%" command.
4583 */
4584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004585nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586{
4587 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004588#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 linenr_T lnum = curwin->w_cursor.lnum;
4590#endif
4591
4592 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
4595 if (cap->count0 > 100)
4596 clearopbeep(cap->oap);
4597 else
4598 {
4599 cap->oap->motion_type = MLINE;
4600 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004601 // Round up, so 'normal 100%' always jumps at the line line.
4602 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4603 // overflow on 32-bits, so use a formula with less accuracy
4604 // to avoid overflows.
4605 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4607 / 100L * cap->count0;
4608 else
4609 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4610 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004611 if (curwin->w_cursor.lnum < 1)
4612 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4614 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4615 beginline(BL_SOL | BL_FIX);
4616 }
4617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 cap->oap->motion_type = MCHAR;
4621 cap->oap->use_reg_one = TRUE;
4622 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4623 clearopbeep(cap->oap);
4624 else
4625 {
4626 setpcmark();
4627 curwin->w_cursor = *pos;
4628 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 }
4633#ifdef FEAT_FOLDING
4634 if (cap->oap->op_type == OP_NOP
4635 && lnum != curwin->w_cursor.lnum
4636 && (fdo_flags & FDO_PERCENT)
4637 && KeyTyped)
4638 foldOpenCursor();
4639#endif
4640}
4641
4642/*
4643 * Handle "(" and ")" commands.
4644 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4645 */
4646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004647nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
4649 cap->oap->motion_type = MCHAR;
4650 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004652 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 curwin->w_set_curswant = TRUE;
4654
4655 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004657 clearopbeep(cap->oap);
4658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004660
4661 // Don't leave the cursor on the NUL past end of line.
4662 adjust_cursor(cap->oap);
4663 curwin->w_cursor.coladd = 0;
4664#ifdef FEAT_FOLDING
4665 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4666 foldOpenCursor();
4667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
4670/*
4671 * "m" command: Mark a position.
4672 */
4673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004674nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004676 if (checkclearop(cap->oap))
4677 return;
4678
4679 if (setmark(cap->nchar) == FAIL)
4680 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681}
4682
4683/*
4684 * "{" and "}" commands.
4685 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4686 */
4687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 cap->oap->motion_type = MCHAR;
4691 cap->oap->inclusive = FALSE;
4692 cap->oap->use_reg_one = TRUE;
4693 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004694 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004696 clearopbeep(cap->oap);
4697 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004699
4700 curwin->w_cursor.coladd = 0;
4701#ifdef FEAT_FOLDING
4702 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4703 foldOpenCursor();
4704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705}
4706
4707/*
4708 * "u" command: Undo or make lower case.
4709 */
4710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004711nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004713 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004715 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 cap->cmdchar = 'g';
4717 cap->nchar = 'u';
4718 nv_operator(cap);
4719 }
4720 else
4721 nv_kundo(cap);
4722}
4723
4724/*
4725 * <Undo> command.
4726 */
4727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004728nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004730 if (checkclearopq(cap->oap))
4731 return;
4732
Bram Moolenaarf2732452018-06-03 14:47:35 +02004733#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004734 if (bt_prompt(curbuf))
4735 {
4736 clearopbeep(cap->oap);
4737 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004739#endif
4740 u_undo((int)cap->count1);
4741 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
4744/*
4745 * Handle the "r" command.
4746 */
4747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 char_u *ptr;
4751 int had_ctrl_v;
4752 long n;
4753
4754 if (checkclearop(cap->oap))
4755 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004756#ifdef FEAT_JOB_CHANNEL
4757 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4758 {
4759 clearopbeep(cap->oap);
4760 return;
4761 }
4762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004764 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (cap->nchar == Ctrl_V)
4766 {
4767 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004768 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (cap->nchar > DEL)
4771 had_ctrl_v = NUL;
4772 }
4773 else
4774 had_ctrl_v = NUL;
4775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004776 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004777 if (IS_SPECIAL(cap->nchar))
4778 {
4779 clearopbeep(cap->oap);
4780 return;
4781 }
4782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (VIsual_active)
4785 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004786 if (got_int)
4787 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004788 if (had_ctrl_v)
4789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004790 // Use a special (negative) number to make a difference between a
4791 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004792 if (cap->nchar == CAR)
4793 cap->nchar = REPLACE_CR_NCHAR;
4794 else if (cap->nchar == NL)
4795 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 nv_operator(cap);
4798 return;
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (virtual_active())
4803 {
4804 if (u_save_cursor() == FAIL)
4805 return;
4806 if (gchar_cursor() == NUL)
4807 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004808 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 coladvance_force((colnr_T)(getviscol() + cap->count1));
4810 curwin->w_cursor.col -= cap->count1;
4811 }
4812 else if (gchar_cursor() == TAB)
4813 coladvance_force(getviscol());
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004816 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004818 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004819 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 clearopbeep(cap->oap);
4822 return;
4823 }
4824
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004825 // Replacing with a TAB is done by edit() when it is complicated because
4826 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4827 // Other characters are done below to avoid problems with things like
4828 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4830 {
4831 stuffnumReadbuff(cap->count1);
4832 stuffcharReadbuff('R');
4833 stuffcharReadbuff('\t');
4834 stuffcharReadbuff(ESC);
4835 return;
4836 }
4837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004838 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (u_save_cursor() == FAIL)
4840 return;
4841
4842 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4843 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004844 // Replace character(s) by a single newline.
4845 // Strange vi behaviour: Only one newline is inserted.
4846 // Delete the characters here.
4847 // Insert the newline with an insert command, takes care of
4848 // autoindent. The insert command depends on being on the last
4849 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 stuffcharReadbuff('\r');
4852 stuffcharReadbuff(ESC);
4853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 invoke_edit(cap, TRUE, 'r', FALSE);
4856 }
4857 else
4858 {
4859 prep_redo(cap->oap->regname, cap->count1,
4860 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4861
4862 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (has_mbyte)
4864 {
4865 int old_State = State;
4866
4867 if (cap->ncharC1 != 0)
4868 AppendCharToRedobuff(cap->ncharC1);
4869 if (cap->ncharC2 != 0)
4870 AppendCharToRedobuff(cap->ncharC2);
4871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // This is slow, but it handles replacing a single-byte with a
4873 // multi-byte and the other way around. Also handles adding
4874 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 for (n = cap->count1; n > 0; --n)
4876 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004877 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004878 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4879 {
4880 int c = ins_copychar(curwin->w_cursor.lnum
4881 + (cap->nchar == Ctrl_Y ? -1 : 1));
4882 if (c != NUL)
4883 ins_char(c);
4884 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004886 ++curwin->w_cursor.col;
4887 }
4888 else
4889 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 State = old_State;
4891 if (cap->ncharC1 != 0)
4892 ins_char(cap->ncharC1);
4893 if (cap->ncharC2 != 0)
4894 ins_char(cap->ncharC2);
4895 }
4896 }
4897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004899 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 for (n = cap->count1; n > 0; --n)
4901 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004902 // Get ptr again, because u_save and/or showmatch() will have
4903 // released the line. This may also happen in ins_copychar().
4904 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004905 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4906 {
4907 int c = ins_copychar(curwin->w_cursor.lnum
4908 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004909
4910 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004911 if (c != NUL)
4912 ptr[curwin->w_cursor.col] = c;
4913 }
4914 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004915 {
4916 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004917 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (p_sm && msg_silent == 0)
4920 showmatch(cap->nchar);
4921 ++curwin->w_cursor.col;
4922 }
4923#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004924 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004926 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaar009b2592004-10-24 19:18:58 +00004928 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004929 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004931 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933#endif
4934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004935 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 changed_bytes(curwin->w_cursor.lnum,
4937 (colnr_T)(curwin->w_cursor.col - cap->count1));
4938 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004939 --curwin->w_cursor.col; // cursor on the last replaced char
4940 // if the character on the left of the current cursor is a multi-byte
4941 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (has_mbyte)
4943 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 curbuf->b_op_end = curwin->w_cursor;
4945 curwin->w_set_curswant = TRUE;
4946 set_last_insert(cap->nchar);
4947 }
4948}
4949
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950/*
4951 * 'o': Exchange start and end of Visual area.
4952 * 'O': same, but in block mode exchange left and right corners.
4953 */
4954 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004955v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 pos_T old_cursor;
4958 colnr_T left, right;
4959
4960 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4961 {
4962 old_cursor = curwin->w_cursor;
4963 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4964 curwin->w_cursor.lnum = VIsual.lnum;
4965 coladvance(left);
4966 VIsual = curwin->w_cursor;
4967
4968 curwin->w_cursor.lnum = old_cursor.lnum;
4969 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 // 'selection "exclusive" and cursor at right-bottom corner: move it
4971 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4973 ++curwin->w_curswant;
4974 coladvance(curwin->w_curswant);
4975 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004977 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 curwin->w_cursor.lnum = VIsual.lnum;
4980 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4981 ++right;
4982 coladvance(right);
4983 VIsual = curwin->w_cursor;
4984
4985 curwin->w_cursor.lnum = old_cursor.lnum;
4986 coladvance(left);
4987 curwin->w_curswant = left;
4988 }
4989 }
4990 else
4991 {
4992 old_cursor = curwin->w_cursor;
4993 curwin->w_cursor = VIsual;
4994 VIsual = old_cursor;
4995 curwin->w_set_curswant = TRUE;
4996 }
4997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999/*
5000 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5001 */
5002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005003nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005005 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 cap->cmdchar = 'c';
5008 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005009 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 VIsual_mode = 'V';
5011 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005012 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005014
5015 if (checkclearopq(cap->oap))
5016 return;
5017
5018 if (!curbuf->b_p_ma)
5019 emsg(_(e_cannot_make_changes_modifiable_is_off));
5020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005022 if (virtual_active())
5023 coladvance(getviscol());
5024 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026}
5027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028/*
5029 * "gr".
5030 */
5031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (VIsual_active)
5035 {
5036 cap->cmdchar = 'r';
5037 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005038 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005039 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005041
zeertzjqf86dea82023-03-05 21:15:06 +00005042 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005043 return;
5044
5045 if (!curbuf->b_p_ma)
5046 emsg(_(e_cannot_make_changes_modifiable_is_off));
5047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005049 if (cap->extra_char == Ctrl_V) // get another character
5050 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005051 if (cap->extra_char < ' ')
5052 // Prefix a control character with CTRL-V to avoid it being used as
5053 // a command.
5054 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005055 stuffcharReadbuff(cap->extra_char);
5056 stuffcharReadbuff(ESC);
5057 if (virtual_active())
5058 coladvance(getviscol());
5059 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063/*
5064 * Swap case for "~" command, when it does not work like an operator.
5065 */
5066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005067n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
5069 long n;
5070 pos_T startpos;
5071 int did_change = 0;
5072#ifdef FEAT_NETBEANS_INTG
5073 pos_T pos;
5074 char_u *ptr;
5075 int count;
5076#endif
5077
5078 if (checkclearopq(cap->oap))
5079 return;
5080
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005081 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
5083 clearopbeep(cap->oap);
5084 return;
5085 }
5086
5087 prep_redo_cmd(cap);
5088
5089 if (u_save_cursor() == FAIL)
5090 return;
5091
5092 startpos = curwin->w_cursor;
5093#ifdef FEAT_NETBEANS_INTG
5094 pos = startpos;
5095#endif
5096 for (n = cap->count1; n > 0; --n)
5097 {
5098 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5099 inc_cursor();
5100 if (gchar_cursor() == NUL)
5101 {
5102 if (vim_strchr(p_ww, '~') != NULL
5103 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5104 {
5105#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005106 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 if (did_change)
5109 {
5110 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005111 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005112 netbeans_removed(curbuf, pos.lnum, pos.col,
5113 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005114 // line may have been flushed, get it again
5115 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005117 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 pos.col = 0;
5120 pos.lnum++;
5121 }
5122#endif
5123 ++curwin->w_cursor.lnum;
5124 curwin->w_cursor.col = 0;
5125 if (n > 1)
5126 {
5127 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5128 break;
5129 u_clearline();
5130 }
5131 }
5132 else
5133 break;
5134 }
5135 }
5136#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005137 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
5139 ptr = ml_get(pos.lnum);
5140 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005141 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5142 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144#endif
5145
5146
5147 check_cursor();
5148 curwin->w_set_curswant = TRUE;
5149 if (did_change)
5150 {
5151 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5152 0L);
5153 curbuf->b_op_start = startpos;
5154 curbuf->b_op_end = curwin->w_cursor;
5155 if (curbuf->b_op_end.col > 0)
5156 --curbuf->b_op_end.col;
5157 }
5158}
5159
5160/*
5161 * Move cursor to mark.
5162 */
5163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005164nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
5166 if (check_mark(pos) == FAIL)
5167 clearop(cap->oap);
5168 else
5169 {
5170 if (cap->cmdchar == '\''
5171 || cap->cmdchar == '`'
5172 || cap->cmdchar == '['
5173 || cap->cmdchar == ']')
5174 setpcmark();
5175 curwin->w_cursor = *pos;
5176 if (flag)
5177 beginline(BL_WHITE | BL_FIX);
5178 else
5179 check_cursor();
5180 }
5181 cap->oap->motion_type = flag ? MLINE : MCHAR;
5182 if (cap->cmdchar == '`')
5183 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005184 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 curwin->w_set_curswant = TRUE;
5186}
5187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188/*
5189 * Handle commands that are operators in Visual mode.
5190 */
5191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005192v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193{
5194 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005196 // Uppercase means linewise, except in block mode, then "D" deletes till
5197 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (isupper(cap->cmdchar))
5199 {
5200 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005201 {
5202 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5206 curwin->w_curswant = MAXCOL;
5207 }
5208 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5209 nv_operator(cap);
5210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
5212/*
5213 * "s" and "S" commands.
5214 */
5215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005216nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005218#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005219 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005220 if (term_swap_diff() == OK)
5221 return;
5222#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005223#ifdef FEAT_JOB_CHANNEL
5224 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5225 {
5226 clearopbeep(cap->oap);
5227 return;
5228 }
5229#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005230 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005233 {
5234 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 cap->cmdchar = 'c';
5238 nv_operator(cap);
5239 }
5240 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 nv_optrans(cap);
5242}
5243
5244/*
5245 * Abbreviated commands.
5246 */
5247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005248nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
5250 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005251 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005253 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (VIsual_active)
5255 v_visop(cap);
5256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 nv_optrans(cap);
5258}
5259
5260/*
5261 * Translate a command into another command.
5262 */
5263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005264nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5267 (char_u *)"d$", (char_u *)"c$",
5268 (char_u *)"cl", (char_u *)"cc",
5269 (char_u *)"yy", (char_u *)":s\r"};
5270 static char_u *str = (char_u *)"xXDCsSY&";
5271
5272 if (!checkclearopq(cap->oap))
5273 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005274 // In Vi "2D" doesn't delete the next line. Can't translate it
5275 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005276 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5277 {
5278 cap->oap->start = curwin->w_cursor;
5279 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005280#ifdef FEAT_EVAL
5281 set_op_var(OP_DELETE);
5282#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005283 cap->count1 = 1;
5284 nv_dollar(cap);
5285 finish_op = TRUE;
5286 ResetRedobuff();
5287 AppendCharToRedobuff('D');
5288 }
5289 else
5290 {
5291 if (cap->count0)
5292 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005293 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 cap->opcount = 0;
5297}
5298
5299/*
5300 * "'" and "`" commands. Also for "g'" and "g`".
5301 * cap->arg is TRUE for "'" and "g'".
5302 */
5303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005304nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
5306 pos_T *pos;
5307 int c;
5308#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005309 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005310 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#endif
5312
5313 if (cap->cmdchar == 'g')
5314 c = cap->extra_char;
5315 else
5316 c = cap->nchar;
5317 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005318 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 if (cap->arg)
5321 {
5322 check_cursor_lnum();
5323 beginline(BL_WHITE | BL_FIX);
5324 }
5325 else
5326 check_cursor();
5327 }
5328 else
5329 nv_cursormark(cap, cap->arg, pos);
5330
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005331 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (!virtual_active())
5333 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005334 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335#ifdef FEAT_FOLDING
5336 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005337 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005338 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 && (fdo_flags & FDO_MARK)
5340 && old_KeyTyped)
5341 foldOpenCursor();
5342#endif
5343}
5344
5345/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005346 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 */
5348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005349nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005352#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005354 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005357 if (checkclearopq(cap->oap))
5358 return;
5359
5360 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005362 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005364 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005366 if (cap->cmdchar == 'g')
5367 pos = movechangelist((int)cap->count1);
5368 else
5369 pos = movemark((int)cap->count1);
5370 if (pos == (pos_T *)-1) // jump to other file
5371 {
5372 curwin->w_set_curswant = TRUE;
5373 check_cursor();
5374 }
5375 else if (pos != NULL) // can jump
5376 nv_cursormark(cap, FALSE, pos);
5377 else if (cap->cmdchar == 'g')
5378 {
5379 if (curbuf->b_changelistlen == 0)
5380 emsg(_(e_changelist_is_empty));
5381 else if (cap->count1 < 0)
5382 emsg(_(e_at_start_of_changelist));
5383 else
5384 emsg(_(e_at_end_of_changelist));
5385 }
5386 else
5387 clearopbeep(cap->oap);
5388# ifdef FEAT_FOLDING
5389 if (cap->oap->op_type == OP_NOP
5390 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5391 && (fdo_flags & FDO_MARK)
5392 && old_KeyTyped)
5393 foldOpenCursor();
5394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395}
5396
5397/*
5398 * Handle '"' command.
5399 */
5400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005401nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 if (checkclearop(cap->oap))
5404 return;
5405#ifdef FEAT_EVAL
5406 if (cap->nchar == '=')
5407 cap->nchar = get_expr_register();
5408#endif
5409 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5410 {
5411 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005412 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#ifdef FEAT_EVAL
5414 set_reg_var(cap->oap->regname);
5415#endif
5416 }
5417 else
5418 clearopbeep(cap->oap);
5419}
5420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421/*
5422 * Handle "v", "V" and "CTRL-V" commands.
5423 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5424 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005425 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 */
5427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005430 if (cap->cmdchar == Ctrl_Q)
5431 cap->cmdchar = Ctrl_V;
5432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5434 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (cap->oap->op_type != OP_NOP)
5436 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005437 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005438 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 return;
5440 }
5441
5442 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005443 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005445 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 else // toggle char/block mode
5448 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 VIsual_mode = cap->cmdchar;
5450 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005451 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005453 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005458 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005460 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 VIsual = curwin->w_cursor;
5462
5463 VIsual_active = TRUE;
5464 VIsual_reselect = TRUE;
5465 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005469 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005471 // For V and ^V, we multiply the number of lines even if there
5472 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5474 {
5475 curwin->w_cursor.lnum +=
5476 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005477 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 }
5479 VIsual_mode = resel_VIsual_mode;
5480 if (VIsual_mode == 'v')
5481 {
5482 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005483 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005484 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005485 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5486 if (*p_sel != 'e')
5487 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005490 curwin->w_curswant = resel_VIsual_vcol;
5491 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005493 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
5495 curwin->w_curswant = MAXCOL;
5496 coladvance((colnr_T)MAXCOL);
5497 }
5498 else if (VIsual_mode == Ctrl_V)
5499 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005500 // Update curswant on the original line, that is where "col" is
5501 // valid.
5502 linenr_T lnum = curwin->w_cursor.lnum;
5503 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005504 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005505 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5506 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 coladvance(curwin->w_curswant);
5508 }
5509 else
5510 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005511 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
5513 else
5514 {
5515 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005516 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 may_start_select('c');
5518 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005519 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005521 if (cap->count0 > 0 && --cap->count1 > 0)
5522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005523 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005524 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5525 nv_right(cap);
5526 else if (VIsual_mode == 'V')
5527 nv_down(cap);
5528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530 }
5531}
5532
5533/*
5534 * Start selection for Shift-movement keys.
5535 */
5536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005539 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 may_start_select('k');
5541 n_start_visual_mode('v');
5542}
5543
5544/*
5545 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005546 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 */
5548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005549may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005551 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5552 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
5555/*
5556 * Start Visual mode "c".
5557 * Should set VIsual_select before calling this.
5558 */
5559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005560n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005562#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005563 int cursor_line_was_concealed = curwin->w_p_cole > 0
5564 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005565#endif
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 VIsual_mode = c;
5568 VIsual_active = TRUE;
5569 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005570
5571 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005572 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005573 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005574 {
5575 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 VIsual = curwin->w_cursor;
5579
5580#ifdef FEAT_FOLDING
5581 foldAdjustVisual();
5582#endif
5583
LemonBoy2bf52dd2022-04-09 18:17:34 +01005584 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005586#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005587 // Check if redraw is needed after changing the state.
5588 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005589#endif
5590
Bram Moolenaar09df3122006-01-23 22:23:09 +00005591 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005594 // Make sure the clipboard gets updated. Needed because start and
5595 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 clip_star.vmode = NUL;
5597#endif
5598
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 // Only need to redraw this line, unless still need to redraw an old
5600 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005601 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
5603 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5604 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5605 }
5606}
5607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609/*
5610 * CTRL-W: Window commands
5611 */
5612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005613nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005615 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005617 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005618 cap->cmdchar = ':';
5619 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005620 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005621 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005622 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626/*
5627 * CTRL-Z: Suspend
5628 */
5629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005630nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005635 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637
5638/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005639 * "gv": Reselect the previous Visual area. If Visual already active,
5640 * exchange previous and current Visual area.
5641 */
5642 static void
5643nv_gv_cmd(cmdarg_T *cap)
5644{
5645 pos_T tpos;
5646 int i;
5647
5648 if (checkclearop(cap->oap))
5649 return;
5650
5651 if (curbuf->b_visual.vi_start.lnum == 0
5652 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5653 || curbuf->b_visual.vi_end.lnum == 0)
5654 {
5655 beep_flush();
5656 return;
5657 }
5658
5659 // set w_cursor to the start of the Visual area, tpos to the end
5660 if (VIsual_active)
5661 {
5662 i = VIsual_mode;
5663 VIsual_mode = curbuf->b_visual.vi_mode;
5664 curbuf->b_visual.vi_mode = i;
5665# ifdef FEAT_EVAL
5666 curbuf->b_visual_mode_eval = i;
5667# endif
5668 i = curwin->w_curswant;
5669 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5670 curbuf->b_visual.vi_curswant = i;
5671
5672 tpos = curbuf->b_visual.vi_end;
5673 curbuf->b_visual.vi_end = curwin->w_cursor;
5674 curwin->w_cursor = curbuf->b_visual.vi_start;
5675 curbuf->b_visual.vi_start = VIsual;
5676 }
5677 else
5678 {
5679 VIsual_mode = curbuf->b_visual.vi_mode;
5680 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5681 tpos = curbuf->b_visual.vi_end;
5682 curwin->w_cursor = curbuf->b_visual.vi_start;
5683 }
5684
5685 VIsual_active = TRUE;
5686 VIsual_reselect = TRUE;
5687
5688 // Set Visual to the start and w_cursor to the end of the Visual
5689 // area. Make sure they are on an existing character.
5690 check_cursor();
5691 VIsual = curwin->w_cursor;
5692 curwin->w_cursor = tpos;
5693 check_cursor();
5694 update_topline();
5695
5696 // When called from normal "g" command: start Select mode when
5697 // 'selectmode' contains "cmd". When called for K_SELECT, always
5698 // start Select mode.
5699 if (cap->arg)
5700 {
5701 VIsual_select = TRUE;
5702 VIsual_select_reg = 0;
5703 }
5704 else
5705 may_start_select('c');
5706 setmouse();
5707#ifdef FEAT_CLIPBOARD
5708 // Make sure the clipboard gets updated. Needed because start and
5709 // end are still the same, and the selection needs to be owned
5710 clip_star.vmode = NUL;
5711#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005712 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005713 showmode();
5714}
5715
5716/*
5717 * "g0", "g^" : Like "0" and "^" but for screen lines.
5718 * "gm": middle of "g0" and "g$".
5719 */
5720 static void
5721nv_g_home_m_cmd(cmdarg_T *cap)
5722{
5723 int i;
5724 int flag = FALSE;
5725
5726 if (cap->nchar == '^')
5727 flag = TRUE;
5728
5729 cap->oap->motion_type = MCHAR;
5730 cap->oap->inclusive = FALSE;
5731 if (curwin->w_p_wrap && curwin->w_width != 0)
5732 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005733 int width1 = curwin->w_width - curwin_col_off();
5734 int width2 = width1 + curwin_col_off2();
5735 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005736
5737 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005738 virtcol = curwin->w_virtcol
5739#ifdef FEAT_PROP_POPUP
5740 - curwin->w_virtcol_first_char
5741#endif
5742 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005743 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005744 if (virtcol >= (colnr_T)width1 && width2 > 0)
5745 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005746 }
5747 else
5748 i = curwin->w_leftcol;
5749 // Go to the middle of the screen line. When 'number' or
5750 // 'relativenumber' is on and lines are wrapping the middle can be more
5751 // to the left.
5752 if (cap->nchar == 'm')
5753 i += (curwin->w_width - curwin_col_off()
5754 + ((curwin->w_p_wrap && i > 0)
5755 ? curwin_col_off2() : 0)) / 2;
5756 coladvance((colnr_T)i);
5757 if (flag)
5758 {
5759 do
5760 i = gchar_cursor();
5761 while (VIM_ISWHITE(i) && oneright() == OK);
5762 curwin->w_valid &= ~VALID_WCOL;
5763 }
5764 curwin->w_set_curswant = TRUE;
Luuk van Baal8667a562023-05-12 15:47:25 +01005765 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005766}
5767
5768/*
5769 * "g_": to the last non-blank character in the line or <count> lines
5770 * downward.
5771 */
5772 static void
5773nv_g_underscore_cmd(cmdarg_T *cap)
5774{
5775 char_u *ptr;
5776
5777 cap->oap->motion_type = MCHAR;
5778 cap->oap->inclusive = TRUE;
5779 curwin->w_curswant = MAXCOL;
5780 if (cursor_down((long)(cap->count1 - 1),
5781 cap->oap->op_type == OP_NOP) == FAIL)
5782 {
5783 clearopbeep(cap->oap);
5784 return;
5785 }
5786
5787 ptr = ml_get_curline();
5788
5789 // In Visual mode we may end up after the line.
5790 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5791 --curwin->w_cursor.col;
5792
5793 // Decrease the cursor column until it's on a non-blank.
5794 while (curwin->w_cursor.col > 0
5795 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5796 --curwin->w_cursor.col;
5797 curwin->w_set_curswant = TRUE;
5798 adjust_for_sel(cap);
5799}
5800
5801/*
5802 * "g$" : Like "$" but for screen lines.
5803 */
5804 static void
5805nv_g_dollar_cmd(cmdarg_T *cap)
5806{
5807 oparg_T *oap = cap->oap;
5808 int i;
5809 int col_off = curwin_col_off();
5810
5811 oap->motion_type = MCHAR;
5812 oap->inclusive = TRUE;
5813 if (curwin->w_p_wrap && curwin->w_width != 0)
5814 {
5815 curwin->w_curswant = MAXCOL; // so we stay at the end
5816 if (cap->count1 == 1)
5817 {
5818 int width1 = curwin->w_width - col_off;
5819 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005820 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005821
5822 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005823 virtcol = curwin->w_virtcol
5824#ifdef FEAT_PROP_POPUP
5825 - curwin->w_virtcol_first_char
5826#endif
5827 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005828 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005829 if (virtcol >= (colnr_T)width1)
5830 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005831 * width2;
5832 coladvance((colnr_T)i);
5833
5834 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005835 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005836 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5837 {
5838 // Check for landing on a character that got split at
5839 // the end of the line. We do not want to advance to
5840 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005841 if (curwin->w_virtcol
5842#ifdef FEAT_PROP_POPUP
5843 - curwin->w_virtcol_first_char
5844#endif
5845 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005846 --curwin->w_cursor.col;
5847 }
5848 }
5849 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5850 clearopbeep(oap);
5851 }
5852 else
5853 {
5854 if (cap->count1 > 1)
5855 // if it fails, let the cursor still move to the last char
5856 (void)cursor_down(cap->count1 - 1, FALSE);
5857
5858 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5859 coladvance((colnr_T)i);
5860
5861 // if the character doesn't fit move one back
5862 if (curwin->w_cursor.col > 0
5863 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5864 {
5865 colnr_T vcol;
5866
5867 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5868 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5869 --curwin->w_cursor.col;
5870 }
5871
5872 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005873 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005874 }
5875}
5876
5877/*
5878 * "gi": start Insert at the last position.
5879 */
5880 static void
5881nv_gi_cmd(cmdarg_T *cap)
5882{
5883 int i;
5884
5885 if (curbuf->b_last_insert.lnum != 0)
5886 {
5887 curwin->w_cursor = curbuf->b_last_insert;
5888 check_cursor_lnum();
5889 i = (int)STRLEN(ml_get_curline());
5890 if (curwin->w_cursor.col > (colnr_T)i)
5891 {
5892 if (virtual_active())
5893 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5894 curwin->w_cursor.col = i;
5895 }
5896 }
5897 cap->cmdchar = 'i';
5898 nv_edit(cap);
5899}
5900
5901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 * Commands starting with "g".
5903 */
5904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005905nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
5910 switch (cap->nchar)
5911 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005912 case Ctrl_A:
5913 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005915 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005916 if (!VIsual_active && cap->nchar == Ctrl_A)
5917 vim_mem_profile_dump();
5918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005920 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005921 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005922 {
5923 cap->arg = TRUE;
5924 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005925 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005926 nv_addsub(cap);
5927 }
5928 else
5929 clearopbeep(oap);
5930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005932 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case 'R':
5934 cap->arg = TRUE;
5935 nv_Replace(cap);
5936 break;
5937
5938 case 'r':
5939 nv_vreplace(cap);
5940 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
5942 case '&':
5943 do_cmdline_cmd((char_u *)"%s//~/&");
5944 break;
5945
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005946 // "gv": Reselect the previous Visual area. If Visual already active,
5947 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005949 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005951
5952 // "gV": Don't reselect the previous Visual area after a Select mode
5953 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 case 'V':
5955 VIsual_reselect = FALSE;
5956 break;
5957
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005958 // "gh": start Select mode.
5959 // "gH": start Select line mode.
5960 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 case K_BS:
5962 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005963 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 case 'h':
5965 case 'H':
5966 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 cap->cmdchar = cap->nchar + ('v' - 'h');
5968 cap->arg = TRUE;
5969 nv_visual(cap);
5970 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005972 // "gn", "gN" visually select next/previous search match
5973 // "gn" selects next match
5974 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005975 case 'N':
5976 case 'n':
5977 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005978 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005981 // "gj" and "gk" two new funny movement keys -- up and down
5982 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 case 'j':
5984 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005985 // with 'nowrap' it works just like the normal "j" command.
5986 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
5988 oap->motion_type = MLINE;
5989 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5990 }
5991 else
5992 i = nv_screengo(oap, FORWARD, cap->count1);
5993 if (i == FAIL)
5994 clearopbeep(oap);
5995 break;
5996
5997 case 'k':
5998 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005999 // with 'nowrap' it works just like the normal "k" command.
6000 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
6002 oap->motion_type = MLINE;
6003 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6004 }
6005 else
6006 i = nv_screengo(oap, BACKWARD, cap->count1);
6007 if (i == FAIL)
6008 clearopbeep(oap);
6009 break;
6010
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006011 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 case 'J':
6013 nv_join(cap);
6014 break;
6015
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006016 // "g0", "g^" : Like "0" and "^" but for screen lines.
6017 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 case '0':
6020 case 'm':
6021 case K_HOME:
6022 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006023 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 break;
6025
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006026 case 'M':
6027 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006028 oap->motion_type = MCHAR;
6029 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006030 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006031 if (cap->count0 > 0 && cap->count0 <= 100)
6032 coladvance((colnr_T)(i * cap->count0 / 100));
6033 else
6034 coladvance((colnr_T)(i / 2));
6035 curwin->w_set_curswant = TRUE;
6036 }
6037 break;
6038
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006039 // "g_": to the last non-blank character in the line or <count> lines
6040 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006042 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 break;
6044
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006045 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 case '$':
6047 case K_END:
6048 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006049 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 break;
6051
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006052 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 case '*':
6054 case '#':
6055#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006056 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006058 case Ctrl_RSB: // :tag or :tselect for current identifier
6059 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 nv_ident(cap);
6061 break;
6062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'e':
6065 case 'E':
6066 oap->motion_type = MCHAR;
6067 curwin->w_set_curswant = TRUE;
6068 oap->inclusive = TRUE;
6069 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6070 clearopbeep(oap);
6071 break;
6072
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006073 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006075 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 break;
6077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006080 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 break;
6082
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case 'I':
6085 beginline(0);
6086 if (!checkclearopq(oap))
6087 invoke_edit(cap, FALSE, 'g', FALSE);
6088 break;
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "gf": goto file, edit file under cursor
6091 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006093 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 nv_gotofile(cap);
6095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006097 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 case '\'':
6099 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006100 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 case '`':
6102 nv_gomark(cap);
6103 break;
6104
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006105 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006107 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 break;
6109
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006110 // "ga": Display the ascii value of the character under the
6111 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case 'a':
6113 do_ascii(NULL);
6114 break;
6115
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006116 // "g8": Display the bytes used for the UTF-8 character under the
6117 // cursor. It is displayed in hex.
6118 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006120 if (cap->count0 == 8)
6121 utf_find_illegal();
6122 else
6123 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006126 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006127 case '<':
6128 show_sb_text();
6129 break;
6130
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006131 // "gg": Goto the first line in file. With a count it goes to
6132 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 case 'g':
6134 cap->arg = FALSE;
6135 nv_goto(cap);
6136 break;
6137
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006138 // Two-character operators:
6139 // "gq" Format text
6140 // "gw" Format text and keep cursor position
6141 // "g~" Toggle the case of the text.
6142 // "gu" Change text to lower case.
6143 // "gU" Change text to upper case.
6144 // "g?" rot13 encoding
6145 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case 'q':
6147 case 'w':
6148 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006149 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 case '~':
6151 case 'u':
6152 case 'U':
6153 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006154 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 nv_operator(cap);
6156 break;
6157
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006158 // "gd": Find first occurrence of pattern under the cursor in the
6159 // current function
6160 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 case 'd':
6162 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006163 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 break;
6165
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006166 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 case K_MIDDLEMOUSE:
6168 case K_MIDDLEDRAG:
6169 case K_MIDDLERELEASE:
6170 case K_LEFTMOUSE:
6171 case K_LEFTDRAG:
6172 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006173 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 case K_RIGHTMOUSE:
6175 case K_RIGHTDRAG:
6176 case K_RIGHTRELEASE:
6177 case K_X1MOUSE:
6178 case K_X1DRAG:
6179 case K_X1RELEASE:
6180 case K_X2MOUSE:
6181 case K_X2DRAG:
6182 case K_X2RELEASE:
6183 mod_mask = MOD_MASK_CTRL;
6184 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
6187 case K_IGNORE:
6188 break;
6189
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006190 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 case 'p':
6192 case 'P':
6193 nv_put(cap);
6194 break;
6195
6196#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006197 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 case 'o':
6199 goto_byte(cap->count0);
6200 break;
6201#endif
6202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006203 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006205 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 do_exmode(TRUE);
6207 break;
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 case ',':
6210 nv_pcmark(cap);
6211 break;
6212
6213 case ';':
6214 cap->count1 = -cap->count1;
6215 nv_pcmark(cap);
6216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006219 if (!checkclearop(oap))
6220 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006222 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006223 if (!checkclearop(oap))
6224 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006225 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226
Bram Moolenaar62a23252020-08-09 14:04:42 +02006227 case TAB:
6228 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6229 clearopbeep(oap);
6230 break;
6231
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006232 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006233 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006234 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006235 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006236 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006237 break;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 default:
6240 clearopbeep(oap);
6241 break;
6242 }
6243}
6244
6245/*
6246 * Handle "o" and "O" commands.
6247 */
6248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006249n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006251#ifdef FEAT_CONCEAL
6252 linenr_T oldline = curwin->w_cursor.lnum;
6253#endif
6254
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006255 if (checkclearopq(cap->oap))
6256 return;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006259 if (cap->cmdchar == 'O')
6260 // Open above the first line of a folded sequence of lines
6261 (void)hasFolding(curwin->w_cursor.lnum,
6262 &curwin->w_cursor.lnum, NULL);
6263 else
6264 // Open below the last line of a folded sequence of lines
6265 (void)hasFolding(curwin->w_cursor.lnum,
6266 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006268 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6269 (cap->cmdchar == 'O' ? 1 : 0)),
6270 (linenr_T)(curwin->w_cursor.lnum +
6271 (cap->cmdchar == 'o' ? 1 : 0))
6272 ) == OK
6273 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6274 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6275 0, NULL) == OK)
6276 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006277#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006278 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6279 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006280#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006281#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006282 if (curwin->w_p_cul)
6283 // force redraw of cursorline
6284 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006285#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006286 // When '#' is in 'cpoptions' ignore the count.
6287 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6288 cap->count1 = 1;
6289 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 }
6291}
6292
6293/*
6294 * "." command: redo last change.
6295 */
6296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006297nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006299 if (checkclearopq(cap->oap))
6300 return;
6301
6302 // If "restart_edit" is TRUE, the last but one command is repeated
6303 // instead of the last command (inserting text). This is used for
6304 // CTRL-O <.> in insert mode.
6305 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6306 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307}
6308
6309/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006310 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 */
6312 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006313nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006315 if (VIsual_select && VIsual_active)
6316 {
6317 int reg;
6318 // Get register name
6319 ++no_mapping;
6320 ++allow_keys;
6321 reg = plain_vgetc();
6322 LANGMAP_ADJUST(reg, TRUE);
6323 --no_mapping;
6324 --allow_keys;
6325
6326 if (reg == '"')
6327 // the unnamed register is 0
6328 reg = 0;
6329
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006330 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006331 return;
6332 }
6333
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006334 if (checkclearopq(cap->oap))
6335 return;
6336
6337 u_redo((int)cap->count1);
6338 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339}
6340
6341/*
6342 * Handle "U" command.
6343 */
6344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006345nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006347 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006348 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006350 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 cap->cmdchar = 'g';
6352 cap->nchar = 'U';
6353 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006354 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006356
6357 if (checkclearopq(cap->oap))
6358 return;
6359
6360 u_undoline();
6361 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362}
6363
6364/*
6365 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6366 * single character.
6367 */
6368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006369nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006371 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006372 {
6373#ifdef FEAT_JOB_CHANNEL
6374 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6375 {
6376 clearopbeep(cap->oap);
6377 return;
6378 }
6379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 else
6383 nv_operator(cap);
6384}
6385
6386/*
6387 * Handle an operator command.
6388 * The actual work is done by do_pending_operator().
6389 */
6390 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006391nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 int op_type;
6394
6395 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006396#ifdef FEAT_JOB_CHANNEL
6397 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6398 {
6399 clearopbeep(cap->oap);
6400 return;
6401 }
6402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006404 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 nv_lineop(cap);
6406 else if (!checkclearop(cap->oap))
6407 {
6408 cap->oap->start = curwin->w_cursor;
6409 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006410#ifdef FEAT_EVAL
6411 set_op_var(op_type);
6412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 }
6414}
6415
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006416#ifdef FEAT_EVAL
6417/*
6418 * Set v:operator to the characters for "optype".
6419 */
6420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006421set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006422{
6423 char_u opchars[3];
6424
6425 if (optype == OP_NOP)
6426 set_vim_var_string(VV_OP, NULL, 0);
6427 else
6428 {
6429 opchars[0] = get_op_char(optype);
6430 opchars[1] = get_extra_op_char(optype);
6431 opchars[2] = NUL;
6432 set_vim_var_string(VV_OP, opchars, -1);
6433 }
6434}
6435#endif
6436
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437/*
6438 * Handle linewise operator "dd", "yy", etc.
6439 *
6440 * "_" is is a strange motion command that helps make operators more logical.
6441 * It is actually implemented, but not documented in the real Vi. This motion
6442 * command actually refers to "the current line". Commands like "dd" and "yy"
6443 * are really an alternate form of "d_" and "y_". It does accept a count, so
6444 * "d3_" works to delete 3 lines.
6445 */
6446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006447nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 cap->oap->motion_type = MLINE;
6450 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6451 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006452 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006453 && cap->oap->motion_force != 'v'
6454 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 || cap->oap->op_type == OP_LSHIFT
6456 || cap->oap->op_type == OP_RSHIFT)
6457 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006458 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 beginline(BL_WHITE | BL_FIX);
6460}
6461
6462/*
6463 * <Home> command.
6464 */
6465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006466nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006468 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006469 if (mod_mask & MOD_MASK_CTRL)
6470 nv_goto(cap);
6471 else
6472 {
6473 cap->count0 = 1;
6474 nv_pipe(cap);
6475 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006476 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6477 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478}
6479
6480/*
6481 * "|" command.
6482 */
6483 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006484nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
6486 cap->oap->motion_type = MCHAR;
6487 cap->oap->inclusive = FALSE;
6488 beginline(0);
6489 if (cap->count0 > 0)
6490 {
6491 coladvance((colnr_T)(cap->count0 - 1));
6492 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6493 }
6494 else
6495 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006496 // keep curswant at the column where we wanted to go, not where
6497 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 curwin->w_set_curswant = FALSE;
6499}
6500
6501/*
6502 * Handle back-word command "b" and "B".
6503 * cap->arg is 1 for "B"
6504 */
6505 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006506nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 cap->oap->motion_type = MCHAR;
6509 cap->oap->inclusive = FALSE;
6510 curwin->w_set_curswant = TRUE;
6511 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6512 clearopbeep(cap->oap);
6513#ifdef FEAT_FOLDING
6514 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6515 foldOpenCursor();
6516#endif
6517}
6518
6519/*
6520 * Handle word motion commands "e", "E", "w" and "W".
6521 * cap->arg is TRUE for "E" and "W".
6522 */
6523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006524nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 int n;
6527 int word_end;
6528 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006529 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006531 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6533 word_end = TRUE;
6534 else
6535 word_end = FALSE;
6536 cap->oap->inclusive = word_end;
6537
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006538 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (!word_end && cap->oap->op_type == OP_CHANGE)
6540 {
6541 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006542 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006544 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006546 // Reproduce a funny Vi behaviour: "cw" on a blank only
6547 // changes one character, not all blanks until the start of
6548 // the next word. Only do this when the 'w' flag is included
6549 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6551 {
6552 cap->oap->inclusive = TRUE;
6553 cap->oap->motion_type = MCHAR;
6554 return;
6555 }
6556 }
6557 else
6558 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006559 // This is a little strange. To match what the real Vi does,
6560 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6561 // that we are not on a space or a TAB. This seems impolite
6562 // at first, but it's really more what we mean when we say
6563 // 'cw'.
6564 // Another strangeness: When standing on the end of a word
6565 // "ce" will change until the end of the next word, but "cw"
6566 // will change only one character! This is done by setting
6567 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 cap->oap->inclusive = TRUE;
6569 word_end = TRUE;
6570 flag = TRUE;
6571 }
6572 }
6573 }
6574
6575 cap->oap->motion_type = MCHAR;
6576 curwin->w_set_curswant = TRUE;
6577 if (word_end)
6578 n = end_word(cap->count1, cap->arg, flag, FALSE);
6579 else
6580 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6581
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006582 // Don't leave the cursor on the NUL past the end of line. Unless we
6583 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006584 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006585 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
6587 if (n == FAIL && cap->oap->op_type == OP_NOP)
6588 clearopbeep(cap->oap);
6589 else
6590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592#ifdef FEAT_FOLDING
6593 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6594 foldOpenCursor();
6595#endif
6596 }
6597}
6598
6599/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006600 * Used after a movement command: If the cursor ends up on the NUL after the
6601 * end of the line, may move it back to the last character and make the motion
6602 * inclusive.
6603 */
6604 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006605adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006606{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006607 // The cursor cannot remain on the NUL when:
6608 // - the column is > 0
6609 // - not in Visual mode or 'selection' is "o"
6610 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006611 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006612 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006613 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006614 {
6615 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006616 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006617 if (has_mbyte)
6618 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006619 oap->inclusive = TRUE;
6620 }
6621}
6622
6623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 * "0" and "^" commands.
6625 * cap->arg is the argument for beginline().
6626 */
6627 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 cap->oap->motion_type = MCHAR;
6631 cap->oap->inclusive = FALSE;
6632 beginline(cap->arg);
6633#ifdef FEAT_FOLDING
6634 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6635 foldOpenCursor();
6636#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006637 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6638 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641/*
6642 * In exclusive Visual mode, may include the last character.
6643 */
6644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006645adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006648 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (has_mbyte)
6651 inc_cursor();
6652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 ++curwin->w_cursor.col;
6654 cap->oap->inclusive = FALSE;
6655 }
6656}
6657
6658/*
6659 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6660 * Should check VIsual_mode before calling this.
6661 * Returns TRUE when backed up to the previous line.
6662 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006663 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006664unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 pos_T *pp;
6667
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006668 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006670 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 pp = &curwin->w_cursor;
6672 else
6673 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if (pp->coladd > 0)
6675 --pp->coladd;
6676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (pp->col > 0)
6678 {
6679 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006680 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 }
6682 else if (pp->lnum > 1)
6683 {
6684 --pp->lnum;
6685 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6686 return TRUE;
6687 }
6688 }
6689 return FALSE;
6690}
6691
6692/*
6693 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6694 */
6695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006696nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006701 VIsual_select_reg = 0;
6702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 else if (VIsual_reselect)
6704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006705 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 cap->arg = TRUE;
6707 nv_g_cmd(cap);
6708 }
6709}
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712/*
6713 * "G", "gg", CTRL-END, CTRL-HOME.
6714 * cap->arg is TRUE for "G".
6715 */
6716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006717nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 linenr_T lnum;
6720
6721 if (cap->arg)
6722 lnum = curbuf->b_ml.ml_line_count;
6723 else
6724 lnum = 1L;
6725 cap->oap->motion_type = MLINE;
6726 setpcmark();
6727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006728 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (cap->count0 != 0)
6730 lnum = cap->count0;
6731 if (lnum < 1L)
6732 lnum = 1L;
6733 else if (lnum > curbuf->b_ml.ml_line_count)
6734 lnum = curbuf->b_ml.ml_line_count;
6735 curwin->w_cursor.lnum = lnum;
6736 beginline(BL_SOL | BL_FIX);
6737#ifdef FEAT_FOLDING
6738 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6739 foldOpenCursor();
6740#endif
6741}
6742
6743/*
6744 * CTRL-\ in Normal mode.
6745 */
6746 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006747nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6750 {
6751 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006752 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006753 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (cmdwin_type != 0)
6756 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (VIsual_active)
6758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006759 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006760 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006762 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (cap->nchar == Ctrl_G && p_im)
6764 restart_edit = 'a';
6765 }
6766 else
6767 clearopbeep(cap->oap);
6768}
6769
6770/*
6771 * ESC in Normal mode: beep, but don't flush buffers.
6772 * Don't even beep if we are canceling a command.
6773 */
6774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 int no_reason;
6778
6779 no_reason = (cap->oap->op_type == OP_NOP
6780 && cap->opcount == 0
6781 && cap->count0 == 0
6782 && cap->oap->regname == 0
6783 && !p_im);
6784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006785 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006787 if (restart_edit == 0 && cmdwin_type == 0
6788 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006789 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006790 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6791
Bram Moolenaar5939c352022-09-24 12:50:45 +01006792 // The user may accidentally do "vim file | grep word" and then
6793 // CTRL-C doesn't show anything. With a changed buffer give the
6794 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006795 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006796 {
6797 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6798
6799 if (out_redir)
6800 mch_errmsg(ms);
6801 else
6802 msg(ms);
6803 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006804 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006805 {
6806 if (out_redir)
6807 {
6808 got_int = FALSE;
6809 do_cmdline_cmd((char_u *)"qa");
6810 }
6811 else
6812 msg(_("Type :qa and press <Enter> to exit Vim"));
6813 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006816 if (restart_edit != 0)
6817 redraw_mode = TRUE; // remove "-- (insert) --"
6818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006819 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6820 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (!p_im)
6822 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (cmdwin_type != 0)
6824 {
6825 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006826 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 return;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006830 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006831 {
6832 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006833 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6834 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006835 cmdwin_result = K_IGNORE;
6836 return;
6837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (VIsual_active)
6840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006841 end_visual_mode(); // stop Visual
6842 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006844 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006846 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006847 {
6848#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006849 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006850 popup_hide_message_win();
6851 else
6852#endif
6853 vim_beep(BO_ESC);
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 clearop(cap->oap);
6856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006857 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6858 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006859 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 restart_edit = 'a';
6861}
6862
6863/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006864 * Move the cursor for the "A" command.
6865 */
6866 void
6867set_cursor_for_append_to_line(void)
6868{
6869 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006870 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006871 {
6872 int save_State = State;
6873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 // Pretend Insert mode here to allow the cursor on the
6875 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006876 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006877 coladvance((colnr_T)MAXCOL);
6878 State = save_State;
6879 }
6880 else
6881 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6882}
6883
6884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006886 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 */
6888 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006891 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6893 cap->cmdchar = 'i';
6894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006895 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006897 {
6898#ifdef FEAT_TERMINAL
6899 if (term_in_normal_mode())
6900 {
6901 end_visual_mode();
6902 clearop(cap->oap);
6903 term_enter_job_mode();
6904 return;
6905 }
6906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006911 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6912 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006916#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006917 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006918 {
6919 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006920 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006921 return;
6922 }
6923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 else if (!curbuf->b_p_ma && !p_im)
6925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006927 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006929 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006930 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006931 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006933 else if (cap->cmdchar == K_PS && VIsual_active)
6934 {
6935 pos_T old_pos = curwin->w_cursor;
6936 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006937 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006939 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006940 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6941 {
6942 shift_delete_registers();
6943 cap->oap->regname = '1';
6944 }
6945 else
6946 cap->oap->regname = '-';
6947 cap->cmdchar = 'd';
6948 cap->nchar = NUL;
6949 nv_operator(cap);
6950 do_pending_operator(cap, 0, FALSE);
6951 cap->cmdchar = K_PS;
6952
zeertzjq7a732522022-03-14 20:46:41 +00006953 if (*ml_get_cursor() != NUL)
6954 {
6955 if (old_visual_mode == 'V')
6956 {
6957 // In linewise Visual mode insert before the beginning of the
6958 // next line.
6959 // When the last line in the buffer was deleted then create a
6960 // new line, otherwise there is not need to move cursor.
6961 // Detect this by checking if cursor moved above Visual area.
6962 if (curwin->w_cursor.lnum < old_pos.lnum
6963 && curwin->w_cursor.lnum < old_visual.lnum)
6964 {
6965 if (u_save_cursor() == OK)
6966 {
6967 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6968 FALSE);
6969 appended_lines(curwin->w_cursor.lnum++, 1L);
6970 }
6971 }
6972 }
6973 // When the last char in the line was deleted then append.
6974 // Detect this by checking if cursor moved before Visual area.
6975 else if (curwin->w_cursor.col < old_pos.col
6976 && curwin->w_cursor.col < old_visual.col)
6977 inc_cursor();
6978 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006981 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 else if (!checkclearopq(cap->oap))
6984 {
6985 switch (cap->cmdchar)
6986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006987 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006988 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 break;
6990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006992 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6993 beginline(BL_WHITE);
6994 else
6995 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 break;
6997
Bram Moolenaara1891842017-02-04 21:34:31 +01006998 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006999 // Bracketed paste works like "a"ppend, unless the cursor is in
7000 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007001 if (curwin->w_cursor.col == 0)
7002 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 case 'a': // "a"ppend is like "i"nsert on the next character.
7006 // increment coladd when in virtual space, increment the
7007 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (virtual_active()
7009 && (curwin->w_cursor.coladd > 0
7010 || *ml_get_cursor() == NUL
7011 || *ml_get_cursor() == TAB))
7012 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007013 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 inc_cursor();
7015 break;
7016 }
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7019 {
7020 int save_State = State;
7021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007022 // Pretend Insert mode here to allow the cursor on the
7023 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007024 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 coladvance(getviscol());
7026 State = save_State;
7027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
7029 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7030 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007031 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007032 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007033 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034}
7035
7036/*
7037 * Invoke edit() and take care of "restart_edit" and the return value.
7038 */
7039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007040invoke_edit(
7041 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007043 int cmd,
7044 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 int restart_edit_save = 0;
7047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007048 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7049 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7050 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 if (repl || !stuff_empty())
7052 restart_edit_save = restart_edit;
7053 else
7054 restart_edit_save = 0;
7055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 restart_edit = 0;
7058
7059 if (edit(cmd, startln, cap->count1))
7060 cap->retval |= CA_COMMAND_BUSY;
7061
7062 if (restart_edit == 0)
7063 restart_edit = restart_edit_save;
7064}
7065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066/*
7067 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7068 */
7069 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007070nv_object(
7071 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 int flag;
7074 int include;
7075 char_u *mps_save;
7076
7077 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007080 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007082 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 mps_save = curbuf->b_p_mps;
7084 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7085
7086 switch (cap->nchar)
7087 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 flag = current_word(cap->oap, cap->count1, include, FALSE);
7090 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007091 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 flag = current_word(cap->oap, cap->count1, include, TRUE);
7093 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 case '(':
7096 case ')':
7097 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7098 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 case '{':
7101 case '}':
7102 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7103 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 case ']':
7106 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7107 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 case '>':
7110 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7111 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007112#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007113 case 't': // "at" = a tag block (xml and html)
7114 // Do not adjust oap->end in do_pending_operator()
7115 // otherwise there are different results for 'dit'
7116 // (note leading whitespace in last line):
7117 // 1) <b> 2) <b>
7118 // foobar foobar
7119 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007120 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007121 flag = current_tagblock(cap->oap, cap->count1, include);
7122 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007123#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 flag = current_par(cap->oap, cap->count1, include, 'p');
7126 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007127 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 flag = current_sent(cap->oap, cap->count1, include);
7129 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007130 case '"': // "a"" = a double quoted string
7131 case '\'': // "a'" = a single quoted string
7132 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007133 flag = current_quote(cap->oap, cap->count1, include,
7134 cap->nchar);
7135 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136#if 0 // TODO
7137 case 'S': // "aS" = a section
7138 case 'f': // "af" = a filename
7139 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140#endif
7141 default:
7142 flag = FAIL;
7143 break;
7144 }
7145
7146 curbuf->b_p_mps = mps_save;
7147 if (flag == FAIL)
7148 clearopbeep(cap->oap);
7149 adjust_cursor_col();
7150 curwin->w_set_curswant = TRUE;
7151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153/*
7154 * "q" command: Start/stop recording.
7155 * "q:", "q/", "q?": edit command-line in command-line window.
7156 */
7157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007158nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
7160 if (cap->oap->op_type == OP_FORMAT)
7161 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007162 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 cap->cmdchar = 'g';
7164 cap->nchar = 'q';
7165 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007166 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007168
7169 if (checkclearop(cap->oap))
7170 return;
7171
7172 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007174 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007176 emsg(_(e_cmdline_window_already_open));
7177 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007179 stuffcharReadbuff(cap->nchar);
7180 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007182 else
7183 // (stop) recording into a named register, unless executing a
7184 // register
7185 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7186 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187}
7188
7189/*
7190 * Handle the "@r" command.
7191 */
7192 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007193nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 if (checkclearop(cap->oap))
7196 return;
7197#ifdef FEAT_EVAL
7198 if (cap->nchar == '=')
7199 {
7200 if (get_expr_register() == NUL)
7201 return;
7202 }
7203#endif
7204 while (cap->count1-- && !got_int)
7205 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007206 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
7208 clearopbeep(cap->oap);
7209 break;
7210 }
7211 line_breakcheck();
7212 }
7213}
7214
7215/*
7216 * Handle the CTRL-U and CTRL-D commands.
7217 */
7218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007219nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7222 || (cap->cmdchar == Ctrl_D
7223 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7224 clearopbeep(cap->oap);
7225 else if (!checkclearop(cap->oap))
7226 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7227}
7228
7229/*
7230 * Handle "J" or "gJ" command.
7231 */
7232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007233nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007235 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007237 nv_operator(cap);
7238 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007240
7241 if (checkclearop(cap->oap))
7242 return;
7243
7244 if (cap->count0 <= 1)
7245 cap->count0 = 2; // default for join is two lines!
7246 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7247 curbuf->b_ml.ml_line_count)
7248 {
7249 // can't join when on the last line
7250 if (cap->count0 <= 2)
7251 {
7252 clearopbeep(cap->oap);
7253 return;
7254 }
7255 cap->count0 = curbuf->b_ml.ml_line_count
7256 - curwin->w_cursor.lnum + 1;
7257 }
7258
7259 prep_redo(cap->oap->regname, cap->count0,
7260 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7261 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262}
7263
7264/*
7265 * "P", "gP", "p" and "gp" commands.
7266 */
7267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007268nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007270 nv_put_opt(cap, FALSE);
7271}
7272
7273/*
7274 * "P", "gP", "p" and "gp" commands.
7275 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7276 */
7277 static void
7278nv_put_opt(cmdarg_T *cap, int fix_indent)
7279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 int regname = 0;
7281 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007282 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007283 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 int dir;
7285 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007286 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
7288 if (cap->oap->op_type != OP_NOP)
7289 {
7290#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007291 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7293 {
7294 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007295 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297 else
7298#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007299 clearopbeep(cap->oap);
7300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007302
Bram Moolenaarf2732452018-06-03 14:47:35 +02007303#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007304 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007305 {
7306 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007307 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007308 }
7309#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007310
7311 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007313 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7314 ? FORWARD : BACKWARD;
7315 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007317 else
7318 dir = (cap->cmdchar == 'P'
7319 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7320 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7321 prep_redo_cmd(cap);
7322 if (cap->cmdchar == 'g')
7323 flags |= PUT_CURSEND;
7324 else if (cap->cmdchar == 'z')
7325 flags |= PUT_BLOCK_INNER;
7326
7327 if (VIsual_active)
7328 {
7329 // Putting in Visual mode: The put text replaces the selected
7330 // text. First delete the selected text, then put the new text.
7331 // Need to save and restore the registers that the delete
7332 // overwrites if the old contents is being put.
7333 was_visual = TRUE;
7334 regname = cap->oap->regname;
7335 keep_registers = cap->cmdchar == 'P';
7336#ifdef FEAT_CLIPBOARD
7337 adjust_clip_reg(&regname);
7338#endif
7339 if (regname == 0 || regname == '"'
7340 || VIM_ISDIGIT(regname) || regname == '-'
7341#ifdef FEAT_CLIPBOARD
7342 || (clip_unnamed && (regname == '*' || regname == '+'))
7343#endif
7344
7345 )
7346 {
7347 // The delete is going to overwrite the register we want to
7348 // put, save it first.
7349 reg1 = get_register(regname, TRUE);
7350 }
7351
7352 // Now delete the selected text. Avoid messages here.
7353 cap->cmdchar = 'd';
7354 cap->nchar = NUL;
7355 cap->oap->regname = keep_registers ? '_' : NUL;
7356 ++msg_silent;
7357 nv_operator(cap);
7358 do_pending_operator(cap, 0, FALSE);
7359 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7360 --msg_silent;
7361
7362 // delete PUT_LINE_BACKWARD;
7363 cap->oap->regname = regname;
7364
7365 if (reg1 != NULL)
7366 {
7367 // Delete probably changed the register we want to put, save
7368 // it first. Then put back what was there before the delete.
7369 reg2 = get_register(regname, FALSE);
7370 put_register(regname, reg1);
7371 }
7372
7373 // When deleted a linewise Visual area, put the register as
7374 // lines to avoid it joined with the next line. When deletion was
7375 // characterwise, split a line when putting lines.
7376 if (VIsual_mode == 'V')
7377 flags |= PUT_LINE;
7378 else if (VIsual_mode == 'v')
7379 flags |= PUT_LINE_SPLIT;
7380 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7381 flags |= PUT_LINE_FORWARD;
7382 dir = BACKWARD;
7383 if ((VIsual_mode != 'V'
7384 && curwin->w_cursor.col < curbuf->b_op_start.col)
7385 || (VIsual_mode == 'V'
7386 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7387 // cursor is at the end of the line or end of file, put
7388 // forward.
7389 dir = FORWARD;
7390 // May have been reset in do_put().
7391 VIsual_active = TRUE;
7392 }
7393 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7394
7395 // If a register was saved, put it back now.
7396 if (reg2 != NULL)
7397 put_register(regname, reg2);
7398
7399 // What to reselect with "gv"? Selecting the just put text seems to
7400 // be the most useful, since the original text was removed.
7401 if (was_visual)
7402 {
7403 curbuf->b_visual.vi_start = curbuf->b_op_start;
7404 curbuf->b_visual.vi_end = curbuf->b_op_end;
7405 // need to adjust cursor position
7406 if (*p_sel == 'e')
7407 inc(&curbuf->b_visual.vi_end);
7408 }
7409
7410 // When all lines were selected and deleted do_put() leaves an empty
7411 // line that needs to be deleted now.
7412 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7413 {
7414 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7415 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7416
7417 // If the cursor was in that line, move it to the end of the last
7418 // line.
7419 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7420 {
7421 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7422 coladvance((colnr_T)MAXCOL);
7423 }
7424 }
7425 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426}
7427
7428/*
7429 * "o" and "O" commands.
7430 */
7431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007432nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
7434#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007435 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7437 {
7438 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007439 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
7441 else
7442#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007443 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007445#ifdef FEAT_JOB_CHANNEL
7446 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007447 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 n_opencmd(cap);
7451}
7452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#ifdef FEAT_NETBEANS_INTG
7454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007455nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457 netbeans_keycommand(cap->nchar);
7458}
7459#endif
7460
7461#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007463nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007465 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466}
7467#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007468
Bram Moolenaar3918c952005-03-15 22:34:55 +00007469/*
7470 * Trigger CursorHold event.
7471 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7472 * input buffer. "did_cursorhold" is set to avoid retriggering.
7473 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007475nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007476{
7477 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7478 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007479 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007480}