blob: 1aad0b1b3aef5620ea7c489683cbe5e0fd90836f [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 Baala109f392023-06-02 14:16:35 +01002362 if (curwin->w_cursor.lnum <= 1)
Bram Moolenaare71996b2021-01-21 17:03:07 +01002363 {
2364 retval = FAIL;
2365 break;
2366 }
Luuk van Baala109f392023-06-02 14:16:35 +01002367 cursor_up_inner(curwin, 1);
2368
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002369 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 if (linelen > width1)
2371 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2372 + 1) * width2;
2373 }
2374 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 if (linelen > width1)
2378 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2379 else
2380 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002381 if (curwin->w_curswant + width2 < (colnr_T)n
2382#ifdef FEAT_FOLDING
2383 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2384#endif
2385 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 curwin->w_curswant += width2;
2388 else
2389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002390 // to next line
Luuk van Baala109f392023-06-02 14:16:35 +01002391 if (curwin->w_cursor.lnum
2392 >= curwin->w_buffer->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 retval = FAIL;
2395 break;
2396 }
Luuk van Baala109f392023-06-02 14:16:35 +01002397 cursor_down_inner(curwin, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 curwin->w_curswant %= width2;
Luuk van Baala109f392023-06-02 14:16:35 +01002399
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002400 // Check if the cursor has moved below the number display
2401 // when width1 < width2 (with cpoptions+=n). Subtract width2
2402 // to get a negative value for w_curswant, which will get
2403 // clipped to column 0.
2404 if (curwin->w_curswant >= width1)
2405 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002406 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 }
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002412 if (virtual_active() && atend)
2413 coladvance(MAXCOL);
2414 else
2415 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2418 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002419 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002420 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002421
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002422 // Check for landing on a character that got split at the end of the
2423 // last line. We want to advance a screenline, not end up in the same
2424 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002426 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002427#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002428 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2429 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002430#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002431
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002432 c = (*mb_ptr2char)(ml_get_cursor());
2433 if (dir == FORWARD && virtcol < curwin->w_curswant
2434 && (curwin->w_curswant <= (colnr_T)width1)
2435 && !vim_isprintc(c) && c > 255)
2436 oneright();
2437
Bram Moolenaar773b1582014-08-29 14:20:51 +02002438 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 && (curwin->w_curswant < (colnr_T)width1
2440 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2441 : ((curwin->w_curswant - width1) % width2
2442 > (colnr_T)width2 / 2)))
2443 --curwin->w_cursor.col;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002448 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 return retval;
2451}
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453/*
2454 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2455 * cap->arg must be TRUE for CTRL-E.
2456 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002458nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460 if (!checkclearop(cap->oap))
2461 scroll_redraw(cap->arg, cap->count1);
2462}
2463
2464/*
2465 * Scroll "count" lines up or down, and redraw.
2466 */
2467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002468scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002471 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#ifdef FEAT_DIFF
2473 int prev_topfill = curwin->w_topfill;
2474#endif
2475 linenr_T prev_lnum = curwin->w_cursor.lnum;
2476
2477 if (up)
2478 scrollup(count, TRUE);
2479 else
2480 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002481 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002483 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2484 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 cursor_correct();
2486 check_cursor_moved(curwin);
2487 curwin->w_valid |= VALID_TOPLINE;
2488
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002489 // If moved back to where we were, at least move the cursor, otherwise
2490 // we get stuck at one position. Don't move the cursor up if the
2491 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002493 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_DIFF
2495 && curwin->w_topfill == prev_topfill
2496#endif
2497 )
2498 {
2499 if (up)
2500 {
2501 if (curwin->w_cursor.lnum > prev_lnum
2502 || cursor_down(1L, FALSE) == FAIL)
2503 break;
2504 }
2505 else
2506 {
2507 if (curwin->w_cursor.lnum < prev_lnum
2508 || prev_topline == 1L
2509 || cursor_up(1L, FALSE) == FAIL)
2510 break;
2511 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // Mark w_topline as valid, otherwise the screen jumps back at the
2513 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 check_cursor_moved(curwin);
2515 curwin->w_valid |= VALID_TOPLINE;
2516 }
2517 }
2518 if (curwin->w_cursor.lnum != prev_lnum)
2519 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002520 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521}
2522
2523/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002524 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2525 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2526 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002527 */
2528 static int
2529nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2530{
2531 int nchar = *nchar_arg;
2532 long n;
2533
2534 // "z123{nchar}": edit the count before obtaining {nchar}
2535 if (checkclearop(cap->oap))
2536 return FALSE;
2537 n = nchar - '0';
2538
2539 for (;;)
2540 {
2541#ifdef USE_ON_FLY_SCROLL
2542 dont_scroll = TRUE; // disallow scrolling here
2543#endif
2544 ++no_mapping;
2545 ++allow_keys; // no mapping for nchar, but allow key codes
2546 nchar = plain_vgetc();
2547 LANGMAP_ADJUST(nchar, TRUE);
2548 --no_mapping;
2549 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002550 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002551
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002552 if (nchar == K_DEL || nchar == K_KDEL)
2553 n /= 10;
2554 else if (VIM_ISDIGIT(nchar))
2555 n = n * 10 + (nchar - '0');
2556 else if (nchar == CAR)
2557 {
2558#ifdef FEAT_GUI
2559 need_mouse_correct = TRUE;
2560#endif
2561 win_setheight((int)n);
2562 break;
2563 }
2564 else if (nchar == 'l'
2565 || nchar == 'h'
2566 || nchar == K_LEFT
2567 || nchar == K_RIGHT)
2568 {
2569 cap->count1 = n ? n * cap->count1 : cap->count1;
2570 *nchar_arg = nchar;
2571 return TRUE;
2572 }
2573 else
2574 {
2575 clearopbeep(cap->oap);
2576 break;
2577 }
2578 }
2579 cap->oap->op_type = OP_NOP;
2580 return FALSE;
2581}
2582
2583#ifdef FEAT_SPELL
2584/*
2585 * "zug" and "zuw": undo "zg" and "zw"
2586 * "zg": add good word to word list
2587 * "zw": add wrong word to word list
2588 * "zG": add good word to temp word list
2589 * "zW": add wrong word to temp word list
2590 */
2591 static int
2592nv_zg_zw(cmdarg_T *cap, int nchar)
2593{
2594 char_u *ptr = NULL;
2595 int len;
2596 int undo = FALSE;
2597
2598 if (nchar == 'u')
2599 {
2600 ++no_mapping;
2601 ++allow_keys; // no mapping for nchar, but allow key codes
2602 nchar = plain_vgetc();
2603 LANGMAP_ADJUST(nchar, TRUE);
2604 --no_mapping;
2605 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002606 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002607
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002608 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2609 {
2610 clearopbeep(cap->oap);
2611 return OK;
2612 }
2613 undo = TRUE;
2614 }
2615
2616 if (checkclearop(cap->oap))
2617 return OK;
2618 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2619 return FAIL;
2620 if (ptr == NULL)
2621 {
2622 pos_T pos = curwin->w_cursor;
2623
2624 // Find bad word under the cursor. When 'spell' is
2625 // off this fails and find_ident_under_cursor() is
2626 // used below.
2627 emsg_off++;
2628 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2629 emsg_off--;
2630 if (len != 0 && curwin->w_cursor.col <= pos.col)
2631 ptr = ml_get_pos(&curwin->w_cursor);
2632 curwin->w_cursor = pos;
2633 }
2634
2635 if (ptr == NULL
2636 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2637 return FAIL;
2638 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2639 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2640 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2641
2642 return OK;
2643}
2644#endif
2645
2646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * Commands that start with "z".
2648 */
2649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002650nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 long n;
2653 colnr_T col;
2654 int nchar = cap->nchar;
2655#ifdef FEAT_FOLDING
2656 long old_fdl = curwin->w_p_fdl;
2657 int old_fen = curwin->w_p_fen;
2658#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002659 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002661 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (
2665#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2667 // and "zC" only in Visual mode. "zj" and "zk" are motion
2668 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 cap->nchar != 'f' && cap->nchar != 'F'
2670 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2671 && cap->nchar != 'j' && cap->nchar != 'k'
2672 &&
2673#endif
2674 checkclearop(cap->oap))
2675 return;
2676
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002677 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2678 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2680 && cap->count0
2681 && cap->count0 != curwin->w_cursor.lnum)
2682 {
2683 setpcmark();
2684 if (cap->count0 > curbuf->b_ml.ml_line_count)
2685 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2686 else
2687 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002688 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690
2691 switch (nchar)
2692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 case '+':
2695 if (cap->count0 == 0)
2696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // No count given: put cursor at the line below screen
2698 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2700 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2701 else
2702 curwin->w_cursor.lnum = curwin->w_botline;
2703 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 case NL:
2706 case CAR:
2707 case K_KENTER:
2708 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002712 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002713 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002720 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002721 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002722 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 break;
2724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // "z^", "z-" and "zb": put cursor at bottom of screen
2726 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2727 // when <count> is at bottom of window, and puts that one at
2728 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (cap->count0 != 0)
2730 {
2731 scroll_cursor_bot(0, TRUE);
2732 curwin->w_cursor.lnum = curwin->w_topline;
2733 }
2734 else if (curwin->w_topline == 1)
2735 curwin->w_cursor.lnum = 1;
2736 else
2737 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 case '-':
2740 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002744 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002745 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 break;
2747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002750 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 case 'h':
2755 case K_LEFT:
2756 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002757 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2758 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 break;
2760
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002761 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002765 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 case 'l':
2767 case K_RIGHT:
2768 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002769 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 break;
2771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 case 's': if (!curwin->w_p_wrap)
2774 {
2775#ifdef FEAT_FOLDING
2776 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else
2779#endif
2780 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002781 if ((long)col > siso)
2782 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 else
2784 col = 0;
2785 if (curwin->w_leftcol != col)
2786 {
2787 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002788 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 }
2791 break;
2792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 case 'e': if (!curwin->w_p_wrap)
2795 {
2796#ifdef FEAT_FOLDING
2797 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 else
2800#endif
2801 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002802 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002803 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 col = 0;
2805 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002806 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (curwin->w_leftcol != col)
2808 {
2809 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002810 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812 }
2813 break;
2814
Christian Brabandt2fa93842021-05-30 22:17:25 +02002815 // "zp", "zP" in block mode put without addind trailing spaces
2816 case 'P':
2817 case 'p': nv_put(cap);
2818 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002819 // "zy" Yank without trailing spaces
2820 case 'y': nv_operator(cap);
2821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // "zF": create fold command
2824 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 case 'F':
2826 case 'f': if (foldManualAllowed(TRUE))
2827 {
2828 cap->nchar = 'f';
2829 nv_operator(cap);
2830 curwin->w_p_fen = TRUE;
2831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2834 {
2835 nv_operator(cap);
2836 finish_op = TRUE;
2837 }
2838 }
2839 else
2840 clearopbeep(cap->oap);
2841 break;
2842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // "zd": delete fold at cursor
2844 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 case 'd':
2846 case 'D': if (foldManualAllowed(FALSE))
2847 {
2848 if (VIsual_active)
2849 nv_operator(cap);
2850 else
2851 deleteFold(curwin->w_cursor.lnum,
2852 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2853 }
2854 break;
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 case 'E': if (foldmethodIsManual(curwin))
2858 {
2859 clearFolding(curwin);
2860 changed_window_setting();
2861 }
2862 else if (foldmethodIsMarker(curwin))
2863 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2864 TRUE, FALSE);
2865 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002866 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case 'n': curwin->w_p_fen = FALSE;
2871 break;
2872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case 'N': curwin->w_p_fen = TRUE;
2875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2883 openFold(curwin->w_cursor.lnum, cap->count1);
2884 else
2885 {
2886 closeFold(curwin->w_cursor.lnum, cap->count1);
2887 curwin->w_p_fen = TRUE;
2888 }
2889 break;
2890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2893 openFoldRecurse(curwin->w_cursor.lnum);
2894 else
2895 {
2896 closeFoldRecurse(curwin->w_cursor.lnum);
2897 curwin->w_p_fen = TRUE;
2898 }
2899 break;
2900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 case 'o': if (VIsual_active)
2903 nv_operator(cap);
2904 else
2905 openFold(curwin->w_cursor.lnum, cap->count1);
2906 break;
2907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 case 'O': if (VIsual_active)
2910 nv_operator(cap);
2911 else
2912 openFoldRecurse(curwin->w_cursor.lnum);
2913 break;
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 case 'c': if (VIsual_active)
2917 nv_operator(cap);
2918 else
2919 closeFold(curwin->w_cursor.lnum, cap->count1);
2920 curwin->w_p_fen = TRUE;
2921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'C': if (VIsual_active)
2925 nv_operator(cap);
2926 else
2927 closeFoldRecurse(curwin->w_cursor.lnum);
2928 curwin->w_p_fen = TRUE;
2929 break;
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case 'v': foldOpenCursor();
2933 break;
2934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 curwin->w_foldinvalid = TRUE; // recompute folds
2938 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 foldOpenCursor();
2940 break;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 curwin->w_foldinvalid = TRUE; // recompute folds
2945 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 break;
2947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002950 {
2951 curwin->w_p_fdl -= cap->count1;
2952 if (curwin->w_p_fdl < 0)
2953 curwin->w_p_fdl = 0;
2954 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 curwin->w_p_fen = TRUE;
2957 break;
2958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 curwin->w_p_fen = TRUE;
2963 break;
2964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002966 case 'r': curwin->w_p_fdl += cap->count1;
2967 {
2968 int d = getDeepestNesting();
2969
2970 if (curwin->w_p_fdl >= d)
2971 curwin->w_p_fdl = d;
2972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 break;
2974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 break;
2979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002980 case 'j': // "zj" move to next fold downwards
2981 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2983 cap->count1) == FAIL)
2984 clearopbeep(cap->oap);
2985 break;
2986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002989#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 case 'g': // "zg": add good word to word list
2992 case 'w': // "zw": add wrong word to word list
2993 case 'G': // "zG": add good word to temp word list
2994 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002995 if (nv_zg_zw(cap, nchar) == FAIL)
2996 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002997 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003000 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003001 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003003#endif
3004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 default: clearopbeep(cap->oap);
3006 }
3007
3008#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (old_fen != curwin->w_p_fen)
3011 {
3012# ifdef FEAT_DIFF
3013 win_T *wp;
3014
3015 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 FOR_ALL_WINDOWS(wp)
3019 {
3020 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3021 {
3022 wp->w_p_fen = curwin->w_p_fen;
3023 changed_window_setting_win(wp);
3024 }
3025 }
3026 }
3027# endif
3028 changed_window_setting();
3029 }
3030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003031 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (old_fdl != curwin->w_p_fdl)
3033 newFoldLevel();
3034#endif
3035}
3036
3037#ifdef FEAT_GUI
3038/*
3039 * Vertical scrollbar movement.
3040 */
3041 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003042nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 if (cap->oap->op_type != OP_NOP)
3045 clearopbeep(cap->oap);
3046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 gui_do_scroll();
3049}
3050
3051/*
3052 * Horizontal scrollbar movement.
3053 */
3054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003055nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 if (cap->oap->op_type != OP_NOP)
3058 clearopbeep(cap->oap);
3059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003061 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063#endif
3064
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003065#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003066/*
3067 * Click in GUI tab.
3068 */
3069 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003071{
3072 if (cap->oap->op_type != OP_NOP)
3073 clearopbeep(cap->oap);
3074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003075 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003076 goto_tabpage(current_tab);
3077}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003078
3079/*
3080 * Selected item in tab line menu.
3081 */
3082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003083nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003084{
3085 if (cap->oap->op_type != OP_NOP)
3086 clearopbeep(cap->oap);
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003089 handle_tabmenu();
3090}
3091
3092/*
3093 * Handle selecting an item of the GUI tab line menu.
3094 * Used in Normal and Insert mode.
3095 */
3096 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003097handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003098{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003099 switch (current_tabmenu)
3100 {
3101 case TABLINE_MENU_CLOSE:
3102 if (current_tab == 0)
3103 do_cmdline_cmd((char_u *)"tabclose");
3104 else
3105 {
3106 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3107 current_tab);
3108 do_cmdline_cmd(IObuff);
3109 }
3110 break;
3111
3112 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003113 if (current_tab == 0)
3114 do_cmdline_cmd((char_u *)"$tabnew");
3115 else
3116 {
3117 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3118 current_tab - 1);
3119 do_cmdline_cmd(IObuff);
3120 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003121 break;
3122
3123 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003124 if (current_tab == 0)
3125 do_cmdline_cmd((char_u *)"browse $tabnew");
3126 else
3127 {
3128 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3129 current_tab - 1);
3130 do_cmdline_cmd(IObuff);
3131 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003132 break;
3133 }
3134}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003135#endif
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
3138 * "Q" command.
3139 */
3140 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003141nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003143 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003145 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003146 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 do_exmode(FALSE);
3148}
3149
3150/*
3151 * Handle a ":" command.
3152 */
3153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003154nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003156 int old_p_im;
3157 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003158 int is_cmdkey = cap->cmdchar == K_COMMAND
3159 || cap->cmdchar == K_SCRIPT_COMMAND;
3160 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaar957cf672020-11-12 14:21:06 +01003162 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003164 nv_operator(cap);
3165 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003167
3168 if (cap->oap->op_type != OP_NOP)
3169 {
3170 // Using ":" as a movement is characterwise exclusive.
3171 cap->oap->motion_type = MCHAR;
3172 cap->oap->inclusive = FALSE;
3173 }
3174 else if (cap->count0 && !is_cmdkey)
3175 {
3176 // translate "count:" into ":.,.+(count - 1)"
3177 stuffcharReadbuff('.');
3178 if (cap->count0 > 1)
3179 {
3180 stuffReadbuff((char_u *)",.+");
3181 stuffnumReadbuff((long)cap->count0 - 1L);
3182 }
3183 }
3184
3185 // When typing, don't type below an old message
3186 if (KeyTyped)
3187 compute_cmdrow();
3188
3189 old_p_im = p_im;
3190
3191 // get a command line and execute it
3192 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3193 if (is_cmdkey)
3194 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3195 else
3196 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3197
3198 // If 'insertmode' changed, enter or exit Insert mode
3199 if (p_im != old_p_im)
3200 {
3201 if (p_im)
3202 restart_edit = 'i';
3203 else
3204 restart_edit = 0;
3205 }
3206
3207 if (cmd_result == FAIL)
3208 // The Ex command failed, do not execute the operator.
3209 clearop(cap->oap);
3210 else if (cap->oap->op_type != OP_NOP
3211 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3212 || cap->oap->start.col >
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003213 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003214 || did_emsg
3215 ))
3216 // The start of the operator has become invalid by the Ex command.
3217 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218}
3219
3220/*
3221 * Handle CTRL-G command.
3222 */
3223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003224nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003229 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 showmode();
3231 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003232 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 fileinfo((int)cap->count0, FALSE, TRUE);
3235}
3236
3237/*
3238 * Handle CTRL-H <Backspace> command.
3239 */
3240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003241nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (VIsual_active && VIsual_select)
3244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003245 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 v_visop(cap);
3247 }
3248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 nv_left(cap);
3250}
3251
3252/*
3253 * CTRL-L: clear screen and redraw.
3254 */
3255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003256nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003258 if (checkclearop(cap->oap))
3259 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003260
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003261#ifdef FEAT_SYN_HL
3262 // Clear all syntax states to force resyncing.
3263 syn_stack_free_all(curwin->w_s);
3264# ifdef FEAT_RELTIME
3265 {
3266 win_T *wp;
3267
3268 FOR_ALL_WINDOWS(wp)
3269 wp->w_s->b_syn_slow = FALSE;
3270 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003273 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003274#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3275# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003276 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003277# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003278 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281
3282/*
3283 * CTRL-O: In Select mode: switch to Visual mode for one command.
3284 * Otherwise: Go to older pcmark.
3285 */
3286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003287nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (VIsual_active && VIsual_select)
3290 {
3291 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003292 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003294 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 cap->count1 = -cap->count1;
3299 nv_pcmark(cap);
3300 }
3301}
3302
3303/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003304 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3305 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 */
3307 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003308nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 if (!checkclearopq(cap->oap))
3311 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3312 GETF_SETMARK|GETF_ALT, FALSE);
3313}
3314
3315/*
3316 * "Z" commands.
3317 */
3318 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003319nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003321 if (checkclearopq(cap->oap))
3322 return;
3323
3324 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003326 // "ZZ": equivalent to ":x".
3327 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 break;
3329
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003331 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 break;
3333
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003334 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3340 */
3341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003342do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 oparg_T oa;
3345 cmdarg_T ca;
3346
3347 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003348 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 ca.oap = &oa;
3350 ca.cmdchar = c1;
3351 ca.nchar = c2;
3352 nv_ident(&ca);
3353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003356 * 'K' normal-mode command. Get the command to lookup the keyword under the
3357 * cursor.
3358 */
3359 static int
3360nv_K_getcmd(
3361 cmdarg_T *cap,
3362 char_u *kp,
3363 int kp_help,
3364 int kp_ex,
3365 char_u **ptr_arg,
3366 int n,
3367 char_u *buf,
3368 unsigned buflen)
3369{
3370 char_u *ptr = *ptr_arg;
3371 int isman;
3372 int isman_s;
3373
3374 if (kp_help)
3375 {
3376 // in the help buffer
3377 STRCPY(buf, "he! ");
3378 return n;
3379 }
3380
3381 if (kp_ex)
3382 {
3383 // 'keywordprog' is an ex command
3384 if (cap->count0 != 0)
3385 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3386 else
3387 STRCPY(buf, kp);
3388 STRCAT(buf, " ");
3389 return n;
3390 }
3391
3392 // An external command will probably use an argument starting
3393 // with "-" as an option. To avoid trouble we skip the "-".
3394 while (*ptr == '-' && n > 0)
3395 {
3396 ++ptr;
3397 --n;
3398 }
3399 if (n == 0)
3400 {
3401 // found dashes only
3402 emsg(_(e_no_identifier_under_cursor));
3403 vim_free(buf);
3404 *ptr_arg = ptr;
3405 return 0;
3406 }
3407
3408 // When a count is given, turn it into a range. Is this
3409 // really what we want?
3410 isman = (STRCMP(kp, "man") == 0);
3411 isman_s = (STRCMP(kp, "man -s") == 0);
3412 if (cap->count0 != 0 && !(isman || isman_s))
3413 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3414
3415 STRCAT(buf, "! ");
3416 if (cap->count0 == 0 && isman_s)
3417 STRCAT(buf, "man");
3418 else
3419 STRCAT(buf, kp);
3420 STRCAT(buf, " ");
3421 if (cap->count0 != 0 && (isman || isman_s))
3422 {
3423 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3424 STRCAT(buf, " ");
3425 }
3426
3427 *ptr_arg = ptr;
3428 return n;
3429}
3430
3431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 * Handle the commands that use the word under the cursor.
3433 * [g] CTRL-] :ta to current identifier
3434 * [g] 'K' run program for current identifier
3435 * [g] '*' / to current identifier or string
3436 * [g] '#' ? to current identifier or string
3437 * g ']' :tselect for current identifier
3438 */
3439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003440nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 char_u *ptr = NULL;
3443 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003444 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003445 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 char_u *kp; // value of 'keywordprg'
3448 int kp_help; // 'keywordprg' is ":he"
3449 int kp_ex; // 'keywordprg' starts with ":"
3450 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003453 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 cmdchar = cap->nchar;
3459 g_cmd = TRUE;
3460 }
3461 else
3462 {
3463 cmdchar = cap->cmdchar;
3464 g_cmd = FALSE;
3465 }
3466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003467 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 cmdchar = '#';
3469
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003470 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3474 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (checkclearopq(cap->oap))
3476 return;
3477 }
3478
3479 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3480 (cmdchar == '*' || cmdchar == '#')
3481 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3482 {
3483 clearop(cap->oap);
3484 return;
3485 }
3486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // Allocate buffer to put the command in. Inserting backslashes can
3488 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3489 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3491 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3492 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003493 if (kp_help && *skipwhite(ptr) == NUL)
3494 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003495 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003496 return;
3497 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003498 kp_ex = (*kp == ':');
3499 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3500 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (buf == NULL)
3502 return;
3503 buf[0] = NUL;
3504
3505 switch (cmdchar)
3506 {
3507 case '*':
3508 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003509 // Put cursor at start of word, makes search skip the word
3510 // under the cursor.
3511 // Call setpcmark() first, so "*``" puts the cursor back where
3512 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 setpcmark();
3514 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3515
3516 if (!g_cmd && vim_iswordp(ptr))
3517 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003518 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 break;
3520
3521 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003522 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3523 if (n == 0)
3524 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 break;
3526
3527 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003528 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529#ifdef FEAT_CSCOPE
3530 if (p_cst)
3531 STRCPY(buf, "cstag ");
3532 else
3533#endif
3534 STRCPY(buf, "ts ");
3535 break;
3536
3537 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003538 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (curbuf->b_help)
3540 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003542 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003543 if (g_cmd)
3544 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003545 else if (cap->count0 == 0)
3546 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003547 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003548 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003552 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003553 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003555 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003556 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003557 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003558 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003559 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003560 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003561 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003562 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003563 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003565 vim_free(buf);
3566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003568 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003569 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003570 {
3571 vim_free(buf);
3572 vim_free(p);
3573 return;
3574 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003575 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003576 STRCAT(buf, p);
3577 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003579 else
3580 {
3581 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003582 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003583 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003584 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003585 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003586 {
3587 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003588 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003589 aux_ptr = (char_u *)"";
3590 else
3591 aux_ptr = (char_u *)"\\|\"\n[";
3592 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003593 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003594 aux_ptr = (char_u *)"\\|\"\n*?[";
3595
3596 p = buf + STRLEN(buf);
3597 while (n-- > 0)
3598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003599 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 if (vim_strchr(aux_ptr, *ptr) != NULL)
3601 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003602 // When current byte is a part of multibyte character, copy all
3603 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003604 if (has_mbyte)
3605 {
3606 int i;
3607 int len = (*mb_ptr2len)(ptr) - 1;
3608
3609 for (i = 0; i < len && n >= 1; ++i, --n)
3610 *p++ = *ptr++;
3611 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003612 *p++ = *ptr++;
3613 }
3614 *p = NUL;
3615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003617 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (cmdchar == '*' || cmdchar == '#')
3619 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003620 if (!g_cmd && (has_mbyte
3621 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3622 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003624
3625 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003626 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003628
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003629 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003632 {
3633 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003635 g_tag_at_cursor = FALSE;
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 vim_free(buf);
3639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * Get visually selected text, within one line only.
3643 * Returns FAIL if more than one line selected.
3644 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003645 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003646get_visual_text(
3647 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 char_u **pp, // return: start of selected text
3649 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 if (VIsual_mode != 'V')
3652 unadjust_for_sel();
3653 if (VIsual.lnum != curwin->w_cursor.lnum)
3654 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003655 if (cap != NULL)
3656 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return FAIL;
3658 }
3659 if (VIsual_mode == 'V')
3660 {
3661 *pp = ml_get_curline();
3662 *lenp = (int)STRLEN(*pp);
3663 }
3664 else
3665 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003666 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 *pp = ml_get_pos(&curwin->w_cursor);
3669 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3670 }
3671 else
3672 {
3673 *pp = ml_get_pos(&VIsual);
3674 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3675 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003676 if (**pp == NUL)
3677 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003678 if (*lenp > 0)
3679 {
3680 if (has_mbyte)
3681 // Correct the length to include all bytes of the last
3682 // character.
3683 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3684 else if ((*pp)[*lenp - 1] == NUL)
3685 // Do not include a trailing NUL.
3686 *lenp -= 1;
3687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 reset_VIsual_and_resel();
3690 return OK;
3691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693/*
3694 * CTRL-T: backwards in tag stack
3695 */
3696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003697nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 if (!checkclearopq(cap->oap))
3700 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3701}
3702
3703/*
3704 * Handle scrolling command 'H', 'L' and 'M'.
3705 */
3706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003707nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 int used = 0;
3710 long n;
3711#ifdef FEAT_FOLDING
3712 linenr_T lnum;
3713#endif
3714 int half;
3715
3716 cap->oap->motion_type = MLINE;
3717 setpcmark();
3718
3719 if (cap->cmdchar == 'L')
3720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003721 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 curwin->w_cursor.lnum = curwin->w_botline - 1;
3723 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3724 curwin->w_cursor.lnum = 1;
3725 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003726 {
3727#ifdef FEAT_FOLDING
3728 if (hasAnyFolding(curwin))
3729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003731 for (n = cap->count1 - 1; n > 0
3732 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3733 {
3734 (void)hasFolding(curwin->w_cursor.lnum,
3735 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003736 if (curwin->w_cursor.lnum > curwin->w_topline)
3737 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003738 }
3739 }
3740 else
3741#endif
3742 curwin->w_cursor.lnum -= cap->count1 - 1;
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 else
3746 {
3747 if (cap->cmdchar == 'M')
3748 {
3749#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 used -= diff_check_fill(curwin, curwin->w_topline)
3752 - curwin->w_topfill;
3753#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3756 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3757 {
3758#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 // Count half he number of filler lines to be "below this
3760 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3762 + n) / 2 >= half)
3763 {
3764 --n;
3765 break;
3766 }
3767#endif
3768 used += plines(curwin->w_topline + n);
3769 if (used >= half)
3770 break;
3771#ifdef FEAT_FOLDING
3772 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3773 n = lnum - curwin->w_topline;
3774#endif
3775 }
3776 if (n > 0 && used > curwin->w_height)
3777 --n;
3778 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003782#ifdef FEAT_FOLDING
3783 if (hasAnyFolding(curwin))
3784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003786 lnum = curwin->w_topline;
3787 while (n-- > 0 && lnum < curwin->w_botline - 1)
3788 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003789 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003790 ++lnum;
3791 }
3792 n = lnum - curwin->w_topline;
3793 }
3794#endif
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 curwin->w_cursor.lnum = curwin->w_topline + n;
3797 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3798 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3799 }
3800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003801 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003802 if (cap->oap->op_type == OP_NOP)
3803 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 beginline(BL_SOL | BL_FIX);
3805}
3806
3807/*
3808 * Cursor right commands.
3809 */
3810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003811nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003814 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003816 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003818 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003819 if (mod_mask & MOD_MASK_CTRL)
3820 cap->arg = TRUE;
3821 nv_wordcmd(cap);
3822 return;
3823 }
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 cap->oap->motion_type = MCHAR;
3826 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003827 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003829 // In virtual edit mode, there's no such thing as "past_line", as lines
3830 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003832 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 for (n = cap->count1; n > 0; --n)
3835 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003836 if ((!past_line && oneright() == FAIL)
3837 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003838 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003840 // <Space> wraps to next line if 'whichwrap' has 's'.
3841 // 'l' wraps to next line if 'whichwrap' has 'l'.
3842 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if ( ((cap->cmdchar == ' '
3844 && vim_strchr(p_ww, 's') != NULL)
3845 || (cap->cmdchar == 'l'
3846 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003847 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 && vim_strchr(p_ww, '>') != NULL))
3849 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003851 // When deleting we also count the NL as a character.
3852 // Set cap->oap->inclusive when last char in the line is
3853 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003854 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003856 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 cap->oap->inclusive = TRUE;
3858 else
3859 {
3860 ++curwin->w_cursor.lnum;
3861 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 curwin->w_set_curswant = TRUE;
3864 cap->oap->inclusive = FALSE;
3865 }
3866 continue;
3867 }
3868 if (cap->oap->op_type == OP_NOP)
3869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003870 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (n == cap->count1)
3872 beep_flush();
3873 }
3874 else
3875 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003876 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 cap->oap->inclusive = TRUE;
3878 }
3879 break;
3880 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003881 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (virtual_active())
3885 oneright();
3886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003889 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 ++curwin->w_cursor.col;
3892 }
3893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895#ifdef FEAT_FOLDING
3896 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3897 && cap->oap->op_type == OP_NOP)
3898 foldOpenCursor();
3899#endif
3900}
3901
3902/*
3903 * Cursor left commands.
3904 *
3905 * Returns TRUE when operator end should not be adjusted.
3906 */
3907 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003908nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 long n;
3911
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003914 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003915 if (mod_mask & MOD_MASK_CTRL)
3916 cap->arg = 1;
3917 nv_bck_word(cap);
3918 return;
3919 }
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 cap->oap->motion_type = MCHAR;
3922 cap->oap->inclusive = FALSE;
3923 for (n = cap->count1; n > 0; --n)
3924 {
3925 if (oneleft() == FAIL)
3926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003927 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3928 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3929 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if ( (((cap->cmdchar == K_BS
3931 || cap->cmdchar == Ctrl_H)
3932 && vim_strchr(p_ww, 'b') != NULL)
3933 || (cap->cmdchar == 'h'
3934 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003935 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 && vim_strchr(p_ww, '<') != NULL))
3937 && curwin->w_cursor.lnum > 1)
3938 {
3939 --(curwin->w_cursor.lnum);
3940 coladvance((colnr_T)MAXCOL);
3941 curwin->w_set_curswant = TRUE;
3942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003943 // When the NL before the first char has to be deleted we
3944 // put the cursor on the NUL after the previous line.
3945 // This is a very special case, be careful!
3946 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if ( (cap->oap->op_type == OP_DELETE
3948 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003949 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003951 char_u *cp = ml_get_cursor();
3952
3953 if (*cp != NUL)
3954 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003955 if (has_mbyte)
3956 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3957 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003958 ++curwin->w_cursor.col;
3959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 cap->retval |= CA_NO_ADJ_OP_END;
3961 }
3962 continue;
3963 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003964 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3966 beep_flush();
3967 break;
3968 }
3969 }
3970#ifdef FEAT_FOLDING
3971 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3972 && cap->oap->op_type == OP_NOP)
3973 foldOpenCursor();
3974#endif
3975}
3976
3977/*
3978 * Cursor up commands.
3979 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3980 */
3981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003982nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003984 if (mod_mask & MOD_MASK_SHIFT)
3985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003986 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987 cap->arg = BACKWARD;
3988 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003989 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003990 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003991
3992 cap->oap->motion_type = MLINE;
3993 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3994 clearopbeep(cap->oap);
3995 else if (cap->arg)
3996 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998
3999/*
4000 * Cursor down commands.
4001 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4002 */
4003 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004004nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004006 if (mod_mask & MOD_MASK_SHIFT)
4007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004008 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 cap->arg = FORWARD;
4010 nv_page(cap);
4011 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004012#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004013 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004014 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4015 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004017 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004020 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 cmdwin_result = CAR;
4022 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004024 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004025 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4026 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4027 {
4028 invoke_prompt_callback();
4029 if (restart_edit == 0)
4030 restart_edit = 'a';
4031 }
4032 else
4033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
4035 cap->oap->motion_type = MLINE;
4036 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4037 clearopbeep(cap->oap);
4038 else if (cap->arg)
4039 beginline(BL_WHITE | BL_FIX);
4040 }
4041 }
4042}
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044/*
4045 * Grab the file name under the cursor and edit it.
4046 */
4047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004048nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004051 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004053 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004055
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004056#ifdef FEAT_PROP_POPUP
4057 if (ERROR_IF_TERM_POPUP_WINDOW)
4058 return;
4059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004061 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 if (ptr != NULL)
4064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004065 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004066 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004067 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004069 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004070 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004071 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004072 {
4073 curwin->w_cursor.lnum = lnum;
4074 check_cursor_lnum();
4075 beginline(BL_SOL | BL_FIX);
4076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 vim_free(ptr);
4078 }
4079 else
4080 clearop(cap->oap);
4081}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083/*
4084 * <End> command: to end of current line or last line.
4085 */
4086 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004087nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004089 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004091 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004093 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 nv_dollar(cap);
4096}
4097
4098/*
4099 * Handle the "$" command.
4100 */
4101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004102nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
4104 cap->oap->motion_type = MCHAR;
4105 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 // In virtual mode when off the edge of a line and an operator
4107 // is pending (whew!) keep the cursor where it is.
4108 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (!virtual_active() || gchar_cursor() != NUL
4110 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004111 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if (cursor_down((long)(cap->count1 - 1),
4113 cap->oap->op_type == OP_NOP) == FAIL)
4114 clearopbeep(cap->oap);
4115#ifdef FEAT_FOLDING
4116 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4117 foldOpenCursor();
4118#endif
4119}
4120
4121/*
4122 * Implementation of '?' and '/' commands.
4123 * If cap->arg is TRUE don't set PC mark.
4124 */
4125 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004126nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
4128 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004129 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4132 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004133 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 cap->cmdchar = 'g';
4135 cap->nchar = '?';
4136 nv_operator(cap);
4137 return;
4138 }
4139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004140 // When using 'incsearch' the cursor may be moved to set a different search
4141 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004142 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 if (cap->searchbuf == NULL)
4145 {
4146 clearop(oap);
4147 return;
4148 }
4149
Bram Moolenaar46539112015-02-17 15:43:57 +01004150 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004151 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004152 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153}
4154
LemonBoya4399382022-04-09 21:04:08 +01004155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156/*
4157 * Handle "N" and "n" commands.
4158 * cap->arg is SEARCH_REV for "N", 0 for "n".
4159 */
4160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004161nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004163 pos_T old = curwin->w_cursor;
4164 int wrapped = FALSE;
4165 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004166
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004167 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004169 // Avoid getting stuck on the current cursor position, which can
4170 // happen when an offset is given and the cursor is on the last char
4171 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004172 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004173 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004174 cap->count1 -= 1;
4175 }
LemonBoya4399382022-04-09 21:04:08 +01004176
4177#ifdef FEAT_SEARCH_EXTRA
4178 // Redraw the window to refresh the highlighted matches.
4179 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004180 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182}
4183
4184/*
4185 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4186 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004187 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004190normal_search(
4191 cmdarg_T *cap,
4192 int dir,
4193 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004194 int opt, // extra flags for do_search()
4195 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004198 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004199#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004200 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
4203 cap->oap->motion_type = MCHAR;
4204 cap->oap->inclusive = FALSE;
4205 cap->oap->use_reg_one = TRUE;
4206 curwin->w_set_curswant = TRUE;
4207
Bram Moolenaara80faa82020-04-12 19:37:17 +02004208 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004209 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004210 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4211 if (wrapped != NULL)
4212 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (i == 0)
4214 clearop(cap->oap);
4215 else
4216 {
4217 if (i == 2)
4218 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#ifdef FEAT_FOLDING
4221 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4222 foldOpenCursor();
4223#endif
4224 }
LemonBoya4399382022-04-09 21:04:08 +01004225#ifdef FEAT_SEARCH_EXTRA
4226 // Redraw the window to refresh the highlighted matches.
4227 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004228 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004231 // "/$" will put the cursor after the end of the line, may need to
4232 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004234 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236
4237/*
4238 * Character search commands.
4239 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4240 * ',' and FALSE for ';'.
4241 * cap->nchar is NUL for ',' and ';' (repeat the search)
4242 */
4243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004244nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 int t_cmd;
4247
4248 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4249 t_cmd = TRUE;
4250 else
4251 t_cmd = FALSE;
4252
4253 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004256 clearopbeep(cap->oap);
4257 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004259
4260 curwin->w_set_curswant = TRUE;
4261 // Include a Tab for "tx" and for "dfx".
4262 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4263 && (t_cmd || cap->oap->op_type != OP_NOP))
4264 {
4265 colnr_T scol, ecol;
4266
4267 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4268 curwin->w_cursor.coladd = ecol - scol;
4269 }
4270 else
4271 curwin->w_cursor.coladd = 0;
4272 adjust_for_sel(cap);
4273#ifdef FEAT_FOLDING
4274 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4275 foldOpenCursor();
4276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277}
4278
4279/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004280 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4281 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4282 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4283 * "[m" or "]m" search for prev/next start of (Java) method.
4284 * "[M" or "]M" search for prev/next end of (Java) method.
4285 */
4286 static void
4287nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4288{
4289 pos_T new_pos = {0, 0, 0};
4290 pos_T *pos = NULL; // init for GCC
4291 pos_T prev_pos;
4292 long n;
4293 int findc;
4294 int c;
4295
4296 if (cap->nchar == '*')
4297 cap->nchar = '/';
4298 prev_pos.lnum = 0;
4299 if (cap->nchar == 'm' || cap->nchar == 'M')
4300 {
4301 if (cap->cmdchar == '[')
4302 findc = '{';
4303 else
4304 findc = '}';
4305 n = 9999;
4306 }
4307 else
4308 {
4309 findc = cap->nchar;
4310 n = cap->count1;
4311 }
4312 for ( ; n > 0; --n)
4313 {
4314 if ((pos = findmatchlimit(cap->oap, findc,
4315 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4316 {
4317 if (new_pos.lnum == 0) // nothing found
4318 {
4319 if (cap->nchar != 'm' && cap->nchar != 'M')
4320 clearopbeep(cap->oap);
4321 }
4322 else
4323 pos = &new_pos; // use last one found
4324 break;
4325 }
4326 prev_pos = new_pos;
4327 curwin->w_cursor = *pos;
4328 new_pos = *pos;
4329 }
4330 curwin->w_cursor = *old_pos;
4331
4332 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4333 // brought us to the match for "[m" and "]M" when inside a method.
4334 // Try finding the '{' or '}' we want to be at.
4335 // Also repeat for the given count.
4336 if (cap->nchar == 'm' || cap->nchar == 'M')
4337 {
4338 // norm is TRUE for "]M" and "[m"
4339 int norm = ((findc == '{') == (cap->nchar == 'm'));
4340
4341 n = cap->count1;
4342 // found a match: we were inside a method
4343 if (prev_pos.lnum != 0)
4344 {
4345 pos = &prev_pos;
4346 curwin->w_cursor = prev_pos;
4347 if (norm)
4348 --n;
4349 }
4350 else
4351 pos = NULL;
4352 while (n > 0)
4353 {
4354 for (;;)
4355 {
4356 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4357 {
4358 // if not found anything, that's an error
4359 if (pos == NULL)
4360 clearopbeep(cap->oap);
4361 n = 0;
4362 break;
4363 }
4364 c = gchar_cursor();
4365 if (c == '{' || c == '}')
4366 {
4367 // Must have found end/start of class: use it.
4368 // Or found the place to be at.
4369 if ((c == findc && norm) || (n == 1 && !norm))
4370 {
4371 new_pos = curwin->w_cursor;
4372 pos = &new_pos;
4373 n = 0;
4374 }
4375 // if no match found at all, we started outside of the
4376 // class and we're inside now. Just go on.
4377 else if (new_pos.lnum == 0)
4378 {
4379 new_pos = curwin->w_cursor;
4380 pos = &new_pos;
4381 }
4382 // found start/end of other method: go to match
4383 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004384 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4385 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004386 n = 0;
4387 else
4388 curwin->w_cursor = *pos;
4389 break;
4390 }
4391 }
4392 --n;
4393 }
4394 curwin->w_cursor = *old_pos;
4395 if (pos == NULL && new_pos.lnum != 0)
4396 clearopbeep(cap->oap);
4397 }
4398 if (pos != NULL)
4399 {
4400 setpcmark();
4401 curwin->w_cursor = *pos;
4402 curwin->w_set_curswant = TRUE;
4403#ifdef FEAT_FOLDING
4404 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4405 && cap->oap->op_type == OP_NOP)
4406 foldOpenCursor();
4407#endif
4408 }
4409}
4410
4411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 * "[" and "]" commands.
4413 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4414 */
4415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004416nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004419 pos_T *pos = NULL; // init for GCC
4420 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 int flag;
4422 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 cap->oap->motion_type = MCHAR;
4425 cap->oap->inclusive = FALSE;
4426 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004427 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004429 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (cap->nchar == 'f')
4431 nv_gotofile(cap);
4432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004435 // Find the occurrence(s) of the identifier or define under cursor
4436 // in current and included files or jump to the first occurrence.
4437 //
4438 // search list jump
4439 // fwd bwd fwd bwd fwd bwd
4440 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4441 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004442 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 char_u *ptr;
4445 int len;
4446
4447 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4448 clearop(cap->oap);
4449 else
4450 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004451 // Make a copy, if the line was changed it will be freed.
4452 ptr = vim_strnsave(ptr, len);
4453 if (ptr == NULL)
4454 return;
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 find_pattern_in_path(ptr, 0, len, TRUE,
4457 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4458 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4459 cap->count1,
4460 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4461 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4462 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4463 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004464 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 curwin->w_set_curswant = TRUE;
4466 }
4467 }
4468 else
4469#endif
4470
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004471 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4472 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4473 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4474 // "[m" or "]m" search for prev/next start of (Java) method.
4475 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if ( (cap->cmdchar == '['
4477 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4478 || (cap->cmdchar == ']'
4479 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004480 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004482 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 else if (cap->nchar == '[' || cap->nchar == ']')
4484 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004485 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 flag = '{';
4487 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004488 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004491 // Imitate strange Vi behaviour: When using "]]" with an operator
4492 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004493 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 (cap->oap->op_type != OP_NOP
4495 && cap->arg == FORWARD && flag == '{')))
4496 clearopbeep(cap->oap);
4497 else
4498 {
4499 if (cap->oap->op_type == OP_NOP)
4500 beginline(BL_WHITE | BL_FIX);
4501#ifdef FEAT_FOLDING
4502 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4503 foldOpenCursor();
4504#endif
4505 }
4506 }
4507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004508 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else if (cap->nchar == 'p' || cap->nchar == 'P')
4510 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004511 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004514 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 else if (cap->nchar == '\'' || cap->nchar == '`')
4516 {
4517 pos = &curwin->w_cursor;
4518 for (n = cap->count1; n > 0; --n)
4519 {
4520 prev_pos = *pos;
4521 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4522 cap->nchar == '\'');
4523 if (pos == NULL)
4524 break;
4525 }
4526 if (pos == NULL)
4527 pos = &prev_pos;
4528 nv_cursormark(cap, cap->nchar == '\'', pos);
4529 }
4530
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004531 // [ or ] followed by a middle mouse click: put selected text with
4532 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004533 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
4535 (void)do_mouse(cap->oap, cap->nchar,
4536 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4537 cap->count1, PUT_FIXINDENT);
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004541 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 else if (cap->nchar == 'z')
4543 {
4544 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4545 cap->count1) == FAIL)
4546 clearopbeep(cap->oap);
4547 }
4548#endif
4549
4550#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004551 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else if (cap->nchar == 'c')
4553 {
4554 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4555 cap->count1) == FAIL)
4556 clearopbeep(cap->oap);
4557 }
4558#endif
4559
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004560#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004561 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004562 else if (cap->nchar == 's' || cap->nchar == 'S')
4563 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004564 setpcmark();
4565 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004566 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4567 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004568 {
4569 clearopbeep(cap->oap);
4570 break;
4571 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004572 else
4573 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004574# ifdef FEAT_FOLDING
4575 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4576 foldOpenCursor();
4577# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004578 }
4579#endif
4580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004581 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 else
4583 clearopbeep(cap->oap);
4584}
4585
4586/*
4587 * Handle Normal mode "%" command.
4588 */
4589 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004590nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
4592 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004593#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 linenr_T lnum = curwin->w_cursor.lnum;
4595#endif
4596
4597 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004598 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
4600 if (cap->count0 > 100)
4601 clearopbeep(cap->oap);
4602 else
4603 {
4604 cap->oap->motion_type = MLINE;
4605 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004606 // Round up, so 'normal 100%' always jumps at the line line.
4607 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4608 // overflow on 32-bits, so use a formula with less accuracy
4609 // to avoid overflows.
4610 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4612 / 100L * cap->count0;
4613 else
4614 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4615 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004616 if (curwin->w_cursor.lnum < 1)
4617 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4619 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4620 beginline(BL_SOL | BL_FIX);
4621 }
4622 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
4625 cap->oap->motion_type = MCHAR;
4626 cap->oap->use_reg_one = TRUE;
4627 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4628 clearopbeep(cap->oap);
4629 else
4630 {
4631 setpcmark();
4632 curwin->w_cursor = *pos;
4633 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637 }
4638#ifdef FEAT_FOLDING
4639 if (cap->oap->op_type == OP_NOP
4640 && lnum != curwin->w_cursor.lnum
4641 && (fdo_flags & FDO_PERCENT)
4642 && KeyTyped)
4643 foldOpenCursor();
4644#endif
4645}
4646
4647/*
4648 * Handle "(" and ")" commands.
4649 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4650 */
4651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004652nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 cap->oap->motion_type = MCHAR;
4655 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004656 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004657 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 curwin->w_set_curswant = TRUE;
4659
4660 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004662 clearopbeep(cap->oap);
4663 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004665
4666 // Don't leave the cursor on the NUL past end of line.
4667 adjust_cursor(cap->oap);
4668 curwin->w_cursor.coladd = 0;
4669#ifdef FEAT_FOLDING
4670 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4671 foldOpenCursor();
4672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673}
4674
4675/*
4676 * "m" command: Mark a position.
4677 */
4678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004679nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004681 if (checkclearop(cap->oap))
4682 return;
4683
4684 if (setmark(cap->nchar) == FAIL)
4685 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686}
4687
4688/*
4689 * "{" and "}" commands.
4690 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4691 */
4692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004693nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 cap->oap->motion_type = MCHAR;
4696 cap->oap->inclusive = FALSE;
4697 cap->oap->use_reg_one = TRUE;
4698 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004699 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004701 clearopbeep(cap->oap);
4702 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004704
4705 curwin->w_cursor.coladd = 0;
4706#ifdef FEAT_FOLDING
4707 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4708 foldOpenCursor();
4709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710}
4711
4712/*
4713 * "u" command: Undo or make lower case.
4714 */
4715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004716nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004718 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004720 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 cap->cmdchar = 'g';
4722 cap->nchar = 'u';
4723 nv_operator(cap);
4724 }
4725 else
4726 nv_kundo(cap);
4727}
4728
4729/*
4730 * <Undo> command.
4731 */
4732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004733nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004735 if (checkclearopq(cap->oap))
4736 return;
4737
Bram Moolenaarf2732452018-06-03 14:47:35 +02004738#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004739 if (bt_prompt(curbuf))
4740 {
4741 clearopbeep(cap->oap);
4742 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004744#endif
4745 u_undo((int)cap->count1);
4746 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
4749/*
4750 * Handle the "r" command.
4751 */
4752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004753nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 char_u *ptr;
4756 int had_ctrl_v;
4757 long n;
4758
4759 if (checkclearop(cap->oap))
4760 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004761#ifdef FEAT_JOB_CHANNEL
4762 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4763 {
4764 clearopbeep(cap->oap);
4765 return;
4766 }
4767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 // get another character
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02004770 if (cap->nchar == Ctrl_V || cap->nchar == Ctrl_Q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004773 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (cap->nchar > DEL)
4776 had_ctrl_v = NUL;
4777 }
4778 else
4779 had_ctrl_v = NUL;
4780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004781 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004782 if (IS_SPECIAL(cap->nchar))
4783 {
4784 clearopbeep(cap->oap);
4785 return;
4786 }
4787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (VIsual_active)
4790 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004791 if (got_int)
4792 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004793 if (had_ctrl_v)
4794 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004795 // Use a special (negative) number to make a difference between a
4796 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004797 if (cap->nchar == CAR)
4798 cap->nchar = REPLACE_CR_NCHAR;
4799 else if (cap->nchar == NL)
4800 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 nv_operator(cap);
4803 return;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004806 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (virtual_active())
4808 {
4809 if (u_save_cursor() == FAIL)
4810 return;
4811 if (gchar_cursor() == NUL)
4812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 coladvance_force((colnr_T)(getviscol() + cap->count1));
4815 curwin->w_cursor.col -= cap->count1;
4816 }
4817 else if (gchar_cursor() == TAB)
4818 coladvance_force(getviscol());
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004821 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004823 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004824 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
4826 clearopbeep(cap->oap);
4827 return;
4828 }
4829
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004830 // Replacing with a TAB is done by edit() when it is complicated because
4831 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4832 // Other characters are done below to avoid problems with things like
4833 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4835 {
4836 stuffnumReadbuff(cap->count1);
4837 stuffcharReadbuff('R');
4838 stuffcharReadbuff('\t');
4839 stuffcharReadbuff(ESC);
4840 return;
4841 }
4842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004843 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (u_save_cursor() == FAIL)
4845 return;
4846
4847 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4848 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004849 // Replace character(s) by a single newline.
4850 // Strange vi behaviour: Only one newline is inserted.
4851 // Delete the characters here.
4852 // Insert the newline with an insert command, takes care of
4853 // autoindent. The insert command depends on being on the last
4854 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004855 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 stuffcharReadbuff('\r');
4857 stuffcharReadbuff(ESC);
4858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004859 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 invoke_edit(cap, TRUE, 'r', FALSE);
4861 }
4862 else
4863 {
4864 prep_redo(cap->oap->regname, cap->count1,
4865 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4866
4867 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 if (has_mbyte)
4869 {
4870 int old_State = State;
4871
4872 if (cap->ncharC1 != 0)
4873 AppendCharToRedobuff(cap->ncharC1);
4874 if (cap->ncharC2 != 0)
4875 AppendCharToRedobuff(cap->ncharC2);
4876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004877 // This is slow, but it handles replacing a single-byte with a
4878 // multi-byte and the other way around. Also handles adding
4879 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 for (n = cap->count1; n > 0; --n)
4881 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004882 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004883 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4884 {
4885 int c = ins_copychar(curwin->w_cursor.lnum
4886 + (cap->nchar == Ctrl_Y ? -1 : 1));
4887 if (c != NUL)
4888 ins_char(c);
4889 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004890 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004891 ++curwin->w_cursor.col;
4892 }
4893 else
4894 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 State = old_State;
4896 if (cap->ncharC1 != 0)
4897 ins_char(cap->ncharC1);
4898 if (cap->ncharC2 != 0)
4899 ins_char(cap->ncharC2);
4900 }
4901 }
4902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004904 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 for (n = cap->count1; n > 0; --n)
4906 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004907 // Get ptr again, because u_save and/or showmatch() will have
4908 // released the line. This may also happen in ins_copychar().
4909 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004910 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4911 {
4912 int c = ins_copychar(curwin->w_cursor.lnum
4913 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004914
4915 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004916 if (c != NUL)
4917 ptr[curwin->w_cursor.col] = c;
4918 }
4919 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004920 {
4921 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004922 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 if (p_sm && msg_silent == 0)
4925 showmatch(cap->nchar);
4926 ++curwin->w_cursor.col;
4927 }
4928#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004929 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004931 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar009b2592004-10-24 19:18:58 +00004933 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004934 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004936 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938#endif
4939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004940 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 changed_bytes(curwin->w_cursor.lnum,
4942 (colnr_T)(curwin->w_cursor.col - cap->count1));
4943 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004944 --curwin->w_cursor.col; // cursor on the last replaced char
4945 // if the character on the left of the current cursor is a multi-byte
4946 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if (has_mbyte)
4948 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 curbuf->b_op_end = curwin->w_cursor;
4950 curwin->w_set_curswant = TRUE;
4951 set_last_insert(cap->nchar);
4952 }
4953}
4954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955/*
4956 * 'o': Exchange start and end of Visual area.
4957 * 'O': same, but in block mode exchange left and right corners.
4958 */
4959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004960v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 pos_T old_cursor;
4963 colnr_T left, right;
4964
4965 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4966 {
4967 old_cursor = curwin->w_cursor;
4968 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4969 curwin->w_cursor.lnum = VIsual.lnum;
4970 coladvance(left);
4971 VIsual = curwin->w_cursor;
4972
4973 curwin->w_cursor.lnum = old_cursor.lnum;
4974 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004975 // 'selection "exclusive" and cursor at right-bottom corner: move it
4976 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4978 ++curwin->w_curswant;
4979 coladvance(curwin->w_curswant);
4980 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004982 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 curwin->w_cursor.lnum = VIsual.lnum;
4985 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4986 ++right;
4987 coladvance(right);
4988 VIsual = curwin->w_cursor;
4989
4990 curwin->w_cursor.lnum = old_cursor.lnum;
4991 coladvance(left);
4992 curwin->w_curswant = left;
4993 }
4994 }
4995 else
4996 {
4997 old_cursor = curwin->w_cursor;
4998 curwin->w_cursor = VIsual;
4999 VIsual = old_cursor;
5000 curwin->w_set_curswant = TRUE;
5001 }
5002}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004/*
5005 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5006 */
5007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005008nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
5012 cap->cmdchar = 'c';
5013 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005014 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 VIsual_mode = 'V';
5016 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005017 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005019
5020 if (checkclearopq(cap->oap))
5021 return;
5022
5023 if (!curbuf->b_p_ma)
5024 emsg(_(e_cannot_make_changes_modifiable_is_off));
5025 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005027 if (virtual_active())
5028 coladvance(getviscol());
5029 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031}
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033/*
5034 * "gr".
5035 */
5036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005037nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (VIsual_active)
5040 {
5041 cap->cmdchar = 'r';
5042 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005044 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005046
zeertzjqf86dea82023-03-05 21:15:06 +00005047 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005048 return;
5049
5050 if (!curbuf->b_p_ma)
5051 emsg(_(e_cannot_make_changes_modifiable_is_off));
5052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02005054 if (cap->extra_char == Ctrl_V || cap->extra_char == Ctrl_Q)
5055 // get another character
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005056 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005057 if (cap->extra_char < ' ')
5058 // Prefix a control character with CTRL-V to avoid it being used as
5059 // a command.
5060 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005061 stuffcharReadbuff(cap->extra_char);
5062 stuffcharReadbuff(ESC);
5063 if (virtual_active())
5064 coladvance(getviscol());
5065 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069/*
5070 * Swap case for "~" command, when it does not work like an operator.
5071 */
5072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005073n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
5075 long n;
5076 pos_T startpos;
5077 int did_change = 0;
5078#ifdef FEAT_NETBEANS_INTG
5079 pos_T pos;
5080 char_u *ptr;
5081 int count;
5082#endif
5083
5084 if (checkclearopq(cap->oap))
5085 return;
5086
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005087 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 clearopbeep(cap->oap);
5090 return;
5091 }
5092
5093 prep_redo_cmd(cap);
5094
5095 if (u_save_cursor() == FAIL)
5096 return;
5097
5098 startpos = curwin->w_cursor;
5099#ifdef FEAT_NETBEANS_INTG
5100 pos = startpos;
5101#endif
5102 for (n = cap->count1; n > 0; --n)
5103 {
5104 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5105 inc_cursor();
5106 if (gchar_cursor() == NUL)
5107 {
5108 if (vim_strchr(p_ww, '~') != NULL
5109 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5110 {
5111#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005112 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 if (did_change)
5115 {
5116 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005117 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005118 netbeans_removed(curbuf, pos.lnum, pos.col,
5119 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005120 // line may have been flushed, get it again
5121 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005123 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 pos.col = 0;
5126 pos.lnum++;
5127 }
5128#endif
5129 ++curwin->w_cursor.lnum;
5130 curwin->w_cursor.col = 0;
5131 if (n > 1)
5132 {
5133 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5134 break;
5135 u_clearline();
5136 }
5137 }
5138 else
5139 break;
5140 }
5141 }
5142#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005143 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 ptr = ml_get(pos.lnum);
5146 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005147 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5148 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150#endif
5151
5152
5153 check_cursor();
5154 curwin->w_set_curswant = TRUE;
5155 if (did_change)
5156 {
5157 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5158 0L);
5159 curbuf->b_op_start = startpos;
5160 curbuf->b_op_end = curwin->w_cursor;
5161 if (curbuf->b_op_end.col > 0)
5162 --curbuf->b_op_end.col;
5163 }
5164}
5165
5166/*
5167 * Move cursor to mark.
5168 */
5169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 if (check_mark(pos) == FAIL)
5173 clearop(cap->oap);
5174 else
5175 {
5176 if (cap->cmdchar == '\''
5177 || cap->cmdchar == '`'
5178 || cap->cmdchar == '['
5179 || cap->cmdchar == ']')
5180 setpcmark();
5181 curwin->w_cursor = *pos;
5182 if (flag)
5183 beginline(BL_WHITE | BL_FIX);
5184 else
5185 check_cursor();
5186 }
5187 cap->oap->motion_type = flag ? MLINE : MCHAR;
5188 if (cap->cmdchar == '`')
5189 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005190 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 curwin->w_set_curswant = TRUE;
5192}
5193
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194/*
5195 * Handle commands that are operators in Visual mode.
5196 */
5197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 // Uppercase means linewise, except in block mode, then "D" deletes till
5203 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (isupper(cap->cmdchar))
5205 {
5206 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005207 {
5208 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5212 curwin->w_curswant = MAXCOL;
5213 }
5214 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5215 nv_operator(cap);
5216}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218/*
5219 * "s" and "S" commands.
5220 */
5221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005224#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005225 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005226 if (term_swap_diff() == OK)
5227 return;
5228#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005229#ifdef FEAT_JOB_CHANNEL
5230 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5231 {
5232 clearopbeep(cap->oap);
5233 return;
5234 }
5235#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005236 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005239 {
5240 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 cap->cmdchar = 'c';
5244 nv_operator(cap);
5245 }
5246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 nv_optrans(cap);
5248}
5249
5250/*
5251 * Abbreviated commands.
5252 */
5253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005257 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005259 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (VIsual_active)
5261 v_visop(cap);
5262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 nv_optrans(cap);
5264}
5265
5266/*
5267 * Translate a command into another command.
5268 */
5269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005270nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5273 (char_u *)"d$", (char_u *)"c$",
5274 (char_u *)"cl", (char_u *)"cc",
5275 (char_u *)"yy", (char_u *)":s\r"};
5276 static char_u *str = (char_u *)"xXDCsSY&";
5277
5278 if (!checkclearopq(cap->oap))
5279 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005280 // In Vi "2D" doesn't delete the next line. Can't translate it
5281 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005282 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5283 {
5284 cap->oap->start = curwin->w_cursor;
5285 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005286#ifdef FEAT_EVAL
5287 set_op_var(OP_DELETE);
5288#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005289 cap->count1 = 1;
5290 nv_dollar(cap);
5291 finish_op = TRUE;
5292 ResetRedobuff();
5293 AppendCharToRedobuff('D');
5294 }
5295 else
5296 {
5297 if (cap->count0)
5298 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005299 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302 cap->opcount = 0;
5303}
5304
5305/*
5306 * "'" and "`" commands. Also for "g'" and "g`".
5307 * cap->arg is TRUE for "'" and "g'".
5308 */
5309 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005310nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
5312 pos_T *pos;
5313 int c;
5314#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005315 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005316 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
5318
5319 if (cap->cmdchar == 'g')
5320 c = cap->extra_char;
5321 else
5322 c = cap->nchar;
5323 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005324 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 if (cap->arg)
5327 {
5328 check_cursor_lnum();
5329 beginline(BL_WHITE | BL_FIX);
5330 }
5331 else
5332 check_cursor();
5333 }
5334 else
5335 nv_cursormark(cap, cap->arg, pos);
5336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005337 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (!virtual_active())
5339 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005340 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#ifdef FEAT_FOLDING
5342 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005343 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005344 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 && (fdo_flags & FDO_MARK)
5346 && old_KeyTyped)
5347 foldOpenCursor();
5348#endif
5349}
5350
5351/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005352 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 */
5354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005358#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005360 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005363 if (checkclearopq(cap->oap))
5364 return;
5365
5366 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005368 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005370 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005372 if (cap->cmdchar == 'g')
5373 pos = movechangelist((int)cap->count1);
5374 else
5375 pos = movemark((int)cap->count1);
5376 if (pos == (pos_T *)-1) // jump to other file
5377 {
5378 curwin->w_set_curswant = TRUE;
5379 check_cursor();
5380 }
5381 else if (pos != NULL) // can jump
5382 nv_cursormark(cap, FALSE, pos);
5383 else if (cap->cmdchar == 'g')
5384 {
5385 if (curbuf->b_changelistlen == 0)
5386 emsg(_(e_changelist_is_empty));
5387 else if (cap->count1 < 0)
5388 emsg(_(e_at_start_of_changelist));
5389 else
5390 emsg(_(e_at_end_of_changelist));
5391 }
5392 else
5393 clearopbeep(cap->oap);
5394# ifdef FEAT_FOLDING
5395 if (cap->oap->op_type == OP_NOP
5396 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5397 && (fdo_flags & FDO_MARK)
5398 && old_KeyTyped)
5399 foldOpenCursor();
5400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401}
5402
5403/*
5404 * Handle '"' command.
5405 */
5406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005407nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 if (checkclearop(cap->oap))
5410 return;
5411#ifdef FEAT_EVAL
5412 if (cap->nchar == '=')
5413 cap->nchar = get_expr_register();
5414#endif
5415 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5416 {
5417 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005418 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#ifdef FEAT_EVAL
5420 set_reg_var(cap->oap->regname);
5421#endif
5422 }
5423 else
5424 clearopbeep(cap->oap);
5425}
5426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427/*
5428 * Handle "v", "V" and "CTRL-V" commands.
5429 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5430 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005431 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 */
5433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005434nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005436 if (cap->cmdchar == Ctrl_Q)
5437 cap->cmdchar = Ctrl_V;
5438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005439 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5440 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (cap->oap->op_type != OP_NOP)
5442 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005443 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return;
5446 }
5447
5448 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005449 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005451 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 else // toggle char/block mode
5454 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 VIsual_mode = cap->cmdchar;
5456 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005457 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005459 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005464 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 VIsual = curwin->w_cursor;
5468
5469 VIsual_active = TRUE;
5470 VIsual_reselect = TRUE;
5471 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005472 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005475 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005476 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005477 // For V and ^V, we multiply the number of lines even if there
5478 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5480 {
5481 curwin->w_cursor.lnum +=
5482 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005483 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485 VIsual_mode = resel_VIsual_mode;
5486 if (VIsual_mode == 'v')
5487 {
5488 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005489 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005490 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005491 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5492 if (*p_sel != 'e')
5493 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005496 curwin->w_curswant = resel_VIsual_vcol;
5497 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005499 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
5501 curwin->w_curswant = MAXCOL;
5502 coladvance((colnr_T)MAXCOL);
5503 }
5504 else if (VIsual_mode == Ctrl_V)
5505 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005506 // Update curswant on the original line, that is where "col" is
5507 // valid.
5508 linenr_T lnum = curwin->w_cursor.lnum;
5509 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005510 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005511 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5512 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 coladvance(curwin->w_curswant);
5514 }
5515 else
5516 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005517 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
5519 else
5520 {
5521 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005522 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 may_start_select('c');
5524 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005525 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005527 if (cap->count0 > 0 && --cap->count1 > 0)
5528 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005529 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005530 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5531 nv_right(cap);
5532 else if (VIsual_mode == 'V')
5533 nv_down(cap);
5534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 }
5536 }
5537}
5538
5539/*
5540 * Start selection for Shift-movement keys.
5541 */
5542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005543start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005545 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 may_start_select('k');
5547 n_start_visual_mode('v');
5548}
5549
5550/*
5551 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005552 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 */
5554 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005555may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005557 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5558 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559}
5560
5561/*
5562 * Start Visual mode "c".
5563 * Should set VIsual_select before calling this.
5564 */
5565 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005566n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005568#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005569 int cursor_line_was_concealed = curwin->w_p_cole > 0
5570 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005571#endif
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 VIsual_mode = c;
5574 VIsual_active = TRUE;
5575 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005576
5577 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005578 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005579 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005580 {
5581 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 VIsual = curwin->w_cursor;
5585
5586#ifdef FEAT_FOLDING
5587 foldAdjustVisual();
5588#endif
5589
LemonBoy2bf52dd2022-04-09 18:17:34 +01005590 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005592#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005593 // Check if redraw is needed after changing the state.
5594 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005595#endif
5596
Bram Moolenaar09df3122006-01-23 22:23:09 +00005597 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005600 // Make sure the clipboard gets updated. Needed because start and
5601 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 clip_star.vmode = NUL;
5603#endif
5604
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005605 // Only need to redraw this line, unless still need to redraw an old
5606 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005607 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5610 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5611 }
5612}
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
5615/*
5616 * CTRL-W: Window commands
5617 */
5618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005619nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005621 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005622 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005624 cap->cmdchar = ':';
5625 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005626 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005627 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005628 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005629 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630}
5631
5632/*
5633 * CTRL-Z: Suspend
5634 */
5635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005636nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005640 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005641 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
5644/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005645 * "gv": Reselect the previous Visual area. If Visual already active,
5646 * exchange previous and current Visual area.
5647 */
5648 static void
5649nv_gv_cmd(cmdarg_T *cap)
5650{
5651 pos_T tpos;
5652 int i;
5653
5654 if (checkclearop(cap->oap))
5655 return;
5656
5657 if (curbuf->b_visual.vi_start.lnum == 0
5658 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5659 || curbuf->b_visual.vi_end.lnum == 0)
5660 {
5661 beep_flush();
5662 return;
5663 }
5664
5665 // set w_cursor to the start of the Visual area, tpos to the end
5666 if (VIsual_active)
5667 {
5668 i = VIsual_mode;
5669 VIsual_mode = curbuf->b_visual.vi_mode;
5670 curbuf->b_visual.vi_mode = i;
5671# ifdef FEAT_EVAL
5672 curbuf->b_visual_mode_eval = i;
5673# endif
5674 i = curwin->w_curswant;
5675 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5676 curbuf->b_visual.vi_curswant = i;
5677
5678 tpos = curbuf->b_visual.vi_end;
5679 curbuf->b_visual.vi_end = curwin->w_cursor;
5680 curwin->w_cursor = curbuf->b_visual.vi_start;
5681 curbuf->b_visual.vi_start = VIsual;
5682 }
5683 else
5684 {
5685 VIsual_mode = curbuf->b_visual.vi_mode;
5686 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5687 tpos = curbuf->b_visual.vi_end;
5688 curwin->w_cursor = curbuf->b_visual.vi_start;
5689 }
5690
5691 VIsual_active = TRUE;
5692 VIsual_reselect = TRUE;
5693
5694 // Set Visual to the start and w_cursor to the end of the Visual
5695 // area. Make sure they are on an existing character.
5696 check_cursor();
5697 VIsual = curwin->w_cursor;
5698 curwin->w_cursor = tpos;
5699 check_cursor();
5700 update_topline();
5701
5702 // When called from normal "g" command: start Select mode when
5703 // 'selectmode' contains "cmd". When called for K_SELECT, always
5704 // start Select mode.
5705 if (cap->arg)
5706 {
5707 VIsual_select = TRUE;
5708 VIsual_select_reg = 0;
5709 }
5710 else
5711 may_start_select('c');
5712 setmouse();
5713#ifdef FEAT_CLIPBOARD
5714 // Make sure the clipboard gets updated. Needed because start and
5715 // end are still the same, and the selection needs to be owned
5716 clip_star.vmode = NUL;
5717#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005718 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005719 showmode();
5720}
5721
5722/*
5723 * "g0", "g^" : Like "0" and "^" but for screen lines.
5724 * "gm": middle of "g0" and "g$".
5725 */
5726 static void
5727nv_g_home_m_cmd(cmdarg_T *cap)
5728{
5729 int i;
5730 int flag = FALSE;
5731
5732 if (cap->nchar == '^')
5733 flag = TRUE;
5734
5735 cap->oap->motion_type = MCHAR;
5736 cap->oap->inclusive = FALSE;
5737 if (curwin->w_p_wrap && curwin->w_width != 0)
5738 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005739 int width1 = curwin->w_width - curwin_col_off();
5740 int width2 = width1 + curwin_col_off2();
5741 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005742
5743 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005744 virtcol = curwin->w_virtcol
5745#ifdef FEAT_PROP_POPUP
5746 - curwin->w_virtcol_first_char
5747#endif
5748 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005749 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005750 if (virtcol >= (colnr_T)width1 && width2 > 0)
5751 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005752 }
5753 else
5754 i = curwin->w_leftcol;
5755 // Go to the middle of the screen line. When 'number' or
5756 // 'relativenumber' is on and lines are wrapping the middle can be more
5757 // to the left.
5758 if (cap->nchar == 'm')
5759 i += (curwin->w_width - curwin_col_off()
5760 + ((curwin->w_p_wrap && i > 0)
5761 ? curwin_col_off2() : 0)) / 2;
5762 coladvance((colnr_T)i);
5763 if (flag)
5764 {
5765 do
5766 i = gchar_cursor();
5767 while (VIM_ISWHITE(i) && oneright() == OK);
5768 curwin->w_valid &= ~VALID_WCOL;
5769 }
5770 curwin->w_set_curswant = TRUE;
Luuk van Baal8667a562023-05-12 15:47:25 +01005771 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005772}
5773
5774/*
5775 * "g_": to the last non-blank character in the line or <count> lines
5776 * downward.
5777 */
5778 static void
5779nv_g_underscore_cmd(cmdarg_T *cap)
5780{
5781 char_u *ptr;
5782
5783 cap->oap->motion_type = MCHAR;
5784 cap->oap->inclusive = TRUE;
5785 curwin->w_curswant = MAXCOL;
5786 if (cursor_down((long)(cap->count1 - 1),
5787 cap->oap->op_type == OP_NOP) == FAIL)
5788 {
5789 clearopbeep(cap->oap);
5790 return;
5791 }
5792
5793 ptr = ml_get_curline();
5794
5795 // In Visual mode we may end up after the line.
5796 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5797 --curwin->w_cursor.col;
5798
5799 // Decrease the cursor column until it's on a non-blank.
5800 while (curwin->w_cursor.col > 0
5801 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5802 --curwin->w_cursor.col;
5803 curwin->w_set_curswant = TRUE;
5804 adjust_for_sel(cap);
5805}
5806
5807/*
5808 * "g$" : Like "$" but for screen lines.
5809 */
5810 static void
5811nv_g_dollar_cmd(cmdarg_T *cap)
5812{
5813 oparg_T *oap = cap->oap;
5814 int i;
5815 int col_off = curwin_col_off();
5816
5817 oap->motion_type = MCHAR;
5818 oap->inclusive = TRUE;
5819 if (curwin->w_p_wrap && curwin->w_width != 0)
5820 {
5821 curwin->w_curswant = MAXCOL; // so we stay at the end
5822 if (cap->count1 == 1)
5823 {
5824 int width1 = curwin->w_width - col_off;
5825 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005826 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005827
5828 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005829 virtcol = curwin->w_virtcol
5830#ifdef FEAT_PROP_POPUP
5831 - curwin->w_virtcol_first_char
5832#endif
5833 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005834 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005835 if (virtcol >= (colnr_T)width1)
5836 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005837 * width2;
5838 coladvance((colnr_T)i);
5839
5840 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005841 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005842 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5843 {
5844 // Check for landing on a character that got split at
5845 // the end of the line. We do not want to advance to
5846 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005847 if (curwin->w_virtcol
5848#ifdef FEAT_PROP_POPUP
5849 - curwin->w_virtcol_first_char
5850#endif
5851 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005852 --curwin->w_cursor.col;
5853 }
5854 }
5855 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5856 clearopbeep(oap);
5857 }
5858 else
5859 {
5860 if (cap->count1 > 1)
5861 // if it fails, let the cursor still move to the last char
5862 (void)cursor_down(cap->count1 - 1, FALSE);
5863
5864 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5865 coladvance((colnr_T)i);
5866
5867 // if the character doesn't fit move one back
5868 if (curwin->w_cursor.col > 0
5869 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5870 {
5871 colnr_T vcol;
5872
5873 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5874 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5875 --curwin->w_cursor.col;
5876 }
5877
5878 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005879 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005880 }
5881}
5882
5883/*
5884 * "gi": start Insert at the last position.
5885 */
5886 static void
5887nv_gi_cmd(cmdarg_T *cap)
5888{
5889 int i;
5890
5891 if (curbuf->b_last_insert.lnum != 0)
5892 {
5893 curwin->w_cursor = curbuf->b_last_insert;
5894 check_cursor_lnum();
5895 i = (int)STRLEN(ml_get_curline());
5896 if (curwin->w_cursor.col > (colnr_T)i)
5897 {
5898 if (virtual_active())
5899 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5900 curwin->w_cursor.col = i;
5901 }
5902 }
5903 cap->cmdchar = 'i';
5904 nv_edit(cap);
5905}
5906
5907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 * Commands starting with "g".
5909 */
5910 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005911nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
5916 switch (cap->nchar)
5917 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005918 case Ctrl_A:
5919 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005921 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005922 if (!VIsual_active && cap->nchar == Ctrl_A)
5923 vim_mem_profile_dump();
5924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005926 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005927 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005928 {
5929 cap->arg = TRUE;
5930 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005931 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005932 nv_addsub(cap);
5933 }
5934 else
5935 clearopbeep(oap);
5936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005938 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case 'R':
5940 cap->arg = TRUE;
5941 nv_Replace(cap);
5942 break;
5943
5944 case 'r':
5945 nv_vreplace(cap);
5946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 case '&':
5949 do_cmdline_cmd((char_u *)"%s//~/&");
5950 break;
5951
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005952 // "gv": Reselect the previous Visual area. If Visual already active,
5953 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005955 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005957
5958 // "gV": Don't reselect the previous Visual area after a Select mode
5959 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 case 'V':
5961 VIsual_reselect = FALSE;
5962 break;
5963
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005964 // "gh": start Select mode.
5965 // "gH": start Select line mode.
5966 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 case K_BS:
5968 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005969 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 case 'h':
5971 case 'H':
5972 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 cap->cmdchar = cap->nchar + ('v' - 'h');
5974 cap->arg = TRUE;
5975 nv_visual(cap);
5976 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005978 // "gn", "gN" visually select next/previous search match
5979 // "gn" selects next match
5980 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005981 case 'N':
5982 case 'n':
5983 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005984 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005987 // "gj" and "gk" two new funny movement keys -- up and down
5988 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 case 'j':
5990 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005991 // with 'nowrap' it works just like the normal "j" command.
5992 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 oap->motion_type = MLINE;
5995 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5996 }
5997 else
5998 i = nv_screengo(oap, FORWARD, cap->count1);
5999 if (i == FAIL)
6000 clearopbeep(oap);
6001 break;
6002
6003 case 'k':
6004 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006005 // with 'nowrap' it works just like the normal "k" command.
6006 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {
6008 oap->motion_type = MLINE;
6009 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6010 }
6011 else
6012 i = nv_screengo(oap, BACKWARD, cap->count1);
6013 if (i == FAIL)
6014 clearopbeep(oap);
6015 break;
6016
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006017 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 case 'J':
6019 nv_join(cap);
6020 break;
6021
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006022 // "g0", "g^" : Like "0" and "^" but for screen lines.
6023 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 case '0':
6026 case 'm':
6027 case K_HOME:
6028 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006029 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 break;
6031
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006032 case 'M':
6033 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006034 oap->motion_type = MCHAR;
6035 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006036 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006037 if (cap->count0 > 0 && cap->count0 <= 100)
6038 coladvance((colnr_T)(i * cap->count0 / 100));
6039 else
6040 coladvance((colnr_T)(i / 2));
6041 curwin->w_set_curswant = TRUE;
6042 }
6043 break;
6044
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006045 // "g_": to the last non-blank character in the line or <count> lines
6046 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 break;
6050
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006051 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 case '$':
6053 case K_END:
6054 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006055 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 break;
6057
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 case '*':
6060 case '#':
6061#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006062 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006064 case Ctrl_RSB: // :tag or :tselect for current identifier
6065 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 nv_ident(cap);
6067 break;
6068
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006069 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case 'e':
6071 case 'E':
6072 oap->motion_type = MCHAR;
6073 curwin->w_set_curswant = TRUE;
6074 oap->inclusive = TRUE;
6075 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6076 clearopbeep(oap);
6077 break;
6078
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006079 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006081 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 break;
6083
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006084 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006086 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 break;
6088
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006089 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 case 'I':
6091 beginline(0);
6092 if (!checkclearopq(oap))
6093 invoke_edit(cap, FALSE, 'g', FALSE);
6094 break;
6095
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006096 // "gf": goto file, edit file under cursor
6097 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006099 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 nv_gotofile(cap);
6101 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006103 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 case '\'':
6105 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 case '`':
6108 nv_gomark(cap);
6109 break;
6110
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006111 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006113 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 break;
6115
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006116 // "ga": Display the ascii value of the character under the
6117 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 case 'a':
6119 do_ascii(NULL);
6120 break;
6121
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006122 // "g8": Display the bytes used for the UTF-8 character under the
6123 // cursor. It is displayed in hex.
6124 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006126 if (cap->count0 == 8)
6127 utf_find_illegal();
6128 else
6129 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006132 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006133 case '<':
6134 show_sb_text();
6135 break;
6136
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006137 // "gg": Goto the first line in file. With a count it goes to
6138 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 case 'g':
6140 cap->arg = FALSE;
6141 nv_goto(cap);
6142 break;
6143
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006144 // Two-character operators:
6145 // "gq" Format text
6146 // "gw" Format text and keep cursor position
6147 // "g~" Toggle the case of the text.
6148 // "gu" Change text to lower case.
6149 // "gU" Change text to upper case.
6150 // "g?" rot13 encoding
6151 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 case 'q':
6153 case 'w':
6154 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006155 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 case '~':
6157 case 'u':
6158 case 'U':
6159 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006160 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 nv_operator(cap);
6162 break;
6163
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006164 // "gd": Find first occurrence of pattern under the cursor in the
6165 // current function
6166 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 case 'd':
6168 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006169 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 break;
6171
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006172 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 case K_MIDDLEMOUSE:
6174 case K_MIDDLEDRAG:
6175 case K_MIDDLERELEASE:
6176 case K_LEFTMOUSE:
6177 case K_LEFTDRAG:
6178 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006179 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 case K_RIGHTMOUSE:
6181 case K_RIGHTDRAG:
6182 case K_RIGHTRELEASE:
6183 case K_X1MOUSE:
6184 case K_X1DRAG:
6185 case K_X1RELEASE:
6186 case K_X2MOUSE:
6187 case K_X2DRAG:
6188 case K_X2RELEASE:
6189 mod_mask = MOD_MASK_CTRL;
6190 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6191 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
6193 case K_IGNORE:
6194 break;
6195
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006196 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 case 'p':
6198 case 'P':
6199 nv_put(cap);
6200 break;
6201
6202#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006203 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case 'o':
6205 goto_byte(cap->count0);
6206 break;
6207#endif
6208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006209 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006211 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 do_exmode(TRUE);
6213 break;
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 case ',':
6216 nv_pcmark(cap);
6217 break;
6218
6219 case ';':
6220 cap->count1 = -cap->count1;
6221 nv_pcmark(cap);
6222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006225 if (!checkclearop(oap))
6226 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006228 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006229 if (!checkclearop(oap))
6230 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006231 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232
Bram Moolenaar62a23252020-08-09 14:04:42 +02006233 case TAB:
6234 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6235 clearopbeep(oap);
6236 break;
6237
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006238 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006239 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006240 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006241 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006242 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006243 break;
6244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 default:
6246 clearopbeep(oap);
6247 break;
6248 }
6249}
6250
6251/*
6252 * Handle "o" and "O" commands.
6253 */
6254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006255n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006257#ifdef FEAT_CONCEAL
6258 linenr_T oldline = curwin->w_cursor.lnum;
6259#endif
6260
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006261 if (checkclearopq(cap->oap))
6262 return;
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006265 if (cap->cmdchar == 'O')
6266 // Open above the first line of a folded sequence of lines
6267 (void)hasFolding(curwin->w_cursor.lnum,
6268 &curwin->w_cursor.lnum, NULL);
6269 else
6270 // Open below the last line of a folded sequence of lines
6271 (void)hasFolding(curwin->w_cursor.lnum,
6272 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006274 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6275 (cap->cmdchar == 'O' ? 1 : 0)),
6276 (linenr_T)(curwin->w_cursor.lnum +
6277 (cap->cmdchar == 'o' ? 1 : 0))
6278 ) == OK
6279 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6280 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6281 0, NULL) == OK)
6282 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006283#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006284 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6285 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006286#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006287#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006288 if (curwin->w_p_cul)
6289 // force redraw of cursorline
6290 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006291#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006292 // When '#' is in 'cpoptions' ignore the count.
6293 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6294 cap->count1 = 1;
6295 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 }
6297}
6298
6299/*
6300 * "." command: redo last change.
6301 */
6302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006303nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006305 if (checkclearopq(cap->oap))
6306 return;
6307
6308 // If "restart_edit" is TRUE, the last but one command is repeated
6309 // instead of the last command (inserting text). This is used for
6310 // CTRL-O <.> in insert mode.
6311 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6312 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313}
6314
6315/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006316 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 */
6318 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006319nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006321 if (VIsual_select && VIsual_active)
6322 {
6323 int reg;
6324 // Get register name
6325 ++no_mapping;
6326 ++allow_keys;
6327 reg = plain_vgetc();
6328 LANGMAP_ADJUST(reg, TRUE);
6329 --no_mapping;
6330 --allow_keys;
6331
6332 if (reg == '"')
6333 // the unnamed register is 0
6334 reg = 0;
6335
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006336 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006337 return;
6338 }
6339
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006340 if (checkclearopq(cap->oap))
6341 return;
6342
6343 u_redo((int)cap->count1);
6344 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345}
6346
6347/*
6348 * Handle "U" command.
6349 */
6350 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006353 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006354 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006356 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 cap->cmdchar = 'g';
6358 cap->nchar = 'U';
6359 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006360 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006362
6363 if (checkclearopq(cap->oap))
6364 return;
6365
6366 u_undoline();
6367 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368}
6369
6370/*
6371 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6372 * single character.
6373 */
6374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006375nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006377 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006378 {
6379#ifdef FEAT_JOB_CHANNEL
6380 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6381 {
6382 clearopbeep(cap->oap);
6383 return;
6384 }
6385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 else
6389 nv_operator(cap);
6390}
6391
6392/*
6393 * Handle an operator command.
6394 * The actual work is done by do_pending_operator().
6395 */
6396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006397nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 int op_type;
6400
6401 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006402#ifdef FEAT_JOB_CHANNEL
6403 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6404 {
6405 clearopbeep(cap->oap);
6406 return;
6407 }
6408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006410 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 nv_lineop(cap);
6412 else if (!checkclearop(cap->oap))
6413 {
6414 cap->oap->start = curwin->w_cursor;
6415 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006416#ifdef FEAT_EVAL
6417 set_op_var(op_type);
6418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 }
6420}
6421
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006422#ifdef FEAT_EVAL
6423/*
6424 * Set v:operator to the characters for "optype".
6425 */
6426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006427set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006428{
6429 char_u opchars[3];
6430
6431 if (optype == OP_NOP)
6432 set_vim_var_string(VV_OP, NULL, 0);
6433 else
6434 {
6435 opchars[0] = get_op_char(optype);
6436 opchars[1] = get_extra_op_char(optype);
6437 opchars[2] = NUL;
6438 set_vim_var_string(VV_OP, opchars, -1);
6439 }
6440}
6441#endif
6442
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443/*
6444 * Handle linewise operator "dd", "yy", etc.
6445 *
6446 * "_" is is a strange motion command that helps make operators more logical.
6447 * It is actually implemented, but not documented in the real Vi. This motion
6448 * command actually refers to "the current line". Commands like "dd" and "yy"
6449 * are really an alternate form of "d_" and "y_". It does accept a count, so
6450 * "d3_" works to delete 3 lines.
6451 */
6452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006453nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 cap->oap->motion_type = MLINE;
6456 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6457 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006458 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006459 && cap->oap->motion_force != 'v'
6460 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 || cap->oap->op_type == OP_LSHIFT
6462 || cap->oap->op_type == OP_RSHIFT)
6463 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006464 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 beginline(BL_WHITE | BL_FIX);
6466}
6467
6468/*
6469 * <Home> command.
6470 */
6471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006474 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006475 if (mod_mask & MOD_MASK_CTRL)
6476 nv_goto(cap);
6477 else
6478 {
6479 cap->count0 = 1;
6480 nv_pipe(cap);
6481 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006482 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6483 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486/*
6487 * "|" command.
6488 */
6489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006490nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 cap->oap->motion_type = MCHAR;
6493 cap->oap->inclusive = FALSE;
6494 beginline(0);
6495 if (cap->count0 > 0)
6496 {
6497 coladvance((colnr_T)(cap->count0 - 1));
6498 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6499 }
6500 else
6501 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006502 // keep curswant at the column where we wanted to go, not where
6503 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 curwin->w_set_curswant = FALSE;
6505}
6506
6507/*
6508 * Handle back-word command "b" and "B".
6509 * cap->arg is 1 for "B"
6510 */
6511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006512nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 cap->oap->motion_type = MCHAR;
6515 cap->oap->inclusive = FALSE;
6516 curwin->w_set_curswant = TRUE;
6517 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6518 clearopbeep(cap->oap);
6519#ifdef FEAT_FOLDING
6520 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6521 foldOpenCursor();
6522#endif
6523}
6524
6525/*
6526 * Handle word motion commands "e", "E", "w" and "W".
6527 * cap->arg is TRUE for "E" and "W".
6528 */
6529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006530nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 int n;
6533 int word_end;
6534 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006535 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006537 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6539 word_end = TRUE;
6540 else
6541 word_end = FALSE;
6542 cap->oap->inclusive = word_end;
6543
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006544 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (!word_end && cap->oap->op_type == OP_CHANGE)
6546 {
6547 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006548 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006550 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006552 // Reproduce a funny Vi behaviour: "cw" on a blank only
6553 // changes one character, not all blanks until the start of
6554 // the next word. Only do this when the 'w' flag is included
6555 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6557 {
6558 cap->oap->inclusive = TRUE;
6559 cap->oap->motion_type = MCHAR;
6560 return;
6561 }
6562 }
6563 else
6564 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006565 // This is a little strange. To match what the real Vi does,
6566 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6567 // that we are not on a space or a TAB. This seems impolite
6568 // at first, but it's really more what we mean when we say
6569 // 'cw'.
6570 // Another strangeness: When standing on the end of a word
6571 // "ce" will change until the end of the next word, but "cw"
6572 // will change only one character! This is done by setting
6573 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 cap->oap->inclusive = TRUE;
6575 word_end = TRUE;
6576 flag = TRUE;
6577 }
6578 }
6579 }
6580
6581 cap->oap->motion_type = MCHAR;
6582 curwin->w_set_curswant = TRUE;
6583 if (word_end)
6584 n = end_word(cap->count1, cap->arg, flag, FALSE);
6585 else
6586 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006588 // Don't leave the cursor on the NUL past the end of line. Unless we
6589 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006590 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 if (n == FAIL && cap->oap->op_type == OP_NOP)
6594 clearopbeep(cap->oap);
6595 else
6596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598#ifdef FEAT_FOLDING
6599 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6600 foldOpenCursor();
6601#endif
6602 }
6603}
6604
6605/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006606 * Used after a movement command: If the cursor ends up on the NUL after the
6607 * end of the line, may move it back to the last character and make the motion
6608 * inclusive.
6609 */
6610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006612{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 // The cursor cannot remain on the NUL when:
6614 // - the column is > 0
6615 // - not in Visual mode or 'selection' is "o"
6616 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006617 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006618 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006619 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006620 {
6621 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006622 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006623 if (has_mbyte)
6624 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006625 oap->inclusive = TRUE;
6626 }
6627}
6628
6629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 * "0" and "^" commands.
6631 * cap->arg is the argument for beginline().
6632 */
6633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 cap->oap->motion_type = MCHAR;
6637 cap->oap->inclusive = FALSE;
6638 beginline(cap->arg);
6639#ifdef FEAT_FOLDING
6640 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6641 foldOpenCursor();
6642#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006643 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6644 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645}
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647/*
6648 * In exclusive Visual mode, may include the last character.
6649 */
6650 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006651adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006654 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (has_mbyte)
6657 inc_cursor();
6658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 ++curwin->w_cursor.col;
6660 cap->oap->inclusive = FALSE;
6661 }
6662}
6663
6664/*
6665 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6666 * Should check VIsual_mode before calling this.
6667 * Returns TRUE when backed up to the previous line.
6668 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006669 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006670unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 pos_T *pp;
6673
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006674 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006676 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 pp = &curwin->w_cursor;
6678 else
6679 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (pp->coladd > 0)
6681 --pp->coladd;
6682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (pp->col > 0)
6684 {
6685 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006686 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688 else if (pp->lnum > 1)
6689 {
6690 --pp->lnum;
6691 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6692 return TRUE;
6693 }
6694 }
6695 return FALSE;
6696}
6697
6698/*
6699 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6700 */
6701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006707 VIsual_select_reg = 0;
6708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 else if (VIsual_reselect)
6710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006711 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 cap->arg = TRUE;
6713 nv_g_cmd(cap);
6714 }
6715}
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718/*
6719 * "G", "gg", CTRL-END, CTRL-HOME.
6720 * cap->arg is TRUE for "G".
6721 */
6722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006723nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 linenr_T lnum;
6726
6727 if (cap->arg)
6728 lnum = curbuf->b_ml.ml_line_count;
6729 else
6730 lnum = 1L;
6731 cap->oap->motion_type = MLINE;
6732 setpcmark();
6733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006734 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (cap->count0 != 0)
6736 lnum = cap->count0;
6737 if (lnum < 1L)
6738 lnum = 1L;
6739 else if (lnum > curbuf->b_ml.ml_line_count)
6740 lnum = curbuf->b_ml.ml_line_count;
6741 curwin->w_cursor.lnum = lnum;
6742 beginline(BL_SOL | BL_FIX);
6743#ifdef FEAT_FOLDING
6744 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6745 foldOpenCursor();
6746#endif
6747}
6748
6749/*
6750 * CTRL-\ in Normal mode.
6751 */
6752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006753nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6756 {
6757 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006758 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006759 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (cmdwin_type != 0)
6762 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (VIsual_active)
6764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006765 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006766 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006768 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 if (cap->nchar == Ctrl_G && p_im)
6770 restart_edit = 'a';
6771 }
6772 else
6773 clearopbeep(cap->oap);
6774}
6775
6776/*
6777 * ESC in Normal mode: beep, but don't flush buffers.
6778 * Don't even beep if we are canceling a command.
6779 */
6780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006781nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 int no_reason;
6784
6785 no_reason = (cap->oap->op_type == OP_NOP
6786 && cap->opcount == 0
6787 && cap->count0 == 0
6788 && cap->oap->regname == 0
6789 && !p_im);
6790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006791 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006793 if (restart_edit == 0 && cmdwin_type == 0
6794 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006795 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006796 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6797
Bram Moolenaar5939c352022-09-24 12:50:45 +01006798 // The user may accidentally do "vim file | grep word" and then
6799 // CTRL-C doesn't show anything. With a changed buffer give the
6800 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006801 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006802 {
6803 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6804
6805 if (out_redir)
6806 mch_errmsg(ms);
6807 else
6808 msg(ms);
6809 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006810 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006811 {
6812 if (out_redir)
6813 {
6814 got_int = FALSE;
6815 do_cmdline_cmd((char_u *)"qa");
6816 }
6817 else
6818 msg(_("Type :qa and press <Enter> to exit Vim"));
6819 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006822 if (restart_edit != 0)
6823 redraw_mode = TRUE; // remove "-- (insert) --"
6824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006825 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6826 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (!p_im)
6828 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 if (cmdwin_type != 0)
6830 {
6831 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006832 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 return;
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006836 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006837 {
6838 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006839 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6840 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006841 cmdwin_result = K_IGNORE;
6842 return;
6843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (VIsual_active)
6846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 end_visual_mode(); // stop Visual
6848 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006850 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006852 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006853 {
6854#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006855 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006856 popup_hide_message_win();
6857 else
6858#endif
6859 vim_beep(BO_ESC);
6860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 clearop(cap->oap);
6862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006863 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6864 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006865 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 restart_edit = 'a';
6867}
6868
6869/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006870 * Move the cursor for the "A" command.
6871 */
6872 void
6873set_cursor_for_append_to_line(void)
6874{
6875 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006876 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006877 {
6878 int save_State = State;
6879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006880 // Pretend Insert mode here to allow the cursor on the
6881 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006882 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006883 coladvance((colnr_T)MAXCOL);
6884 State = save_State;
6885 }
6886 else
6887 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6888}
6889
6890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006892 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 */
6894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006895nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006897 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6899 cap->cmdchar = 'i';
6900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006903 {
6904#ifdef FEAT_TERMINAL
6905 if (term_in_normal_mode())
6906 {
6907 end_visual_mode();
6908 clearop(cap->oap);
6909 term_enter_job_mode();
6910 return;
6911 }
6912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006917 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6918 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006922#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006923 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006924 {
6925 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006926 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006927 return;
6928 }
6929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 else if (!curbuf->b_p_ma && !p_im)
6931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006932 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006933 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006935 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006937 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006939 else if (cap->cmdchar == K_PS && VIsual_active)
6940 {
6941 pos_T old_pos = curwin->w_cursor;
6942 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006943 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006945 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006946 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6947 {
6948 shift_delete_registers();
6949 cap->oap->regname = '1';
6950 }
6951 else
6952 cap->oap->regname = '-';
6953 cap->cmdchar = 'd';
6954 cap->nchar = NUL;
6955 nv_operator(cap);
6956 do_pending_operator(cap, 0, FALSE);
6957 cap->cmdchar = K_PS;
6958
zeertzjq7a732522022-03-14 20:46:41 +00006959 if (*ml_get_cursor() != NUL)
6960 {
6961 if (old_visual_mode == 'V')
6962 {
6963 // In linewise Visual mode insert before the beginning of the
6964 // next line.
6965 // When the last line in the buffer was deleted then create a
6966 // new line, otherwise there is not need to move cursor.
6967 // Detect this by checking if cursor moved above Visual area.
6968 if (curwin->w_cursor.lnum < old_pos.lnum
6969 && curwin->w_cursor.lnum < old_visual.lnum)
6970 {
6971 if (u_save_cursor() == OK)
6972 {
6973 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6974 FALSE);
6975 appended_lines(curwin->w_cursor.lnum++, 1L);
6976 }
6977 }
6978 }
6979 // When the last char in the line was deleted then append.
6980 // Detect this by checking if cursor moved before Visual area.
6981 else if (curwin->w_cursor.col < old_pos.col
6982 && curwin->w_cursor.col < old_visual.col)
6983 inc_cursor();
6984 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006986 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006987 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else if (!checkclearopq(cap->oap))
6990 {
6991 switch (cap->cmdchar)
6992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006993 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006994 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 break;
6996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006997 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006998 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6999 beginline(BL_WHITE);
7000 else
7001 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 break;
7003
Bram Moolenaara1891842017-02-04 21:34:31 +01007004 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // Bracketed paste works like "a"ppend, unless the cursor is in
7006 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007007 if (curwin->w_cursor.col == 0)
7008 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007009 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 case 'a': // "a"ppend is like "i"nsert on the next character.
7012 // increment coladd when in virtual space, increment the
7013 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (virtual_active()
7015 && (curwin->w_cursor.coladd > 0
7016 || *ml_get_cursor() == NUL
7017 || *ml_get_cursor() == TAB))
7018 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007019 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 inc_cursor();
7021 break;
7022 }
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7025 {
7026 int save_State = State;
7027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // Pretend Insert mode here to allow the cursor on the
7029 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007030 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 coladvance(getviscol());
7032 State = save_State;
7033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7036 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007037 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007039 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040}
7041
7042/*
7043 * Invoke edit() and take care of "restart_edit" and the return value.
7044 */
7045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007046invoke_edit(
7047 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007048 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007049 int cmd,
7050 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int restart_edit_save = 0;
7053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7055 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7056 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (repl || !stuff_empty())
7058 restart_edit_save = restart_edit;
7059 else
7060 restart_edit_save = 0;
7061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 restart_edit = 0;
7064
7065 if (edit(cmd, startln, cap->count1))
7066 cap->retval |= CA_COMMAND_BUSY;
7067
7068 if (restart_edit == 0)
7069 restart_edit = restart_edit_save;
7070}
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072/*
7073 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7074 */
7075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076nv_object(
7077 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 int flag;
7080 int include;
7081 char_u *mps_save;
7082
7083 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 mps_save = curbuf->b_p_mps;
7090 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7091
7092 switch (cap->nchar)
7093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 flag = current_word(cap->oap, cap->count1, include, FALSE);
7096 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 flag = current_word(cap->oap, cap->count1, include, TRUE);
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 case '(':
7102 case ')':
7103 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7104 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 case '{':
7107 case '}':
7108 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7109 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 case ']':
7112 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7113 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007114 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 case '>':
7116 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7117 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007118#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 case 't': // "at" = a tag block (xml and html)
7120 // Do not adjust oap->end in do_pending_operator()
7121 // otherwise there are different results for 'dit'
7122 // (note leading whitespace in last line):
7123 // 1) <b> 2) <b>
7124 // foobar foobar
7125 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007126 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007127 flag = current_tagblock(cap->oap, cap->count1, include);
7128 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007129#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007130 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 flag = current_par(cap->oap, cap->count1, include, 'p');
7132 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 flag = current_sent(cap->oap, cap->count1, include);
7135 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 case '"': // "a"" = a double quoted string
7137 case '\'': // "a'" = a single quoted string
7138 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007139 flag = current_quote(cap->oap, cap->count1, include,
7140 cap->nchar);
7141 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142#if 0 // TODO
7143 case 'S': // "aS" = a section
7144 case 'f': // "af" = a filename
7145 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146#endif
7147 default:
7148 flag = FAIL;
7149 break;
7150 }
7151
7152 curbuf->b_p_mps = mps_save;
7153 if (flag == FAIL)
7154 clearopbeep(cap->oap);
7155 adjust_cursor_col();
7156 curwin->w_set_curswant = TRUE;
7157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159/*
7160 * "q" command: Start/stop recording.
7161 * "q:", "q/", "q?": edit command-line in command-line window.
7162 */
7163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007164nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 if (cap->oap->op_type == OP_FORMAT)
7167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007168 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 cap->cmdchar = 'g';
7170 cap->nchar = 'q';
7171 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007172 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007174
7175 if (checkclearop(cap->oap))
7176 return;
7177
7178 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007180 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007182 emsg(_(e_cmdline_window_already_open));
7183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007185 stuffcharReadbuff(cap->nchar);
7186 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007188 else
7189 // (stop) recording into a named register, unless executing a
7190 // register
7191 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7192 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195/*
7196 * Handle the "@r" command.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 if (checkclearop(cap->oap))
7202 return;
7203#ifdef FEAT_EVAL
7204 if (cap->nchar == '=')
7205 {
7206 if (get_expr_register() == NUL)
7207 return;
7208 }
7209#endif
7210 while (cap->count1-- && !got_int)
7211 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007212 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 clearopbeep(cap->oap);
7215 break;
7216 }
7217 line_breakcheck();
7218 }
7219}
7220
7221/*
7222 * Handle the CTRL-U and CTRL-D commands.
7223 */
7224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007225nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
7227 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7228 || (cap->cmdchar == Ctrl_D
7229 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7230 clearopbeep(cap->oap);
7231 else if (!checkclearop(cap->oap))
7232 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7233}
7234
7235/*
7236 * Handle "J" or "gJ" command.
7237 */
7238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007239nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007241 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007243 nv_operator(cap);
7244 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007246
7247 if (checkclearop(cap->oap))
7248 return;
7249
7250 if (cap->count0 <= 1)
7251 cap->count0 = 2; // default for join is two lines!
7252 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7253 curbuf->b_ml.ml_line_count)
7254 {
7255 // can't join when on the last line
7256 if (cap->count0 <= 2)
7257 {
7258 clearopbeep(cap->oap);
7259 return;
7260 }
7261 cap->count0 = curbuf->b_ml.ml_line_count
7262 - curwin->w_cursor.lnum + 1;
7263 }
7264
7265 prep_redo(cap->oap->regname, cap->count0,
7266 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7267 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268}
7269
7270/*
7271 * "P", "gP", "p" and "gp" commands.
7272 */
7273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007274nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007276 nv_put_opt(cap, FALSE);
7277}
7278
7279/*
7280 * "P", "gP", "p" and "gp" commands.
7281 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7282 */
7283 static void
7284nv_put_opt(cmdarg_T *cap, int fix_indent)
7285{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 int regname = 0;
7287 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007288 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007289 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 int dir;
7291 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007292 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 if (cap->oap->op_type != OP_NOP)
7295 {
7296#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007297 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7299 {
7300 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007301 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303 else
7304#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007305 clearopbeep(cap->oap);
7306 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007308
Bram Moolenaarf2732452018-06-03 14:47:35 +02007309#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007310 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007311 {
7312 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007313 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007314 }
7315#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007316
7317 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007319 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7320 ? FORWARD : BACKWARD;
7321 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007323 else
7324 dir = (cap->cmdchar == 'P'
7325 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7326 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7327 prep_redo_cmd(cap);
7328 if (cap->cmdchar == 'g')
7329 flags |= PUT_CURSEND;
7330 else if (cap->cmdchar == 'z')
7331 flags |= PUT_BLOCK_INNER;
7332
7333 if (VIsual_active)
7334 {
7335 // Putting in Visual mode: The put text replaces the selected
7336 // text. First delete the selected text, then put the new text.
7337 // Need to save and restore the registers that the delete
7338 // overwrites if the old contents is being put.
7339 was_visual = TRUE;
7340 regname = cap->oap->regname;
7341 keep_registers = cap->cmdchar == 'P';
7342#ifdef FEAT_CLIPBOARD
7343 adjust_clip_reg(&regname);
7344#endif
7345 if (regname == 0 || regname == '"'
7346 || VIM_ISDIGIT(regname) || regname == '-'
7347#ifdef FEAT_CLIPBOARD
7348 || (clip_unnamed && (regname == '*' || regname == '+'))
7349#endif
7350
7351 )
7352 {
7353 // The delete is going to overwrite the register we want to
7354 // put, save it first.
7355 reg1 = get_register(regname, TRUE);
7356 }
7357
7358 // Now delete the selected text. Avoid messages here.
7359 cap->cmdchar = 'd';
7360 cap->nchar = NUL;
7361 cap->oap->regname = keep_registers ? '_' : NUL;
7362 ++msg_silent;
7363 nv_operator(cap);
7364 do_pending_operator(cap, 0, FALSE);
7365 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7366 --msg_silent;
7367
7368 // delete PUT_LINE_BACKWARD;
7369 cap->oap->regname = regname;
7370
7371 if (reg1 != NULL)
7372 {
7373 // Delete probably changed the register we want to put, save
7374 // it first. Then put back what was there before the delete.
7375 reg2 = get_register(regname, FALSE);
7376 put_register(regname, reg1);
7377 }
7378
7379 // When deleted a linewise Visual area, put the register as
7380 // lines to avoid it joined with the next line. When deletion was
7381 // characterwise, split a line when putting lines.
7382 if (VIsual_mode == 'V')
7383 flags |= PUT_LINE;
7384 else if (VIsual_mode == 'v')
7385 flags |= PUT_LINE_SPLIT;
7386 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7387 flags |= PUT_LINE_FORWARD;
7388 dir = BACKWARD;
7389 if ((VIsual_mode != 'V'
7390 && curwin->w_cursor.col < curbuf->b_op_start.col)
7391 || (VIsual_mode == 'V'
7392 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7393 // cursor is at the end of the line or end of file, put
7394 // forward.
7395 dir = FORWARD;
7396 // May have been reset in do_put().
7397 VIsual_active = TRUE;
7398 }
7399 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7400
7401 // If a register was saved, put it back now.
7402 if (reg2 != NULL)
7403 put_register(regname, reg2);
7404
7405 // What to reselect with "gv"? Selecting the just put text seems to
7406 // be the most useful, since the original text was removed.
7407 if (was_visual)
7408 {
7409 curbuf->b_visual.vi_start = curbuf->b_op_start;
7410 curbuf->b_visual.vi_end = curbuf->b_op_end;
7411 // need to adjust cursor position
7412 if (*p_sel == 'e')
7413 inc(&curbuf->b_visual.vi_end);
7414 }
7415
7416 // When all lines were selected and deleted do_put() leaves an empty
7417 // line that needs to be deleted now.
7418 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7419 {
7420 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7421 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7422
7423 // If the cursor was in that line, move it to the end of the last
7424 // line.
7425 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7426 {
7427 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7428 coladvance((colnr_T)MAXCOL);
7429 }
7430 }
7431 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434/*
7435 * "o" and "O" commands.
7436 */
7437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007438nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007441 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7443 {
7444 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007445 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
7447 else
7448#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007449 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007451#ifdef FEAT_JOB_CHANNEL
7452 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007453 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 n_opencmd(cap);
7457}
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459#ifdef FEAT_NETBEANS_INTG
7460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007461nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
7463 netbeans_keycommand(cap->nchar);
7464}
7465#endif
7466
7467#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007469nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007471 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472}
7473#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007474
Bram Moolenaar3918c952005-03-15 22:34:55 +00007475/*
7476 * Trigger CursorHold event.
7477 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7478 * input buffer. "did_cursorhold" is set to avoid retriggering.
7479 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007480 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007481nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007482{
7483 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7484 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007485 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007486}