blob: 6f947a6c3c71d9ebd91872a16e31dedacd7f3c3b [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
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When 'keymodel' contains "startsel" some keys start Select/Visual
919 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (!VIsual_active && km_startsel)
921 {
922 if (nv_cmds[idx].cmd_flags & NV_SS)
923 {
924 start_selection();
925 unshift_special(&ca);
926 idx = find_command(ca.cmdchar);
927 }
928 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
929 && (mod_mask & MOD_MASK_SHIFT))
930 {
931 start_selection();
932 mod_mask &= ~MOD_MASK_SHIFT;
933 }
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000936 // Execute the command!
937 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ca.arg = nv_cmds[idx].cmd_arg;
939 (nv_cmds[idx].cmd_func)(&ca);
940
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000941 // If we didn't start or finish an operator, reset oap->regname, unless we
942 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (!finish_op
944 && !oap->op_type
945 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
946 {
947 clearop(oap);
948#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200949 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951 }
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // Get the length of mapped chars again after typing a count, second
954 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000955 if (old_mapped_len > 0)
956 old_mapped_len = typebuf_maplen();
957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000958 // If an operation is pending, handle it. But not for K_IGNORE or
959 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200960 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100961 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000963 // Wait for a moment when a message is displayed that will be overwritten
964 // by the mode message.
965 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
966 normal_cmd_wait_for_msg();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969normal_end:
970
971 msg_nowait = FALSE;
972
Bram Moolenaarcc613032020-06-07 21:31:18 +0200973#ifdef FEAT_EVAL
974 if (finish_op)
975 reset_reg_var();
976#endif
977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100978 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000980 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#endif
982 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100983 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100985 // Redraw the cursor with another shape, if we were in Operator-pending
986 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +0000987 if (prev_finish_op || ca.cmdchar == 'r'
988 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100990 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991# ifdef FEAT_MOUSESHAPE
992 update_mouseshape(-1);
993# endif
994 }
995#endif
996
Bram Moolenaara983fe92008-07-31 20:04:27 +0000997 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100998 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 vim_free(ca.searchbuf);
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (has_mbyte)
1005 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (curwin->w_p_scb && toplevel)
1008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001009 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 do_check_scrollbind(TRUE);
1011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaar860cae12010-06-05 23:22:07 +02001013 if (curwin->w_p_crb && toplevel)
1014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001015 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001016 do_check_cursorbind();
1017 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001018
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001019#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001021 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001022 restart_edit = 0;
1023#endif
1024
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001025 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1026 // if still inside a mapping that started in Visual mode).
1027 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1030 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && !(ca.retval & CA_COMMAND_BUSY)
1032 && stuff_empty()
1033 && oap->regname == 0)
1034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (restart_VIsual_select == 1)
1036 {
1037 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001038 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 showmode();
1040 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001041 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001043 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 (void)edit(restart_edit, FALSE, 1L);
1045 }
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (restart_VIsual_select == 2)
1048 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 opcount = ca.opcount;
1052}
1053
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001054#ifdef FEAT_EVAL
1055/*
1056 * Set v:count and v:count1 according to "cap".
1057 * Set v:prevcount only when "set_prevcount" is TRUE.
1058 */
1059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001060set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001061{
1062 long count = cap->count0;
1063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001065 if (cap->opcount != 0)
1066 count = cap->opcount * (count == 0 ? 1 : count);
1067 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001069}
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001073 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * if not.
1075 */
1076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001077check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
1079 static int did_check = FALSE;
1080
1081 if (full_screen)
1082 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001083 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001084 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 did_check = TRUE;
1086 }
1087}
1088
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001089#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1090/*
1091 * Call yank_do_autocmd() for "regname".
1092 */
1093 static void
1094call_yank_do_autocmd(int regname)
1095{
1096 oparg_T oa;
1097 yankreg_T *reg;
1098
1099 clear_oparg(&oa);
1100 oa.regname = regname;
1101 oa.op_type = OP_YANK;
1102 oa.is_VIsual = TRUE;
1103 reg = get_register(regname, TRUE);
1104 yank_do_autocmd(&oa, reg);
1105 free_register(reg);
1106}
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001110 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001111 * This function or the next should ALWAYS be called to end Visual mode, except
1112 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
1114 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001115end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001116{
1117 end_visual_mode_keep_button();
1118 reset_held_button();
1119}
1120
1121 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001122end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001125 // If we are using the clipboard, then remember what was selected in case
1126 // we need to paste it somewhere while we still own the selection.
1127 // Only do this when the clipboard is already owned. Don't want to grab
1128 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (clip_star.available && clip_star.owned)
1130 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001131
1132# if defined(FEAT_EVAL)
1133 // Emit a TextYankPost for the automatic copy of the selection into the
1134 // star and/or plus register.
1135 if (has_textyankpost())
1136 {
1137 if (clip_isautosel_star())
1138 call_yank_do_autocmd('*');
1139 if (clip_isautosel_plus())
1140 call_yank_do_autocmd('+');
1141 }
1142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144
1145 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 setmouse();
1147 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001149 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001150 curbuf->b_visual.vi_mode = VIsual_mode;
1151 curbuf->b_visual.vi_start = VIsual;
1152 curbuf->b_visual.vi_end = curwin->w_cursor;
1153 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#ifdef FEAT_EVAL
1155 curbuf->b_visual_mode_eval = VIsual_mode;
1156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (!virtual_active())
1158 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001159 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001161 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001162 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163}
1164
1165/*
1166 * Reset VIsual_active and VIsual_reselect.
1167 */
1168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001169reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 if (VIsual_active)
1172 {
1173 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001174 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 VIsual_reselect = FALSE;
1177}
1178
1179/*
1180 * Reset VIsual_active and VIsual_reselect if it's set.
1181 */
1182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001183reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 if (VIsual_active)
1186 {
1187 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001188 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 VIsual_reselect = FALSE;
1190 }
1191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001193 void
1194restore_visual_mode(void)
1195{
1196 if (VIsual_mode_orig != NUL)
1197 {
1198 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1199 VIsual_mode_orig = NUL;
1200 }
1201}
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203/*
1204 * Check for a balloon-eval special item to include when searching for an
1205 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1206 * Returns TRUE if the character at "*ptr" should be included.
1207 * "dir" is FORWARD or BACKWARD, the direction of searching.
1208 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1209 * "bnp" points to a counter for square brackets.
1210 */
1211 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001212find_is_eval_item(
1213 char_u *ptr,
1214 int *colp,
1215 int *bnp,
1216 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001218 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1220 ++*bnp;
1221 if (*bnp > 0)
1222 {
1223 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1224 --*bnp;
1225 return TRUE;
1226 }
1227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001228 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (*ptr == '.')
1230 return TRUE;
1231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1234 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1235 {
1236 *colp += dir;
1237 return TRUE;
1238 }
1239 return FALSE;
1240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242/*
1243 * Find the identifier under or to the right of the cursor.
1244 * "find_type" can have one of three values:
1245 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001246 * FIND_STRING: find any non-white text
1247 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001248 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 *
1250 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001251 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001253 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * This doesn't match the real Vi but I like it a little better and it
1255 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * When FIND_IDENT isn't defined, we backup until a blank.
1258 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * Returns the length of the text, or zero if no text is found.
1260 * If text is found, a pointer to the text is put in "*text". This
1261 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 */
1263 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001264find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268}
1269
1270/*
1271 * Like find_ident_under_cursor(), but for any window and any position.
1272 * However: Uses 'iskeyword' from the current window!.
1273 */
1274 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001275find_ident_at_pos(
1276 win_T *wp,
1277 linenr_T lnum,
1278 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001279 char_u **text,
1280 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001281 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001284 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 int this_class = 0;
1287 int prev_class;
1288 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001289 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001291 // if i == 0: try to find an identifier
1292 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1294 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1295 {
1296 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001297 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 */
1299 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (has_mbyte)
1301 {
1302 while (ptr[col] != NUL)
1303 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001304 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 this_class = mb_get_class(ptr + col);
1308 if (this_class != 0 && (i == 1 || this_class != 1))
1309 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001310 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
1313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001315 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 )
1318 ++col;
1319
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001320 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001324 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (has_mbyte)
1327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001328 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1330 this_class = mb_get_class((char_u *)"a");
1331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001333 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1336 prev_class = mb_get_class(ptr + prevcol);
1337 if (this_class != prev_class
1338 && (i == 0
1339 || prev_class == 0
1340 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && (!(find_type & FIND_EVAL)
1342 || prevcol == 0
1343 || !find_is_eval_item(ptr + prevcol, &prevcol,
1344 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 )
1346 break;
1347 col = prevcol;
1348 }
1349
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001350 // If we don't want just any old text, or we've found an
1351 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (this_class > 2)
1353 this_class = 2;
1354 if (!(find_type & FIND_STRING) || this_class == 2)
1355 break;
1356 }
1357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 while (col > 0
1360 && ((i == 0
1361 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001362 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 && (!(find_type & FIND_IDENT)
1364 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 || ((find_type & FIND_EVAL)
1366 && col > 1
1367 && find_is_eval_item(ptr + col - 1, &col,
1368 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 ))
1370 --col;
1371
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001372 // If we don't want just any old text, or we've found an
1373 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1375 break;
1376 }
1377 }
1378
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001379 if (ptr[col] == NUL || (i == 0
1380 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001382 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001383 if ((find_type & FIND_NOERROR) == 0)
1384 {
1385 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001386 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001387 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001388 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return 0;
1391 }
1392 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001393 *text = ptr;
1394 if (textcol != NULL)
1395 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001398 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 bn = 0;
1401 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (has_mbyte)
1404 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001405 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 this_class = mb_get_class(ptr);
1407 while (ptr[col] != NUL
1408 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1409 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 || ((find_type & FIND_EVAL)
1411 && col <= (int)startcol
1412 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001414 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001418 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 || ((find_type & FIND_EVAL)
1420 && col <= (int)startcol
1421 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425 return col;
1426}
1427
1428/*
1429 * Prepare for redo of a normal command.
1430 */
1431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001432prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 prep_redo(cap->oap->regname, cap->count0,
1435 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1436}
1437
1438/*
1439 * Prepare for redo of any command.
1440 * Note that only the last argument can be a multi-byte char.
1441 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001442 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001443prep_redo(
1444 int regname,
1445 long num,
1446 int cmd1,
1447 int cmd2,
1448 int cmd3,
1449 int cmd4,
1450 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001452 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1453}
1454
1455/*
1456 * Prepare for redo of any command with extra count after "cmd2".
1457 */
1458 void
1459prep_redo_num2(
1460 int regname,
1461 long num1,
1462 int cmd1,
1463 int cmd2,
1464 long num2,
1465 int cmd3,
1466 int cmd4,
1467 int cmd5)
1468{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001470
1471#ifdef FEAT_EVAL
1472 // Put info about a mapping in the redo buffer, so that "." will use the
1473 // same script context.
1474 may_add_last_used_map_to_redobuff();
1475#endif
1476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001477 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 AppendCharToRedobuff('"');
1480 AppendCharToRedobuff(regname);
1481 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001482 if (num1 != 0)
1483 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (cmd1 != NUL)
1485 AppendCharToRedobuff(cmd1);
1486 if (cmd2 != NUL)
1487 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001488 if (num2 != 0)
1489 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (cmd3 != NUL)
1491 AppendCharToRedobuff(cmd3);
1492 if (cmd4 != NUL)
1493 AppendCharToRedobuff(cmd4);
1494 if (cmd5 != NUL)
1495 AppendCharToRedobuff(cmd5);
1496}
1497
1498/*
1499 * check for operator active and clear it
1500 *
1501 * return TRUE if operator was active
1502 */
1503 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001504checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 if (oap->op_type == OP_NOP)
1507 return FALSE;
1508 clearopbeep(oap);
1509 return TRUE;
1510}
1511
1512/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001513 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001515 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
1517 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001518checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001520 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 return FALSE;
1522 clearopbeep(oap);
1523 return TRUE;
1524}
1525
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001526 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001527clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 oap->op_type = OP_NOP;
1530 oap->regname = 0;
1531 oap->motion_force = NUL;
1532 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001533 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001537clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 clearop(oap);
1540 beep_flush();
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
1544 * Remove the shift modifier from a special key.
1545 */
1546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001547unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 switch (cap->cmdchar)
1550 {
1551 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1552 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1553 case K_S_UP: cap->cmdchar = K_UP; break;
1554 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1555 case K_S_HOME: cap->cmdchar = K_HOME; break;
1556 case K_S_END: cap->cmdchar = K_END; break;
1557 }
1558 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001561/*
1562 * If the mode is currently displayed clear the command line or update the
1563 * command displayed.
1564 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001566may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001567{
1568 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001569 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001570 else
1571 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001572}
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
1575 * Routines for displaying a partly typed command
1576 */
1577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579static int showcmd_is_clear = TRUE;
1580static int showcmd_visual = FALSE;
1581
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001582static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001585clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 if (!p_sc)
1588 return;
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (VIsual_active && !char_avail())
1591 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001592 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 long lines;
1594 colnr_T leftcol, rightcol;
1595 linenr_T top, bot;
1596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001597 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001598 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 top = VIsual.lnum;
1601 bot = curwin->w_cursor.lnum;
1602 }
1603 else
1604 {
1605 top = curwin->w_cursor.lnum;
1606 bot = VIsual.lnum;
1607 }
1608# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001609 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001610 (void)hasFolding(top, &top, NULL);
1611 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612# endif
1613 lines = bot - top + 1;
1614
1615 if (VIsual_mode == Ctrl_V)
1616 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001617# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001618 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001619 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001621 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001622 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001623 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001626# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001627 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001628 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1631 (long)(rightcol - leftcol + 1));
1632 }
1633 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1634 sprintf((char *)showcmd_buf, "%ld", lines);
1635 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001636 {
1637 char_u *s, *e;
1638 int l;
1639 int bytes = 0;
1640 int chars = 0;
1641
1642 if (cursor_bot)
1643 {
1644 s = ml_get_pos(&VIsual);
1645 e = ml_get_cursor();
1646 }
1647 else
1648 {
1649 s = ml_get_cursor();
1650 e = ml_get_pos(&VIsual);
1651 }
1652 while ((*p_sel != 'e') ? s <= e : s < e)
1653 {
1654 l = (*mb_ptr2len)(s);
1655 if (l == 0)
1656 {
1657 ++bytes;
1658 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001659 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001660 }
1661 bytes += l;
1662 ++chars;
1663 s += l;
1664 }
1665 if (bytes == chars)
1666 sprintf((char *)showcmd_buf, "%d", chars);
1667 else
1668 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1669 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001670 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 showcmd_visual = TRUE;
1672 }
1673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 showcmd_buf[0] = NUL;
1676 showcmd_visual = FALSE;
1677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (showcmd_is_clear)
1680 return;
1681 }
1682
1683 display_showcmd();
1684}
1685
1686/*
1687 * Add 'c' to string of shown command chars.
1688 * Return TRUE if output has been written (and setcursor() has been called).
1689 */
1690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001691add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 char_u *p;
1694 int old_len;
1695 int extra_len;
1696 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
1698 static int ignore[] =
1699 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001700#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1702 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001703#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001704 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001705 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1707 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001708 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001710 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 0
1712 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar09df3122006-01-23 22:23:09 +00001714 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return FALSE;
1716
1717 if (showcmd_visual)
1718 {
1719 showcmd_buf[0] = NUL;
1720 showcmd_visual = FALSE;
1721 }
1722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (IS_SPECIAL(c))
1725 for (i = 0; ignore[i] != 0; ++i)
1726 if (ignore[i] == c)
1727 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001730 if (*p == ' ')
1731 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 old_len = (int)STRLEN(showcmd_buf);
1733 extra_len = (int)STRLEN(p);
1734 overflow = old_len + extra_len - SHOWCMD_COLS;
1735 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001736 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1737 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 STRCAT(showcmd_buf, p);
1739
1740 if (char_avail())
1741 return FALSE;
1742
1743 display_showcmd();
1744
1745 return TRUE;
1746}
1747
1748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 if (!add_to_showcmd(c))
1752 setcursor();
1753}
1754
1755/*
1756 * Delete 'len' characters from the end of the shown command.
1757 */
1758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int old_len;
1762
1763 if (!p_sc)
1764 return;
1765
1766 old_len = (int)STRLEN(showcmd_buf);
1767 if (len > old_len)
1768 len = old_len;
1769 showcmd_buf[old_len - len] = NUL;
1770
1771 if (!char_avail())
1772 display_showcmd();
1773}
1774
1775/*
1776 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1777 * something and there is a partial mapping.
1778 */
1779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 if (p_sc)
1783 STRCPY(old_showcmd_buf, showcmd_buf);
1784}
1785
1786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001787pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 if (!p_sc)
1790 return;
1791
1792 STRCPY(showcmd_buf, old_showcmd_buf);
1793
1794 display_showcmd();
1795}
1796
1797 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001798display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
Luuk van Baalba936f62022-12-15 13:15:39 +00001800 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Luuk van Baalba936f62022-12-15 13:15:39 +00001802 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 cursor_off();
1804
Luuk van Baalba936f62022-12-15 13:15:39 +00001805 if (*p_sloc == 's')
1806 win_redr_status(curwin, FALSE);
1807 else if (*p_sloc == 't')
1808 draw_tabline();
1809 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001811 if (!showcmd_is_clear)
1812 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Luuk van Baalba936f62022-12-15 13:15:39 +00001814 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1815 // spaces
1816 screen_puts((char_u *)" " + len,
1817 (int)Rows - 1, sc_col + len, 0);
1818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
1824 * When "check" is FALSE, prepare for commands that scroll the window.
1825 * When "check" is TRUE, take care of scroll-binding after the window has
1826 * scrolled. Called from normal_cmd() and edit().
1827 */
1828 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001829do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 static win_T *old_curwin = NULL;
1832 static linenr_T old_topline = 0;
1833#ifdef FEAT_DIFF
1834 static int old_topfill = 0;
1835#endif
1836 static buf_T *old_buf = NULL;
1837 static colnr_T old_leftcol = 0;
1838
1839 if (check && curwin->w_p_scb)
1840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001841 // If a ":syncbind" command was just used, don't scroll, only reset
1842 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (did_syncbind)
1844 did_syncbind = FALSE;
1845 else if (curwin == old_curwin)
1846 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001847 // Synchronize other windows, as necessary according to
1848 // 'scrollbind'. Don't do this after an ":edit" command, except
1849 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if ((curwin->w_buffer == old_buf
1851#ifdef FEAT_DIFF
1852 || curwin->w_p_diff
1853#endif
1854 )
1855 && (curwin->w_topline != old_topline
1856#ifdef FEAT_DIFF
1857 || curwin->w_topfill != old_topfill
1858#endif
1859 || curwin->w_leftcol != old_leftcol))
1860 {
1861 check_scrollbind(curwin->w_topline - old_topline,
1862 (long)(curwin->w_leftcol - old_leftcol));
1863 }
1864 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001865 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001867 // When switching between windows, make sure that the relative
1868 // vertical offset is valid for the new window. The relative
1869 // offset is invalid whenever another 'scrollbind' window has
1870 // scrolled to a point that would force the current window to
1871 // scroll past the beginning or end of its buffer. When the
1872 // resync is performed, some of the other 'scrollbind' windows may
1873 // need to jump so that the current window's relative position is
1874 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1876 }
1877 curwin->w_scbind_pos = curwin->w_topline;
1878 }
1879
1880 old_curwin = curwin;
1881 old_topline = curwin->w_topline;
1882#ifdef FEAT_DIFF
1883 old_topfill = curwin->w_topfill;
1884#endif
1885 old_buf = curwin->w_buffer;
1886 old_leftcol = curwin->w_leftcol;
1887}
1888
1889/*
1890 * Synchronize any windows that have "scrollbind" set, based on the
1891 * number of rows by which the current window has changed
1892 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1893 */
1894 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001895check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 int want_ver;
1898 int want_hor;
1899 win_T *old_curwin = curwin;
1900 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 int old_VIsual_select = VIsual_select;
1902 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 colnr_T tgt_leftcol = curwin->w_leftcol;
1904 long topline;
1905 long y;
1906
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001907 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1909#ifdef FEAT_DIFF
1910 want_ver |= old_curwin->w_p_diff;
1911#endif
1912 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1913
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001914 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001916 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001919 // skip original window and windows with 'noscrollbind'
1920 if (curwin == old_curwin || !curwin->w_p_scb)
1921 continue;
1922
1923 // do the vertical scroll
1924 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001927 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
zeertzjq101d57b2022-07-31 18:34:32 +01001929 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
zeertzjq101d57b2022-07-31 18:34:32 +01001931 else
1932#endif
1933 {
1934 curwin->w_scbind_pos += topline_diff;
1935 topline = curwin->w_scbind_pos;
1936 if (topline > curbuf->b_ml.ml_line_count)
1937 topline = curbuf->b_ml.ml_line_count;
1938 if (topline < 1)
1939 topline = 1;
1940
1941 y = topline - curwin->w_topline;
1942 if (y > 0)
1943 scrollup(y, FALSE);
1944 else
1945 scrolldown(-y, FALSE);
1946 }
1947
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001948 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001949 cursor_correct();
1950 curwin->w_redr_status = TRUE;
1951 }
1952
1953 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001954 if (want_hor)
1955 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001958 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 VIsual_select = old_VIsual_select;
1960 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 curwin = old_curwin;
1962 curbuf = old_curbuf;
1963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965/*
1966 * Command character that's ignored.
1967 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001968 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001973 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974}
1975
1976/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001977 * Command character that doesn't do anything, but unlike nv_ignore() does
1978 * start edit(). Used for "startinsert" executed while starting up.
1979 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001981nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001982{
1983}
1984
1985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 * Command character doesn't exist.
1987 */
1988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001989nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 clearopbeep(cap->oap);
1992}
1993
1994/*
1995 * <Help> and <F1> commands.
1996 */
1997 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001998nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 if (!checkclearopq(cap->oap))
2001 ex_help(NULL);
2002}
2003
2004/*
2005 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2006 */
2007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002010#ifdef FEAT_JOB_CHANNEL
2011 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2012 clearopbeep(cap->oap);
2013 else
2014#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002015 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002016 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002017 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002018 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2019 op_addsub(cap->oap, cap->count1, cap->arg);
2020 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002021 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002022 else if (VIsual_active)
2023 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002024 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002025 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026}
2027
2028/*
2029 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2030 */
2031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002032nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002034 if (checkclearop(cap->oap))
2035 return;
2036
2037 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002038 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002039 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2040 if (cap->arg == BACKWARD)
2041 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002042 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002043 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002045 else
2046 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047}
2048
2049/*
2050 * Implementation of "gd" and "gD" command.
2051 */
2052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053nv_gd(
2054 oparg_T *oap,
2055 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002056 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 char_u *ptr;
2060
Bram Moolenaard9d30582005-05-18 22:10:28 +00002061 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002062 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002063 == FAIL)
2064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002066 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002067 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002068
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002069#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002070 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2071 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002072#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002073 // clear any search statistics
2074 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2075 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002076}
2077
2078/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002079 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2080 * otherwise.
2081 */
2082 static int
2083is_ident(char_u *line, int offset)
2084{
2085 int i;
2086 int incomment = FALSE;
2087 int instring = 0;
2088 int prev = 0;
2089
2090 for (i = 0; i < offset && line[i] != NUL; i++)
2091 {
2092 if (instring != 0)
2093 {
2094 if (prev != '\\' && line[i] == instring)
2095 instring = 0;
2096 }
2097 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2098 {
2099 instring = line[i];
2100 }
2101 else
2102 {
2103 if (incomment)
2104 {
2105 if (prev == '*' && line[i] == '/')
2106 incomment = FALSE;
2107 }
2108 else if (prev == '/' && line[i] == '*')
2109 {
2110 incomment = TRUE;
2111 }
2112 else if (prev == '/' && line[i] == '/')
2113 {
2114 return FALSE;
2115 }
2116 }
2117
2118 prev = line[i];
2119 }
2120
2121 return incomment == FALSE && instring == 0;
2122}
2123
2124/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002125 * Search for variable declaration of "ptr[len]".
2126 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2127 * current file ("gD").
2128 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002129 * Return FAIL when not found.
2130 */
2131 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002132find_decl(
2133 char_u *ptr,
2134 int len,
2135 int locally,
2136 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002137 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002138{
2139 char_u *pat;
2140 pos_T old_pos;
2141 pos_T par_pos;
2142 pos_T found_pos;
2143 int t;
2144 int save_p_ws;
2145 int save_p_scs;
2146 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002147 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002148 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002149 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002150
2151 if ((pat = alloc(len + 7)) == NULL)
2152 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002154 // Put "\V" before the pattern to avoid that the special meaning of "."
2155 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002156 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2157 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 old_pos = curwin->w_cursor;
2159 save_p_ws = p_ws;
2160 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002161 p_ws = FALSE; // don't wrap around end of file now
2162 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002164 // With "gD" go to line 1.
2165 // With "gd" Search back for the start of the current function, then go
2166 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002167 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002169 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002171 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173 else
2174 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002175 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2177 --curwin->w_cursor.lnum;
2178 }
2179 curwin->w_cursor.col = 0;
2180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002181 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002182 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002183 for (;;)
2184 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002185 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002186 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002188 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002189
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002190 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002191 {
2192 pos_T *pos;
2193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // Check that the block the match is in doesn't end before the
2195 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002196 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2197 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2198 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // There can't be a useful match before the end of this block.
2201 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002202 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002203 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002204 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002205 }
2206
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002207 if (t == FAIL)
2208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 if (found_pos.lnum != 0)
2211 {
2212 curwin->w_cursor = found_pos;
2213 t = OK;
2214 }
2215 break;
2216 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002217 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 ++curwin->w_cursor.lnum;
2221 curwin->w_cursor.col = 0;
2222 continue;
2223 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002224 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002226 // If the current position is not a valid identifier and a previous
2227 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002228 if (!valid && found_pos.lnum != 0)
2229 {
2230 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002231 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002232 }
2233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002234 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002235 if (valid && !locally)
2236 break;
2237 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002238 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002240 if (found_pos.lnum != 0)
2241 curwin->w_cursor = found_pos;
2242 break;
2243 }
2244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // For finding a local variable and the match is before the "{" or
2246 // inside a comment, continue searching. For K&R style function
2247 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002248 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002249 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002250 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002251 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002252 // Remove SEARCH_START from flags to avoid getting stuck at one
2253 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002254 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002256
2257 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002259 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 curwin->w_cursor = old_pos;
2261 }
2262 else
2263 {
2264 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002265 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 reset_search_dir();
2267 }
2268
2269 vim_free(pat);
2270 p_ws = save_p_ws;
2271 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272
2273 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274}
2275
2276/*
2277 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2278 * lines rather than lines in the file.
2279 * 'dist' must be positive.
2280 *
2281 * Return OK if able to move cursor, FAIL otherwise.
2282 */
2283 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002286 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 int retval = OK;
2288 int atend = FALSE;
2289 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002290 int col_off1; // margin offset for first screen line
2291 int col_off2; // margin offset for wrapped screen line
2292 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002293 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002296 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 col_off1 = curwin_col_off();
2299 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002300 width1 = curwin->w_width - col_off1;
2301 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002302 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002306 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002307 // Instead of sticking at the last character of the buffer line we
2308 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (curwin->w_curswant == MAXCOL)
2310 {
2311 atend = TRUE;
2312 validate_virtcol();
2313 if (width1 <= 0)
2314 curwin->w_curswant = 0;
2315 else
2316 {
2317 curwin->w_curswant = width1 - 1;
2318 if (curwin->w_virtcol > curwin->w_curswant)
2319 curwin->w_curswant += ((curwin->w_virtcol
2320 - curwin->w_curswant - 1) / width2 + 1) * width2;
2321 }
2322 }
2323 else
2324 {
2325 if (linelen > width1)
2326 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2327 else
2328 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002329 if (curwin->w_curswant >= (colnr_T)n)
2330 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332
2333 while (dist--)
2334 {
2335 if (dir == BACKWARD)
2336 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002337 if ((long)curwin->w_curswant >= width1
2338#ifdef FEAT_FOLDING
2339 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2340#endif
2341 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002342 // Move back within the line. This can give a negative value
2343 // for w_curswant if width1 < width2 (with cpoptions+=n),
2344 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 curwin->w_curswant -= width2;
2346 else
2347 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002348 // to previous line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002349 if (!cursor_up_inner(curwin, 1))
Bram Moolenaare71996b2021-01-21 17:03:07 +01002350 {
2351 retval = FAIL;
2352 break;
2353 }
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002354 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (linelen > width1)
2356 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2357 + 1) * width2;
2358 }
2359 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002360 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362 if (linelen > width1)
2363 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2364 else
2365 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002366 if (curwin->w_curswant + width2 < (colnr_T)n
2367#ifdef FEAT_FOLDING
2368 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2369#endif
2370 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 curwin->w_curswant += width2;
2373 else
2374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // to next line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002376 if (!cursor_down_inner(curwin, 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 retval = FAIL;
2379 break;
2380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002382 // Check if the cursor has moved below the number display
2383 // when width1 < width2 (with cpoptions+=n). Subtract width2
2384 // to get a negative value for w_curswant, which will get
2385 // clipped to column 0.
2386 if (curwin->w_curswant >= width1)
2387 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002388 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 }
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002394 if (virtual_active() && atend)
2395 coladvance(MAXCOL);
2396 else
2397 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2400 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002401 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002402 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002403
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002404 // Check for landing on a character that got split at the end of the
2405 // last line. We want to advance a screenline, not end up in the same
2406 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002408 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002409#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002410 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2411 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002412#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002413
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002414 c = (*mb_ptr2char)(ml_get_cursor());
2415 if (dir == FORWARD && virtcol < curwin->w_curswant
2416 && (curwin->w_curswant <= (colnr_T)width1)
2417 && !vim_isprintc(c) && c > 255)
2418 oneright();
2419
Bram Moolenaar773b1582014-08-29 14:20:51 +02002420 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 && (curwin->w_curswant < (colnr_T)width1
2422 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2423 : ((curwin->w_curswant - width1) % width2
2424 > (colnr_T)width2 / 2)))
2425 --curwin->w_cursor.col;
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002429 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002430 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432 return retval;
2433}
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435/*
2436 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2437 * cap->arg must be TRUE for CTRL-E.
2438 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002440nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 if (!checkclearop(cap->oap))
2443 scroll_redraw(cap->arg, cap->count1);
2444}
2445
2446/*
2447 * Scroll "count" lines up or down, and redraw.
2448 */
2449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002450scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002453 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#ifdef FEAT_DIFF
2455 int prev_topfill = curwin->w_topfill;
2456#endif
2457 linenr_T prev_lnum = curwin->w_cursor.lnum;
2458
2459 if (up)
2460 scrollup(count, TRUE);
2461 else
2462 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002463 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002465 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2466 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 cursor_correct();
2468 check_cursor_moved(curwin);
2469 curwin->w_valid |= VALID_TOPLINE;
2470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002471 // If moved back to where we were, at least move the cursor, otherwise
2472 // we get stuck at one position. Don't move the cursor up if the
2473 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002475 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476#ifdef FEAT_DIFF
2477 && curwin->w_topfill == prev_topfill
2478#endif
2479 )
2480 {
2481 if (up)
2482 {
2483 if (curwin->w_cursor.lnum > prev_lnum
2484 || cursor_down(1L, FALSE) == FAIL)
2485 break;
2486 }
2487 else
2488 {
2489 if (curwin->w_cursor.lnum < prev_lnum
2490 || prev_topline == 1L
2491 || cursor_up(1L, FALSE) == FAIL)
2492 break;
2493 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002494 // Mark w_topline as valid, otherwise the screen jumps back at the
2495 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 check_cursor_moved(curwin);
2497 curwin->w_valid |= VALID_TOPLINE;
2498 }
2499 }
2500 if (curwin->w_cursor.lnum != prev_lnum)
2501 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002502 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503}
2504
2505/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002506 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2507 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2508 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002509 */
2510 static int
2511nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2512{
2513 int nchar = *nchar_arg;
2514 long n;
2515
2516 // "z123{nchar}": edit the count before obtaining {nchar}
2517 if (checkclearop(cap->oap))
2518 return FALSE;
2519 n = nchar - '0';
2520
2521 for (;;)
2522 {
2523#ifdef USE_ON_FLY_SCROLL
2524 dont_scroll = TRUE; // disallow scrolling here
2525#endif
2526 ++no_mapping;
2527 ++allow_keys; // no mapping for nchar, but allow key codes
2528 nchar = plain_vgetc();
2529 LANGMAP_ADJUST(nchar, TRUE);
2530 --no_mapping;
2531 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002532 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002533
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002534 if (nchar == K_DEL || nchar == K_KDEL)
2535 n /= 10;
2536 else if (VIM_ISDIGIT(nchar))
2537 n = n * 10 + (nchar - '0');
2538 else if (nchar == CAR)
2539 {
2540#ifdef FEAT_GUI
2541 need_mouse_correct = TRUE;
2542#endif
2543 win_setheight((int)n);
2544 break;
2545 }
2546 else if (nchar == 'l'
2547 || nchar == 'h'
2548 || nchar == K_LEFT
2549 || nchar == K_RIGHT)
2550 {
2551 cap->count1 = n ? n * cap->count1 : cap->count1;
2552 *nchar_arg = nchar;
2553 return TRUE;
2554 }
2555 else
2556 {
2557 clearopbeep(cap->oap);
2558 break;
2559 }
2560 }
2561 cap->oap->op_type = OP_NOP;
2562 return FALSE;
2563}
2564
2565#ifdef FEAT_SPELL
2566/*
2567 * "zug" and "zuw": undo "zg" and "zw"
2568 * "zg": add good word to word list
2569 * "zw": add wrong word to word list
2570 * "zG": add good word to temp word list
2571 * "zW": add wrong word to temp word list
2572 */
2573 static int
2574nv_zg_zw(cmdarg_T *cap, int nchar)
2575{
2576 char_u *ptr = NULL;
2577 int len;
2578 int undo = FALSE;
2579
2580 if (nchar == 'u')
2581 {
2582 ++no_mapping;
2583 ++allow_keys; // no mapping for nchar, but allow key codes
2584 nchar = plain_vgetc();
2585 LANGMAP_ADJUST(nchar, TRUE);
2586 --no_mapping;
2587 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002588 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002589
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002590 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2591 {
2592 clearopbeep(cap->oap);
2593 return OK;
2594 }
2595 undo = TRUE;
2596 }
2597
2598 if (checkclearop(cap->oap))
2599 return OK;
2600 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2601 return FAIL;
2602 if (ptr == NULL)
2603 {
2604 pos_T pos = curwin->w_cursor;
2605
2606 // Find bad word under the cursor. When 'spell' is
2607 // off this fails and find_ident_under_cursor() is
2608 // used below.
2609 emsg_off++;
2610 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2611 emsg_off--;
2612 if (len != 0 && curwin->w_cursor.col <= pos.col)
2613 ptr = ml_get_pos(&curwin->w_cursor);
2614 curwin->w_cursor = pos;
2615 }
2616
2617 if (ptr == NULL
2618 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2619 return FAIL;
2620 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2621 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2622 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2623
2624 return OK;
2625}
2626#endif
2627
2628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 * Commands that start with "z".
2630 */
2631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002632nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 long n;
2635 colnr_T col;
2636 int nchar = cap->nchar;
2637#ifdef FEAT_FOLDING
2638 long old_fdl = curwin->w_p_fdl;
2639 int old_fen = curwin->w_p_fen;
2640#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002641 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002643 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (
2647#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2649 // and "zC" only in Visual mode. "zj" and "zk" are motion
2650 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 cap->nchar != 'f' && cap->nchar != 'F'
2652 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2653 && cap->nchar != 'j' && cap->nchar != 'k'
2654 &&
2655#endif
2656 checkclearop(cap->oap))
2657 return;
2658
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002659 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2660 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2662 && cap->count0
2663 && cap->count0 != curwin->w_cursor.lnum)
2664 {
2665 setpcmark();
2666 if (cap->count0 > curbuf->b_ml.ml_line_count)
2667 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2668 else
2669 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002670 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672
2673 switch (nchar)
2674 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002675 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 case '+':
2677 if (cap->count0 == 0)
2678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002679 // No count given: put cursor at the line below screen
2680 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2682 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2683 else
2684 curwin->w_cursor.lnum = curwin->w_botline;
2685 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002686 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 case NL:
2688 case CAR:
2689 case K_KENTER:
2690 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002691 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002694 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002695 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 break;
2697
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002700 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002702 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002703 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002704 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 break;
2706
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002707 // "z^", "z-" and "zb": put cursor at bottom of screen
2708 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2709 // when <count> is at bottom of window, and puts that one at
2710 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (cap->count0 != 0)
2712 {
2713 scroll_cursor_bot(0, TRUE);
2714 curwin->w_cursor.lnum = curwin->w_topline;
2715 }
2716 else if (curwin->w_topline == 1)
2717 curwin->w_cursor.lnum = 1;
2718 else
2719 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 case '-':
2722 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002726 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002727 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 break;
2729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002732 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 case 'h':
2737 case K_LEFT:
2738 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002739 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2740 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
2742
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002743 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002744 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002747 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 case 'l':
2749 case K_RIGHT:
2750 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002751 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 break;
2753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 case 's': if (!curwin->w_p_wrap)
2756 {
2757#ifdef FEAT_FOLDING
2758 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 else
2761#endif
2762 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002763 if ((long)col > siso)
2764 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else
2766 col = 0;
2767 if (curwin->w_leftcol != col)
2768 {
2769 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002770 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 }
2773 break;
2774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 case 'e': if (!curwin->w_p_wrap)
2777 {
2778#ifdef FEAT_FOLDING
2779 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else
2782#endif
2783 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002784 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002785 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 col = 0;
2787 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002788 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if (curwin->w_leftcol != col)
2790 {
2791 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002792 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 }
2795 break;
2796
Christian Brabandt2fa93842021-05-30 22:17:25 +02002797 // "zp", "zP" in block mode put without addind trailing spaces
2798 case 'P':
2799 case 'p': nv_put(cap);
2800 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002801 // "zy" Yank without trailing spaces
2802 case 'y': nv_operator(cap);
2803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 // "zF": create fold command
2806 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 case 'F':
2808 case 'f': if (foldManualAllowed(TRUE))
2809 {
2810 cap->nchar = 'f';
2811 nv_operator(cap);
2812 curwin->w_p_fen = TRUE;
2813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2816 {
2817 nv_operator(cap);
2818 finish_op = TRUE;
2819 }
2820 }
2821 else
2822 clearopbeep(cap->oap);
2823 break;
2824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // "zd": delete fold at cursor
2826 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 case 'd':
2828 case 'D': if (foldManualAllowed(FALSE))
2829 {
2830 if (VIsual_active)
2831 nv_operator(cap);
2832 else
2833 deleteFold(curwin->w_cursor.lnum,
2834 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2835 }
2836 break;
2837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 case 'E': if (foldmethodIsManual(curwin))
2840 {
2841 clearFolding(curwin);
2842 changed_window_setting();
2843 }
2844 else if (foldmethodIsMarker(curwin))
2845 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2846 TRUE, FALSE);
2847 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002848 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case 'n': curwin->w_p_fen = FALSE;
2853 break;
2854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case 'N': curwin->w_p_fen = TRUE;
2857 break;
2858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2861 break;
2862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2865 openFold(curwin->w_cursor.lnum, cap->count1);
2866 else
2867 {
2868 closeFold(curwin->w_cursor.lnum, cap->count1);
2869 curwin->w_p_fen = TRUE;
2870 }
2871 break;
2872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2875 openFoldRecurse(curwin->w_cursor.lnum);
2876 else
2877 {
2878 closeFoldRecurse(curwin->w_cursor.lnum);
2879 curwin->w_p_fen = TRUE;
2880 }
2881 break;
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case 'o': if (VIsual_active)
2885 nv_operator(cap);
2886 else
2887 openFold(curwin->w_cursor.lnum, cap->count1);
2888 break;
2889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 case 'O': if (VIsual_active)
2892 nv_operator(cap);
2893 else
2894 openFoldRecurse(curwin->w_cursor.lnum);
2895 break;
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case 'c': if (VIsual_active)
2899 nv_operator(cap);
2900 else
2901 closeFold(curwin->w_cursor.lnum, cap->count1);
2902 curwin->w_p_fen = TRUE;
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'C': if (VIsual_active)
2907 nv_operator(cap);
2908 else
2909 closeFoldRecurse(curwin->w_cursor.lnum);
2910 curwin->w_p_fen = TRUE;
2911 break;
2912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002913 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 case 'v': foldOpenCursor();
2915 break;
2916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 curwin->w_foldinvalid = TRUE; // recompute folds
2920 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 foldOpenCursor();
2922 break;
2923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 curwin->w_foldinvalid = TRUE; // recompute folds
2927 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 break;
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002932 {
2933 curwin->w_p_fdl -= cap->count1;
2934 if (curwin->w_p_fdl < 0)
2935 curwin->w_p_fdl = 0;
2936 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 curwin->w_p_fen = TRUE;
2939 break;
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 curwin->w_p_fen = TRUE;
2945 break;
2946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002948 case 'r': curwin->w_p_fdl += cap->count1;
2949 {
2950 int d = getDeepestNesting();
2951
2952 if (curwin->w_p_fdl >= d)
2953 curwin->w_p_fdl = d;
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 break;
2956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 break;
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 case 'j': // "zj" move to next fold downwards
2963 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2965 cap->count1) == FAIL)
2966 clearopbeep(cap->oap);
2967 break;
2968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002971#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 case 'g': // "zg": add good word to word list
2974 case 'w': // "zw": add wrong word to word list
2975 case 'G': // "zG": add good word to temp word list
2976 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002977 if (nv_zg_zw(cap, nchar) == FAIL)
2978 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002979 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002981 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002982 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002983 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002984 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002985#endif
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 default: clearopbeep(cap->oap);
2988 }
2989
2990#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (old_fen != curwin->w_p_fen)
2993 {
2994# ifdef FEAT_DIFF
2995 win_T *wp;
2996
2997 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2998 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 FOR_ALL_WINDOWS(wp)
3001 {
3002 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3003 {
3004 wp->w_p_fen = curwin->w_p_fen;
3005 changed_window_setting_win(wp);
3006 }
3007 }
3008 }
3009# endif
3010 changed_window_setting();
3011 }
3012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 if (old_fdl != curwin->w_p_fdl)
3015 newFoldLevel();
3016#endif
3017}
3018
3019#ifdef FEAT_GUI
3020/*
3021 * Vertical scrollbar movement.
3022 */
3023 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003024nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 if (cap->oap->op_type != OP_NOP)
3027 clearopbeep(cap->oap);
3028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003029 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 gui_do_scroll();
3031}
3032
3033/*
3034 * Horizontal scrollbar movement.
3035 */
3036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003037nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 if (cap->oap->op_type != OP_NOP)
3040 clearopbeep(cap->oap);
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003043 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044}
3045#endif
3046
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003047#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003048/*
3049 * Click in GUI tab.
3050 */
3051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003053{
3054 if (cap->oap->op_type != OP_NOP)
3055 clearopbeep(cap->oap);
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003058 goto_tabpage(current_tab);
3059}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003060
3061/*
3062 * Selected item in tab line menu.
3063 */
3064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003065nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003066{
3067 if (cap->oap->op_type != OP_NOP)
3068 clearopbeep(cap->oap);
3069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003071 handle_tabmenu();
3072}
3073
3074/*
3075 * Handle selecting an item of the GUI tab line menu.
3076 * Used in Normal and Insert mode.
3077 */
3078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003079handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003080{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003081 switch (current_tabmenu)
3082 {
3083 case TABLINE_MENU_CLOSE:
3084 if (current_tab == 0)
3085 do_cmdline_cmd((char_u *)"tabclose");
3086 else
3087 {
3088 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3089 current_tab);
3090 do_cmdline_cmd(IObuff);
3091 }
3092 break;
3093
3094 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003095 if (current_tab == 0)
3096 do_cmdline_cmd((char_u *)"$tabnew");
3097 else
3098 {
3099 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3100 current_tab - 1);
3101 do_cmdline_cmd(IObuff);
3102 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003103 break;
3104
3105 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003106 if (current_tab == 0)
3107 do_cmdline_cmd((char_u *)"browse $tabnew");
3108 else
3109 {
3110 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3111 current_tab - 1);
3112 do_cmdline_cmd(IObuff);
3113 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003114 break;
3115 }
3116}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003117#endif
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119/*
3120 * "Q" command.
3121 */
3122 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003123nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003125 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003127 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003128 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 do_exmode(FALSE);
3130}
3131
3132/*
3133 * Handle a ":" command.
3134 */
3135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003136nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003138 int old_p_im;
3139 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003140 int is_cmdkey = cap->cmdchar == K_COMMAND
3141 || cap->cmdchar == K_SCRIPT_COMMAND;
3142 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
Bram Moolenaar957cf672020-11-12 14:21:06 +01003144 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003146 nv_operator(cap);
3147 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003149
3150 if (cap->oap->op_type != OP_NOP)
3151 {
3152 // Using ":" as a movement is characterwise exclusive.
3153 cap->oap->motion_type = MCHAR;
3154 cap->oap->inclusive = FALSE;
3155 }
3156 else if (cap->count0 && !is_cmdkey)
3157 {
3158 // translate "count:" into ":.,.+(count - 1)"
3159 stuffcharReadbuff('.');
3160 if (cap->count0 > 1)
3161 {
3162 stuffReadbuff((char_u *)",.+");
3163 stuffnumReadbuff((long)cap->count0 - 1L);
3164 }
3165 }
3166
3167 // When typing, don't type below an old message
3168 if (KeyTyped)
3169 compute_cmdrow();
3170
3171 old_p_im = p_im;
3172
3173 // get a command line and execute it
3174 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3175 if (is_cmdkey)
3176 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3177 else
3178 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3179
3180 // If 'insertmode' changed, enter or exit Insert mode
3181 if (p_im != old_p_im)
3182 {
3183 if (p_im)
3184 restart_edit = 'i';
3185 else
3186 restart_edit = 0;
3187 }
3188
3189 if (cmd_result == FAIL)
3190 // The Ex command failed, do not execute the operator.
3191 clearop(cap->oap);
3192 else if (cap->oap->op_type != OP_NOP
3193 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3194 || cap->oap->start.col >
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003195 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003196 || did_emsg
3197 ))
3198 // The start of the operator has become invalid by the Ex command.
3199 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200}
3201
3202/*
3203 * Handle CTRL-G command.
3204 */
3205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003206nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003208 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
3210 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003211 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 showmode();
3213 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003214 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 fileinfo((int)cap->count0, FALSE, TRUE);
3217}
3218
3219/*
3220 * Handle CTRL-H <Backspace> command.
3221 */
3222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003223nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (VIsual_active && VIsual_select)
3226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 v_visop(cap);
3229 }
3230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 nv_left(cap);
3232}
3233
3234/*
3235 * CTRL-L: clear screen and redraw.
3236 */
3237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003238nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003240 if (checkclearop(cap->oap))
3241 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003242
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003243#ifdef FEAT_SYN_HL
3244 // Clear all syntax states to force resyncing.
3245 syn_stack_free_all(curwin->w_s);
3246# ifdef FEAT_RELTIME
3247 {
3248 win_T *wp;
3249
3250 FOR_ALL_WINDOWS(wp)
3251 wp->w_s->b_syn_slow = FALSE;
3252 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003255 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003256#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3257# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003258 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003259# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003260 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
3264/*
3265 * CTRL-O: In Select mode: switch to Visual mode for one command.
3266 * Otherwise: Go to older pcmark.
3267 */
3268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (VIsual_active && VIsual_select)
3272 {
3273 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003274 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003276 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 cap->count1 = -cap->count1;
3281 nv_pcmark(cap);
3282 }
3283}
3284
3285/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003286 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3287 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
3289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003290nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292 if (!checkclearopq(cap->oap))
3293 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3294 GETF_SETMARK|GETF_ALT, FALSE);
3295}
3296
3297/*
3298 * "Z" commands.
3299 */
3300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003303 if (checkclearopq(cap->oap))
3304 return;
3305
3306 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003308 // "ZZ": equivalent to ":x".
3309 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 break;
3311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003312 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003313 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 break;
3315
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003316 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318}
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320/*
3321 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3322 */
3323 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003324do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 oparg_T oa;
3327 cmdarg_T ca;
3328
3329 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003330 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 ca.oap = &oa;
3332 ca.cmdchar = c1;
3333 ca.nchar = c2;
3334 nv_ident(&ca);
3335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
3337/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003338 * 'K' normal-mode command. Get the command to lookup the keyword under the
3339 * cursor.
3340 */
3341 static int
3342nv_K_getcmd(
3343 cmdarg_T *cap,
3344 char_u *kp,
3345 int kp_help,
3346 int kp_ex,
3347 char_u **ptr_arg,
3348 int n,
3349 char_u *buf,
3350 unsigned buflen)
3351{
3352 char_u *ptr = *ptr_arg;
3353 int isman;
3354 int isman_s;
3355
3356 if (kp_help)
3357 {
3358 // in the help buffer
3359 STRCPY(buf, "he! ");
3360 return n;
3361 }
3362
3363 if (kp_ex)
3364 {
3365 // 'keywordprog' is an ex command
3366 if (cap->count0 != 0)
3367 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3368 else
3369 STRCPY(buf, kp);
3370 STRCAT(buf, " ");
3371 return n;
3372 }
3373
3374 // An external command will probably use an argument starting
3375 // with "-" as an option. To avoid trouble we skip the "-".
3376 while (*ptr == '-' && n > 0)
3377 {
3378 ++ptr;
3379 --n;
3380 }
3381 if (n == 0)
3382 {
3383 // found dashes only
3384 emsg(_(e_no_identifier_under_cursor));
3385 vim_free(buf);
3386 *ptr_arg = ptr;
3387 return 0;
3388 }
3389
3390 // When a count is given, turn it into a range. Is this
3391 // really what we want?
3392 isman = (STRCMP(kp, "man") == 0);
3393 isman_s = (STRCMP(kp, "man -s") == 0);
3394 if (cap->count0 != 0 && !(isman || isman_s))
3395 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3396
3397 STRCAT(buf, "! ");
3398 if (cap->count0 == 0 && isman_s)
3399 STRCAT(buf, "man");
3400 else
3401 STRCAT(buf, kp);
3402 STRCAT(buf, " ");
3403 if (cap->count0 != 0 && (isman || isman_s))
3404 {
3405 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3406 STRCAT(buf, " ");
3407 }
3408
3409 *ptr_arg = ptr;
3410 return n;
3411}
3412
3413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Handle the commands that use the word under the cursor.
3415 * [g] CTRL-] :ta to current identifier
3416 * [g] 'K' run program for current identifier
3417 * [g] '*' / to current identifier or string
3418 * [g] '#' ? to current identifier or string
3419 * g ']' :tselect for current identifier
3420 */
3421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003422nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 char_u *ptr = NULL;
3425 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003426 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003427 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003429 char_u *kp; // value of 'keywordprg'
3430 int kp_help; // 'keywordprg' is ":he"
3431 int kp_ex; // 'keywordprg' starts with ":"
3432 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003435 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003438 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 cmdchar = cap->nchar;
3441 g_cmd = TRUE;
3442 }
3443 else
3444 {
3445 cmdchar = cap->cmdchar;
3446 g_cmd = FALSE;
3447 }
3448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003449 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 cmdchar = '#';
3451
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003452 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3456 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (checkclearopq(cap->oap))
3458 return;
3459 }
3460
3461 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3462 (cmdchar == '*' || cmdchar == '#')
3463 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3464 {
3465 clearop(cap->oap);
3466 return;
3467 }
3468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003469 // Allocate buffer to put the command in. Inserting backslashes can
3470 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3471 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3473 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3474 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003475 if (kp_help && *skipwhite(ptr) == NUL)
3476 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003477 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003478 return;
3479 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003480 kp_ex = (*kp == ':');
3481 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3482 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (buf == NULL)
3484 return;
3485 buf[0] = NUL;
3486
3487 switch (cmdchar)
3488 {
3489 case '*':
3490 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003491 // Put cursor at start of word, makes search skip the word
3492 // under the cursor.
3493 // Call setpcmark() first, so "*``" puts the cursor back where
3494 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 setpcmark();
3496 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3497
3498 if (!g_cmd && vim_iswordp(ptr))
3499 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 break;
3502
3503 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003504 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3505 if (n == 0)
3506 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 break;
3508
3509 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003510 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#ifdef FEAT_CSCOPE
3512 if (p_cst)
3513 STRCPY(buf, "cstag ");
3514 else
3515#endif
3516 STRCPY(buf, "ts ");
3517 break;
3518
3519 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003520 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (curbuf->b_help)
3522 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003524 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003525 if (g_cmd)
3526 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003527 else if (cap->count0 == 0)
3528 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003529 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003530 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003534 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003535 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003537 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003538 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003539 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003540 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003541 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003542 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003543 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003544 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003545 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003547 vim_free(buf);
3548 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003550 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003551 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003552 {
3553 vim_free(buf);
3554 vim_free(p);
3555 return;
3556 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003557 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 STRCAT(buf, p);
3559 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 else
3562 {
3563 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003564 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003565 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003566 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003567 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003568 {
3569 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003570 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003571 aux_ptr = (char_u *)"";
3572 else
3573 aux_ptr = (char_u *)"\\|\"\n[";
3574 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003575 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003576 aux_ptr = (char_u *)"\\|\"\n*?[";
3577
3578 p = buf + STRLEN(buf);
3579 while (n-- > 0)
3580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003581 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003582 if (vim_strchr(aux_ptr, *ptr) != NULL)
3583 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 // When current byte is a part of multibyte character, copy all
3585 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003586 if (has_mbyte)
3587 {
3588 int i;
3589 int len = (*mb_ptr2len)(ptr) - 1;
3590
3591 for (i = 0; i < len && n >= 1; ++i, --n)
3592 *p++ = *ptr++;
3593 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003594 *p++ = *ptr++;
3595 }
3596 *p = NUL;
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003599 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (cmdchar == '*' || cmdchar == '#')
3601 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003602 if (!g_cmd && (has_mbyte
3603 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3604 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003606
3607 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003608 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003610
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003611 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003614 {
3615 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003617 g_tag_at_cursor = FALSE;
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 vim_free(buf);
3621}
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
3624 * Get visually selected text, within one line only.
3625 * Returns FAIL if more than one line selected.
3626 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003627 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003628get_visual_text(
3629 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003630 char_u **pp, // return: start of selected text
3631 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 if (VIsual_mode != 'V')
3634 unadjust_for_sel();
3635 if (VIsual.lnum != curwin->w_cursor.lnum)
3636 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003637 if (cap != NULL)
3638 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return FAIL;
3640 }
3641 if (VIsual_mode == 'V')
3642 {
3643 *pp = ml_get_curline();
3644 *lenp = (int)STRLEN(*pp);
3645 }
3646 else
3647 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003648 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
3650 *pp = ml_get_pos(&curwin->w_cursor);
3651 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3652 }
3653 else
3654 {
3655 *pp = ml_get_pos(&VIsual);
3656 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3657 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003658 if (**pp == NUL)
3659 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003660 if (*lenp > 0)
3661 {
3662 if (has_mbyte)
3663 // Correct the length to include all bytes of the last
3664 // character.
3665 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3666 else if ((*pp)[*lenp - 1] == NUL)
3667 // Do not include a trailing NUL.
3668 *lenp -= 1;
3669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 reset_VIsual_and_resel();
3672 return OK;
3673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675/*
3676 * CTRL-T: backwards in tag stack
3677 */
3678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003679nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 if (!checkclearopq(cap->oap))
3682 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3683}
3684
3685/*
3686 * Handle scrolling command 'H', 'L' and 'M'.
3687 */
3688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003689nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 int used = 0;
3692 long n;
3693#ifdef FEAT_FOLDING
3694 linenr_T lnum;
3695#endif
3696 int half;
3697
3698 cap->oap->motion_type = MLINE;
3699 setpcmark();
3700
3701 if (cap->cmdchar == 'L')
3702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003703 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 curwin->w_cursor.lnum = curwin->w_botline - 1;
3705 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3706 curwin->w_cursor.lnum = 1;
3707 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003708 {
3709#ifdef FEAT_FOLDING
3710 if (hasAnyFolding(curwin))
3711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003712 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003713 for (n = cap->count1 - 1; n > 0
3714 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3715 {
3716 (void)hasFolding(curwin->w_cursor.lnum,
3717 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003718 if (curwin->w_cursor.lnum > curwin->w_topline)
3719 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003720 }
3721 }
3722 else
3723#endif
3724 curwin->w_cursor.lnum -= cap->count1 - 1;
3725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 else
3728 {
3729 if (cap->cmdchar == 'M')
3730 {
3731#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003732 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 used -= diff_check_fill(curwin, curwin->w_topline)
3734 - curwin->w_topfill;
3735#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003736 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3738 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3739 {
3740#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003741 // Count half he number of filler lines to be "below this
3742 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3744 + n) / 2 >= half)
3745 {
3746 --n;
3747 break;
3748 }
3749#endif
3750 used += plines(curwin->w_topline + n);
3751 if (used >= half)
3752 break;
3753#ifdef FEAT_FOLDING
3754 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3755 n = lnum - curwin->w_topline;
3756#endif
3757 }
3758 if (n > 0 && used > curwin->w_height)
3759 --n;
3760 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003761 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003764#ifdef FEAT_FOLDING
3765 if (hasAnyFolding(curwin))
3766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003767 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003768 lnum = curwin->w_topline;
3769 while (n-- > 0 && lnum < curwin->w_botline - 1)
3770 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003771 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003772 ++lnum;
3773 }
3774 n = lnum - curwin->w_topline;
3775 }
3776#endif
3777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 curwin->w_cursor.lnum = curwin->w_topline + n;
3779 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3780 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3781 }
3782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003783 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003784 if (cap->oap->op_type == OP_NOP)
3785 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 beginline(BL_SOL | BL_FIX);
3787}
3788
3789/*
3790 * Cursor right commands.
3791 */
3792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003793nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003796 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003798 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003800 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003801 if (mod_mask & MOD_MASK_CTRL)
3802 cap->arg = TRUE;
3803 nv_wordcmd(cap);
3804 return;
3805 }
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 cap->oap->motion_type = MCHAR;
3808 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003809 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003811 // In virtual edit mode, there's no such thing as "past_line", as lines
3812 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003814 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816 for (n = cap->count1; n > 0; --n)
3817 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003818 if ((!past_line && oneright() == FAIL)
3819 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003820 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003822 // <Space> wraps to next line if 'whichwrap' has 's'.
3823 // 'l' wraps to next line if 'whichwrap' has 'l'.
3824 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if ( ((cap->cmdchar == ' '
3826 && vim_strchr(p_ww, 's') != NULL)
3827 || (cap->cmdchar == 'l'
3828 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003829 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 && vim_strchr(p_ww, '>') != NULL))
3831 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3832 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003833 // When deleting we also count the NL as a character.
3834 // Set cap->oap->inclusive when last char in the line is
3835 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003836 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003838 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 cap->oap->inclusive = TRUE;
3840 else
3841 {
3842 ++curwin->w_cursor.lnum;
3843 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 curwin->w_set_curswant = TRUE;
3846 cap->oap->inclusive = FALSE;
3847 }
3848 continue;
3849 }
3850 if (cap->oap->op_type == OP_NOP)
3851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003852 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (n == cap->count1)
3854 beep_flush();
3855 }
3856 else
3857 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003858 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 cap->oap->inclusive = TRUE;
3860 }
3861 break;
3862 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003863 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
3865 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (virtual_active())
3867 oneright();
3868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003871 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 ++curwin->w_cursor.col;
3874 }
3875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877#ifdef FEAT_FOLDING
3878 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3879 && cap->oap->op_type == OP_NOP)
3880 foldOpenCursor();
3881#endif
3882}
3883
3884/*
3885 * Cursor left commands.
3886 *
3887 * Returns TRUE when operator end should not be adjusted.
3888 */
3889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003890nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 long n;
3893
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003894 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003896 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003897 if (mod_mask & MOD_MASK_CTRL)
3898 cap->arg = 1;
3899 nv_bck_word(cap);
3900 return;
3901 }
3902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 cap->oap->motion_type = MCHAR;
3904 cap->oap->inclusive = FALSE;
3905 for (n = cap->count1; n > 0; --n)
3906 {
3907 if (oneleft() == FAIL)
3908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003909 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3910 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3911 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if ( (((cap->cmdchar == K_BS
3913 || cap->cmdchar == Ctrl_H)
3914 && vim_strchr(p_ww, 'b') != NULL)
3915 || (cap->cmdchar == 'h'
3916 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003917 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 && vim_strchr(p_ww, '<') != NULL))
3919 && curwin->w_cursor.lnum > 1)
3920 {
3921 --(curwin->w_cursor.lnum);
3922 coladvance((colnr_T)MAXCOL);
3923 curwin->w_set_curswant = TRUE;
3924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003925 // When the NL before the first char has to be deleted we
3926 // put the cursor on the NUL after the previous line.
3927 // This is a very special case, be careful!
3928 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if ( (cap->oap->op_type == OP_DELETE
3930 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003931 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003933 char_u *cp = ml_get_cursor();
3934
3935 if (*cp != NUL)
3936 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003937 if (has_mbyte)
3938 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3939 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003940 ++curwin->w_cursor.col;
3941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 cap->retval |= CA_NO_ADJ_OP_END;
3943 }
3944 continue;
3945 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003946 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3948 beep_flush();
3949 break;
3950 }
3951 }
3952#ifdef FEAT_FOLDING
3953 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3954 && cap->oap->op_type == OP_NOP)
3955 foldOpenCursor();
3956#endif
3957}
3958
3959/*
3960 * Cursor up commands.
3961 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3962 */
3963 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003964nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003966 if (mod_mask & MOD_MASK_SHIFT)
3967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003968 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003969 cap->arg = BACKWARD;
3970 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003971 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003973
3974 cap->oap->motion_type = MLINE;
3975 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3976 clearopbeep(cap->oap);
3977 else if (cap->arg)
3978 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979}
3980
3981/*
3982 * Cursor down commands.
3983 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3984 */
3985 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003986nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988 if (mod_mask & MOD_MASK_SHIFT)
3989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003990 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003991 cap->arg = FORWARD;
3992 nv_page(cap);
3993 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003994#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003995 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003996 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
3997 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004001 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004002 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 cmdwin_result = CAR;
4004 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004005#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004006 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004007 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4008 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4009 {
4010 invoke_prompt_callback();
4011 if (restart_edit == 0)
4012 restart_edit = 'a';
4013 }
4014 else
4015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 cap->oap->motion_type = MLINE;
4018 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4019 clearopbeep(cap->oap);
4020 else if (cap->arg)
4021 beginline(BL_WHITE | BL_FIX);
4022 }
4023 }
4024}
4025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026/*
4027 * Grab the file name under the cursor and edit it.
4028 */
4029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004030nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004033 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004035 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004037
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004038#ifdef FEAT_PROP_POPUP
4039 if (ERROR_IF_TERM_POPUP_WINDOW)
4040 return;
4041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004043 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 if (ptr != NULL)
4046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004047 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004048 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004049 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004051 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004052 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004053 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004054 {
4055 curwin->w_cursor.lnum = lnum;
4056 check_cursor_lnum();
4057 beginline(BL_SOL | BL_FIX);
4058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 vim_free(ptr);
4060 }
4061 else
4062 clearop(cap->oap);
4063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065/*
4066 * <End> command: to end of current line or last line.
4067 */
4068 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004071 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004073 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004075 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 nv_dollar(cap);
4078}
4079
4080/*
4081 * Handle the "$" command.
4082 */
4083 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004084nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 cap->oap->motion_type = MCHAR;
4087 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004088 // In virtual mode when off the edge of a line and an operator
4089 // is pending (whew!) keep the cursor where it is.
4090 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (!virtual_active() || gchar_cursor() != NUL
4092 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004093 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (cursor_down((long)(cap->count1 - 1),
4095 cap->oap->op_type == OP_NOP) == FAIL)
4096 clearopbeep(cap->oap);
4097#ifdef FEAT_FOLDING
4098 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4099 foldOpenCursor();
4100#endif
4101}
4102
4103/*
4104 * Implementation of '?' and '/' commands.
4105 * If cap->arg is TRUE don't set PC mark.
4106 */
4107 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004108nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004111 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004115 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 cap->cmdchar = 'g';
4117 cap->nchar = '?';
4118 nv_operator(cap);
4119 return;
4120 }
4121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004122 // When using 'incsearch' the cursor may be moved to set a different search
4123 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004124 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 if (cap->searchbuf == NULL)
4127 {
4128 clearop(oap);
4129 return;
4130 }
4131
Bram Moolenaar46539112015-02-17 15:43:57 +01004132 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004133 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004134 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135}
4136
LemonBoya4399382022-04-09 21:04:08 +01004137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138/*
4139 * Handle "N" and "n" commands.
4140 * cap->arg is SEARCH_REV for "N", 0 for "n".
4141 */
4142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004143nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004145 pos_T old = curwin->w_cursor;
4146 int wrapped = FALSE;
4147 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004148
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004149 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004151 // Avoid getting stuck on the current cursor position, which can
4152 // happen when an offset is given and the cursor is on the last char
4153 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004154 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004155 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004156 cap->count1 -= 1;
4157 }
LemonBoya4399382022-04-09 21:04:08 +01004158
4159#ifdef FEAT_SEARCH_EXTRA
4160 // Redraw the window to refresh the highlighted matches.
4161 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004162 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164}
4165
4166/*
4167 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4168 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004171 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004172normal_search(
4173 cmdarg_T *cap,
4174 int dir,
4175 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004176 int opt, // extra flags for do_search()
4177 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004180 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004181#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004182 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
4185 cap->oap->motion_type = MCHAR;
4186 cap->oap->inclusive = FALSE;
4187 cap->oap->use_reg_one = TRUE;
4188 curwin->w_set_curswant = TRUE;
4189
Bram Moolenaara80faa82020-04-12 19:37:17 +02004190 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004191 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004192 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4193 if (wrapped != NULL)
4194 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (i == 0)
4196 clearop(cap->oap);
4197 else
4198 {
4199 if (i == 2)
4200 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202#ifdef FEAT_FOLDING
4203 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4204 foldOpenCursor();
4205#endif
4206 }
LemonBoya4399382022-04-09 21:04:08 +01004207#ifdef FEAT_SEARCH_EXTRA
4208 // Redraw the window to refresh the highlighted matches.
4209 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004210 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004213 // "/$" will put the cursor after the end of the line, may need to
4214 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004216 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217}
4218
4219/*
4220 * Character search commands.
4221 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4222 * ',' and FALSE for ';'.
4223 * cap->nchar is NUL for ',' and ';' (repeat the search)
4224 */
4225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004226nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 int t_cmd;
4229
4230 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4231 t_cmd = TRUE;
4232 else
4233 t_cmd = FALSE;
4234
4235 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004238 clearopbeep(cap->oap);
4239 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004241
4242 curwin->w_set_curswant = TRUE;
4243 // Include a Tab for "tx" and for "dfx".
4244 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4245 && (t_cmd || cap->oap->op_type != OP_NOP))
4246 {
4247 colnr_T scol, ecol;
4248
4249 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4250 curwin->w_cursor.coladd = ecol - scol;
4251 }
4252 else
4253 curwin->w_cursor.coladd = 0;
4254 adjust_for_sel(cap);
4255#ifdef FEAT_FOLDING
4256 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4257 foldOpenCursor();
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259}
4260
4261/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004262 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4263 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4264 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4265 * "[m" or "]m" search for prev/next start of (Java) method.
4266 * "[M" or "]M" search for prev/next end of (Java) method.
4267 */
4268 static void
4269nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4270{
4271 pos_T new_pos = {0, 0, 0};
4272 pos_T *pos = NULL; // init for GCC
4273 pos_T prev_pos;
4274 long n;
4275 int findc;
4276 int c;
4277
4278 if (cap->nchar == '*')
4279 cap->nchar = '/';
4280 prev_pos.lnum = 0;
4281 if (cap->nchar == 'm' || cap->nchar == 'M')
4282 {
4283 if (cap->cmdchar == '[')
4284 findc = '{';
4285 else
4286 findc = '}';
4287 n = 9999;
4288 }
4289 else
4290 {
4291 findc = cap->nchar;
4292 n = cap->count1;
4293 }
4294 for ( ; n > 0; --n)
4295 {
4296 if ((pos = findmatchlimit(cap->oap, findc,
4297 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4298 {
4299 if (new_pos.lnum == 0) // nothing found
4300 {
4301 if (cap->nchar != 'm' && cap->nchar != 'M')
4302 clearopbeep(cap->oap);
4303 }
4304 else
4305 pos = &new_pos; // use last one found
4306 break;
4307 }
4308 prev_pos = new_pos;
4309 curwin->w_cursor = *pos;
4310 new_pos = *pos;
4311 }
4312 curwin->w_cursor = *old_pos;
4313
4314 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4315 // brought us to the match for "[m" and "]M" when inside a method.
4316 // Try finding the '{' or '}' we want to be at.
4317 // Also repeat for the given count.
4318 if (cap->nchar == 'm' || cap->nchar == 'M')
4319 {
4320 // norm is TRUE for "]M" and "[m"
4321 int norm = ((findc == '{') == (cap->nchar == 'm'));
4322
4323 n = cap->count1;
4324 // found a match: we were inside a method
4325 if (prev_pos.lnum != 0)
4326 {
4327 pos = &prev_pos;
4328 curwin->w_cursor = prev_pos;
4329 if (norm)
4330 --n;
4331 }
4332 else
4333 pos = NULL;
4334 while (n > 0)
4335 {
4336 for (;;)
4337 {
4338 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4339 {
4340 // if not found anything, that's an error
4341 if (pos == NULL)
4342 clearopbeep(cap->oap);
4343 n = 0;
4344 break;
4345 }
4346 c = gchar_cursor();
4347 if (c == '{' || c == '}')
4348 {
4349 // Must have found end/start of class: use it.
4350 // Or found the place to be at.
4351 if ((c == findc && norm) || (n == 1 && !norm))
4352 {
4353 new_pos = curwin->w_cursor;
4354 pos = &new_pos;
4355 n = 0;
4356 }
4357 // if no match found at all, we started outside of the
4358 // class and we're inside now. Just go on.
4359 else if (new_pos.lnum == 0)
4360 {
4361 new_pos = curwin->w_cursor;
4362 pos = &new_pos;
4363 }
4364 // found start/end of other method: go to match
4365 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004366 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4367 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004368 n = 0;
4369 else
4370 curwin->w_cursor = *pos;
4371 break;
4372 }
4373 }
4374 --n;
4375 }
4376 curwin->w_cursor = *old_pos;
4377 if (pos == NULL && new_pos.lnum != 0)
4378 clearopbeep(cap->oap);
4379 }
4380 if (pos != NULL)
4381 {
4382 setpcmark();
4383 curwin->w_cursor = *pos;
4384 curwin->w_set_curswant = TRUE;
4385#ifdef FEAT_FOLDING
4386 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4387 && cap->oap->op_type == OP_NOP)
4388 foldOpenCursor();
4389#endif
4390 }
4391}
4392
4393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 * "[" and "]" commands.
4395 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4396 */
4397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004398nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 pos_T *pos = NULL; // init for GCC
4402 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 int flag;
4404 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 cap->oap->motion_type = MCHAR;
4407 cap->oap->inclusive = FALSE;
4408 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004409 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004411 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (cap->nchar == 'f')
4413 nv_gotofile(cap);
4414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004417 // Find the occurrence(s) of the identifier or define under cursor
4418 // in current and included files or jump to the first occurrence.
4419 //
4420 // search list jump
4421 // fwd bwd fwd bwd fwd bwd
4422 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4423 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004424 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 char_u *ptr;
4427 int len;
4428
4429 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4430 clearop(cap->oap);
4431 else
4432 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004433 // Make a copy, if the line was changed it will be freed.
4434 ptr = vim_strnsave(ptr, len);
4435 if (ptr == NULL)
4436 return;
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 find_pattern_in_path(ptr, 0, len, TRUE,
4439 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4440 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4441 cap->count1,
4442 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4443 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4444 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4445 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004446 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 curwin->w_set_curswant = TRUE;
4448 }
4449 }
4450 else
4451#endif
4452
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004453 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4454 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4455 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4456 // "[m" or "]m" search for prev/next start of (Java) method.
4457 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if ( (cap->cmdchar == '['
4459 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4460 || (cap->cmdchar == ']'
4461 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004462 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004464 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else if (cap->nchar == '[' || cap->nchar == ']')
4466 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004467 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 flag = '{';
4469 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004470 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004473 // Imitate strange Vi behaviour: When using "]]" with an operator
4474 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004475 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 (cap->oap->op_type != OP_NOP
4477 && cap->arg == FORWARD && flag == '{')))
4478 clearopbeep(cap->oap);
4479 else
4480 {
4481 if (cap->oap->op_type == OP_NOP)
4482 beginline(BL_WHITE | BL_FIX);
4483#ifdef FEAT_FOLDING
4484 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4485 foldOpenCursor();
4486#endif
4487 }
4488 }
4489
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004490 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 else if (cap->nchar == 'p' || cap->nchar == 'P')
4492 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004493 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004496 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 else if (cap->nchar == '\'' || cap->nchar == '`')
4498 {
4499 pos = &curwin->w_cursor;
4500 for (n = cap->count1; n > 0; --n)
4501 {
4502 prev_pos = *pos;
4503 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4504 cap->nchar == '\'');
4505 if (pos == NULL)
4506 break;
4507 }
4508 if (pos == NULL)
4509 pos = &prev_pos;
4510 nv_cursormark(cap, cap->nchar == '\'', pos);
4511 }
4512
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004513 // [ or ] followed by a middle mouse click: put selected text with
4514 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004515 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 (void)do_mouse(cap->oap, cap->nchar,
4518 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4519 cap->count1, PUT_FIXINDENT);
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004523 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else if (cap->nchar == 'z')
4525 {
4526 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4527 cap->count1) == FAIL)
4528 clearopbeep(cap->oap);
4529 }
4530#endif
4531
4532#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004533 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 else if (cap->nchar == 'c')
4535 {
4536 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4537 cap->count1) == FAIL)
4538 clearopbeep(cap->oap);
4539 }
4540#endif
4541
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004542#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004543 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004544 else if (cap->nchar == 's' || cap->nchar == 'S')
4545 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004546 setpcmark();
4547 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004548 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4549 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004550 {
4551 clearopbeep(cap->oap);
4552 break;
4553 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004554 else
4555 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004556# ifdef FEAT_FOLDING
4557 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4558 foldOpenCursor();
4559# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004560 }
4561#endif
4562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004563 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 else
4565 clearopbeep(cap->oap);
4566}
4567
4568/*
4569 * Handle Normal mode "%" command.
4570 */
4571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004572nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
4574 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004575#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 linenr_T lnum = curwin->w_cursor.lnum;
4577#endif
4578
4579 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004580 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 if (cap->count0 > 100)
4583 clearopbeep(cap->oap);
4584 else
4585 {
4586 cap->oap->motion_type = MLINE;
4587 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004588 // Round up, so 'normal 100%' always jumps at the line line.
4589 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4590 // overflow on 32-bits, so use a formula with less accuracy
4591 // to avoid overflows.
4592 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4594 / 100L * cap->count0;
4595 else
4596 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4597 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004598 if (curwin->w_cursor.lnum < 1)
4599 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4601 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4602 beginline(BL_SOL | BL_FIX);
4603 }
4604 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 cap->oap->motion_type = MCHAR;
4608 cap->oap->use_reg_one = TRUE;
4609 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4610 clearopbeep(cap->oap);
4611 else
4612 {
4613 setpcmark();
4614 curwin->w_cursor = *pos;
4615 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619 }
4620#ifdef FEAT_FOLDING
4621 if (cap->oap->op_type == OP_NOP
4622 && lnum != curwin->w_cursor.lnum
4623 && (fdo_flags & FDO_PERCENT)
4624 && KeyTyped)
4625 foldOpenCursor();
4626#endif
4627}
4628
4629/*
4630 * Handle "(" and ")" commands.
4631 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4632 */
4633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004634nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 cap->oap->motion_type = MCHAR;
4637 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004639 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 curwin->w_set_curswant = TRUE;
4641
4642 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004644 clearopbeep(cap->oap);
4645 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004647
4648 // Don't leave the cursor on the NUL past end of line.
4649 adjust_cursor(cap->oap);
4650 curwin->w_cursor.coladd = 0;
4651#ifdef FEAT_FOLDING
4652 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4653 foldOpenCursor();
4654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656
4657/*
4658 * "m" command: Mark a position.
4659 */
4660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004661nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004663 if (checkclearop(cap->oap))
4664 return;
4665
4666 if (setmark(cap->nchar) == FAIL)
4667 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
4670/*
4671 * "{" and "}" commands.
4672 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4673 */
4674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004675nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 cap->oap->motion_type = MCHAR;
4678 cap->oap->inclusive = FALSE;
4679 cap->oap->use_reg_one = TRUE;
4680 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004681 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004683 clearopbeep(cap->oap);
4684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004686
4687 curwin->w_cursor.coladd = 0;
4688#ifdef FEAT_FOLDING
4689 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4690 foldOpenCursor();
4691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692}
4693
4694/*
4695 * "u" command: Undo or make lower case.
4696 */
4697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004698nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004700 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004702 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 cap->cmdchar = 'g';
4704 cap->nchar = 'u';
4705 nv_operator(cap);
4706 }
4707 else
4708 nv_kundo(cap);
4709}
4710
4711/*
4712 * <Undo> command.
4713 */
4714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004715nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004717 if (checkclearopq(cap->oap))
4718 return;
4719
Bram Moolenaarf2732452018-06-03 14:47:35 +02004720#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004721 if (bt_prompt(curbuf))
4722 {
4723 clearopbeep(cap->oap);
4724 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004726#endif
4727 u_undo((int)cap->count1);
4728 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729}
4730
4731/*
4732 * Handle the "r" command.
4733 */
4734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004735nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 char_u *ptr;
4738 int had_ctrl_v;
4739 long n;
4740
4741 if (checkclearop(cap->oap))
4742 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004743#ifdef FEAT_JOB_CHANNEL
4744 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4745 {
4746 clearopbeep(cap->oap);
4747 return;
4748 }
4749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004751 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (cap->nchar == Ctrl_V)
4753 {
4754 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004755 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004756 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (cap->nchar > DEL)
4758 had_ctrl_v = NUL;
4759 }
4760 else
4761 had_ctrl_v = NUL;
4762
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004763 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004764 if (IS_SPECIAL(cap->nchar))
4765 {
4766 clearopbeep(cap->oap);
4767 return;
4768 }
4769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (VIsual_active)
4772 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004773 if (got_int)
4774 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004775 if (had_ctrl_v)
4776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004777 // Use a special (negative) number to make a difference between a
4778 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004779 if (cap->nchar == CAR)
4780 cap->nchar = REPLACE_CR_NCHAR;
4781 else if (cap->nchar == NL)
4782 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 nv_operator(cap);
4785 return;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (virtual_active())
4790 {
4791 if (u_save_cursor() == FAIL)
4792 return;
4793 if (gchar_cursor() == NUL)
4794 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004795 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 coladvance_force((colnr_T)(getviscol() + cap->count1));
4797 curwin->w_cursor.col -= cap->count1;
4798 }
4799 else if (gchar_cursor() == TAB)
4800 coladvance_force(getviscol());
4801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004805 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004806 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 clearopbeep(cap->oap);
4809 return;
4810 }
4811
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004812 // Replacing with a TAB is done by edit() when it is complicated because
4813 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4814 // Other characters are done below to avoid problems with things like
4815 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4817 {
4818 stuffnumReadbuff(cap->count1);
4819 stuffcharReadbuff('R');
4820 stuffcharReadbuff('\t');
4821 stuffcharReadbuff(ESC);
4822 return;
4823 }
4824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004825 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (u_save_cursor() == FAIL)
4827 return;
4828
4829 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4830 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004831 // Replace character(s) by a single newline.
4832 // Strange vi behaviour: Only one newline is inserted.
4833 // Delete the characters here.
4834 // Insert the newline with an insert command, takes care of
4835 // autoindent. The insert command depends on being on the last
4836 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004837 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 stuffcharReadbuff('\r');
4839 stuffcharReadbuff(ESC);
4840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004841 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 invoke_edit(cap, TRUE, 'r', FALSE);
4843 }
4844 else
4845 {
4846 prep_redo(cap->oap->regname, cap->count1,
4847 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4848
4849 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (has_mbyte)
4851 {
4852 int old_State = State;
4853
4854 if (cap->ncharC1 != 0)
4855 AppendCharToRedobuff(cap->ncharC1);
4856 if (cap->ncharC2 != 0)
4857 AppendCharToRedobuff(cap->ncharC2);
4858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004859 // This is slow, but it handles replacing a single-byte with a
4860 // multi-byte and the other way around. Also handles adding
4861 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 for (n = cap->count1; n > 0; --n)
4863 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004864 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004865 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4866 {
4867 int c = ins_copychar(curwin->w_cursor.lnum
4868 + (cap->nchar == Ctrl_Y ? -1 : 1));
4869 if (c != NUL)
4870 ins_char(c);
4871 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004873 ++curwin->w_cursor.col;
4874 }
4875 else
4876 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 State = old_State;
4878 if (cap->ncharC1 != 0)
4879 ins_char(cap->ncharC1);
4880 if (cap->ncharC2 != 0)
4881 ins_char(cap->ncharC2);
4882 }
4883 }
4884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004886 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 for (n = cap->count1; n > 0; --n)
4888 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004889 // Get ptr again, because u_save and/or showmatch() will have
4890 // released the line. This may also happen in ins_copychar().
4891 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004892 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4893 {
4894 int c = ins_copychar(curwin->w_cursor.lnum
4895 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004896
4897 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004898 if (c != NUL)
4899 ptr[curwin->w_cursor.col] = c;
4900 }
4901 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004902 {
4903 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004904 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (p_sm && msg_silent == 0)
4907 showmatch(cap->nchar);
4908 ++curwin->w_cursor.col;
4909 }
4910#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004911 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004913 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaar009b2592004-10-24 19:18:58 +00004915 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004916 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004918 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920#endif
4921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004922 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 changed_bytes(curwin->w_cursor.lnum,
4924 (colnr_T)(curwin->w_cursor.col - cap->count1));
4925 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004926 --curwin->w_cursor.col; // cursor on the last replaced char
4927 // if the character on the left of the current cursor is a multi-byte
4928 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (has_mbyte)
4930 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 curbuf->b_op_end = curwin->w_cursor;
4932 curwin->w_set_curswant = TRUE;
4933 set_last_insert(cap->nchar);
4934 }
4935}
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937/*
4938 * 'o': Exchange start and end of Visual area.
4939 * 'O': same, but in block mode exchange left and right corners.
4940 */
4941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004942v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 pos_T old_cursor;
4945 colnr_T left, right;
4946
4947 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4948 {
4949 old_cursor = curwin->w_cursor;
4950 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4951 curwin->w_cursor.lnum = VIsual.lnum;
4952 coladvance(left);
4953 VIsual = curwin->w_cursor;
4954
4955 curwin->w_cursor.lnum = old_cursor.lnum;
4956 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 // 'selection "exclusive" and cursor at right-bottom corner: move it
4958 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4960 ++curwin->w_curswant;
4961 coladvance(curwin->w_curswant);
4962 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004964 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 curwin->w_cursor.lnum = VIsual.lnum;
4967 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4968 ++right;
4969 coladvance(right);
4970 VIsual = curwin->w_cursor;
4971
4972 curwin->w_cursor.lnum = old_cursor.lnum;
4973 coladvance(left);
4974 curwin->w_curswant = left;
4975 }
4976 }
4977 else
4978 {
4979 old_cursor = curwin->w_cursor;
4980 curwin->w_cursor = VIsual;
4981 VIsual = old_cursor;
4982 curwin->w_set_curswant = TRUE;
4983 }
4984}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
4986/*
4987 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4988 */
4989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004990nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004992 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
4994 cap->cmdchar = 'c';
4995 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004996 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 VIsual_mode = 'V';
4998 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004999 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005001
5002 if (checkclearopq(cap->oap))
5003 return;
5004
5005 if (!curbuf->b_p_ma)
5006 emsg(_(e_cannot_make_changes_modifiable_is_off));
5007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005009 if (virtual_active())
5010 coladvance(getviscol());
5011 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013}
5014
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015/*
5016 * "gr".
5017 */
5018 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005019nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (VIsual_active)
5022 {
5023 cap->cmdchar = 'r';
5024 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005025 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005026 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005028
zeertzjqf86dea82023-03-05 21:15:06 +00005029 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005030 return;
5031
5032 if (!curbuf->b_p_ma)
5033 emsg(_(e_cannot_make_changes_modifiable_is_off));
5034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005036 if (cap->extra_char == Ctrl_V) // get another character
5037 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005038 if (cap->extra_char < ' ')
5039 // Prefix a control character with CTRL-V to avoid it being used as
5040 // a command.
5041 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005042 stuffcharReadbuff(cap->extra_char);
5043 stuffcharReadbuff(ESC);
5044 if (virtual_active())
5045 coladvance(getviscol());
5046 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050/*
5051 * Swap case for "~" command, when it does not work like an operator.
5052 */
5053 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005054n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 long n;
5057 pos_T startpos;
5058 int did_change = 0;
5059#ifdef FEAT_NETBEANS_INTG
5060 pos_T pos;
5061 char_u *ptr;
5062 int count;
5063#endif
5064
5065 if (checkclearopq(cap->oap))
5066 return;
5067
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005068 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 clearopbeep(cap->oap);
5071 return;
5072 }
5073
5074 prep_redo_cmd(cap);
5075
5076 if (u_save_cursor() == FAIL)
5077 return;
5078
5079 startpos = curwin->w_cursor;
5080#ifdef FEAT_NETBEANS_INTG
5081 pos = startpos;
5082#endif
5083 for (n = cap->count1; n > 0; --n)
5084 {
5085 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5086 inc_cursor();
5087 if (gchar_cursor() == NUL)
5088 {
5089 if (vim_strchr(p_ww, '~') != NULL
5090 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5091 {
5092#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005093 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 if (did_change)
5096 {
5097 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005098 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005099 netbeans_removed(curbuf, pos.lnum, pos.col,
5100 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005101 // line may have been flushed, get it again
5102 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005104 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106 pos.col = 0;
5107 pos.lnum++;
5108 }
5109#endif
5110 ++curwin->w_cursor.lnum;
5111 curwin->w_cursor.col = 0;
5112 if (n > 1)
5113 {
5114 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5115 break;
5116 u_clearline();
5117 }
5118 }
5119 else
5120 break;
5121 }
5122 }
5123#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005124 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 ptr = ml_get(pos.lnum);
5127 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005128 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5129 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131#endif
5132
5133
5134 check_cursor();
5135 curwin->w_set_curswant = TRUE;
5136 if (did_change)
5137 {
5138 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5139 0L);
5140 curbuf->b_op_start = startpos;
5141 curbuf->b_op_end = curwin->w_cursor;
5142 if (curbuf->b_op_end.col > 0)
5143 --curbuf->b_op_end.col;
5144 }
5145}
5146
5147/*
5148 * Move cursor to mark.
5149 */
5150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005151nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 if (check_mark(pos) == FAIL)
5154 clearop(cap->oap);
5155 else
5156 {
5157 if (cap->cmdchar == '\''
5158 || cap->cmdchar == '`'
5159 || cap->cmdchar == '['
5160 || cap->cmdchar == ']')
5161 setpcmark();
5162 curwin->w_cursor = *pos;
5163 if (flag)
5164 beginline(BL_WHITE | BL_FIX);
5165 else
5166 check_cursor();
5167 }
5168 cap->oap->motion_type = flag ? MLINE : MCHAR;
5169 if (cap->cmdchar == '`')
5170 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005171 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 curwin->w_set_curswant = TRUE;
5173}
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175/*
5176 * Handle commands that are operators in Visual mode.
5177 */
5178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005179v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
5181 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005183 // Uppercase means linewise, except in block mode, then "D" deletes till
5184 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (isupper(cap->cmdchar))
5186 {
5187 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005188 {
5189 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5193 curwin->w_curswant = MAXCOL;
5194 }
5195 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5196 nv_operator(cap);
5197}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199/*
5200 * "s" and "S" commands.
5201 */
5202 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005203nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005205#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005206 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005207 if (term_swap_diff() == OK)
5208 return;
5209#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005210#ifdef FEAT_JOB_CHANNEL
5211 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5212 {
5213 clearopbeep(cap->oap);
5214 return;
5215 }
5216#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005217 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005220 {
5221 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 cap->cmdchar = 'c';
5225 nv_operator(cap);
5226 }
5227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 nv_optrans(cap);
5229}
5230
5231/*
5232 * Abbreviated commands.
5233 */
5234 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005235nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005238 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (VIsual_active)
5242 v_visop(cap);
5243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 nv_optrans(cap);
5245}
5246
5247/*
5248 * Translate a command into another command.
5249 */
5250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5254 (char_u *)"d$", (char_u *)"c$",
5255 (char_u *)"cl", (char_u *)"cc",
5256 (char_u *)"yy", (char_u *)":s\r"};
5257 static char_u *str = (char_u *)"xXDCsSY&";
5258
5259 if (!checkclearopq(cap->oap))
5260 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005261 // In Vi "2D" doesn't delete the next line. Can't translate it
5262 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005263 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5264 {
5265 cap->oap->start = curwin->w_cursor;
5266 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005267#ifdef FEAT_EVAL
5268 set_op_var(OP_DELETE);
5269#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005270 cap->count1 = 1;
5271 nv_dollar(cap);
5272 finish_op = TRUE;
5273 ResetRedobuff();
5274 AppendCharToRedobuff('D');
5275 }
5276 else
5277 {
5278 if (cap->count0)
5279 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005280 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283 cap->opcount = 0;
5284}
5285
5286/*
5287 * "'" and "`" commands. Also for "g'" and "g`".
5288 * cap->arg is TRUE for "'" and "g'".
5289 */
5290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005291nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 pos_T *pos;
5294 int c;
5295#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005296 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005297 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#endif
5299
5300 if (cap->cmdchar == 'g')
5301 c = cap->extra_char;
5302 else
5303 c = cap->nchar;
5304 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005305 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 if (cap->arg)
5308 {
5309 check_cursor_lnum();
5310 beginline(BL_WHITE | BL_FIX);
5311 }
5312 else
5313 check_cursor();
5314 }
5315 else
5316 nv_cursormark(cap, cap->arg, pos);
5317
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005318 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 if (!virtual_active())
5320 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005321 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#ifdef FEAT_FOLDING
5323 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005324 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005325 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 && (fdo_flags & FDO_MARK)
5327 && old_KeyTyped)
5328 foldOpenCursor();
5329#endif
5330}
5331
5332/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005333 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 */
5335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005336nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005339#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005344 if (checkclearopq(cap->oap))
5345 return;
5346
5347 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005349 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005351 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005353 if (cap->cmdchar == 'g')
5354 pos = movechangelist((int)cap->count1);
5355 else
5356 pos = movemark((int)cap->count1);
5357 if (pos == (pos_T *)-1) // jump to other file
5358 {
5359 curwin->w_set_curswant = TRUE;
5360 check_cursor();
5361 }
5362 else if (pos != NULL) // can jump
5363 nv_cursormark(cap, FALSE, pos);
5364 else if (cap->cmdchar == 'g')
5365 {
5366 if (curbuf->b_changelistlen == 0)
5367 emsg(_(e_changelist_is_empty));
5368 else if (cap->count1 < 0)
5369 emsg(_(e_at_start_of_changelist));
5370 else
5371 emsg(_(e_at_end_of_changelist));
5372 }
5373 else
5374 clearopbeep(cap->oap);
5375# ifdef FEAT_FOLDING
5376 if (cap->oap->op_type == OP_NOP
5377 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5378 && (fdo_flags & FDO_MARK)
5379 && old_KeyTyped)
5380 foldOpenCursor();
5381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382}
5383
5384/*
5385 * Handle '"' command.
5386 */
5387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005388nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
5390 if (checkclearop(cap->oap))
5391 return;
5392#ifdef FEAT_EVAL
5393 if (cap->nchar == '=')
5394 cap->nchar = get_expr_register();
5395#endif
5396 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5397 {
5398 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005399 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#ifdef FEAT_EVAL
5401 set_reg_var(cap->oap->regname);
5402#endif
5403 }
5404 else
5405 clearopbeep(cap->oap);
5406}
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408/*
5409 * Handle "v", "V" and "CTRL-V" commands.
5410 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5411 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005412 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 */
5414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005417 if (cap->cmdchar == Ctrl_Q)
5418 cap->cmdchar = Ctrl_V;
5419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005420 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5421 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 if (cap->oap->op_type != OP_NOP)
5423 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005424 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005425 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return;
5427 }
5428
5429 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005430 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005432 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 else // toggle char/block mode
5435 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 VIsual_mode = cap->cmdchar;
5437 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005438 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005440 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {
5444 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005445 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 VIsual = curwin->w_cursor;
5449
5450 VIsual_active = TRUE;
5451 VIsual_reselect = TRUE;
5452 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005456 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005458 // For V and ^V, we multiply the number of lines even if there
5459 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5461 {
5462 curwin->w_cursor.lnum +=
5463 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005464 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466 VIsual_mode = resel_VIsual_mode;
5467 if (VIsual_mode == 'v')
5468 {
5469 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005470 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005471 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005472 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5473 if (*p_sel != 'e')
5474 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005477 curwin->w_curswant = resel_VIsual_vcol;
5478 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005480 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 curwin->w_curswant = MAXCOL;
5483 coladvance((colnr_T)MAXCOL);
5484 }
5485 else if (VIsual_mode == Ctrl_V)
5486 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005487 // Update curswant on the original line, that is where "col" is
5488 // valid.
5489 linenr_T lnum = curwin->w_cursor.lnum;
5490 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005491 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005492 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5493 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 coladvance(curwin->w_curswant);
5495 }
5496 else
5497 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005498 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500 else
5501 {
5502 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005503 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 may_start_select('c');
5505 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005506 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005507 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005508 if (cap->count0 > 0 && --cap->count1 > 0)
5509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005510 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005511 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5512 nv_right(cap);
5513 else if (VIsual_mode == 'V')
5514 nv_down(cap);
5515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517 }
5518}
5519
5520/*
5521 * Start selection for Shift-movement keys.
5522 */
5523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005524start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 may_start_select('k');
5528 n_start_visual_mode('v');
5529}
5530
5531/*
5532 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005533 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 */
5535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005536may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005538 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5539 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540}
5541
5542/*
5543 * Start Visual mode "c".
5544 * Should set VIsual_select before calling this.
5545 */
5546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005547n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005549#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005550 int cursor_line_was_concealed = curwin->w_p_cole > 0
5551 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005552#endif
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 VIsual_mode = c;
5555 VIsual_active = TRUE;
5556 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005557
5558 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005559 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005560 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005561 {
5562 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 VIsual = curwin->w_cursor;
5566
5567#ifdef FEAT_FOLDING
5568 foldAdjustVisual();
5569#endif
5570
LemonBoy2bf52dd2022-04-09 18:17:34 +01005571 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005573#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005574 // Check if redraw is needed after changing the state.
5575 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005576#endif
5577
Bram Moolenaar09df3122006-01-23 22:23:09 +00005578 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005579 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005581 // Make sure the clipboard gets updated. Needed because start and
5582 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 clip_star.vmode = NUL;
5584#endif
5585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005586 // Only need to redraw this line, unless still need to redraw an old
5587 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005588 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
5590 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5591 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5592 }
5593}
5594
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
5596/*
5597 * CTRL-W: Window commands
5598 */
5599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005600nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005602 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005603 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005605 cap->cmdchar = ':';
5606 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005607 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005608 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005609 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005610 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611}
5612
5613/*
5614 * CTRL-Z: Suspend
5615 */
5616 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005617nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005622 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624
5625/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005626 * "gv": Reselect the previous Visual area. If Visual already active,
5627 * exchange previous and current Visual area.
5628 */
5629 static void
5630nv_gv_cmd(cmdarg_T *cap)
5631{
5632 pos_T tpos;
5633 int i;
5634
5635 if (checkclearop(cap->oap))
5636 return;
5637
5638 if (curbuf->b_visual.vi_start.lnum == 0
5639 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5640 || curbuf->b_visual.vi_end.lnum == 0)
5641 {
5642 beep_flush();
5643 return;
5644 }
5645
5646 // set w_cursor to the start of the Visual area, tpos to the end
5647 if (VIsual_active)
5648 {
5649 i = VIsual_mode;
5650 VIsual_mode = curbuf->b_visual.vi_mode;
5651 curbuf->b_visual.vi_mode = i;
5652# ifdef FEAT_EVAL
5653 curbuf->b_visual_mode_eval = i;
5654# endif
5655 i = curwin->w_curswant;
5656 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5657 curbuf->b_visual.vi_curswant = i;
5658
5659 tpos = curbuf->b_visual.vi_end;
5660 curbuf->b_visual.vi_end = curwin->w_cursor;
5661 curwin->w_cursor = curbuf->b_visual.vi_start;
5662 curbuf->b_visual.vi_start = VIsual;
5663 }
5664 else
5665 {
5666 VIsual_mode = curbuf->b_visual.vi_mode;
5667 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5668 tpos = curbuf->b_visual.vi_end;
5669 curwin->w_cursor = curbuf->b_visual.vi_start;
5670 }
5671
5672 VIsual_active = TRUE;
5673 VIsual_reselect = TRUE;
5674
5675 // Set Visual to the start and w_cursor to the end of the Visual
5676 // area. Make sure they are on an existing character.
5677 check_cursor();
5678 VIsual = curwin->w_cursor;
5679 curwin->w_cursor = tpos;
5680 check_cursor();
5681 update_topline();
5682
5683 // When called from normal "g" command: start Select mode when
5684 // 'selectmode' contains "cmd". When called for K_SELECT, always
5685 // start Select mode.
5686 if (cap->arg)
5687 {
5688 VIsual_select = TRUE;
5689 VIsual_select_reg = 0;
5690 }
5691 else
5692 may_start_select('c');
5693 setmouse();
5694#ifdef FEAT_CLIPBOARD
5695 // Make sure the clipboard gets updated. Needed because start and
5696 // end are still the same, and the selection needs to be owned
5697 clip_star.vmode = NUL;
5698#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005699 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005700 showmode();
5701}
5702
5703/*
5704 * "g0", "g^" : Like "0" and "^" but for screen lines.
5705 * "gm": middle of "g0" and "g$".
5706 */
5707 static void
5708nv_g_home_m_cmd(cmdarg_T *cap)
5709{
5710 int i;
5711 int flag = FALSE;
5712
5713 if (cap->nchar == '^')
5714 flag = TRUE;
5715
5716 cap->oap->motion_type = MCHAR;
5717 cap->oap->inclusive = FALSE;
5718 if (curwin->w_p_wrap && curwin->w_width != 0)
5719 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005720 int width1 = curwin->w_width - curwin_col_off();
5721 int width2 = width1 + curwin_col_off2();
5722 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005723
5724 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005725 virtcol = curwin->w_virtcol
5726#ifdef FEAT_PROP_POPUP
5727 - curwin->w_virtcol_first_char
5728#endif
5729 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005730 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005731 if (virtcol >= (colnr_T)width1 && width2 > 0)
5732 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005733 }
5734 else
5735 i = curwin->w_leftcol;
5736 // Go to the middle of the screen line. When 'number' or
5737 // 'relativenumber' is on and lines are wrapping the middle can be more
5738 // to the left.
5739 if (cap->nchar == 'm')
5740 i += (curwin->w_width - curwin_col_off()
5741 + ((curwin->w_p_wrap && i > 0)
5742 ? curwin_col_off2() : 0)) / 2;
5743 coladvance((colnr_T)i);
5744 if (flag)
5745 {
5746 do
5747 i = gchar_cursor();
5748 while (VIM_ISWHITE(i) && oneright() == OK);
5749 curwin->w_valid &= ~VALID_WCOL;
5750 }
5751 curwin->w_set_curswant = TRUE;
5752}
5753
5754/*
5755 * "g_": to the last non-blank character in the line or <count> lines
5756 * downward.
5757 */
5758 static void
5759nv_g_underscore_cmd(cmdarg_T *cap)
5760{
5761 char_u *ptr;
5762
5763 cap->oap->motion_type = MCHAR;
5764 cap->oap->inclusive = TRUE;
5765 curwin->w_curswant = MAXCOL;
5766 if (cursor_down((long)(cap->count1 - 1),
5767 cap->oap->op_type == OP_NOP) == FAIL)
5768 {
5769 clearopbeep(cap->oap);
5770 return;
5771 }
5772
5773 ptr = ml_get_curline();
5774
5775 // In Visual mode we may end up after the line.
5776 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5777 --curwin->w_cursor.col;
5778
5779 // Decrease the cursor column until it's on a non-blank.
5780 while (curwin->w_cursor.col > 0
5781 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5782 --curwin->w_cursor.col;
5783 curwin->w_set_curswant = TRUE;
5784 adjust_for_sel(cap);
5785}
5786
5787/*
5788 * "g$" : Like "$" but for screen lines.
5789 */
5790 static void
5791nv_g_dollar_cmd(cmdarg_T *cap)
5792{
5793 oparg_T *oap = cap->oap;
5794 int i;
5795 int col_off = curwin_col_off();
5796
5797 oap->motion_type = MCHAR;
5798 oap->inclusive = TRUE;
5799 if (curwin->w_p_wrap && curwin->w_width != 0)
5800 {
5801 curwin->w_curswant = MAXCOL; // so we stay at the end
5802 if (cap->count1 == 1)
5803 {
5804 int width1 = curwin->w_width - col_off;
5805 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005806 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005807
5808 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005809 virtcol = curwin->w_virtcol
5810#ifdef FEAT_PROP_POPUP
5811 - curwin->w_virtcol_first_char
5812#endif
5813 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005814 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005815 if (virtcol >= (colnr_T)width1)
5816 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005817 * width2;
5818 coladvance((colnr_T)i);
5819
5820 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005821 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005822 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5823 {
5824 // Check for landing on a character that got split at
5825 // the end of the line. We do not want to advance to
5826 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005827 if (curwin->w_virtcol
5828#ifdef FEAT_PROP_POPUP
5829 - curwin->w_virtcol_first_char
5830#endif
5831 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005832 --curwin->w_cursor.col;
5833 }
5834 }
5835 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5836 clearopbeep(oap);
5837 }
5838 else
5839 {
5840 if (cap->count1 > 1)
5841 // if it fails, let the cursor still move to the last char
5842 (void)cursor_down(cap->count1 - 1, FALSE);
5843
5844 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5845 coladvance((colnr_T)i);
5846
5847 // if the character doesn't fit move one back
5848 if (curwin->w_cursor.col > 0
5849 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5850 {
5851 colnr_T vcol;
5852
5853 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5854 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5855 --curwin->w_cursor.col;
5856 }
5857
5858 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005859 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005860 }
5861}
5862
5863/*
5864 * "gi": start Insert at the last position.
5865 */
5866 static void
5867nv_gi_cmd(cmdarg_T *cap)
5868{
5869 int i;
5870
5871 if (curbuf->b_last_insert.lnum != 0)
5872 {
5873 curwin->w_cursor = curbuf->b_last_insert;
5874 check_cursor_lnum();
5875 i = (int)STRLEN(ml_get_curline());
5876 if (curwin->w_cursor.col > (colnr_T)i)
5877 {
5878 if (virtual_active())
5879 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5880 curwin->w_cursor.col = i;
5881 }
5882 }
5883 cap->cmdchar = 'i';
5884 nv_edit(cap);
5885}
5886
5887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 * Commands starting with "g".
5889 */
5890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005891nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
5893 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
5896 switch (cap->nchar)
5897 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005898 case Ctrl_A:
5899 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005901 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005902 if (!VIsual_active && cap->nchar == Ctrl_A)
5903 vim_mem_profile_dump();
5904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005906 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005907 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005908 {
5909 cap->arg = TRUE;
5910 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005911 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005912 nv_addsub(cap);
5913 }
5914 else
5915 clearopbeep(oap);
5916 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005918 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 case 'R':
5920 cap->arg = TRUE;
5921 nv_Replace(cap);
5922 break;
5923
5924 case 'r':
5925 nv_vreplace(cap);
5926 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928 case '&':
5929 do_cmdline_cmd((char_u *)"%s//~/&");
5930 break;
5931
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005932 // "gv": Reselect the previous Visual area. If Visual already active,
5933 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005935 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005937
5938 // "gV": Don't reselect the previous Visual area after a Select mode
5939 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 case 'V':
5941 VIsual_reselect = FALSE;
5942 break;
5943
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005944 // "gh": start Select mode.
5945 // "gH": start Select line mode.
5946 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 case K_BS:
5948 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005949 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 case 'h':
5951 case 'H':
5952 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 cap->cmdchar = cap->nchar + ('v' - 'h');
5954 cap->arg = TRUE;
5955 nv_visual(cap);
5956 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005958 // "gn", "gN" visually select next/previous search match
5959 // "gn" selects next match
5960 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005961 case 'N':
5962 case 'n':
5963 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005964 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005965 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005967 // "gj" and "gk" two new funny movement keys -- up and down
5968 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 case 'j':
5970 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005971 // with 'nowrap' it works just like the normal "j" command.
5972 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
5974 oap->motion_type = MLINE;
5975 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5976 }
5977 else
5978 i = nv_screengo(oap, FORWARD, cap->count1);
5979 if (i == FAIL)
5980 clearopbeep(oap);
5981 break;
5982
5983 case 'k':
5984 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005985 // with 'nowrap' it works just like the normal "k" command.
5986 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
5988 oap->motion_type = MLINE;
5989 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5990 }
5991 else
5992 i = nv_screengo(oap, BACKWARD, cap->count1);
5993 if (i == FAIL)
5994 clearopbeep(oap);
5995 break;
5996
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005997 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 case 'J':
5999 nv_join(cap);
6000 break;
6001
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006002 // "g0", "g^" : Like "0" and "^" but for screen lines.
6003 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 case '0':
6006 case 'm':
6007 case K_HOME:
6008 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006009 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 break;
6011
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006012 case 'M':
6013 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006014 oap->motion_type = MCHAR;
6015 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006016 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006017 if (cap->count0 > 0 && cap->count0 <= 100)
6018 coladvance((colnr_T)(i * cap->count0 / 100));
6019 else
6020 coladvance((colnr_T)(i / 2));
6021 curwin->w_set_curswant = TRUE;
6022 }
6023 break;
6024
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006025 // "g_": to the last non-blank character in the line or <count> lines
6026 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006028 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 break;
6030
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006031 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 case '$':
6033 case K_END:
6034 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006035 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 break;
6037
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006038 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 case '*':
6040 case '#':
6041#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006042 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006044 case Ctrl_RSB: // :tag or :tselect for current identifier
6045 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 nv_ident(cap);
6047 break;
6048
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006049 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 case 'e':
6051 case 'E':
6052 oap->motion_type = MCHAR;
6053 curwin->w_set_curswant = TRUE;
6054 oap->inclusive = TRUE;
6055 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6056 clearopbeep(oap);
6057 break;
6058
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006059 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006061 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 break;
6063
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006064 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006066 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 break;
6068
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006069 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case 'I':
6071 beginline(0);
6072 if (!checkclearopq(oap))
6073 invoke_edit(cap, FALSE, 'g', FALSE);
6074 break;
6075
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006076 // "gf": goto file, edit file under cursor
6077 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006079 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 nv_gotofile(cap);
6081 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case '\'':
6085 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006086 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 case '`':
6088 nv_gomark(cap);
6089 break;
6090
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006091 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006093 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 break;
6095
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006096 // "ga": Display the ascii value of the character under the
6097 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 case 'a':
6099 do_ascii(NULL);
6100 break;
6101
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006102 // "g8": Display the bytes used for the UTF-8 character under the
6103 // cursor. It is displayed in hex.
6104 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006106 if (cap->count0 == 8)
6107 utf_find_illegal();
6108 else
6109 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006112 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006113 case '<':
6114 show_sb_text();
6115 break;
6116
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006117 // "gg": Goto the first line in file. With a count it goes to
6118 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 case 'g':
6120 cap->arg = FALSE;
6121 nv_goto(cap);
6122 break;
6123
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006124 // Two-character operators:
6125 // "gq" Format text
6126 // "gw" Format text and keep cursor position
6127 // "g~" Toggle the case of the text.
6128 // "gu" Change text to lower case.
6129 // "gU" Change text to upper case.
6130 // "g?" rot13 encoding
6131 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 case 'q':
6133 case 'w':
6134 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006135 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 case '~':
6137 case 'u':
6138 case 'U':
6139 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006140 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 nv_operator(cap);
6142 break;
6143
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006144 // "gd": Find first occurrence of pattern under the cursor in the
6145 // current function
6146 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 case 'd':
6148 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006149 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 break;
6151
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006152 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 case K_MIDDLEMOUSE:
6154 case K_MIDDLEDRAG:
6155 case K_MIDDLERELEASE:
6156 case K_LEFTMOUSE:
6157 case K_LEFTDRAG:
6158 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006159 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 case K_RIGHTMOUSE:
6161 case K_RIGHTDRAG:
6162 case K_RIGHTRELEASE:
6163 case K_X1MOUSE:
6164 case K_X1DRAG:
6165 case K_X1RELEASE:
6166 case K_X2MOUSE:
6167 case K_X2DRAG:
6168 case K_X2RELEASE:
6169 mod_mask = MOD_MASK_CTRL;
6170 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 case K_IGNORE:
6174 break;
6175
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006176 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 case 'p':
6178 case 'P':
6179 nv_put(cap);
6180 break;
6181
6182#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006183 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case 'o':
6185 goto_byte(cap->count0);
6186 break;
6187#endif
6188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006189 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006191 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 do_exmode(TRUE);
6193 break;
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 case ',':
6196 nv_pcmark(cap);
6197 break;
6198
6199 case ';':
6200 cap->count1 = -cap->count1;
6201 nv_pcmark(cap);
6202 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006205 if (!checkclearop(oap))
6206 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006208 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006209 if (!checkclearop(oap))
6210 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006211 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212
Bram Moolenaar62a23252020-08-09 14:04:42 +02006213 case TAB:
6214 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6215 clearopbeep(oap);
6216 break;
6217
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006218 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006219 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006220 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006221 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006222 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006223 break;
6224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 default:
6226 clearopbeep(oap);
6227 break;
6228 }
6229}
6230
6231/*
6232 * Handle "o" and "O" commands.
6233 */
6234 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006235n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006237#ifdef FEAT_CONCEAL
6238 linenr_T oldline = curwin->w_cursor.lnum;
6239#endif
6240
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006241 if (checkclearopq(cap->oap))
6242 return;
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006245 if (cap->cmdchar == 'O')
6246 // Open above the first line of a folded sequence of lines
6247 (void)hasFolding(curwin->w_cursor.lnum,
6248 &curwin->w_cursor.lnum, NULL);
6249 else
6250 // Open below the last line of a folded sequence of lines
6251 (void)hasFolding(curwin->w_cursor.lnum,
6252 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006254 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6255 (cap->cmdchar == 'O' ? 1 : 0)),
6256 (linenr_T)(curwin->w_cursor.lnum +
6257 (cap->cmdchar == 'o' ? 1 : 0))
6258 ) == OK
6259 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6260 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6261 0, NULL) == OK)
6262 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006263#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006264 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6265 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006266#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006267#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006268 if (curwin->w_p_cul)
6269 // force redraw of cursorline
6270 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006271#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006272 // When '#' is in 'cpoptions' ignore the count.
6273 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6274 cap->count1 = 1;
6275 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277}
6278
6279/*
6280 * "." command: redo last change.
6281 */
6282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006283nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006285 if (checkclearopq(cap->oap))
6286 return;
6287
6288 // If "restart_edit" is TRUE, the last but one command is repeated
6289 // instead of the last command (inserting text). This is used for
6290 // CTRL-O <.> in insert mode.
6291 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6292 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293}
6294
6295/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006296 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 */
6298 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006299nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006301 if (VIsual_select && VIsual_active)
6302 {
6303 int reg;
6304 // Get register name
6305 ++no_mapping;
6306 ++allow_keys;
6307 reg = plain_vgetc();
6308 LANGMAP_ADJUST(reg, TRUE);
6309 --no_mapping;
6310 --allow_keys;
6311
6312 if (reg == '"')
6313 // the unnamed register is 0
6314 reg = 0;
6315
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006316 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006317 return;
6318 }
6319
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006320 if (checkclearopq(cap->oap))
6321 return;
6322
6323 u_redo((int)cap->count1);
6324 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325}
6326
6327/*
6328 * Handle "U" command.
6329 */
6330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006331nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006333 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006334 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006336 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 cap->cmdchar = 'g';
6338 cap->nchar = 'U';
6339 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006340 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006342
6343 if (checkclearopq(cap->oap))
6344 return;
6345
6346 u_undoline();
6347 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350/*
6351 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6352 * single character.
6353 */
6354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006355nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006357 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006358 {
6359#ifdef FEAT_JOB_CHANNEL
6360 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6361 {
6362 clearopbeep(cap->oap);
6363 return;
6364 }
6365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 else
6369 nv_operator(cap);
6370}
6371
6372/*
6373 * Handle an operator command.
6374 * The actual work is done by do_pending_operator().
6375 */
6376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 int op_type;
6380
6381 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006382#ifdef FEAT_JOB_CHANNEL
6383 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6384 {
6385 clearopbeep(cap->oap);
6386 return;
6387 }
6388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006390 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 nv_lineop(cap);
6392 else if (!checkclearop(cap->oap))
6393 {
6394 cap->oap->start = curwin->w_cursor;
6395 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006396#ifdef FEAT_EVAL
6397 set_op_var(op_type);
6398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400}
6401
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006402#ifdef FEAT_EVAL
6403/*
6404 * Set v:operator to the characters for "optype".
6405 */
6406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006407set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006408{
6409 char_u opchars[3];
6410
6411 if (optype == OP_NOP)
6412 set_vim_var_string(VV_OP, NULL, 0);
6413 else
6414 {
6415 opchars[0] = get_op_char(optype);
6416 opchars[1] = get_extra_op_char(optype);
6417 opchars[2] = NUL;
6418 set_vim_var_string(VV_OP, opchars, -1);
6419 }
6420}
6421#endif
6422
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423/*
6424 * Handle linewise operator "dd", "yy", etc.
6425 *
6426 * "_" is is a strange motion command that helps make operators more logical.
6427 * It is actually implemented, but not documented in the real Vi. This motion
6428 * command actually refers to "the current line". Commands like "dd" and "yy"
6429 * are really an alternate form of "d_" and "y_". It does accept a count, so
6430 * "d3_" works to delete 3 lines.
6431 */
6432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006433nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 cap->oap->motion_type = MLINE;
6436 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6437 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006439 && cap->oap->motion_force != 'v'
6440 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 || cap->oap->op_type == OP_LSHIFT
6442 || cap->oap->op_type == OP_RSHIFT)
6443 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006444 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 beginline(BL_WHITE | BL_FIX);
6446}
6447
6448/*
6449 * <Home> command.
6450 */
6451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006452nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006454 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006455 if (mod_mask & MOD_MASK_CTRL)
6456 nv_goto(cap);
6457 else
6458 {
6459 cap->count0 = 1;
6460 nv_pipe(cap);
6461 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006462 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6463 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465
6466/*
6467 * "|" command.
6468 */
6469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006470nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 cap->oap->motion_type = MCHAR;
6473 cap->oap->inclusive = FALSE;
6474 beginline(0);
6475 if (cap->count0 > 0)
6476 {
6477 coladvance((colnr_T)(cap->count0 - 1));
6478 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6479 }
6480 else
6481 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006482 // keep curswant at the column where we wanted to go, not where
6483 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 curwin->w_set_curswant = FALSE;
6485}
6486
6487/*
6488 * Handle back-word command "b" and "B".
6489 * cap->arg is 1 for "B"
6490 */
6491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006492nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 cap->oap->motion_type = MCHAR;
6495 cap->oap->inclusive = FALSE;
6496 curwin->w_set_curswant = TRUE;
6497 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6498 clearopbeep(cap->oap);
6499#ifdef FEAT_FOLDING
6500 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6501 foldOpenCursor();
6502#endif
6503}
6504
6505/*
6506 * Handle word motion commands "e", "E", "w" and "W".
6507 * cap->arg is TRUE for "E" and "W".
6508 */
6509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006510nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 int n;
6513 int word_end;
6514 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006515 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006517 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6519 word_end = TRUE;
6520 else
6521 word_end = FALSE;
6522 cap->oap->inclusive = word_end;
6523
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006524 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (!word_end && cap->oap->op_type == OP_CHANGE)
6526 {
6527 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006530 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006532 // Reproduce a funny Vi behaviour: "cw" on a blank only
6533 // changes one character, not all blanks until the start of
6534 // the next word. Only do this when the 'w' flag is included
6535 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6537 {
6538 cap->oap->inclusive = TRUE;
6539 cap->oap->motion_type = MCHAR;
6540 return;
6541 }
6542 }
6543 else
6544 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006545 // This is a little strange. To match what the real Vi does,
6546 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6547 // that we are not on a space or a TAB. This seems impolite
6548 // at first, but it's really more what we mean when we say
6549 // 'cw'.
6550 // Another strangeness: When standing on the end of a word
6551 // "ce" will change until the end of the next word, but "cw"
6552 // will change only one character! This is done by setting
6553 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 cap->oap->inclusive = TRUE;
6555 word_end = TRUE;
6556 flag = TRUE;
6557 }
6558 }
6559 }
6560
6561 cap->oap->motion_type = MCHAR;
6562 curwin->w_set_curswant = TRUE;
6563 if (word_end)
6564 n = end_word(cap->count1, cap->arg, flag, FALSE);
6565 else
6566 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006568 // Don't leave the cursor on the NUL past the end of line. Unless we
6569 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006570 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006571 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 if (n == FAIL && cap->oap->op_type == OP_NOP)
6574 clearopbeep(cap->oap);
6575 else
6576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578#ifdef FEAT_FOLDING
6579 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6580 foldOpenCursor();
6581#endif
6582 }
6583}
6584
6585/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006586 * Used after a movement command: If the cursor ends up on the NUL after the
6587 * end of the line, may move it back to the last character and make the motion
6588 * inclusive.
6589 */
6590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006591adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006592{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006593 // The cursor cannot remain on the NUL when:
6594 // - the column is > 0
6595 // - not in Visual mode or 'selection' is "o"
6596 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006598 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006599 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006600 {
6601 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006603 if (has_mbyte)
6604 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006605 oap->inclusive = TRUE;
6606 }
6607}
6608
6609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 * "0" and "^" commands.
6611 * cap->arg is the argument for beginline().
6612 */
6613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 cap->oap->motion_type = MCHAR;
6617 cap->oap->inclusive = FALSE;
6618 beginline(cap->arg);
6619#ifdef FEAT_FOLDING
6620 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6621 foldOpenCursor();
6622#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006623 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6624 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
6628 * In exclusive Visual mode, may include the last character.
6629 */
6630 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006631adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006634 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (has_mbyte)
6637 inc_cursor();
6638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 ++curwin->w_cursor.col;
6640 cap->oap->inclusive = FALSE;
6641 }
6642}
6643
6644/*
6645 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6646 * Should check VIsual_mode before calling this.
6647 * Returns TRUE when backed up to the previous line.
6648 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006650unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 pos_T *pp;
6653
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006654 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006656 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 pp = &curwin->w_cursor;
6658 else
6659 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if (pp->coladd > 0)
6661 --pp->coladd;
6662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (pp->col > 0)
6664 {
6665 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006666 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
6668 else if (pp->lnum > 1)
6669 {
6670 --pp->lnum;
6671 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6672 return TRUE;
6673 }
6674 }
6675 return FALSE;
6676}
6677
6678/*
6679 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6680 */
6681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006687 VIsual_select_reg = 0;
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 else if (VIsual_reselect)
6690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006691 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 cap->arg = TRUE;
6693 nv_g_cmd(cap);
6694 }
6695}
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
6698/*
6699 * "G", "gg", CTRL-END, CTRL-HOME.
6700 * cap->arg is TRUE for "G".
6701 */
6702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 linenr_T lnum;
6706
6707 if (cap->arg)
6708 lnum = curbuf->b_ml.ml_line_count;
6709 else
6710 lnum = 1L;
6711 cap->oap->motion_type = MLINE;
6712 setpcmark();
6713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006714 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (cap->count0 != 0)
6716 lnum = cap->count0;
6717 if (lnum < 1L)
6718 lnum = 1L;
6719 else if (lnum > curbuf->b_ml.ml_line_count)
6720 lnum = curbuf->b_ml.ml_line_count;
6721 curwin->w_cursor.lnum = lnum;
6722 beginline(BL_SOL | BL_FIX);
6723#ifdef FEAT_FOLDING
6724 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6725 foldOpenCursor();
6726#endif
6727}
6728
6729/*
6730 * CTRL-\ in Normal mode.
6731 */
6732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006733nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6736 {
6737 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006738 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (cmdwin_type != 0)
6742 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (VIsual_active)
6744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006746 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006748 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (cap->nchar == Ctrl_G && p_im)
6750 restart_edit = 'a';
6751 }
6752 else
6753 clearopbeep(cap->oap);
6754}
6755
6756/*
6757 * ESC in Normal mode: beep, but don't flush buffers.
6758 * Don't even beep if we are canceling a command.
6759 */
6760 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 int no_reason;
6764
6765 no_reason = (cap->oap->op_type == OP_NOP
6766 && cap->opcount == 0
6767 && cap->count0 == 0
6768 && cap->oap->regname == 0
6769 && !p_im);
6770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006771 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006773 if (restart_edit == 0 && cmdwin_type == 0
6774 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006775 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006776 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6777
Bram Moolenaar5939c352022-09-24 12:50:45 +01006778 // The user may accidentally do "vim file | grep word" and then
6779 // CTRL-C doesn't show anything. With a changed buffer give the
6780 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006781 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006782 {
6783 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6784
6785 if (out_redir)
6786 mch_errmsg(ms);
6787 else
6788 msg(ms);
6789 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006790 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006791 {
6792 if (out_redir)
6793 {
6794 got_int = FALSE;
6795 do_cmdline_cmd((char_u *)"qa");
6796 }
6797 else
6798 msg(_("Type :qa and press <Enter> to exit Vim"));
6799 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006802 if (restart_edit != 0)
6803 redraw_mode = TRUE; // remove "-- (insert) --"
6804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6806 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (!p_im)
6808 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (cmdwin_type != 0)
6810 {
6811 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006812 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 return;
6814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006816 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006817 {
6818 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006819 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6820 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006821 cmdwin_result = K_IGNORE;
6822 return;
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (VIsual_active)
6826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006827 end_visual_mode(); // stop Visual
6828 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006830 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006832 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006833 {
6834#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006835 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006836 popup_hide_message_win();
6837 else
6838#endif
6839 vim_beep(BO_ESC);
6840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 clearop(cap->oap);
6842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006843 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6844 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006845 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 restart_edit = 'a';
6847}
6848
6849/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006850 * Move the cursor for the "A" command.
6851 */
6852 void
6853set_cursor_for_append_to_line(void)
6854{
6855 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006856 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006857 {
6858 int save_State = State;
6859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006860 // Pretend Insert mode here to allow the cursor on the
6861 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006862 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006863 coladvance((colnr_T)MAXCOL);
6864 State = save_State;
6865 }
6866 else
6867 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6868}
6869
6870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006872 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 */
6874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006875nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006877 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6879 cap->cmdchar = 'i';
6880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006881 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006883 {
6884#ifdef FEAT_TERMINAL
6885 if (term_in_normal_mode())
6886 {
6887 end_visual_mode();
6888 clearop(cap->oap);
6889 term_enter_job_mode();
6890 return;
6891 }
6892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006896 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006897 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6898 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006902#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006903 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006904 {
6905 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006906 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006907 return;
6908 }
6909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 else if (!curbuf->b_p_ma && !p_im)
6911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006913 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006915 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006917 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006919 else if (cap->cmdchar == K_PS && VIsual_active)
6920 {
6921 pos_T old_pos = curwin->w_cursor;
6922 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006923 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006926 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6927 {
6928 shift_delete_registers();
6929 cap->oap->regname = '1';
6930 }
6931 else
6932 cap->oap->regname = '-';
6933 cap->cmdchar = 'd';
6934 cap->nchar = NUL;
6935 nv_operator(cap);
6936 do_pending_operator(cap, 0, FALSE);
6937 cap->cmdchar = K_PS;
6938
zeertzjq7a732522022-03-14 20:46:41 +00006939 if (*ml_get_cursor() != NUL)
6940 {
6941 if (old_visual_mode == 'V')
6942 {
6943 // In linewise Visual mode insert before the beginning of the
6944 // next line.
6945 // When the last line in the buffer was deleted then create a
6946 // new line, otherwise there is not need to move cursor.
6947 // Detect this by checking if cursor moved above Visual area.
6948 if (curwin->w_cursor.lnum < old_pos.lnum
6949 && curwin->w_cursor.lnum < old_visual.lnum)
6950 {
6951 if (u_save_cursor() == OK)
6952 {
6953 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6954 FALSE);
6955 appended_lines(curwin->w_cursor.lnum++, 1L);
6956 }
6957 }
6958 }
6959 // When the last char in the line was deleted then append.
6960 // Detect this by checking if cursor moved before Visual area.
6961 else if (curwin->w_cursor.col < old_pos.col
6962 && curwin->w_cursor.col < old_visual.col)
6963 inc_cursor();
6964 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006967 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 else if (!checkclearopq(cap->oap))
6970 {
6971 switch (cap->cmdchar)
6972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006973 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006974 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 break;
6976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006977 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006978 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6979 beginline(BL_WHITE);
6980 else
6981 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 break;
6983
Bram Moolenaara1891842017-02-04 21:34:31 +01006984 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 // Bracketed paste works like "a"ppend, unless the cursor is in
6986 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006987 if (curwin->w_cursor.col == 0)
6988 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006989 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 case 'a': // "a"ppend is like "i"nsert on the next character.
6992 // increment coladd when in virtual space, increment the
6993 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 if (virtual_active()
6995 && (curwin->w_cursor.coladd > 0
6996 || *ml_get_cursor() == NUL
6997 || *ml_get_cursor() == TAB))
6998 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006999 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 inc_cursor();
7001 break;
7002 }
7003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7005 {
7006 int save_State = State;
7007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007008 // Pretend Insert mode here to allow the cursor on the
7009 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007010 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 coladvance(getviscol());
7012 State = save_State;
7013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7016 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007017 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007018 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007019 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020}
7021
7022/*
7023 * Invoke edit() and take care of "restart_edit" and the return value.
7024 */
7025 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007026invoke_edit(
7027 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007029 int cmd,
7030 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 int restart_edit_save = 0;
7033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007034 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7035 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7036 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 if (repl || !stuff_empty())
7038 restart_edit_save = restart_edit;
7039 else
7040 restart_edit_save = 0;
7041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 restart_edit = 0;
7044
7045 if (edit(cmd, startln, cap->count1))
7046 cap->retval |= CA_COMMAND_BUSY;
7047
7048 if (restart_edit == 0)
7049 restart_edit = restart_edit_save;
7050}
7051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052/*
7053 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7054 */
7055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007056nv_object(
7057 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 int flag;
7060 int include;
7061 char_u *mps_save;
7062
7063 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007066 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 mps_save = curbuf->b_p_mps;
7070 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7071
7072 switch (cap->nchar)
7073 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 flag = current_word(cap->oap, cap->count1, include, FALSE);
7076 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 flag = current_word(cap->oap, cap->count1, include, TRUE);
7079 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007080 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 case '(':
7082 case ')':
7083 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7084 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007085 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 case '{':
7087 case '}':
7088 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7089 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 case ']':
7092 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7093 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 case '>':
7096 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7097 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007098#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 case 't': // "at" = a tag block (xml and html)
7100 // Do not adjust oap->end in do_pending_operator()
7101 // otherwise there are different results for 'dit'
7102 // (note leading whitespace in last line):
7103 // 1) <b> 2) <b>
7104 // foobar foobar
7105 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007106 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007107 flag = current_tagblock(cap->oap, cap->count1, include);
7108 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007109#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 flag = current_par(cap->oap, cap->count1, include, 'p');
7112 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007113 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 flag = current_sent(cap->oap, cap->count1, include);
7115 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007116 case '"': // "a"" = a double quoted string
7117 case '\'': // "a'" = a single quoted string
7118 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007119 flag = current_quote(cap->oap, cap->count1, include,
7120 cap->nchar);
7121 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007122#if 0 // TODO
7123 case 'S': // "aS" = a section
7124 case 'f': // "af" = a filename
7125 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126#endif
7127 default:
7128 flag = FAIL;
7129 break;
7130 }
7131
7132 curbuf->b_p_mps = mps_save;
7133 if (flag == FAIL)
7134 clearopbeep(cap->oap);
7135 adjust_cursor_col();
7136 curwin->w_set_curswant = TRUE;
7137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139/*
7140 * "q" command: Start/stop recording.
7141 * "q:", "q/", "q?": edit command-line in command-line window.
7142 */
7143 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007144nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
7146 if (cap->oap->op_type == OP_FORMAT)
7147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 cap->cmdchar = 'g';
7150 cap->nchar = 'q';
7151 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007152 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007154
7155 if (checkclearop(cap->oap))
7156 return;
7157
7158 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007160 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007162 emsg(_(e_cmdline_window_already_open));
7163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007165 stuffcharReadbuff(cap->nchar);
7166 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007168 else
7169 // (stop) recording into a named register, unless executing a
7170 // register
7171 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7172 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
7175/*
7176 * Handle the "@r" command.
7177 */
7178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007179nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 if (checkclearop(cap->oap))
7182 return;
7183#ifdef FEAT_EVAL
7184 if (cap->nchar == '=')
7185 {
7186 if (get_expr_register() == NUL)
7187 return;
7188 }
7189#endif
7190 while (cap->count1-- && !got_int)
7191 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007192 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
7194 clearopbeep(cap->oap);
7195 break;
7196 }
7197 line_breakcheck();
7198 }
7199}
7200
7201/*
7202 * Handle the CTRL-U and CTRL-D commands.
7203 */
7204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007205nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7208 || (cap->cmdchar == Ctrl_D
7209 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7210 clearopbeep(cap->oap);
7211 else if (!checkclearop(cap->oap))
7212 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7213}
7214
7215/*
7216 * Handle "J" or "gJ" command.
7217 */
7218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007219nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007223 nv_operator(cap);
7224 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007226
7227 if (checkclearop(cap->oap))
7228 return;
7229
7230 if (cap->count0 <= 1)
7231 cap->count0 = 2; // default for join is two lines!
7232 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7233 curbuf->b_ml.ml_line_count)
7234 {
7235 // can't join when on the last line
7236 if (cap->count0 <= 2)
7237 {
7238 clearopbeep(cap->oap);
7239 return;
7240 }
7241 cap->count0 = curbuf->b_ml.ml_line_count
7242 - curwin->w_cursor.lnum + 1;
7243 }
7244
7245 prep_redo(cap->oap->regname, cap->count0,
7246 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7247 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248}
7249
7250/*
7251 * "P", "gP", "p" and "gp" commands.
7252 */
7253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007254nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007256 nv_put_opt(cap, FALSE);
7257}
7258
7259/*
7260 * "P", "gP", "p" and "gp" commands.
7261 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7262 */
7263 static void
7264nv_put_opt(cmdarg_T *cap, int fix_indent)
7265{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 int regname = 0;
7267 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007268 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007269 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 int dir;
7271 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007272 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
7274 if (cap->oap->op_type != OP_NOP)
7275 {
7276#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007277 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7279 {
7280 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007281 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 }
7283 else
7284#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007285 clearopbeep(cap->oap);
7286 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007288
Bram Moolenaarf2732452018-06-03 14:47:35 +02007289#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007290 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007291 {
7292 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007293 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007294 }
7295#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007296
7297 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007299 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7300 ? FORWARD : BACKWARD;
7301 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007303 else
7304 dir = (cap->cmdchar == 'P'
7305 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7306 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7307 prep_redo_cmd(cap);
7308 if (cap->cmdchar == 'g')
7309 flags |= PUT_CURSEND;
7310 else if (cap->cmdchar == 'z')
7311 flags |= PUT_BLOCK_INNER;
7312
7313 if (VIsual_active)
7314 {
7315 // Putting in Visual mode: The put text replaces the selected
7316 // text. First delete the selected text, then put the new text.
7317 // Need to save and restore the registers that the delete
7318 // overwrites if the old contents is being put.
7319 was_visual = TRUE;
7320 regname = cap->oap->regname;
7321 keep_registers = cap->cmdchar == 'P';
7322#ifdef FEAT_CLIPBOARD
7323 adjust_clip_reg(&regname);
7324#endif
7325 if (regname == 0 || regname == '"'
7326 || VIM_ISDIGIT(regname) || regname == '-'
7327#ifdef FEAT_CLIPBOARD
7328 || (clip_unnamed && (regname == '*' || regname == '+'))
7329#endif
7330
7331 )
7332 {
7333 // The delete is going to overwrite the register we want to
7334 // put, save it first.
7335 reg1 = get_register(regname, TRUE);
7336 }
7337
7338 // Now delete the selected text. Avoid messages here.
7339 cap->cmdchar = 'd';
7340 cap->nchar = NUL;
7341 cap->oap->regname = keep_registers ? '_' : NUL;
7342 ++msg_silent;
7343 nv_operator(cap);
7344 do_pending_operator(cap, 0, FALSE);
7345 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7346 --msg_silent;
7347
7348 // delete PUT_LINE_BACKWARD;
7349 cap->oap->regname = regname;
7350
7351 if (reg1 != NULL)
7352 {
7353 // Delete probably changed the register we want to put, save
7354 // it first. Then put back what was there before the delete.
7355 reg2 = get_register(regname, FALSE);
7356 put_register(regname, reg1);
7357 }
7358
7359 // When deleted a linewise Visual area, put the register as
7360 // lines to avoid it joined with the next line. When deletion was
7361 // characterwise, split a line when putting lines.
7362 if (VIsual_mode == 'V')
7363 flags |= PUT_LINE;
7364 else if (VIsual_mode == 'v')
7365 flags |= PUT_LINE_SPLIT;
7366 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7367 flags |= PUT_LINE_FORWARD;
7368 dir = BACKWARD;
7369 if ((VIsual_mode != 'V'
7370 && curwin->w_cursor.col < curbuf->b_op_start.col)
7371 || (VIsual_mode == 'V'
7372 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7373 // cursor is at the end of the line or end of file, put
7374 // forward.
7375 dir = FORWARD;
7376 // May have been reset in do_put().
7377 VIsual_active = TRUE;
7378 }
7379 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7380
7381 // If a register was saved, put it back now.
7382 if (reg2 != NULL)
7383 put_register(regname, reg2);
7384
7385 // What to reselect with "gv"? Selecting the just put text seems to
7386 // be the most useful, since the original text was removed.
7387 if (was_visual)
7388 {
7389 curbuf->b_visual.vi_start = curbuf->b_op_start;
7390 curbuf->b_visual.vi_end = curbuf->b_op_end;
7391 // need to adjust cursor position
7392 if (*p_sel == 'e')
7393 inc(&curbuf->b_visual.vi_end);
7394 }
7395
7396 // When all lines were selected and deleted do_put() leaves an empty
7397 // line that needs to be deleted now.
7398 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7399 {
7400 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7401 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7402
7403 // If the cursor was in that line, move it to the end of the last
7404 // line.
7405 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7406 {
7407 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7408 coladvance((colnr_T)MAXCOL);
7409 }
7410 }
7411 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
7414/*
7415 * "o" and "O" commands.
7416 */
7417 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007418nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
7420#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007421 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7423 {
7424 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007425 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 else
7428#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007429 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007431#ifdef FEAT_JOB_CHANNEL
7432 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007433 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 n_opencmd(cap);
7437}
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439#ifdef FEAT_NETBEANS_INTG
7440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007441nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
7443 netbeans_keycommand(cap->nchar);
7444}
7445#endif
7446
7447#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007449nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007451 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452}
7453#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007454
Bram Moolenaar3918c952005-03-15 22:34:55 +00007455/*
7456 * Trigger CursorHold event.
7457 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7458 * input buffer. "did_cursorhold" is set to avoid retriggering.
7459 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007461nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007462{
7463 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7464 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007465 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007466}