blob: da01480ebcae53fe7796fb04797a1a493e6120a8 [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
902 if (ca.nchar == ESC)
903 {
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.
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000987 if (prev_finish_op || ca.cmdchar == 'r')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# ifdef FEAT_MOUSESHAPE
991 update_mouseshape(-1);
992# endif
993 }
994#endif
995
Bram Moolenaara983fe92008-07-31 20:04:27 +0000996 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100997 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 vim_free(ca.searchbuf);
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (has_mbyte)
1004 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (curwin->w_p_scb && toplevel)
1007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001008 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 do_check_scrollbind(TRUE);
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar860cae12010-06-05 23:22:07 +02001012 if (curwin->w_p_crb && toplevel)
1013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001015 do_check_cursorbind();
1016 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001018#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001020 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001021 restart_edit = 0;
1022#endif
1023
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001024 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1025 // if still inside a mapping that started in Visual mode).
1026 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1029 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !(ca.retval & CA_COMMAND_BUSY)
1031 && stuff_empty()
1032 && oap->regname == 0)
1033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_VIsual_select == 1)
1035 {
1036 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001037 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 showmode();
1039 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001040 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001042 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)edit(restart_edit, FALSE, 1L);
1044 }
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (restart_VIsual_select == 2)
1047 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 opcount = ca.opcount;
1051}
1052
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053#ifdef FEAT_EVAL
1054/*
1055 * Set v:count and v:count1 according to "cap".
1056 * Set v:prevcount only when "set_prevcount" is TRUE.
1057 */
1058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001060{
1061 long count = cap->count0;
1062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064 if (cap->opcount != 0)
1065 count = cap->opcount * (count == 0 ? 1 : count);
1066 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068}
1069#endif
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001072 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * if not.
1074 */
1075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 static int did_check = FALSE;
1079
1080 if (full_screen)
1081 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001082 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 did_check = TRUE;
1085 }
1086}
1087
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001088#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1089/*
1090 * Call yank_do_autocmd() for "regname".
1091 */
1092 static void
1093call_yank_do_autocmd(int regname)
1094{
1095 oparg_T oa;
1096 yankreg_T *reg;
1097
1098 clear_oparg(&oa);
1099 oa.regname = regname;
1100 oa.op_type = OP_YANK;
1101 oa.is_VIsual = TRUE;
1102 reg = get_register(regname, TRUE);
1103 yank_do_autocmd(&oa, reg);
1104 free_register(reg);
1105}
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001109 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001110 * This function or the next should ALWAYS be called to end Visual mode, except
1111 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001114end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001115{
1116 end_visual_mode_keep_button();
1117 reset_held_button();
1118}
1119
1120 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001121end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001124 // If we are using the clipboard, then remember what was selected in case
1125 // we need to paste it somewhere while we still own the selection.
1126 // Only do this when the clipboard is already owned. Don't want to grab
1127 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (clip_star.available && clip_star.owned)
1129 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001130
1131# if defined(FEAT_EVAL)
1132 // Emit a TextYankPost for the automatic copy of the selection into the
1133 // star and/or plus register.
1134 if (has_textyankpost())
1135 {
1136 if (clip_isautosel_star())
1137 call_yank_do_autocmd('*');
1138 if (clip_isautosel_plus())
1139 call_yank_do_autocmd('+');
1140 }
1141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 setmouse();
1146 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001149 curbuf->b_visual.vi_mode = VIsual_mode;
1150 curbuf->b_visual.vi_start = VIsual;
1151 curbuf->b_visual.vi_end = curwin->w_cursor;
1152 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153#ifdef FEAT_EVAL
1154 curbuf->b_visual_mode_eval = VIsual_mode;
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!virtual_active())
1157 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001158 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001160 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001161 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162}
1163
1164/*
1165 * Reset VIsual_active and VIsual_reselect.
1166 */
1167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001168reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 if (VIsual_active)
1171 {
1172 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001173 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 VIsual_reselect = FALSE;
1176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect if it's set.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 VIsual_reselect = FALSE;
1189 }
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001192 void
1193restore_visual_mode(void)
1194{
1195 if (VIsual_mode_orig != NUL)
1196 {
1197 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1198 VIsual_mode_orig = NUL;
1199 }
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Check for a balloon-eval special item to include when searching for an
1204 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1205 * Returns TRUE if the character at "*ptr" should be included.
1206 * "dir" is FORWARD or BACKWARD, the direction of searching.
1207 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1208 * "bnp" points to a counter for square brackets.
1209 */
1210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001211find_is_eval_item(
1212 char_u *ptr,
1213 int *colp,
1214 int *bnp,
1215 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1219 ++*bnp;
1220 if (*bnp > 0)
1221 {
1222 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1223 --*bnp;
1224 return TRUE;
1225 }
1226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (*ptr == '.')
1229 return TRUE;
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1233 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1234 {
1235 *colp += dir;
1236 return TRUE;
1237 }
1238 return FALSE;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241/*
1242 * Find the identifier under or to the right of the cursor.
1243 * "find_type" can have one of three values:
1244 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * FIND_STRING: find any non-white text
1246 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001247 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *
1249 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001250 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * This doesn't match the real Vi but I like it a little better and it
1254 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001255 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * When FIND_IDENT isn't defined, we backup until a blank.
1257 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001258 * Returns the length of the text, or zero if no text is found.
1259 * If text is found, a pointer to the text is put in "*text". This
1260 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
1262 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001263find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267}
1268
1269/*
1270 * Like find_ident_under_cursor(), but for any window and any position.
1271 * However: Uses 'iskeyword' from the current window!.
1272 */
1273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274find_ident_at_pos(
1275 win_T *wp,
1276 linenr_T lnum,
1277 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001278 char_u **text,
1279 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001280 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001283 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int this_class = 0;
1286 int prev_class;
1287 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001288 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001290 // if i == 0: try to find an identifier
1291 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1293 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1294 {
1295 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (has_mbyte)
1300 {
1301 while (ptr[col] != NUL)
1302 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001303 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 this_class = mb_get_class(ptr + col);
1307 if (this_class != 0 && (i == 1 || this_class != 1))
1308 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 )
1317 ++col;
1318
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001319 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (has_mbyte)
1326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1329 this_class = mb_get_class((char_u *)"a");
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001332 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1335 prev_class = mb_get_class(ptr + prevcol);
1336 if (this_class != prev_class
1337 && (i == 0
1338 || prev_class == 0
1339 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && (!(find_type & FIND_EVAL)
1341 || prevcol == 0
1342 || !find_is_eval_item(ptr + prevcol, &prevcol,
1343 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 )
1345 break;
1346 col = prevcol;
1347 }
1348
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001349 // If we don't want just any old text, or we've found an
1350 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (this_class > 2)
1352 this_class = 2;
1353 if (!(find_type & FIND_STRING) || this_class == 2)
1354 break;
1355 }
1356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 while (col > 0
1359 && ((i == 0
1360 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001361 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 && (!(find_type & FIND_IDENT)
1363 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || ((find_type & FIND_EVAL)
1365 && col > 1
1366 && find_is_eval_item(ptr + col - 1, &col,
1367 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ))
1369 --col;
1370
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001371 // If we don't want just any old text, or we've found an
1372 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1374 break;
1375 }
1376 }
1377
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001378 if (ptr[col] == NUL || (i == 0
1379 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001381 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001382 if ((find_type & FIND_NOERROR) == 0)
1383 {
1384 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001385 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return 0;
1390 }
1391 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001392 *text = ptr;
1393 if (textcol != NULL)
1394 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 bn = 0;
1400 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (has_mbyte)
1403 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001404 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 this_class = mb_get_class(ptr);
1406 while (ptr[col] != NUL
1407 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1408 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || ((find_type & FIND_EVAL)
1410 && col <= (int)startcol
1411 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001413 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001417 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 || ((find_type & FIND_EVAL)
1419 && col <= (int)startcol
1420 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 return col;
1425}
1426
1427/*
1428 * Prepare for redo of a normal command.
1429 */
1430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001431prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 prep_redo(cap->oap->regname, cap->count0,
1434 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1435}
1436
1437/*
1438 * Prepare for redo of any command.
1439 * Note that only the last argument can be a multi-byte char.
1440 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001442prep_redo(
1443 int regname,
1444 long num,
1445 int cmd1,
1446 int cmd2,
1447 int cmd3,
1448 int cmd4,
1449 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001451 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1452}
1453
1454/*
1455 * Prepare for redo of any command with extra count after "cmd2".
1456 */
1457 void
1458prep_redo_num2(
1459 int regname,
1460 long num1,
1461 int cmd1,
1462 int cmd2,
1463 long num2,
1464 int cmd3,
1465 int cmd4,
1466 int cmd5)
1467{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001469
1470#ifdef FEAT_EVAL
1471 // Put info about a mapping in the redo buffer, so that "." will use the
1472 // same script context.
1473 may_add_last_used_map_to_redobuff();
1474#endif
1475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
1478 AppendCharToRedobuff('"');
1479 AppendCharToRedobuff(regname);
1480 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001481 if (num1 != 0)
1482 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (cmd1 != NUL)
1484 AppendCharToRedobuff(cmd1);
1485 if (cmd2 != NUL)
1486 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001487 if (num2 != 0)
1488 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (cmd3 != NUL)
1490 AppendCharToRedobuff(cmd3);
1491 if (cmd4 != NUL)
1492 AppendCharToRedobuff(cmd4);
1493 if (cmd5 != NUL)
1494 AppendCharToRedobuff(cmd5);
1495}
1496
1497/*
1498 * check for operator active and clear it
1499 *
1500 * return TRUE if operator was active
1501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 if (oap->op_type == OP_NOP)
1506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
1511/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001512 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001514 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001519 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001526clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 oap->op_type = OP_NOP;
1529 oap->regname = 0;
1530 oap->motion_force = NUL;
1531 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001532 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 clearop(oap);
1539 beep_flush();
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
1543 * Remove the shift modifier from a special key.
1544 */
1545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 switch (cap->cmdchar)
1549 {
1550 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1551 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1552 case K_S_UP: cap->cmdchar = K_UP; break;
1553 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1554 case K_S_HOME: cap->cmdchar = K_HOME; break;
1555 case K_S_END: cap->cmdchar = K_END; break;
1556 }
1557 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001560/*
1561 * If the mode is currently displayed clear the command line or update the
1562 * command displayed.
1563 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566{
1567 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001569 else
1570 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001571}
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/*
1574 * Routines for displaying a partly typed command
1575 */
1576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001577static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static int showcmd_is_clear = TRUE;
1579static int showcmd_visual = FALSE;
1580
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001581static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001584clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 if (!p_sc)
1587 return;
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (VIsual_active && !char_avail())
1590 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001591 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 long lines;
1593 colnr_T leftcol, rightcol;
1594 linenr_T top, bot;
1595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001597 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 top = VIsual.lnum;
1600 bot = curwin->w_cursor.lnum;
1601 }
1602 else
1603 {
1604 top = curwin->w_cursor.lnum;
1605 bot = VIsual.lnum;
1606 }
1607# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001609 (void)hasFolding(top, &top, NULL);
1610 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611# endif
1612 lines = bot - top + 1;
1613
1614 if (VIsual_mode == Ctrl_V)
1615 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001616# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001618 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001622 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001626 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001627 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1630 (long)(rightcol - leftcol + 1));
1631 }
1632 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1633 sprintf((char *)showcmd_buf, "%ld", lines);
1634 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001635 {
1636 char_u *s, *e;
1637 int l;
1638 int bytes = 0;
1639 int chars = 0;
1640
1641 if (cursor_bot)
1642 {
1643 s = ml_get_pos(&VIsual);
1644 e = ml_get_cursor();
1645 }
1646 else
1647 {
1648 s = ml_get_cursor();
1649 e = ml_get_pos(&VIsual);
1650 }
1651 while ((*p_sel != 'e') ? s <= e : s < e)
1652 {
1653 l = (*mb_ptr2len)(s);
1654 if (l == 0)
1655 {
1656 ++bytes;
1657 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001658 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001659 }
1660 bytes += l;
1661 ++chars;
1662 s += l;
1663 }
1664 if (bytes == chars)
1665 sprintf((char *)showcmd_buf, "%d", chars);
1666 else
1667 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 showcmd_visual = TRUE;
1671 }
1672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 showcmd_buf[0] = NUL;
1675 showcmd_visual = FALSE;
1676
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001677 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (showcmd_is_clear)
1679 return;
1680 }
1681
1682 display_showcmd();
1683}
1684
1685/*
1686 * Add 'c' to string of shown command chars.
1687 * Return TRUE if output has been written (and setcursor() has been called).
1688 */
1689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001690add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 char_u *p;
1693 int old_len;
1694 int extra_len;
1695 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int i;
1697 static int ignore[] =
1698 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1701 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001702#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001703 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001704 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1706 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001707 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001709 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 0
1711 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar09df3122006-01-23 22:23:09 +00001713 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return FALSE;
1715
1716 if (showcmd_visual)
1717 {
1718 showcmd_buf[0] = NUL;
1719 showcmd_visual = FALSE;
1720 }
1721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (IS_SPECIAL(c))
1724 for (i = 0; ignore[i] != 0; ++i)
1725 if (ignore[i] == c)
1726 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001729 if (*p == ' ')
1730 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 old_len = (int)STRLEN(showcmd_buf);
1732 extra_len = (int)STRLEN(p);
1733 overflow = old_len + extra_len - SHOWCMD_COLS;
1734 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001735 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1736 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 STRCAT(showcmd_buf, p);
1738
1739 if (char_avail())
1740 return FALSE;
1741
1742 display_showcmd();
1743
1744 return TRUE;
1745}
1746
1747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001748add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 if (!add_to_showcmd(c))
1751 setcursor();
1752}
1753
1754/*
1755 * Delete 'len' characters from the end of the shown command.
1756 */
1757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 int old_len;
1761
1762 if (!p_sc)
1763 return;
1764
1765 old_len = (int)STRLEN(showcmd_buf);
1766 if (len > old_len)
1767 len = old_len;
1768 showcmd_buf[old_len - len] = NUL;
1769
1770 if (!char_avail())
1771 display_showcmd();
1772}
1773
1774/*
1775 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1776 * something and there is a partial mapping.
1777 */
1778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 if (p_sc)
1782 STRCPY(old_showcmd_buf, showcmd_buf);
1783}
1784
1785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 if (!p_sc)
1789 return;
1790
1791 STRCPY(showcmd_buf, old_showcmd_buf);
1792
1793 display_showcmd();
1794}
1795
1796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Luuk van Baalba936f62022-12-15 13:15:39 +00001799 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
Luuk van Baalba936f62022-12-15 13:15:39 +00001801 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 cursor_off();
1803
Luuk van Baalba936f62022-12-15 13:15:39 +00001804 if (*p_sloc == 's')
1805 win_redr_status(curwin, FALSE);
1806 else if (*p_sloc == 't')
1807 draw_tabline();
1808 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001810 if (!showcmd_is_clear)
1811 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Luuk van Baalba936f62022-12-15 13:15:39 +00001813 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1814 // spaces
1815 screen_puts((char_u *)" " + len,
1816 (int)Rows - 1, sc_col + len, 0);
1817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * When "check" is FALSE, prepare for commands that scroll the window.
1824 * When "check" is TRUE, take care of scroll-binding after the window has
1825 * scrolled. Called from normal_cmd() and edit().
1826 */
1827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001828do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 static win_T *old_curwin = NULL;
1831 static linenr_T old_topline = 0;
1832#ifdef FEAT_DIFF
1833 static int old_topfill = 0;
1834#endif
1835 static buf_T *old_buf = NULL;
1836 static colnr_T old_leftcol = 0;
1837
1838 if (check && curwin->w_p_scb)
1839 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001840 // If a ":syncbind" command was just used, don't scroll, only reset
1841 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (did_syncbind)
1843 did_syncbind = FALSE;
1844 else if (curwin == old_curwin)
1845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001846 // Synchronize other windows, as necessary according to
1847 // 'scrollbind'. Don't do this after an ":edit" command, except
1848 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if ((curwin->w_buffer == old_buf
1850#ifdef FEAT_DIFF
1851 || curwin->w_p_diff
1852#endif
1853 )
1854 && (curwin->w_topline != old_topline
1855#ifdef FEAT_DIFF
1856 || curwin->w_topfill != old_topfill
1857#endif
1858 || curwin->w_leftcol != old_leftcol))
1859 {
1860 check_scrollbind(curwin->w_topline - old_topline,
1861 (long)(curwin->w_leftcol - old_leftcol));
1862 }
1863 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001866 // When switching between windows, make sure that the relative
1867 // vertical offset is valid for the new window. The relative
1868 // offset is invalid whenever another 'scrollbind' window has
1869 // scrolled to a point that would force the current window to
1870 // scroll past the beginning or end of its buffer. When the
1871 // resync is performed, some of the other 'scrollbind' windows may
1872 // need to jump so that the current window's relative position is
1873 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1875 }
1876 curwin->w_scbind_pos = curwin->w_topline;
1877 }
1878
1879 old_curwin = curwin;
1880 old_topline = curwin->w_topline;
1881#ifdef FEAT_DIFF
1882 old_topfill = curwin->w_topfill;
1883#endif
1884 old_buf = curwin->w_buffer;
1885 old_leftcol = curwin->w_leftcol;
1886}
1887
1888/*
1889 * Synchronize any windows that have "scrollbind" set, based on the
1890 * number of rows by which the current window has changed
1891 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1892 */
1893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001894check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 int want_ver;
1897 int want_hor;
1898 win_T *old_curwin = curwin;
1899 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int old_VIsual_select = VIsual_select;
1901 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 colnr_T tgt_leftcol = curwin->w_leftcol;
1903 long topline;
1904 long y;
1905
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001906 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1908#ifdef FEAT_DIFF
1909 want_ver |= old_curwin->w_p_diff;
1910#endif
1911 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1912
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001913 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001915 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001918 // skip original window and windows with 'noscrollbind'
1919 if (curwin == old_curwin || !curwin->w_p_scb)
1920 continue;
1921
1922 // do the vertical scroll
1923 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001926 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
zeertzjq101d57b2022-07-31 18:34:32 +01001928 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
zeertzjq101d57b2022-07-31 18:34:32 +01001930 else
1931#endif
1932 {
1933 curwin->w_scbind_pos += topline_diff;
1934 topline = curwin->w_scbind_pos;
1935 if (topline > curbuf->b_ml.ml_line_count)
1936 topline = curbuf->b_ml.ml_line_count;
1937 if (topline < 1)
1938 topline = 1;
1939
1940 y = topline - curwin->w_topline;
1941 if (y > 0)
1942 scrollup(y, FALSE);
1943 else
1944 scrolldown(-y, FALSE);
1945 }
1946
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001947 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001948 cursor_correct();
1949 curwin->w_redr_status = TRUE;
1950 }
1951
1952 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001953 if (want_hor)
1954 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001957 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 VIsual_select = old_VIsual_select;
1959 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 curwin = old_curwin;
1961 curbuf = old_curbuf;
1962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964/*
1965 * Command character that's ignored.
1966 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001967 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001970nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001972 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
1975/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001976 * Command character that doesn't do anything, but unlike nv_ignore() does
1977 * start edit(). Used for "startinsert" executed while starting up.
1978 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001981{
1982}
1983
1984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 * Command character doesn't exist.
1986 */
1987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001988nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 clearopbeep(cap->oap);
1991}
1992
1993/*
1994 * <Help> and <F1> commands.
1995 */
1996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 if (!checkclearopq(cap->oap))
2000 ex_help(NULL);
2001}
2002
2003/*
2004 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2005 */
2006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002009#ifdef FEAT_JOB_CHANNEL
2010 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2011 clearopbeep(cap->oap);
2012 else
2013#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002014 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002015 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002016 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002017 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2018 op_addsub(cap->oap, cap->count1, cap->arg);
2019 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002020 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002021 else if (VIsual_active)
2022 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002023 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002024 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025}
2026
2027/*
2028 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2029 */
2030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002033 if (checkclearop(cap->oap))
2034 return;
2035
2036 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002038 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2039 if (cap->arg == BACKWARD)
2040 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002042 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002043 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002044 else
2045 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046}
2047
2048/*
2049 * Implementation of "gd" and "gD" command.
2050 */
2051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002052nv_gd(
2053 oparg_T *oap,
2054 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 char_u *ptr;
2059
Bram Moolenaard9d30582005-05-18 22:10:28 +00002060 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002061 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002062 == FAIL)
2063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002065 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002066 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002067
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002068#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002069 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2070 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002071#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002072 // clear any search statistics
2073 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2074 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002075}
2076
2077/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002078 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2079 * otherwise.
2080 */
2081 static int
2082is_ident(char_u *line, int offset)
2083{
2084 int i;
2085 int incomment = FALSE;
2086 int instring = 0;
2087 int prev = 0;
2088
2089 for (i = 0; i < offset && line[i] != NUL; i++)
2090 {
2091 if (instring != 0)
2092 {
2093 if (prev != '\\' && line[i] == instring)
2094 instring = 0;
2095 }
2096 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2097 {
2098 instring = line[i];
2099 }
2100 else
2101 {
2102 if (incomment)
2103 {
2104 if (prev == '*' && line[i] == '/')
2105 incomment = FALSE;
2106 }
2107 else if (prev == '/' && line[i] == '*')
2108 {
2109 incomment = TRUE;
2110 }
2111 else if (prev == '/' && line[i] == '/')
2112 {
2113 return FALSE;
2114 }
2115 }
2116
2117 prev = line[i];
2118 }
2119
2120 return incomment == FALSE && instring == 0;
2121}
2122
2123/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002124 * Search for variable declaration of "ptr[len]".
2125 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2126 * current file ("gD").
2127 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002128 * Return FAIL when not found.
2129 */
2130 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131find_decl(
2132 char_u *ptr,
2133 int len,
2134 int locally,
2135 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002136 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002137{
2138 char_u *pat;
2139 pos_T old_pos;
2140 pos_T par_pos;
2141 pos_T found_pos;
2142 int t;
2143 int save_p_ws;
2144 int save_p_scs;
2145 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002146 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002147 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002148 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002149
2150 if ((pat = alloc(len + 7)) == NULL)
2151 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002153 // Put "\V" before the pattern to avoid that the special meaning of "."
2154 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002155 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2156 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 old_pos = curwin->w_cursor;
2158 save_p_ws = p_ws;
2159 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002160 p_ws = FALSE; // don't wrap around end of file now
2161 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002163 // With "gD" go to line 1.
2164 // With "gd" Search back for the start of the current function, then go
2165 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002166 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002168 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002170 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 else
2173 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002174 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2176 --curwin->w_cursor.lnum;
2177 }
2178 curwin->w_cursor.col = 0;
2179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002181 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002182 for (;;)
2183 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002184 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002185 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002186 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002188
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002189 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002190 {
2191 pos_T *pos;
2192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // Check that the block the match is in doesn't end before the
2194 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2196 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2197 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002198 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 // There can't be a useful match before the end of this block.
2200 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002201 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002202 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002203 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002204 }
2205
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002206 if (t == FAIL)
2207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002208 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002209 if (found_pos.lnum != 0)
2210 {
2211 curwin->w_cursor = found_pos;
2212 t = OK;
2213 }
2214 break;
2215 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002216 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002219 ++curwin->w_cursor.lnum;
2220 curwin->w_cursor.col = 0;
2221 continue;
2222 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002223 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 // If the current position is not a valid identifier and a previous
2226 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 if (!valid && found_pos.lnum != 0)
2228 {
2229 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 }
2232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002233 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 if (valid && !locally)
2235 break;
2236 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 if (found_pos.lnum != 0)
2240 curwin->w_cursor = found_pos;
2241 break;
2242 }
2243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // For finding a local variable and the match is before the "{" or
2245 // inside a comment, continue searching. For K&R style function
2246 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002247 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002248 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002249 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002250 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // Remove SEARCH_START from flags to avoid getting stuck at one
2252 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002253 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002255
2256 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002258 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 curwin->w_cursor = old_pos;
2260 }
2261 else
2262 {
2263 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002264 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 reset_search_dir();
2266 }
2267
2268 vim_free(pat);
2269 p_ws = save_p_ws;
2270 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002271
2272 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273}
2274
2275/*
2276 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2277 * lines rather than lines in the file.
2278 * 'dist' must be positive.
2279 *
2280 * Return OK if able to move cursor, FAIL otherwise.
2281 */
2282 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002285 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int retval = OK;
2287 int atend = FALSE;
2288 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 int col_off1; // margin offset for first screen line
2290 int col_off2; // margin offset for wrapped screen line
2291 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002292 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002295 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 col_off1 = curwin_col_off();
2298 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002299 width1 = curwin->w_width - col_off1;
2300 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002301 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002305 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002306 // Instead of sticking at the last character of the buffer line we
2307 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (curwin->w_curswant == MAXCOL)
2309 {
2310 atend = TRUE;
2311 validate_virtcol();
2312 if (width1 <= 0)
2313 curwin->w_curswant = 0;
2314 else
2315 {
2316 curwin->w_curswant = width1 - 1;
2317 if (curwin->w_virtcol > curwin->w_curswant)
2318 curwin->w_curswant += ((curwin->w_virtcol
2319 - curwin->w_curswant - 1) / width2 + 1) * width2;
2320 }
2321 }
2322 else
2323 {
2324 if (linelen > width1)
2325 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2326 else
2327 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002328 if (curwin->w_curswant >= (colnr_T)n)
2329 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
2332 while (dist--)
2333 {
2334 if (dir == BACKWARD)
2335 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002336 if ((long)curwin->w_curswant >= width1
2337#ifdef FEAT_FOLDING
2338 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2339#endif
2340 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002341 // Move back within the line. This can give a negative value
2342 // for w_curswant if width1 < width2 (with cpoptions+=n),
2343 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 curwin->w_curswant -= width2;
2345 else
2346 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // to previous line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002348 if (!cursor_up_inner(curwin, 1))
Bram Moolenaare71996b2021-01-21 17:03:07 +01002349 {
2350 retval = FAIL;
2351 break;
2352 }
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002353 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (linelen > width1)
2355 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2356 + 1) * width2;
2357 }
2358 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002359 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 if (linelen > width1)
2362 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2363 else
2364 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002365 if (curwin->w_curswant + width2 < (colnr_T)n
2366#ifdef FEAT_FOLDING
2367 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2368#endif
2369 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 curwin->w_curswant += width2;
2372 else
2373 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002374 // to next line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002375 if (!cursor_down_inner(curwin, 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 retval = FAIL;
2378 break;
2379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002381 // Check if the cursor has moved below the number display
2382 // when width1 < width2 (with cpoptions+=n). Subtract width2
2383 // to get a negative value for w_curswant, which will get
2384 // clipped to column 0.
2385 if (curwin->w_curswant >= width1)
2386 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002387 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389 }
2390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002393 if (virtual_active() && atend)
2394 coladvance(MAXCOL);
2395 else
2396 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2399 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002400 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002401 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002402
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002403 // Check for landing on a character that got split at the end of the
2404 // last line. We want to advance a screenline, not end up in the same
2405 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002407 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002408#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002409 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2410 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002411#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002412
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002413 c = (*mb_ptr2char)(ml_get_cursor());
2414 if (dir == FORWARD && virtcol < curwin->w_curswant
2415 && (curwin->w_curswant <= (colnr_T)width1)
2416 && !vim_isprintc(c) && c > 255)
2417 oneright();
2418
Bram Moolenaar773b1582014-08-29 14:20:51 +02002419 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 && (curwin->w_curswant < (colnr_T)width1
2421 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2422 : ((curwin->w_curswant - width1) % width2
2423 > (colnr_T)width2 / 2)))
2424 --curwin->w_cursor.col;
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002429 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 return retval;
2432}
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
2435 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2436 * cap->arg must be TRUE for CTRL-E.
2437 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002438 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002439nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 if (!checkclearop(cap->oap))
2442 scroll_redraw(cap->arg, cap->count1);
2443}
2444
2445/*
2446 * Scroll "count" lines up or down, and redraw.
2447 */
2448 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002449scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450{
2451 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002452 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#ifdef FEAT_DIFF
2454 int prev_topfill = curwin->w_topfill;
2455#endif
2456 linenr_T prev_lnum = curwin->w_cursor.lnum;
2457
2458 if (up)
2459 scrollup(count, TRUE);
2460 else
2461 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002462 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002464 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2465 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 cursor_correct();
2467 check_cursor_moved(curwin);
2468 curwin->w_valid |= VALID_TOPLINE;
2469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002470 // If moved back to where we were, at least move the cursor, otherwise
2471 // we get stuck at one position. Don't move the cursor up if the
2472 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002474 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_DIFF
2476 && curwin->w_topfill == prev_topfill
2477#endif
2478 )
2479 {
2480 if (up)
2481 {
2482 if (curwin->w_cursor.lnum > prev_lnum
2483 || cursor_down(1L, FALSE) == FAIL)
2484 break;
2485 }
2486 else
2487 {
2488 if (curwin->w_cursor.lnum < prev_lnum
2489 || prev_topline == 1L
2490 || cursor_up(1L, FALSE) == FAIL)
2491 break;
2492 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002493 // Mark w_topline as valid, otherwise the screen jumps back at the
2494 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 check_cursor_moved(curwin);
2496 curwin->w_valid |= VALID_TOPLINE;
2497 }
2498 }
2499 if (curwin->w_cursor.lnum != prev_lnum)
2500 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002501 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502}
2503
2504/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002505 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2506 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2507 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002508 */
2509 static int
2510nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2511{
2512 int nchar = *nchar_arg;
2513 long n;
2514
2515 // "z123{nchar}": edit the count before obtaining {nchar}
2516 if (checkclearop(cap->oap))
2517 return FALSE;
2518 n = nchar - '0';
2519
2520 for (;;)
2521 {
2522#ifdef USE_ON_FLY_SCROLL
2523 dont_scroll = TRUE; // disallow scrolling here
2524#endif
2525 ++no_mapping;
2526 ++allow_keys; // no mapping for nchar, but allow key codes
2527 nchar = plain_vgetc();
2528 LANGMAP_ADJUST(nchar, TRUE);
2529 --no_mapping;
2530 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002531 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002532
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002533 if (nchar == K_DEL || nchar == K_KDEL)
2534 n /= 10;
2535 else if (VIM_ISDIGIT(nchar))
2536 n = n * 10 + (nchar - '0');
2537 else if (nchar == CAR)
2538 {
2539#ifdef FEAT_GUI
2540 need_mouse_correct = TRUE;
2541#endif
2542 win_setheight((int)n);
2543 break;
2544 }
2545 else if (nchar == 'l'
2546 || nchar == 'h'
2547 || nchar == K_LEFT
2548 || nchar == K_RIGHT)
2549 {
2550 cap->count1 = n ? n * cap->count1 : cap->count1;
2551 *nchar_arg = nchar;
2552 return TRUE;
2553 }
2554 else
2555 {
2556 clearopbeep(cap->oap);
2557 break;
2558 }
2559 }
2560 cap->oap->op_type = OP_NOP;
2561 return FALSE;
2562}
2563
2564#ifdef FEAT_SPELL
2565/*
2566 * "zug" and "zuw": undo "zg" and "zw"
2567 * "zg": add good word to word list
2568 * "zw": add wrong word to word list
2569 * "zG": add good word to temp word list
2570 * "zW": add wrong word to temp word list
2571 */
2572 static int
2573nv_zg_zw(cmdarg_T *cap, int nchar)
2574{
2575 char_u *ptr = NULL;
2576 int len;
2577 int undo = FALSE;
2578
2579 if (nchar == 'u')
2580 {
2581 ++no_mapping;
2582 ++allow_keys; // no mapping for nchar, but allow key codes
2583 nchar = plain_vgetc();
2584 LANGMAP_ADJUST(nchar, TRUE);
2585 --no_mapping;
2586 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002587 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002588
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002589 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2590 {
2591 clearopbeep(cap->oap);
2592 return OK;
2593 }
2594 undo = TRUE;
2595 }
2596
2597 if (checkclearop(cap->oap))
2598 return OK;
2599 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2600 return FAIL;
2601 if (ptr == NULL)
2602 {
2603 pos_T pos = curwin->w_cursor;
2604
2605 // Find bad word under the cursor. When 'spell' is
2606 // off this fails and find_ident_under_cursor() is
2607 // used below.
2608 emsg_off++;
2609 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2610 emsg_off--;
2611 if (len != 0 && curwin->w_cursor.col <= pos.col)
2612 ptr = ml_get_pos(&curwin->w_cursor);
2613 curwin->w_cursor = pos;
2614 }
2615
2616 if (ptr == NULL
2617 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2618 return FAIL;
2619 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2620 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2621 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2622
2623 return OK;
2624}
2625#endif
2626
2627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 * Commands that start with "z".
2629 */
2630 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002631nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 long n;
2634 colnr_T col;
2635 int nchar = cap->nchar;
2636#ifdef FEAT_FOLDING
2637 long old_fdl = curwin->w_p_fdl;
2638 int old_fen = curwin->w_p_fen;
2639#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002640 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002642 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (
2646#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002647 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2648 // and "zC" only in Visual mode. "zj" and "zk" are motion
2649 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 cap->nchar != 'f' && cap->nchar != 'F'
2651 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2652 && cap->nchar != 'j' && cap->nchar != 'k'
2653 &&
2654#endif
2655 checkclearop(cap->oap))
2656 return;
2657
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002658 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2659 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2661 && cap->count0
2662 && cap->count0 != curwin->w_cursor.lnum)
2663 {
2664 setpcmark();
2665 if (cap->count0 > curbuf->b_ml.ml_line_count)
2666 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2667 else
2668 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002669 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671
2672 switch (nchar)
2673 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002674 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 case '+':
2676 if (cap->count0 == 0)
2677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 // No count given: put cursor at the line below screen
2679 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2681 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2682 else
2683 curwin->w_cursor.lnum = curwin->w_botline;
2684 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002685 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 case NL:
2687 case CAR:
2688 case K_KENTER:
2689 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002690 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002693 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002694 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 break;
2696
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002701 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002702 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002703 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 break;
2705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // "z^", "z-" and "zb": put cursor at bottom of screen
2707 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2708 // when <count> is at bottom of window, and puts that one at
2709 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (cap->count0 != 0)
2711 {
2712 scroll_cursor_bot(0, TRUE);
2713 curwin->w_cursor.lnum = curwin->w_topline;
2714 }
2715 else if (curwin->w_topline == 1)
2716 curwin->w_cursor.lnum = 1;
2717 else
2718 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 case '-':
2721 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002725 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002726 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 break;
2728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002731 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 case 'h':
2736 case K_LEFT:
2737 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002738 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2739 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 break;
2741
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002742 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002743 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002746 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 case 'l':
2748 case K_RIGHT:
2749 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002750 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 break;
2752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 case 's': if (!curwin->w_p_wrap)
2755 {
2756#ifdef FEAT_FOLDING
2757 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 else
2760#endif
2761 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002762 if ((long)col > siso)
2763 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else
2765 col = 0;
2766 if (curwin->w_leftcol != col)
2767 {
2768 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002769 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 }
2772 break;
2773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 case 'e': if (!curwin->w_p_wrap)
2776 {
2777#ifdef FEAT_FOLDING
2778 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781#endif
2782 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002783 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002784 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 col = 0;
2786 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002787 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (curwin->w_leftcol != col)
2789 {
2790 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002791 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793 }
2794 break;
2795
Christian Brabandt2fa93842021-05-30 22:17:25 +02002796 // "zp", "zP" in block mode put without addind trailing spaces
2797 case 'P':
2798 case 'p': nv_put(cap);
2799 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002800 // "zy" Yank without trailing spaces
2801 case 'y': nv_operator(cap);
2802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // "zF": create fold command
2805 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 case 'F':
2807 case 'f': if (foldManualAllowed(TRUE))
2808 {
2809 cap->nchar = 'f';
2810 nv_operator(cap);
2811 curwin->w_p_fen = TRUE;
2812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002813 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2815 {
2816 nv_operator(cap);
2817 finish_op = TRUE;
2818 }
2819 }
2820 else
2821 clearopbeep(cap->oap);
2822 break;
2823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // "zd": delete fold at cursor
2825 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 case 'd':
2827 case 'D': if (foldManualAllowed(FALSE))
2828 {
2829 if (VIsual_active)
2830 nv_operator(cap);
2831 else
2832 deleteFold(curwin->w_cursor.lnum,
2833 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2834 }
2835 break;
2836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case 'E': if (foldmethodIsManual(curwin))
2839 {
2840 clearFolding(curwin);
2841 changed_window_setting();
2842 }
2843 else if (foldmethodIsMarker(curwin))
2844 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2845 TRUE, FALSE);
2846 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002847 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 break;
2849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case 'n': curwin->w_p_fen = FALSE;
2852 break;
2853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 case 'N': curwin->w_p_fen = TRUE;
2856 break;
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2860 break;
2861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2864 openFold(curwin->w_cursor.lnum, cap->count1);
2865 else
2866 {
2867 closeFold(curwin->w_cursor.lnum, cap->count1);
2868 curwin->w_p_fen = TRUE;
2869 }
2870 break;
2871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2874 openFoldRecurse(curwin->w_cursor.lnum);
2875 else
2876 {
2877 closeFoldRecurse(curwin->w_cursor.lnum);
2878 curwin->w_p_fen = TRUE;
2879 }
2880 break;
2881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 case 'o': if (VIsual_active)
2884 nv_operator(cap);
2885 else
2886 openFold(curwin->w_cursor.lnum, cap->count1);
2887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 'O': if (VIsual_active)
2891 nv_operator(cap);
2892 else
2893 openFoldRecurse(curwin->w_cursor.lnum);
2894 break;
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 case 'c': if (VIsual_active)
2898 nv_operator(cap);
2899 else
2900 closeFold(curwin->w_cursor.lnum, cap->count1);
2901 curwin->w_p_fen = TRUE;
2902 break;
2903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 case 'C': if (VIsual_active)
2906 nv_operator(cap);
2907 else
2908 closeFoldRecurse(curwin->w_cursor.lnum);
2909 curwin->w_p_fen = TRUE;
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'v': foldOpenCursor();
2914 break;
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 curwin->w_foldinvalid = TRUE; // recompute folds
2919 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 foldOpenCursor();
2921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 curwin->w_foldinvalid = TRUE; // recompute folds
2926 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 break;
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002931 {
2932 curwin->w_p_fdl -= cap->count1;
2933 if (curwin->w_p_fdl < 0)
2934 curwin->w_p_fdl = 0;
2935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 curwin->w_p_fen = TRUE;
2938 break;
2939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 curwin->w_p_fen = TRUE;
2944 break;
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002947 case 'r': curwin->w_p_fdl += cap->count1;
2948 {
2949 int d = getDeepestNesting();
2950
2951 if (curwin->w_p_fdl >= d)
2952 curwin->w_p_fdl = d;
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 break;
2960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 case 'j': // "zj" move to next fold downwards
2962 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2964 cap->count1) == FAIL)
2965 clearopbeep(cap->oap);
2966 break;
2967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002970#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 case 'g': // "zg": add good word to word list
2973 case 'w': // "zw": add wrong word to word list
2974 case 'G': // "zG": add good word to temp word list
2975 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002976 if (nv_zg_zw(cap, nchar) == FAIL)
2977 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002978 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002980 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002981 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002982 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002983 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002984#endif
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 default: clearopbeep(cap->oap);
2987 }
2988
2989#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (old_fen != curwin->w_p_fen)
2992 {
2993# ifdef FEAT_DIFF
2994 win_T *wp;
2995
2996 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 FOR_ALL_WINDOWS(wp)
3000 {
3001 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3002 {
3003 wp->w_p_fen = curwin->w_p_fen;
3004 changed_window_setting_win(wp);
3005 }
3006 }
3007 }
3008# endif
3009 changed_window_setting();
3010 }
3011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (old_fdl != curwin->w_p_fdl)
3014 newFoldLevel();
3015#endif
3016}
3017
3018#ifdef FEAT_GUI
3019/*
3020 * Vertical scrollbar movement.
3021 */
3022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 if (cap->oap->op_type != OP_NOP)
3026 clearopbeep(cap->oap);
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 gui_do_scroll();
3030}
3031
3032/*
3033 * Horizontal scrollbar movement.
3034 */
3035 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003036nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 if (cap->oap->op_type != OP_NOP)
3039 clearopbeep(cap->oap);
3040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003041 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003042 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043}
3044#endif
3045
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003046#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003047/*
3048 * Click in GUI tab.
3049 */
3050 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003052{
3053 if (cap->oap->op_type != OP_NOP)
3054 clearopbeep(cap->oap);
3055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003057 goto_tabpage(current_tab);
3058}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003059
3060/*
3061 * Selected item in tab line menu.
3062 */
3063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003064nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003065{
3066 if (cap->oap->op_type != OP_NOP)
3067 clearopbeep(cap->oap);
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003070 handle_tabmenu();
3071}
3072
3073/*
3074 * Handle selecting an item of the GUI tab line menu.
3075 * Used in Normal and Insert mode.
3076 */
3077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003079{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003080 switch (current_tabmenu)
3081 {
3082 case TABLINE_MENU_CLOSE:
3083 if (current_tab == 0)
3084 do_cmdline_cmd((char_u *)"tabclose");
3085 else
3086 {
3087 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3088 current_tab);
3089 do_cmdline_cmd(IObuff);
3090 }
3091 break;
3092
3093 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003094 if (current_tab == 0)
3095 do_cmdline_cmd((char_u *)"$tabnew");
3096 else
3097 {
3098 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3099 current_tab - 1);
3100 do_cmdline_cmd(IObuff);
3101 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003102 break;
3103
3104 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003105 if (current_tab == 0)
3106 do_cmdline_cmd((char_u *)"browse $tabnew");
3107 else
3108 {
3109 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3110 current_tab - 1);
3111 do_cmdline_cmd(IObuff);
3112 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003113 break;
3114 }
3115}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003116#endif
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118/*
3119 * "Q" command.
3120 */
3121 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003122nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003124 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003126 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003127 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 do_exmode(FALSE);
3129}
3130
3131/*
3132 * Handle a ":" command.
3133 */
3134 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003135nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003137 int old_p_im;
3138 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003139 int is_cmdkey = cap->cmdchar == K_COMMAND
3140 || cap->cmdchar == K_SCRIPT_COMMAND;
3141 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaar957cf672020-11-12 14:21:06 +01003143 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003145 nv_operator(cap);
3146 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003148
3149 if (cap->oap->op_type != OP_NOP)
3150 {
3151 // Using ":" as a movement is characterwise exclusive.
3152 cap->oap->motion_type = MCHAR;
3153 cap->oap->inclusive = FALSE;
3154 }
3155 else if (cap->count0 && !is_cmdkey)
3156 {
3157 // translate "count:" into ":.,.+(count - 1)"
3158 stuffcharReadbuff('.');
3159 if (cap->count0 > 1)
3160 {
3161 stuffReadbuff((char_u *)",.+");
3162 stuffnumReadbuff((long)cap->count0 - 1L);
3163 }
3164 }
3165
3166 // When typing, don't type below an old message
3167 if (KeyTyped)
3168 compute_cmdrow();
3169
3170 old_p_im = p_im;
3171
3172 // get a command line and execute it
3173 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3174 if (is_cmdkey)
3175 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3176 else
3177 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3178
3179 // If 'insertmode' changed, enter or exit Insert mode
3180 if (p_im != old_p_im)
3181 {
3182 if (p_im)
3183 restart_edit = 'i';
3184 else
3185 restart_edit = 0;
3186 }
3187
3188 if (cmd_result == FAIL)
3189 // The Ex command failed, do not execute the operator.
3190 clearop(cap->oap);
3191 else if (cap->oap->op_type != OP_NOP
3192 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3193 || cap->oap->start.col >
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003194 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003195 || did_emsg
3196 ))
3197 // The start of the operator has become invalid by the Ex command.
3198 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199}
3200
3201/*
3202 * Handle CTRL-G command.
3203 */
3204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003205nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003207 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
3209 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003210 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 showmode();
3212 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003213 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 fileinfo((int)cap->count0, FALSE, TRUE);
3216}
3217
3218/*
3219 * Handle CTRL-H <Backspace> command.
3220 */
3221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003222nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (VIsual_active && VIsual_select)
3225 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 v_visop(cap);
3228 }
3229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 nv_left(cap);
3231}
3232
3233/*
3234 * CTRL-L: clear screen and redraw.
3235 */
3236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003237nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003239 if (checkclearop(cap->oap))
3240 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003241
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003242#ifdef FEAT_SYN_HL
3243 // Clear all syntax states to force resyncing.
3244 syn_stack_free_all(curwin->w_s);
3245# ifdef FEAT_RELTIME
3246 {
3247 win_T *wp;
3248
3249 FOR_ALL_WINDOWS(wp)
3250 wp->w_s->b_syn_slow = FALSE;
3251 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003254 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003255#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3256# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003257 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003258# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003259 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
3263/*
3264 * CTRL-O: In Select mode: switch to Visual mode for one command.
3265 * Otherwise: Go to older pcmark.
3266 */
3267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003268nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 if (VIsual_active && VIsual_select)
3271 {
3272 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003273 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003275 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 cap->count1 = -cap->count1;
3280 nv_pcmark(cap);
3281 }
3282}
3283
3284/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003285 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3286 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 */
3288 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003289nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 if (!checkclearopq(cap->oap))
3292 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3293 GETF_SETMARK|GETF_ALT, FALSE);
3294}
3295
3296/*
3297 * "Z" commands.
3298 */
3299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003302 if (checkclearopq(cap->oap))
3303 return;
3304
3305 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003307 // "ZZ": equivalent to ":x".
3308 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 break;
3310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003311 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003312 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 break;
3314
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003315 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317}
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319/*
3320 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3321 */
3322 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003323do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 oparg_T oa;
3326 cmdarg_T ca;
3327
3328 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003329 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 ca.oap = &oa;
3331 ca.cmdchar = c1;
3332 ca.nchar = c2;
3333 nv_ident(&ca);
3334}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003337 * 'K' normal-mode command. Get the command to lookup the keyword under the
3338 * cursor.
3339 */
3340 static int
3341nv_K_getcmd(
3342 cmdarg_T *cap,
3343 char_u *kp,
3344 int kp_help,
3345 int kp_ex,
3346 char_u **ptr_arg,
3347 int n,
3348 char_u *buf,
3349 unsigned buflen)
3350{
3351 char_u *ptr = *ptr_arg;
3352 int isman;
3353 int isman_s;
3354
3355 if (kp_help)
3356 {
3357 // in the help buffer
3358 STRCPY(buf, "he! ");
3359 return n;
3360 }
3361
3362 if (kp_ex)
3363 {
3364 // 'keywordprog' is an ex command
3365 if (cap->count0 != 0)
3366 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3367 else
3368 STRCPY(buf, kp);
3369 STRCAT(buf, " ");
3370 return n;
3371 }
3372
3373 // An external command will probably use an argument starting
3374 // with "-" as an option. To avoid trouble we skip the "-".
3375 while (*ptr == '-' && n > 0)
3376 {
3377 ++ptr;
3378 --n;
3379 }
3380 if (n == 0)
3381 {
3382 // found dashes only
3383 emsg(_(e_no_identifier_under_cursor));
3384 vim_free(buf);
3385 *ptr_arg = ptr;
3386 return 0;
3387 }
3388
3389 // When a count is given, turn it into a range. Is this
3390 // really what we want?
3391 isman = (STRCMP(kp, "man") == 0);
3392 isman_s = (STRCMP(kp, "man -s") == 0);
3393 if (cap->count0 != 0 && !(isman || isman_s))
3394 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3395
3396 STRCAT(buf, "! ");
3397 if (cap->count0 == 0 && isman_s)
3398 STRCAT(buf, "man");
3399 else
3400 STRCAT(buf, kp);
3401 STRCAT(buf, " ");
3402 if (cap->count0 != 0 && (isman || isman_s))
3403 {
3404 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3405 STRCAT(buf, " ");
3406 }
3407
3408 *ptr_arg = ptr;
3409 return n;
3410}
3411
3412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 * Handle the commands that use the word under the cursor.
3414 * [g] CTRL-] :ta to current identifier
3415 * [g] 'K' run program for current identifier
3416 * [g] '*' / to current identifier or string
3417 * [g] '#' ? to current identifier or string
3418 * g ']' :tselect for current identifier
3419 */
3420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003421nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 char_u *ptr = NULL;
3424 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003425 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003426 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 char_u *kp; // value of 'keywordprg'
3429 int kp_help; // 'keywordprg' is ":he"
3430 int kp_ex; // 'keywordprg' starts with ":"
3431 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003433 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003434 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003437 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 cmdchar = cap->nchar;
3440 g_cmd = TRUE;
3441 }
3442 else
3443 {
3444 cmdchar = cap->cmdchar;
3445 g_cmd = FALSE;
3446 }
3447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 cmdchar = '#';
3450
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003451 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3455 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (checkclearopq(cap->oap))
3457 return;
3458 }
3459
3460 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3461 (cmdchar == '*' || cmdchar == '#')
3462 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3463 {
3464 clearop(cap->oap);
3465 return;
3466 }
3467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // Allocate buffer to put the command in. Inserting backslashes can
3469 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3470 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3472 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3473 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003474 if (kp_help && *skipwhite(ptr) == NUL)
3475 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003476 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003477 return;
3478 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003479 kp_ex = (*kp == ':');
3480 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3481 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (buf == NULL)
3483 return;
3484 buf[0] = NUL;
3485
3486 switch (cmdchar)
3487 {
3488 case '*':
3489 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003490 // Put cursor at start of word, makes search skip the word
3491 // under the cursor.
3492 // Call setpcmark() first, so "*``" puts the cursor back where
3493 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 setpcmark();
3495 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3496
3497 if (!g_cmd && vim_iswordp(ptr))
3498 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003499 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501
3502 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003503 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3504 if (n == 0)
3505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507
3508 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003509 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#ifdef FEAT_CSCOPE
3511 if (p_cst)
3512 STRCPY(buf, "cstag ");
3513 else
3514#endif
3515 STRCPY(buf, "ts ");
3516 break;
3517
3518 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003519 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (curbuf->b_help)
3521 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003523 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003524 if (g_cmd)
3525 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003526 else if (cap->count0 == 0)
3527 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003528 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003529 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003533 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003534 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003536 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003537 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003538 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003539 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003540 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003541 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003542 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003543 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003544 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003546 vim_free(buf);
3547 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003549 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003550 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003551 {
3552 vim_free(buf);
3553 vim_free(p);
3554 return;
3555 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003556 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003557 STRCAT(buf, p);
3558 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003560 else
3561 {
3562 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003563 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003564 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003565 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003566 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003567 {
3568 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003569 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003570 aux_ptr = (char_u *)"";
3571 else
3572 aux_ptr = (char_u *)"\\|\"\n[";
3573 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003574 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 aux_ptr = (char_u *)"\\|\"\n*?[";
3576
3577 p = buf + STRLEN(buf);
3578 while (n-- > 0)
3579 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003580 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003581 if (vim_strchr(aux_ptr, *ptr) != NULL)
3582 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // When current byte is a part of multibyte character, copy all
3584 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003585 if (has_mbyte)
3586 {
3587 int i;
3588 int len = (*mb_ptr2len)(ptr) - 1;
3589
3590 for (i = 0; i < len && n >= 1; ++i, --n)
3591 *p++ = *ptr++;
3592 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003593 *p++ = *ptr++;
3594 }
3595 *p = NUL;
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003598 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (cmdchar == '*' || cmdchar == '#')
3600 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003601 if (!g_cmd && (has_mbyte
3602 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3603 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003605
3606 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003607 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003609
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003610 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003613 {
3614 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003616 g_tag_at_cursor = FALSE;
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619 vim_free(buf);
3620}
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622/*
3623 * Get visually selected text, within one line only.
3624 * Returns FAIL if more than one line selected.
3625 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003627get_visual_text(
3628 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003629 char_u **pp, // return: start of selected text
3630 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632 if (VIsual_mode != 'V')
3633 unadjust_for_sel();
3634 if (VIsual.lnum != curwin->w_cursor.lnum)
3635 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003636 if (cap != NULL)
3637 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639 }
3640 if (VIsual_mode == 'V')
3641 {
3642 *pp = ml_get_curline();
3643 *lenp = (int)STRLEN(*pp);
3644 }
3645 else
3646 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003647 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
3649 *pp = ml_get_pos(&curwin->w_cursor);
3650 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3651 }
3652 else
3653 {
3654 *pp = ml_get_pos(&VIsual);
3655 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3656 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003657 if (**pp == NUL)
3658 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003659 if (*lenp > 0)
3660 {
3661 if (has_mbyte)
3662 // Correct the length to include all bytes of the last
3663 // character.
3664 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3665 else if ((*pp)[*lenp - 1] == NUL)
3666 // Do not include a trailing NUL.
3667 *lenp -= 1;
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670 reset_VIsual_and_resel();
3671 return OK;
3672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674/*
3675 * CTRL-T: backwards in tag stack
3676 */
3677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003678nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 if (!checkclearopq(cap->oap))
3681 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3682}
3683
3684/*
3685 * Handle scrolling command 'H', 'L' and 'M'.
3686 */
3687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003688nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 int used = 0;
3691 long n;
3692#ifdef FEAT_FOLDING
3693 linenr_T lnum;
3694#endif
3695 int half;
3696
3697 cap->oap->motion_type = MLINE;
3698 setpcmark();
3699
3700 if (cap->cmdchar == 'L')
3701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003702 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 curwin->w_cursor.lnum = curwin->w_botline - 1;
3704 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3705 curwin->w_cursor.lnum = 1;
3706 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003707 {
3708#ifdef FEAT_FOLDING
3709 if (hasAnyFolding(curwin))
3710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003711 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003712 for (n = cap->count1 - 1; n > 0
3713 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3714 {
3715 (void)hasFolding(curwin->w_cursor.lnum,
3716 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003717 if (curwin->w_cursor.lnum > curwin->w_topline)
3718 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003719 }
3720 }
3721 else
3722#endif
3723 curwin->w_cursor.lnum -= cap->count1 - 1;
3724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 else
3727 {
3728 if (cap->cmdchar == 'M')
3729 {
3730#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003731 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 used -= diff_check_fill(curwin, curwin->w_topline)
3733 - curwin->w_topfill;
3734#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003735 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3737 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3738 {
3739#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003740 // Count half he number of filler lines to be "below this
3741 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3743 + n) / 2 >= half)
3744 {
3745 --n;
3746 break;
3747 }
3748#endif
3749 used += plines(curwin->w_topline + n);
3750 if (used >= half)
3751 break;
3752#ifdef FEAT_FOLDING
3753 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3754 n = lnum - curwin->w_topline;
3755#endif
3756 }
3757 if (n > 0 && used > curwin->w_height)
3758 --n;
3759 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003760 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003763#ifdef FEAT_FOLDING
3764 if (hasAnyFolding(curwin))
3765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003767 lnum = curwin->w_topline;
3768 while (n-- > 0 && lnum < curwin->w_botline - 1)
3769 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003770 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003771 ++lnum;
3772 }
3773 n = lnum - curwin->w_topline;
3774 }
3775#endif
3776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 curwin->w_cursor.lnum = curwin->w_topline + n;
3778 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3779 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3780 }
3781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003782 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003783 if (cap->oap->op_type == OP_NOP)
3784 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 beginline(BL_SOL | BL_FIX);
3786}
3787
3788/*
3789 * Cursor right commands.
3790 */
3791 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003792nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003795 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003797 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003799 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003800 if (mod_mask & MOD_MASK_CTRL)
3801 cap->arg = TRUE;
3802 nv_wordcmd(cap);
3803 return;
3804 }
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 cap->oap->motion_type = MCHAR;
3807 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003808 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003810 // In virtual edit mode, there's no such thing as "past_line", as lines
3811 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003813 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 for (n = cap->count1; n > 0; --n)
3816 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003817 if ((!past_line && oneright() == FAIL)
3818 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003819 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003821 // <Space> wraps to next line if 'whichwrap' has 's'.
3822 // 'l' wraps to next line if 'whichwrap' has 'l'.
3823 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if ( ((cap->cmdchar == ' '
3825 && vim_strchr(p_ww, 's') != NULL)
3826 || (cap->cmdchar == 'l'
3827 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003828 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 && vim_strchr(p_ww, '>') != NULL))
3830 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 // When deleting we also count the NL as a character.
3833 // Set cap->oap->inclusive when last char in the line is
3834 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003835 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003837 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 cap->oap->inclusive = TRUE;
3839 else
3840 {
3841 ++curwin->w_cursor.lnum;
3842 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 curwin->w_set_curswant = TRUE;
3845 cap->oap->inclusive = FALSE;
3846 }
3847 continue;
3848 }
3849 if (cap->oap->op_type == OP_NOP)
3850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003851 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if (n == cap->count1)
3853 beep_flush();
3854 }
3855 else
3856 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003857 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 cap->oap->inclusive = TRUE;
3859 }
3860 break;
3861 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003862 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
3864 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (virtual_active())
3866 oneright();
3867 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003870 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 ++curwin->w_cursor.col;
3873 }
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876#ifdef FEAT_FOLDING
3877 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3878 && cap->oap->op_type == OP_NOP)
3879 foldOpenCursor();
3880#endif
3881}
3882
3883/*
3884 * Cursor left commands.
3885 *
3886 * Returns TRUE when operator end should not be adjusted.
3887 */
3888 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003889nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891 long n;
3892
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003893 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003895 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003896 if (mod_mask & MOD_MASK_CTRL)
3897 cap->arg = 1;
3898 nv_bck_word(cap);
3899 return;
3900 }
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 cap->oap->motion_type = MCHAR;
3903 cap->oap->inclusive = FALSE;
3904 for (n = cap->count1; n > 0; --n)
3905 {
3906 if (oneleft() == FAIL)
3907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003908 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3909 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3910 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if ( (((cap->cmdchar == K_BS
3912 || cap->cmdchar == Ctrl_H)
3913 && vim_strchr(p_ww, 'b') != NULL)
3914 || (cap->cmdchar == 'h'
3915 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003916 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 && vim_strchr(p_ww, '<') != NULL))
3918 && curwin->w_cursor.lnum > 1)
3919 {
3920 --(curwin->w_cursor.lnum);
3921 coladvance((colnr_T)MAXCOL);
3922 curwin->w_set_curswant = TRUE;
3923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003924 // When the NL before the first char has to be deleted we
3925 // put the cursor on the NUL after the previous line.
3926 // This is a very special case, be careful!
3927 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if ( (cap->oap->op_type == OP_DELETE
3929 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003930 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003932 char_u *cp = ml_get_cursor();
3933
3934 if (*cp != NUL)
3935 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003936 if (has_mbyte)
3937 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3938 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003939 ++curwin->w_cursor.col;
3940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 cap->retval |= CA_NO_ADJ_OP_END;
3942 }
3943 continue;
3944 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003945 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3947 beep_flush();
3948 break;
3949 }
3950 }
3951#ifdef FEAT_FOLDING
3952 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3953 && cap->oap->op_type == OP_NOP)
3954 foldOpenCursor();
3955#endif
3956}
3957
3958/*
3959 * Cursor up commands.
3960 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3961 */
3962 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003963nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 if (mod_mask & MOD_MASK_SHIFT)
3966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003967 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003968 cap->arg = BACKWARD;
3969 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003970 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003971 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003972
3973 cap->oap->motion_type = MLINE;
3974 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3975 clearopbeep(cap->oap);
3976 else if (cap->arg)
3977 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978}
3979
3980/*
3981 * Cursor down commands.
3982 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3983 */
3984 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003985nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987 if (mod_mask & MOD_MASK_SHIFT)
3988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003989 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003990 cap->arg = FORWARD;
3991 nv_page(cap);
3992 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003993#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003994 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003995 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
3996 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003998 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004000 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004001 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 cmdwin_result = CAR;
4003 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004004#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004005 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004006 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4007 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4008 {
4009 invoke_prompt_callback();
4010 if (restart_edit == 0)
4011 restart_edit = 'a';
4012 }
4013 else
4014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 cap->oap->motion_type = MLINE;
4017 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4018 clearopbeep(cap->oap);
4019 else if (cap->arg)
4020 beginline(BL_WHITE | BL_FIX);
4021 }
4022 }
4023}
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025/*
4026 * Grab the file name under the cursor and edit it.
4027 */
4028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004029nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
4031 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004032 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004034 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004036
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004037#ifdef FEAT_PROP_POPUP
4038 if (ERROR_IF_TERM_POPUP_WINDOW)
4039 return;
4040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004042 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044 if (ptr != NULL)
4045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004046 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004047 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004048 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004050 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004051 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004052 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004053 {
4054 curwin->w_cursor.lnum = lnum;
4055 check_cursor_lnum();
4056 beginline(BL_SOL | BL_FIX);
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 vim_free(ptr);
4059 }
4060 else
4061 clearop(cap->oap);
4062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064/*
4065 * <End> command: to end of current line or last line.
4066 */
4067 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004068nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004070 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004072 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004074 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 nv_dollar(cap);
4077}
4078
4079/*
4080 * Handle the "$" command.
4081 */
4082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004083nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
4085 cap->oap->motion_type = MCHAR;
4086 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004087 // In virtual mode when off the edge of a line and an operator
4088 // is pending (whew!) keep the cursor where it is.
4089 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (!virtual_active() || gchar_cursor() != NUL
4091 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004092 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (cursor_down((long)(cap->count1 - 1),
4094 cap->oap->op_type == OP_NOP) == FAIL)
4095 clearopbeep(cap->oap);
4096#ifdef FEAT_FOLDING
4097 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4098 foldOpenCursor();
4099#endif
4100}
4101
4102/*
4103 * Implementation of '?' and '/' commands.
4104 * If cap->arg is TRUE don't set PC mark.
4105 */
4106 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004107nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004110 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4113 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004114 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 cap->cmdchar = 'g';
4116 cap->nchar = '?';
4117 nv_operator(cap);
4118 return;
4119 }
4120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004121 // When using 'incsearch' the cursor may be moved to set a different search
4122 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004123 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125 if (cap->searchbuf == NULL)
4126 {
4127 clearop(oap);
4128 return;
4129 }
4130
Bram Moolenaar46539112015-02-17 15:43:57 +01004131 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004132 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004133 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134}
4135
LemonBoya4399382022-04-09 21:04:08 +01004136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137/*
4138 * Handle "N" and "n" commands.
4139 * cap->arg is SEARCH_REV for "N", 0 for "n".
4140 */
4141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004142nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004144 pos_T old = curwin->w_cursor;
4145 int wrapped = FALSE;
4146 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004147
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004148 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004149 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004150 // Avoid getting stuck on the current cursor position, which can
4151 // happen when an offset is given and the cursor is on the last char
4152 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004153 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004154 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004155 cap->count1 -= 1;
4156 }
LemonBoya4399382022-04-09 21:04:08 +01004157
4158#ifdef FEAT_SEARCH_EXTRA
4159 // Redraw the window to refresh the highlighted matches.
4160 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004161 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163}
4164
4165/*
4166 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4167 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004168 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004170 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004171normal_search(
4172 cmdarg_T *cap,
4173 int dir,
4174 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004175 int opt, // extra flags for do_search()
4176 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004179 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004180#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004181 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 cap->oap->motion_type = MCHAR;
4185 cap->oap->inclusive = FALSE;
4186 cap->oap->use_reg_one = TRUE;
4187 curwin->w_set_curswant = TRUE;
4188
Bram Moolenaara80faa82020-04-12 19:37:17 +02004189 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004190 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004191 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4192 if (wrapped != NULL)
4193 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (i == 0)
4195 clearop(cap->oap);
4196 else
4197 {
4198 if (i == 2)
4199 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#ifdef FEAT_FOLDING
4202 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4203 foldOpenCursor();
4204#endif
4205 }
LemonBoya4399382022-04-09 21:04:08 +01004206#ifdef FEAT_SEARCH_EXTRA
4207 // Redraw the window to refresh the highlighted matches.
4208 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004209 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004212 // "/$" will put the cursor after the end of the line, may need to
4213 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004215 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216}
4217
4218/*
4219 * Character search commands.
4220 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4221 * ',' and FALSE for ';'.
4222 * cap->nchar is NUL for ',' and ';' (repeat the search)
4223 */
4224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004225nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 int t_cmd;
4228
4229 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4230 t_cmd = TRUE;
4231 else
4232 t_cmd = FALSE;
4233
4234 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004237 clearopbeep(cap->oap);
4238 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004240
4241 curwin->w_set_curswant = TRUE;
4242 // Include a Tab for "tx" and for "dfx".
4243 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4244 && (t_cmd || cap->oap->op_type != OP_NOP))
4245 {
4246 colnr_T scol, ecol;
4247
4248 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4249 curwin->w_cursor.coladd = ecol - scol;
4250 }
4251 else
4252 curwin->w_cursor.coladd = 0;
4253 adjust_for_sel(cap);
4254#ifdef FEAT_FOLDING
4255 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4256 foldOpenCursor();
4257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258}
4259
4260/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004261 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4262 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4263 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4264 * "[m" or "]m" search for prev/next start of (Java) method.
4265 * "[M" or "]M" search for prev/next end of (Java) method.
4266 */
4267 static void
4268nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4269{
4270 pos_T new_pos = {0, 0, 0};
4271 pos_T *pos = NULL; // init for GCC
4272 pos_T prev_pos;
4273 long n;
4274 int findc;
4275 int c;
4276
4277 if (cap->nchar == '*')
4278 cap->nchar = '/';
4279 prev_pos.lnum = 0;
4280 if (cap->nchar == 'm' || cap->nchar == 'M')
4281 {
4282 if (cap->cmdchar == '[')
4283 findc = '{';
4284 else
4285 findc = '}';
4286 n = 9999;
4287 }
4288 else
4289 {
4290 findc = cap->nchar;
4291 n = cap->count1;
4292 }
4293 for ( ; n > 0; --n)
4294 {
4295 if ((pos = findmatchlimit(cap->oap, findc,
4296 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4297 {
4298 if (new_pos.lnum == 0) // nothing found
4299 {
4300 if (cap->nchar != 'm' && cap->nchar != 'M')
4301 clearopbeep(cap->oap);
4302 }
4303 else
4304 pos = &new_pos; // use last one found
4305 break;
4306 }
4307 prev_pos = new_pos;
4308 curwin->w_cursor = *pos;
4309 new_pos = *pos;
4310 }
4311 curwin->w_cursor = *old_pos;
4312
4313 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4314 // brought us to the match for "[m" and "]M" when inside a method.
4315 // Try finding the '{' or '}' we want to be at.
4316 // Also repeat for the given count.
4317 if (cap->nchar == 'm' || cap->nchar == 'M')
4318 {
4319 // norm is TRUE for "]M" and "[m"
4320 int norm = ((findc == '{') == (cap->nchar == 'm'));
4321
4322 n = cap->count1;
4323 // found a match: we were inside a method
4324 if (prev_pos.lnum != 0)
4325 {
4326 pos = &prev_pos;
4327 curwin->w_cursor = prev_pos;
4328 if (norm)
4329 --n;
4330 }
4331 else
4332 pos = NULL;
4333 while (n > 0)
4334 {
4335 for (;;)
4336 {
4337 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4338 {
4339 // if not found anything, that's an error
4340 if (pos == NULL)
4341 clearopbeep(cap->oap);
4342 n = 0;
4343 break;
4344 }
4345 c = gchar_cursor();
4346 if (c == '{' || c == '}')
4347 {
4348 // Must have found end/start of class: use it.
4349 // Or found the place to be at.
4350 if ((c == findc && norm) || (n == 1 && !norm))
4351 {
4352 new_pos = curwin->w_cursor;
4353 pos = &new_pos;
4354 n = 0;
4355 }
4356 // if no match found at all, we started outside of the
4357 // class and we're inside now. Just go on.
4358 else if (new_pos.lnum == 0)
4359 {
4360 new_pos = curwin->w_cursor;
4361 pos = &new_pos;
4362 }
4363 // found start/end of other method: go to match
4364 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004365 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4366 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004367 n = 0;
4368 else
4369 curwin->w_cursor = *pos;
4370 break;
4371 }
4372 }
4373 --n;
4374 }
4375 curwin->w_cursor = *old_pos;
4376 if (pos == NULL && new_pos.lnum != 0)
4377 clearopbeep(cap->oap);
4378 }
4379 if (pos != NULL)
4380 {
4381 setpcmark();
4382 curwin->w_cursor = *pos;
4383 curwin->w_set_curswant = TRUE;
4384#ifdef FEAT_FOLDING
4385 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4386 && cap->oap->op_type == OP_NOP)
4387 foldOpenCursor();
4388#endif
4389 }
4390}
4391
4392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 * "[" and "]" commands.
4394 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4395 */
4396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004397nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004400 pos_T *pos = NULL; // init for GCC
4401 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int flag;
4403 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 cap->oap->motion_type = MCHAR;
4406 cap->oap->inclusive = FALSE;
4407 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004408 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004410 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if (cap->nchar == 'f')
4412 nv_gotofile(cap);
4413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004416 // Find the occurrence(s) of the identifier or define under cursor
4417 // in current and included files or jump to the first occurrence.
4418 //
4419 // search list jump
4420 // fwd bwd fwd bwd fwd bwd
4421 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4422 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004423 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 char_u *ptr;
4426 int len;
4427
4428 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4429 clearop(cap->oap);
4430 else
4431 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004432 // Make a copy, if the line was changed it will be freed.
4433 ptr = vim_strnsave(ptr, len);
4434 if (ptr == NULL)
4435 return;
4436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 find_pattern_in_path(ptr, 0, len, TRUE,
4438 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4439 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4440 cap->count1,
4441 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4442 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4443 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4444 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004445 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 curwin->w_set_curswant = TRUE;
4447 }
4448 }
4449 else
4450#endif
4451
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004452 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4453 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4454 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4455 // "[m" or "]m" search for prev/next start of (Java) method.
4456 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if ( (cap->cmdchar == '['
4458 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4459 || (cap->cmdchar == ']'
4460 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004461 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004463 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 else if (cap->nchar == '[' || cap->nchar == ']')
4465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004466 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 flag = '{';
4468 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004469 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004472 // Imitate strange Vi behaviour: When using "]]" with an operator
4473 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004474 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 (cap->oap->op_type != OP_NOP
4476 && cap->arg == FORWARD && flag == '{')))
4477 clearopbeep(cap->oap);
4478 else
4479 {
4480 if (cap->oap->op_type == OP_NOP)
4481 beginline(BL_WHITE | BL_FIX);
4482#ifdef FEAT_FOLDING
4483 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4484 foldOpenCursor();
4485#endif
4486 }
4487 }
4488
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004489 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else if (cap->nchar == 'p' || cap->nchar == 'P')
4491 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004492 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004495 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 else if (cap->nchar == '\'' || cap->nchar == '`')
4497 {
4498 pos = &curwin->w_cursor;
4499 for (n = cap->count1; n > 0; --n)
4500 {
4501 prev_pos = *pos;
4502 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4503 cap->nchar == '\'');
4504 if (pos == NULL)
4505 break;
4506 }
4507 if (pos == NULL)
4508 pos = &prev_pos;
4509 nv_cursormark(cap, cap->nchar == '\'', pos);
4510 }
4511
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004512 // [ or ] followed by a middle mouse click: put selected text with
4513 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004514 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 (void)do_mouse(cap->oap, cap->nchar,
4517 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4518 cap->count1, PUT_FIXINDENT);
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
4521#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else if (cap->nchar == 'z')
4524 {
4525 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4526 cap->count1) == FAIL)
4527 clearopbeep(cap->oap);
4528 }
4529#endif
4530
4531#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004532 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else if (cap->nchar == 'c')
4534 {
4535 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4536 cap->count1) == FAIL)
4537 clearopbeep(cap->oap);
4538 }
4539#endif
4540
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004541#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004542 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004543 else if (cap->nchar == 's' || cap->nchar == 'S')
4544 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004545 setpcmark();
4546 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004547 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4548 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004549 {
4550 clearopbeep(cap->oap);
4551 break;
4552 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004553 else
4554 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004555# ifdef FEAT_FOLDING
4556 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4557 foldOpenCursor();
4558# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004559 }
4560#endif
4561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004562 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else
4564 clearopbeep(cap->oap);
4565}
4566
4567/*
4568 * Handle Normal mode "%" command.
4569 */
4570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004571nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572{
4573 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004574#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 linenr_T lnum = curwin->w_cursor.lnum;
4576#endif
4577
4578 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004579 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 if (cap->count0 > 100)
4582 clearopbeep(cap->oap);
4583 else
4584 {
4585 cap->oap->motion_type = MLINE;
4586 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004587 // Round up, so 'normal 100%' always jumps at the line line.
4588 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4589 // overflow on 32-bits, so use a formula with less accuracy
4590 // to avoid overflows.
4591 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4593 / 100L * cap->count0;
4594 else
4595 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4596 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004597 if (curwin->w_cursor.lnum < 1)
4598 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4600 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4601 beginline(BL_SOL | BL_FIX);
4602 }
4603 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004604 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 cap->oap->motion_type = MCHAR;
4607 cap->oap->use_reg_one = TRUE;
4608 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4609 clearopbeep(cap->oap);
4610 else
4611 {
4612 setpcmark();
4613 curwin->w_cursor = *pos;
4614 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 }
4619#ifdef FEAT_FOLDING
4620 if (cap->oap->op_type == OP_NOP
4621 && lnum != curwin->w_cursor.lnum
4622 && (fdo_flags & FDO_PERCENT)
4623 && KeyTyped)
4624 foldOpenCursor();
4625#endif
4626}
4627
4628/*
4629 * Handle "(" and ")" commands.
4630 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4631 */
4632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004633nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 cap->oap->motion_type = MCHAR;
4636 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004638 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 curwin->w_set_curswant = TRUE;
4640
4641 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004643 clearopbeep(cap->oap);
4644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004646
4647 // Don't leave the cursor on the NUL past end of line.
4648 adjust_cursor(cap->oap);
4649 curwin->w_cursor.coladd = 0;
4650#ifdef FEAT_FOLDING
4651 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4652 foldOpenCursor();
4653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654}
4655
4656/*
4657 * "m" command: Mark a position.
4658 */
4659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004660nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004662 if (checkclearop(cap->oap))
4663 return;
4664
4665 if (setmark(cap->nchar) == FAIL)
4666 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667}
4668
4669/*
4670 * "{" and "}" commands.
4671 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4672 */
4673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004674nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 cap->oap->motion_type = MCHAR;
4677 cap->oap->inclusive = FALSE;
4678 cap->oap->use_reg_one = TRUE;
4679 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004680 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004682 clearopbeep(cap->oap);
4683 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004685
4686 curwin->w_cursor.coladd = 0;
4687#ifdef FEAT_FOLDING
4688 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4689 foldOpenCursor();
4690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
4693/*
4694 * "u" command: Undo or make lower case.
4695 */
4696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004697nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004699 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004701 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 cap->cmdchar = 'g';
4703 cap->nchar = 'u';
4704 nv_operator(cap);
4705 }
4706 else
4707 nv_kundo(cap);
4708}
4709
4710/*
4711 * <Undo> command.
4712 */
4713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004714nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004716 if (checkclearopq(cap->oap))
4717 return;
4718
Bram Moolenaarf2732452018-06-03 14:47:35 +02004719#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004720 if (bt_prompt(curbuf))
4721 {
4722 clearopbeep(cap->oap);
4723 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004725#endif
4726 u_undo((int)cap->count1);
4727 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728}
4729
4730/*
4731 * Handle the "r" command.
4732 */
4733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004734nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 char_u *ptr;
4737 int had_ctrl_v;
4738 long n;
4739
4740 if (checkclearop(cap->oap))
4741 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004742#ifdef FEAT_JOB_CHANNEL
4743 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4744 {
4745 clearopbeep(cap->oap);
4746 return;
4747 }
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004750 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (cap->nchar == Ctrl_V)
4752 {
4753 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004754 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004755 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if (cap->nchar > DEL)
4757 had_ctrl_v = NUL;
4758 }
4759 else
4760 had_ctrl_v = NUL;
4761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004762 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004763 if (IS_SPECIAL(cap->nchar))
4764 {
4765 clearopbeep(cap->oap);
4766 return;
4767 }
4768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (VIsual_active)
4771 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004772 if (got_int)
4773 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004774 if (had_ctrl_v)
4775 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004776 // Use a special (negative) number to make a difference between a
4777 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004778 if (cap->nchar == CAR)
4779 cap->nchar = REPLACE_CR_NCHAR;
4780 else if (cap->nchar == NL)
4781 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 nv_operator(cap);
4784 return;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (virtual_active())
4789 {
4790 if (u_save_cursor() == FAIL)
4791 return;
4792 if (gchar_cursor() == NUL)
4793 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 coladvance_force((colnr_T)(getviscol() + cap->count1));
4796 curwin->w_cursor.col -= cap->count1;
4797 }
4798 else if (gchar_cursor() == TAB)
4799 coladvance_force(getviscol());
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004804 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004805 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
4807 clearopbeep(cap->oap);
4808 return;
4809 }
4810
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004811 // Replacing with a TAB is done by edit() when it is complicated because
4812 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4813 // Other characters are done below to avoid problems with things like
4814 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4816 {
4817 stuffnumReadbuff(cap->count1);
4818 stuffcharReadbuff('R');
4819 stuffcharReadbuff('\t');
4820 stuffcharReadbuff(ESC);
4821 return;
4822 }
4823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004824 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if (u_save_cursor() == FAIL)
4826 return;
4827
4828 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4829 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004830 // Replace character(s) by a single newline.
4831 // Strange vi behaviour: Only one newline is inserted.
4832 // Delete the characters here.
4833 // Insert the newline with an insert command, takes care of
4834 // autoindent. The insert command depends on being on the last
4835 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 stuffcharReadbuff('\r');
4838 stuffcharReadbuff(ESC);
4839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004840 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 invoke_edit(cap, TRUE, 'r', FALSE);
4842 }
4843 else
4844 {
4845 prep_redo(cap->oap->regname, cap->count1,
4846 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4847
4848 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (has_mbyte)
4850 {
4851 int old_State = State;
4852
4853 if (cap->ncharC1 != 0)
4854 AppendCharToRedobuff(cap->ncharC1);
4855 if (cap->ncharC2 != 0)
4856 AppendCharToRedobuff(cap->ncharC2);
4857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 // This is slow, but it handles replacing a single-byte with a
4859 // multi-byte and the other way around. Also handles adding
4860 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 for (n = cap->count1; n > 0; --n)
4862 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004863 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004864 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4865 {
4866 int c = ins_copychar(curwin->w_cursor.lnum
4867 + (cap->nchar == Ctrl_Y ? -1 : 1));
4868 if (c != NUL)
4869 ins_char(c);
4870 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004872 ++curwin->w_cursor.col;
4873 }
4874 else
4875 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 State = old_State;
4877 if (cap->ncharC1 != 0)
4878 ins_char(cap->ncharC1);
4879 if (cap->ncharC2 != 0)
4880 ins_char(cap->ncharC2);
4881 }
4882 }
4883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004885 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 for (n = cap->count1; n > 0; --n)
4887 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004888 // Get ptr again, because u_save and/or showmatch() will have
4889 // released the line. This may also happen in ins_copychar().
4890 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004891 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4892 {
4893 int c = ins_copychar(curwin->w_cursor.lnum
4894 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004895
4896 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004897 if (c != NUL)
4898 ptr[curwin->w_cursor.col] = c;
4899 }
4900 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004901 {
4902 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004903 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (p_sm && msg_silent == 0)
4906 showmatch(cap->nchar);
4907 ++curwin->w_cursor.col;
4908 }
4909#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004910 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004912 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaar009b2592004-10-24 19:18:58 +00004914 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004915 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004917 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919#endif
4920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004921 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 changed_bytes(curwin->w_cursor.lnum,
4923 (colnr_T)(curwin->w_cursor.col - cap->count1));
4924 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004925 --curwin->w_cursor.col; // cursor on the last replaced char
4926 // if the character on the left of the current cursor is a multi-byte
4927 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (has_mbyte)
4929 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 curbuf->b_op_end = curwin->w_cursor;
4931 curwin->w_set_curswant = TRUE;
4932 set_last_insert(cap->nchar);
4933 }
4934}
4935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936/*
4937 * 'o': Exchange start and end of Visual area.
4938 * 'O': same, but in block mode exchange left and right corners.
4939 */
4940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004941v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942{
4943 pos_T old_cursor;
4944 colnr_T left, right;
4945
4946 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4947 {
4948 old_cursor = curwin->w_cursor;
4949 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4950 curwin->w_cursor.lnum = VIsual.lnum;
4951 coladvance(left);
4952 VIsual = curwin->w_cursor;
4953
4954 curwin->w_cursor.lnum = old_cursor.lnum;
4955 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004956 // 'selection "exclusive" and cursor at right-bottom corner: move it
4957 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4959 ++curwin->w_curswant;
4960 coladvance(curwin->w_curswant);
4961 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004963 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
4965 curwin->w_cursor.lnum = VIsual.lnum;
4966 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4967 ++right;
4968 coladvance(right);
4969 VIsual = curwin->w_cursor;
4970
4971 curwin->w_cursor.lnum = old_cursor.lnum;
4972 coladvance(left);
4973 curwin->w_curswant = left;
4974 }
4975 }
4976 else
4977 {
4978 old_cursor = curwin->w_cursor;
4979 curwin->w_cursor = VIsual;
4980 VIsual = old_cursor;
4981 curwin->w_set_curswant = TRUE;
4982 }
4983}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985/*
4986 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4987 */
4988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004991 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 cap->cmdchar = 'c';
4994 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004995 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 VIsual_mode = 'V';
4997 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004998 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005000
5001 if (checkclearopq(cap->oap))
5002 return;
5003
5004 if (!curbuf->b_p_ma)
5005 emsg(_(e_cannot_make_changes_modifiable_is_off));
5006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005008 if (virtual_active())
5009 coladvance(getviscol());
5010 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012}
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014/*
5015 * "gr".
5016 */
5017 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005018nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (VIsual_active)
5021 {
5022 cap->cmdchar = 'r';
5023 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005024 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005025 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005027
zeertzjq4f026ea2023-02-26 14:47:24 +00005028 if (checkclearopq(cap->oap) || cap->extra_char == ESC)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005029 return;
5030
5031 if (!curbuf->b_p_ma)
5032 emsg(_(e_cannot_make_changes_modifiable_is_off));
5033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005035 if (cap->extra_char == Ctrl_V) // get another character
5036 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005037 if (cap->extra_char < ' ')
5038 // Prefix a control character with CTRL-V to avoid it being used as
5039 // a command.
5040 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005041 stuffcharReadbuff(cap->extra_char);
5042 stuffcharReadbuff(ESC);
5043 if (virtual_active())
5044 coladvance(getviscol());
5045 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049/*
5050 * Swap case for "~" command, when it does not work like an operator.
5051 */
5052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005053n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 long n;
5056 pos_T startpos;
5057 int did_change = 0;
5058#ifdef FEAT_NETBEANS_INTG
5059 pos_T pos;
5060 char_u *ptr;
5061 int count;
5062#endif
5063
5064 if (checkclearopq(cap->oap))
5065 return;
5066
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005067 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 clearopbeep(cap->oap);
5070 return;
5071 }
5072
5073 prep_redo_cmd(cap);
5074
5075 if (u_save_cursor() == FAIL)
5076 return;
5077
5078 startpos = curwin->w_cursor;
5079#ifdef FEAT_NETBEANS_INTG
5080 pos = startpos;
5081#endif
5082 for (n = cap->count1; n > 0; --n)
5083 {
5084 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5085 inc_cursor();
5086 if (gchar_cursor() == NUL)
5087 {
5088 if (vim_strchr(p_ww, '~') != NULL
5089 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5090 {
5091#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005092 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 if (did_change)
5095 {
5096 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005097 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005098 netbeans_removed(curbuf, pos.lnum, pos.col,
5099 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005100 // line may have been flushed, get it again
5101 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005103 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 pos.col = 0;
5106 pos.lnum++;
5107 }
5108#endif
5109 ++curwin->w_cursor.lnum;
5110 curwin->w_cursor.col = 0;
5111 if (n > 1)
5112 {
5113 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5114 break;
5115 u_clearline();
5116 }
5117 }
5118 else
5119 break;
5120 }
5121 }
5122#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005123 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 ptr = ml_get(pos.lnum);
5126 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005127 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5128 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130#endif
5131
5132
5133 check_cursor();
5134 curwin->w_set_curswant = TRUE;
5135 if (did_change)
5136 {
5137 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5138 0L);
5139 curbuf->b_op_start = startpos;
5140 curbuf->b_op_end = curwin->w_cursor;
5141 if (curbuf->b_op_end.col > 0)
5142 --curbuf->b_op_end.col;
5143 }
5144}
5145
5146/*
5147 * Move cursor to mark.
5148 */
5149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005150nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 if (check_mark(pos) == FAIL)
5153 clearop(cap->oap);
5154 else
5155 {
5156 if (cap->cmdchar == '\''
5157 || cap->cmdchar == '`'
5158 || cap->cmdchar == '['
5159 || cap->cmdchar == ']')
5160 setpcmark();
5161 curwin->w_cursor = *pos;
5162 if (flag)
5163 beginline(BL_WHITE | BL_FIX);
5164 else
5165 check_cursor();
5166 }
5167 cap->oap->motion_type = flag ? MLINE : MCHAR;
5168 if (cap->cmdchar == '`')
5169 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 curwin->w_set_curswant = TRUE;
5172}
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Handle commands that are operators in Visual mode.
5176 */
5177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005182 // Uppercase means linewise, except in block mode, then "D" deletes till
5183 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (isupper(cap->cmdchar))
5185 {
5186 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005187 {
5188 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5192 curwin->w_curswant = MAXCOL;
5193 }
5194 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5195 nv_operator(cap);
5196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198/*
5199 * "s" and "S" commands.
5200 */
5201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005204#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005205 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005206 if (term_swap_diff() == OK)
5207 return;
5208#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005209#ifdef FEAT_JOB_CHANNEL
5210 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5211 {
5212 clearopbeep(cap->oap);
5213 return;
5214 }
5215#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005216 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005219 {
5220 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 cap->cmdchar = 'c';
5224 nv_operator(cap);
5225 }
5226 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 nv_optrans(cap);
5228}
5229
5230/*
5231 * Abbreviated commands.
5232 */
5233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005234nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005239 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (VIsual_active)
5241 v_visop(cap);
5242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 nv_optrans(cap);
5244}
5245
5246/*
5247 * Translate a command into another command.
5248 */
5249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5253 (char_u *)"d$", (char_u *)"c$",
5254 (char_u *)"cl", (char_u *)"cc",
5255 (char_u *)"yy", (char_u *)":s\r"};
5256 static char_u *str = (char_u *)"xXDCsSY&";
5257
5258 if (!checkclearopq(cap->oap))
5259 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005260 // In Vi "2D" doesn't delete the next line. Can't translate it
5261 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005262 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5263 {
5264 cap->oap->start = curwin->w_cursor;
5265 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005266#ifdef FEAT_EVAL
5267 set_op_var(OP_DELETE);
5268#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005269 cap->count1 = 1;
5270 nv_dollar(cap);
5271 finish_op = TRUE;
5272 ResetRedobuff();
5273 AppendCharToRedobuff('D');
5274 }
5275 else
5276 {
5277 if (cap->count0)
5278 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005279 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 cap->opcount = 0;
5283}
5284
5285/*
5286 * "'" and "`" commands. Also for "g'" and "g`".
5287 * cap->arg is TRUE for "'" and "g'".
5288 */
5289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005290nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 pos_T *pos;
5293 int c;
5294#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005295 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005296 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#endif
5298
5299 if (cap->cmdchar == 'g')
5300 c = cap->extra_char;
5301 else
5302 c = cap->nchar;
5303 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005304 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
5306 if (cap->arg)
5307 {
5308 check_cursor_lnum();
5309 beginline(BL_WHITE | BL_FIX);
5310 }
5311 else
5312 check_cursor();
5313 }
5314 else
5315 nv_cursormark(cap, cap->arg, pos);
5316
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005317 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (!virtual_active())
5319 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005320 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#ifdef FEAT_FOLDING
5322 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005323 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005324 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 && (fdo_flags & FDO_MARK)
5326 && old_KeyTyped)
5327 foldOpenCursor();
5328#endif
5329}
5330
5331/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005332 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 */
5334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005335nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005338#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005343 if (checkclearopq(cap->oap))
5344 return;
5345
5346 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005348 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005350 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005352 if (cap->cmdchar == 'g')
5353 pos = movechangelist((int)cap->count1);
5354 else
5355 pos = movemark((int)cap->count1);
5356 if (pos == (pos_T *)-1) // jump to other file
5357 {
5358 curwin->w_set_curswant = TRUE;
5359 check_cursor();
5360 }
5361 else if (pos != NULL) // can jump
5362 nv_cursormark(cap, FALSE, pos);
5363 else if (cap->cmdchar == 'g')
5364 {
5365 if (curbuf->b_changelistlen == 0)
5366 emsg(_(e_changelist_is_empty));
5367 else if (cap->count1 < 0)
5368 emsg(_(e_at_start_of_changelist));
5369 else
5370 emsg(_(e_at_end_of_changelist));
5371 }
5372 else
5373 clearopbeep(cap->oap);
5374# ifdef FEAT_FOLDING
5375 if (cap->oap->op_type == OP_NOP
5376 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5377 && (fdo_flags & FDO_MARK)
5378 && old_KeyTyped)
5379 foldOpenCursor();
5380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381}
5382
5383/*
5384 * Handle '"' command.
5385 */
5386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 if (checkclearop(cap->oap))
5390 return;
5391#ifdef FEAT_EVAL
5392 if (cap->nchar == '=')
5393 cap->nchar = get_expr_register();
5394#endif
5395 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5396 {
5397 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005398 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#ifdef FEAT_EVAL
5400 set_reg_var(cap->oap->regname);
5401#endif
5402 }
5403 else
5404 clearopbeep(cap->oap);
5405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Handle "v", "V" and "CTRL-V" commands.
5409 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5410 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005411 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 */
5413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005414nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005416 if (cap->cmdchar == Ctrl_Q)
5417 cap->cmdchar = Ctrl_V;
5418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5420 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (cap->oap->op_type != OP_NOP)
5422 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005423 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005424 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 return;
5426 }
5427
5428 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005431 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 else // toggle char/block mode
5434 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 VIsual_mode = cap->cmdchar;
5436 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005437 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005439 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005444 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 VIsual = curwin->w_cursor;
5448
5449 VIsual_active = TRUE;
5450 VIsual_reselect = TRUE;
5451 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005455 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005457 // For V and ^V, we multiply the number of lines even if there
5458 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5460 {
5461 curwin->w_cursor.lnum +=
5462 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005463 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 VIsual_mode = resel_VIsual_mode;
5466 if (VIsual_mode == 'v')
5467 {
5468 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005469 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005470 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005471 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5472 if (*p_sel != 'e')
5473 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005476 curwin->w_curswant = resel_VIsual_vcol;
5477 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005479 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
5481 curwin->w_curswant = MAXCOL;
5482 coladvance((colnr_T)MAXCOL);
5483 }
5484 else if (VIsual_mode == Ctrl_V)
5485 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005486 // Update curswant on the original line, that is where "col" is
5487 // valid.
5488 linenr_T lnum = curwin->w_cursor.lnum;
5489 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005490 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005491 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5492 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 coladvance(curwin->w_curswant);
5494 }
5495 else
5496 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005497 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499 else
5500 {
5501 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005502 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 may_start_select('c');
5504 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005505 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005506 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005507 if (cap->count0 > 0 && --cap->count1 > 0)
5508 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005509 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005510 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5511 nv_right(cap);
5512 else if (VIsual_mode == 'V')
5513 nv_down(cap);
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516 }
5517}
5518
5519/*
5520 * Start selection for Shift-movement keys.
5521 */
5522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005525 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 may_start_select('k');
5527 n_start_visual_mode('v');
5528}
5529
5530/*
5531 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005532 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 */
5534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005535may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005537 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5538 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541/*
5542 * Start Visual mode "c".
5543 * Should set VIsual_select before calling this.
5544 */
5545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005546n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005548#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005549 int cursor_line_was_concealed = curwin->w_p_cole > 0
5550 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005551#endif
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 VIsual_mode = c;
5554 VIsual_active = TRUE;
5555 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005556
5557 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005558 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005559 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005560 {
5561 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 VIsual = curwin->w_cursor;
5565
5566#ifdef FEAT_FOLDING
5567 foldAdjustVisual();
5568#endif
5569
LemonBoy2bf52dd2022-04-09 18:17:34 +01005570 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005572#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005573 // Check if redraw is needed after changing the state.
5574 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005575#endif
5576
Bram Moolenaar09df3122006-01-23 22:23:09 +00005577 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005580 // Make sure the clipboard gets updated. Needed because start and
5581 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 clip_star.vmode = NUL;
5583#endif
5584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005585 // Only need to redraw this line, unless still need to redraw an old
5586 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005587 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
5589 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5590 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5591 }
5592}
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595/*
5596 * CTRL-W: Window commands
5597 */
5598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005599nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005601 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005602 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005604 cap->cmdchar = ':';
5605 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005606 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005607 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005608 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005609 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610}
5611
5612/*
5613 * CTRL-Z: Suspend
5614 */
5615 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005616nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005621 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623
5624/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005625 * "gv": Reselect the previous Visual area. If Visual already active,
5626 * exchange previous and current Visual area.
5627 */
5628 static void
5629nv_gv_cmd(cmdarg_T *cap)
5630{
5631 pos_T tpos;
5632 int i;
5633
5634 if (checkclearop(cap->oap))
5635 return;
5636
5637 if (curbuf->b_visual.vi_start.lnum == 0
5638 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5639 || curbuf->b_visual.vi_end.lnum == 0)
5640 {
5641 beep_flush();
5642 return;
5643 }
5644
5645 // set w_cursor to the start of the Visual area, tpos to the end
5646 if (VIsual_active)
5647 {
5648 i = VIsual_mode;
5649 VIsual_mode = curbuf->b_visual.vi_mode;
5650 curbuf->b_visual.vi_mode = i;
5651# ifdef FEAT_EVAL
5652 curbuf->b_visual_mode_eval = i;
5653# endif
5654 i = curwin->w_curswant;
5655 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5656 curbuf->b_visual.vi_curswant = i;
5657
5658 tpos = curbuf->b_visual.vi_end;
5659 curbuf->b_visual.vi_end = curwin->w_cursor;
5660 curwin->w_cursor = curbuf->b_visual.vi_start;
5661 curbuf->b_visual.vi_start = VIsual;
5662 }
5663 else
5664 {
5665 VIsual_mode = curbuf->b_visual.vi_mode;
5666 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5667 tpos = curbuf->b_visual.vi_end;
5668 curwin->w_cursor = curbuf->b_visual.vi_start;
5669 }
5670
5671 VIsual_active = TRUE;
5672 VIsual_reselect = TRUE;
5673
5674 // Set Visual to the start and w_cursor to the end of the Visual
5675 // area. Make sure they are on an existing character.
5676 check_cursor();
5677 VIsual = curwin->w_cursor;
5678 curwin->w_cursor = tpos;
5679 check_cursor();
5680 update_topline();
5681
5682 // When called from normal "g" command: start Select mode when
5683 // 'selectmode' contains "cmd". When called for K_SELECT, always
5684 // start Select mode.
5685 if (cap->arg)
5686 {
5687 VIsual_select = TRUE;
5688 VIsual_select_reg = 0;
5689 }
5690 else
5691 may_start_select('c');
5692 setmouse();
5693#ifdef FEAT_CLIPBOARD
5694 // Make sure the clipboard gets updated. Needed because start and
5695 // end are still the same, and the selection needs to be owned
5696 clip_star.vmode = NUL;
5697#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005698 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005699 showmode();
5700}
5701
5702/*
5703 * "g0", "g^" : Like "0" and "^" but for screen lines.
5704 * "gm": middle of "g0" and "g$".
5705 */
5706 static void
5707nv_g_home_m_cmd(cmdarg_T *cap)
5708{
5709 int i;
5710 int flag = FALSE;
5711
5712 if (cap->nchar == '^')
5713 flag = TRUE;
5714
5715 cap->oap->motion_type = MCHAR;
5716 cap->oap->inclusive = FALSE;
5717 if (curwin->w_p_wrap && curwin->w_width != 0)
5718 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005719 int width1 = curwin->w_width - curwin_col_off();
5720 int width2 = width1 + curwin_col_off2();
5721 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005722
5723 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005724 virtcol = curwin->w_virtcol
5725#ifdef FEAT_PROP_POPUP
5726 - curwin->w_virtcol_first_char
5727#endif
5728 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005729 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005730 if (virtcol >= (colnr_T)width1 && width2 > 0)
5731 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005732 }
5733 else
5734 i = curwin->w_leftcol;
5735 // Go to the middle of the screen line. When 'number' or
5736 // 'relativenumber' is on and lines are wrapping the middle can be more
5737 // to the left.
5738 if (cap->nchar == 'm')
5739 i += (curwin->w_width - curwin_col_off()
5740 + ((curwin->w_p_wrap && i > 0)
5741 ? curwin_col_off2() : 0)) / 2;
5742 coladvance((colnr_T)i);
5743 if (flag)
5744 {
5745 do
5746 i = gchar_cursor();
5747 while (VIM_ISWHITE(i) && oneright() == OK);
5748 curwin->w_valid &= ~VALID_WCOL;
5749 }
5750 curwin->w_set_curswant = TRUE;
5751}
5752
5753/*
5754 * "g_": to the last non-blank character in the line or <count> lines
5755 * downward.
5756 */
5757 static void
5758nv_g_underscore_cmd(cmdarg_T *cap)
5759{
5760 char_u *ptr;
5761
5762 cap->oap->motion_type = MCHAR;
5763 cap->oap->inclusive = TRUE;
5764 curwin->w_curswant = MAXCOL;
5765 if (cursor_down((long)(cap->count1 - 1),
5766 cap->oap->op_type == OP_NOP) == FAIL)
5767 {
5768 clearopbeep(cap->oap);
5769 return;
5770 }
5771
5772 ptr = ml_get_curline();
5773
5774 // In Visual mode we may end up after the line.
5775 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5776 --curwin->w_cursor.col;
5777
5778 // Decrease the cursor column until it's on a non-blank.
5779 while (curwin->w_cursor.col > 0
5780 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5781 --curwin->w_cursor.col;
5782 curwin->w_set_curswant = TRUE;
5783 adjust_for_sel(cap);
5784}
5785
5786/*
5787 * "g$" : Like "$" but for screen lines.
5788 */
5789 static void
5790nv_g_dollar_cmd(cmdarg_T *cap)
5791{
5792 oparg_T *oap = cap->oap;
5793 int i;
5794 int col_off = curwin_col_off();
5795
5796 oap->motion_type = MCHAR;
5797 oap->inclusive = TRUE;
5798 if (curwin->w_p_wrap && curwin->w_width != 0)
5799 {
5800 curwin->w_curswant = MAXCOL; // so we stay at the end
5801 if (cap->count1 == 1)
5802 {
5803 int width1 = curwin->w_width - col_off;
5804 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005805 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005806
5807 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005808 virtcol = curwin->w_virtcol
5809#ifdef FEAT_PROP_POPUP
5810 - curwin->w_virtcol_first_char
5811#endif
5812 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005813 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005814 if (virtcol >= (colnr_T)width1)
5815 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005816 * width2;
5817 coladvance((colnr_T)i);
5818
5819 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005820 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005821 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5822 {
5823 // Check for landing on a character that got split at
5824 // the end of the line. We do not want to advance to
5825 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005826 if (curwin->w_virtcol
5827#ifdef FEAT_PROP_POPUP
5828 - curwin->w_virtcol_first_char
5829#endif
5830 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005831 --curwin->w_cursor.col;
5832 }
5833 }
5834 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5835 clearopbeep(oap);
5836 }
5837 else
5838 {
5839 if (cap->count1 > 1)
5840 // if it fails, let the cursor still move to the last char
5841 (void)cursor_down(cap->count1 - 1, FALSE);
5842
5843 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5844 coladvance((colnr_T)i);
5845
5846 // if the character doesn't fit move one back
5847 if (curwin->w_cursor.col > 0
5848 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5849 {
5850 colnr_T vcol;
5851
5852 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5853 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5854 --curwin->w_cursor.col;
5855 }
5856
5857 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005858 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005859 }
5860}
5861
5862/*
5863 * "gi": start Insert at the last position.
5864 */
5865 static void
5866nv_gi_cmd(cmdarg_T *cap)
5867{
5868 int i;
5869
5870 if (curbuf->b_last_insert.lnum != 0)
5871 {
5872 curwin->w_cursor = curbuf->b_last_insert;
5873 check_cursor_lnum();
5874 i = (int)STRLEN(ml_get_curline());
5875 if (curwin->w_cursor.col > (colnr_T)i)
5876 {
5877 if (virtual_active())
5878 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5879 curwin->w_cursor.col = i;
5880 }
5881 }
5882 cap->cmdchar = 'i';
5883 nv_edit(cap);
5884}
5885
5886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 * Commands starting with "g".
5888 */
5889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005890nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 switch (cap->nchar)
5896 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005897 case Ctrl_A:
5898 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005900 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005901 if (!VIsual_active && cap->nchar == Ctrl_A)
5902 vim_mem_profile_dump();
5903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005905 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005906 if (VIsual_active)
5907 {
5908 cap->arg = TRUE;
5909 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005910 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005911 nv_addsub(cap);
5912 }
5913 else
5914 clearopbeep(oap);
5915 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005917 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 case 'R':
5919 cap->arg = TRUE;
5920 nv_Replace(cap);
5921 break;
5922
5923 case 'r':
5924 nv_vreplace(cap);
5925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 case '&':
5928 do_cmdline_cmd((char_u *)"%s//~/&");
5929 break;
5930
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005931 // "gv": Reselect the previous Visual area. If Visual already active,
5932 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005934 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005936
5937 // "gV": Don't reselect the previous Visual area after a Select mode
5938 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case 'V':
5940 VIsual_reselect = FALSE;
5941 break;
5942
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005943 // "gh": start Select mode.
5944 // "gH": start Select line mode.
5945 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case K_BS:
5947 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005948 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case 'h':
5950 case 'H':
5951 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 cap->cmdchar = cap->nchar + ('v' - 'h');
5953 cap->arg = TRUE;
5954 nv_visual(cap);
5955 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005957 // "gn", "gN" visually select next/previous search match
5958 // "gn" selects next match
5959 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005960 case 'N':
5961 case 'n':
5962 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005963 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005964 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005966 // "gj" and "gk" two new funny movement keys -- up and down
5967 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 case 'j':
5969 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005970 // with 'nowrap' it works just like the normal "j" command.
5971 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 oap->motion_type = MLINE;
5974 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5975 }
5976 else
5977 i = nv_screengo(oap, FORWARD, cap->count1);
5978 if (i == FAIL)
5979 clearopbeep(oap);
5980 break;
5981
5982 case 'k':
5983 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005984 // with 'nowrap' it works just like the normal "k" command.
5985 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 oap->motion_type = MLINE;
5988 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5989 }
5990 else
5991 i = nv_screengo(oap, BACKWARD, cap->count1);
5992 if (i == FAIL)
5993 clearopbeep(oap);
5994 break;
5995
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005996 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 case 'J':
5998 nv_join(cap);
5999 break;
6000
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006001 // "g0", "g^" : Like "0" and "^" but for screen lines.
6002 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 case '0':
6005 case 'm':
6006 case K_HOME:
6007 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 break;
6010
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006011 case 'M':
6012 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006013 oap->motion_type = MCHAR;
6014 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006015 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006016 if (cap->count0 > 0 && cap->count0 <= 100)
6017 coladvance((colnr_T)(i * cap->count0 / 100));
6018 else
6019 coladvance((colnr_T)(i / 2));
6020 curwin->w_set_curswant = TRUE;
6021 }
6022 break;
6023
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006024 // "g_": to the last non-blank character in the line or <count> lines
6025 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006027 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 case '$':
6032 case K_END:
6033 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 break;
6036
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 case '*':
6039 case '#':
6040#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006041 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006043 case Ctrl_RSB: // :tag or :tselect for current identifier
6044 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 nv_ident(cap);
6046 break;
6047
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 case 'e':
6050 case 'E':
6051 oap->motion_type = MCHAR;
6052 curwin->w_set_curswant = TRUE;
6053 oap->inclusive = TRUE;
6054 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6055 clearopbeep(oap);
6056 break;
6057
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006060 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 break;
6062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006065 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 break;
6067
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006068 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 case 'I':
6070 beginline(0);
6071 if (!checkclearopq(oap))
6072 invoke_edit(cap, FALSE, 'g', FALSE);
6073 break;
6074
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 // "gf": goto file, edit file under cursor
6076 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006078 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 nv_gotofile(cap);
6080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 case '\'':
6084 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006085 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 case '`':
6087 nv_gomark(cap);
6088 break;
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006092 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 break;
6094
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006095 // "ga": Display the ascii value of the character under the
6096 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 case 'a':
6098 do_ascii(NULL);
6099 break;
6100
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006101 // "g8": Display the bytes used for the UTF-8 character under the
6102 // cursor. It is displayed in hex.
6103 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006105 if (cap->count0 == 8)
6106 utf_find_illegal();
6107 else
6108 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006111 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006112 case '<':
6113 show_sb_text();
6114 break;
6115
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006116 // "gg": Goto the first line in file. With a count it goes to
6117 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 case 'g':
6119 cap->arg = FALSE;
6120 nv_goto(cap);
6121 break;
6122
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006123 // Two-character operators:
6124 // "gq" Format text
6125 // "gw" Format text and keep cursor position
6126 // "g~" Toggle the case of the text.
6127 // "gu" Change text to lower case.
6128 // "gU" Change text to upper case.
6129 // "g?" rot13 encoding
6130 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 case 'q':
6132 case 'w':
6133 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 case '~':
6136 case 'u':
6137 case 'U':
6138 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006139 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 nv_operator(cap);
6141 break;
6142
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006143 // "gd": Find first occurrence of pattern under the cursor in the
6144 // current function
6145 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case 'd':
6147 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006148 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 break;
6150
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006151 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 case K_MIDDLEMOUSE:
6153 case K_MIDDLEDRAG:
6154 case K_MIDDLERELEASE:
6155 case K_LEFTMOUSE:
6156 case K_LEFTDRAG:
6157 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006158 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 case K_RIGHTMOUSE:
6160 case K_RIGHTDRAG:
6161 case K_RIGHTRELEASE:
6162 case K_X1MOUSE:
6163 case K_X1DRAG:
6164 case K_X1RELEASE:
6165 case K_X2MOUSE:
6166 case K_X2DRAG:
6167 case K_X2RELEASE:
6168 mod_mask = MOD_MASK_CTRL;
6169 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
6172 case K_IGNORE:
6173 break;
6174
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006175 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 case 'p':
6177 case 'P':
6178 nv_put(cap);
6179 break;
6180
6181#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006182 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 case 'o':
6184 goto_byte(cap->count0);
6185 break;
6186#endif
6187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006190 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 do_exmode(TRUE);
6192 break;
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 case ',':
6195 nv_pcmark(cap);
6196 break;
6197
6198 case ';':
6199 cap->count1 = -cap->count1;
6200 nv_pcmark(cap);
6201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006204 if (!checkclearop(oap))
6205 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006207 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006208 if (!checkclearop(oap))
6209 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006210 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211
Bram Moolenaar62a23252020-08-09 14:04:42 +02006212 case TAB:
6213 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6214 clearopbeep(oap);
6215 break;
6216
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006217 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006219 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006220 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006221 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006222 break;
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 default:
6225 clearopbeep(oap);
6226 break;
6227 }
6228}
6229
6230/*
6231 * Handle "o" and "O" commands.
6232 */
6233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006236#ifdef FEAT_CONCEAL
6237 linenr_T oldline = curwin->w_cursor.lnum;
6238#endif
6239
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006240 if (checkclearopq(cap->oap))
6241 return;
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006244 if (cap->cmdchar == 'O')
6245 // Open above the first line of a folded sequence of lines
6246 (void)hasFolding(curwin->w_cursor.lnum,
6247 &curwin->w_cursor.lnum, NULL);
6248 else
6249 // Open below the last line of a folded sequence of lines
6250 (void)hasFolding(curwin->w_cursor.lnum,
6251 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006253 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6254 (cap->cmdchar == 'O' ? 1 : 0)),
6255 (linenr_T)(curwin->w_cursor.lnum +
6256 (cap->cmdchar == 'o' ? 1 : 0))
6257 ) == OK
6258 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6259 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6260 0, NULL) == OK)
6261 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006262#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006263 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6264 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006265#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006266#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006267 if (curwin->w_p_cul)
6268 // force redraw of cursorline
6269 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006270#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006271 // When '#' is in 'cpoptions' ignore the count.
6272 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6273 cap->count1 = 1;
6274 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
6276}
6277
6278/*
6279 * "." command: redo last change.
6280 */
6281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006282nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006284 if (checkclearopq(cap->oap))
6285 return;
6286
6287 // If "restart_edit" is TRUE, the last but one command is repeated
6288 // instead of the last command (inserting text). This is used for
6289 // CTRL-O <.> in insert mode.
6290 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6291 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292}
6293
6294/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006295 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 */
6297 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006298nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006300 if (VIsual_select && VIsual_active)
6301 {
6302 int reg;
6303 // Get register name
6304 ++no_mapping;
6305 ++allow_keys;
6306 reg = plain_vgetc();
6307 LANGMAP_ADJUST(reg, TRUE);
6308 --no_mapping;
6309 --allow_keys;
6310
6311 if (reg == '"')
6312 // the unnamed register is 0
6313 reg = 0;
6314
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006315 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006316 return;
6317 }
6318
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006319 if (checkclearopq(cap->oap))
6320 return;
6321
6322 u_redo((int)cap->count1);
6323 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324}
6325
6326/*
6327 * Handle "U" command.
6328 */
6329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006330nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006332 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006333 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006335 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 cap->cmdchar = 'g';
6337 cap->nchar = 'U';
6338 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006339 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006341
6342 if (checkclearopq(cap->oap))
6343 return;
6344
6345 u_undoline();
6346 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347}
6348
6349/*
6350 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6351 * single character.
6352 */
6353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006354nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006356 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006357 {
6358#ifdef FEAT_JOB_CHANNEL
6359 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6360 {
6361 clearopbeep(cap->oap);
6362 return;
6363 }
6364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 else
6368 nv_operator(cap);
6369}
6370
6371/*
6372 * Handle an operator command.
6373 * The actual work is done by do_pending_operator().
6374 */
6375 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006376nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 int op_type;
6379
6380 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006381#ifdef FEAT_JOB_CHANNEL
6382 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6383 {
6384 clearopbeep(cap->oap);
6385 return;
6386 }
6387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006389 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 nv_lineop(cap);
6391 else if (!checkclearop(cap->oap))
6392 {
6393 cap->oap->start = curwin->w_cursor;
6394 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006395#ifdef FEAT_EVAL
6396 set_op_var(op_type);
6397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399}
6400
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006401#ifdef FEAT_EVAL
6402/*
6403 * Set v:operator to the characters for "optype".
6404 */
6405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006406set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006407{
6408 char_u opchars[3];
6409
6410 if (optype == OP_NOP)
6411 set_vim_var_string(VV_OP, NULL, 0);
6412 else
6413 {
6414 opchars[0] = get_op_char(optype);
6415 opchars[1] = get_extra_op_char(optype);
6416 opchars[2] = NUL;
6417 set_vim_var_string(VV_OP, opchars, -1);
6418 }
6419}
6420#endif
6421
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422/*
6423 * Handle linewise operator "dd", "yy", etc.
6424 *
6425 * "_" is is a strange motion command that helps make operators more logical.
6426 * It is actually implemented, but not documented in the real Vi. This motion
6427 * command actually refers to "the current line". Commands like "dd" and "yy"
6428 * are really an alternate form of "d_" and "y_". It does accept a count, so
6429 * "d3_" works to delete 3 lines.
6430 */
6431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 cap->oap->motion_type = MLINE;
6435 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6436 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006437 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006438 && cap->oap->motion_force != 'v'
6439 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 || cap->oap->op_type == OP_LSHIFT
6441 || cap->oap->op_type == OP_RSHIFT)
6442 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 beginline(BL_WHITE | BL_FIX);
6445}
6446
6447/*
6448 * <Home> command.
6449 */
6450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006451nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006453 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006454 if (mod_mask & MOD_MASK_CTRL)
6455 nv_goto(cap);
6456 else
6457 {
6458 cap->count0 = 1;
6459 nv_pipe(cap);
6460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006461 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6462 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
6465/*
6466 * "|" command.
6467 */
6468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006469nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 cap->oap->motion_type = MCHAR;
6472 cap->oap->inclusive = FALSE;
6473 beginline(0);
6474 if (cap->count0 > 0)
6475 {
6476 coladvance((colnr_T)(cap->count0 - 1));
6477 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6478 }
6479 else
6480 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006481 // keep curswant at the column where we wanted to go, not where
6482 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 curwin->w_set_curswant = FALSE;
6484}
6485
6486/*
6487 * Handle back-word command "b" and "B".
6488 * cap->arg is 1 for "B"
6489 */
6490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006491nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 cap->oap->motion_type = MCHAR;
6494 cap->oap->inclusive = FALSE;
6495 curwin->w_set_curswant = TRUE;
6496 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6497 clearopbeep(cap->oap);
6498#ifdef FEAT_FOLDING
6499 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6500 foldOpenCursor();
6501#endif
6502}
6503
6504/*
6505 * Handle word motion commands "e", "E", "w" and "W".
6506 * cap->arg is TRUE for "E" and "W".
6507 */
6508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 int n;
6512 int word_end;
6513 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006514 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006516 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6518 word_end = TRUE;
6519 else
6520 word_end = FALSE;
6521 cap->oap->inclusive = word_end;
6522
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006523 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (!word_end && cap->oap->op_type == OP_CHANGE)
6525 {
6526 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006527 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006529 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006531 // Reproduce a funny Vi behaviour: "cw" on a blank only
6532 // changes one character, not all blanks until the start of
6533 // the next word. Only do this when the 'w' flag is included
6534 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6536 {
6537 cap->oap->inclusive = TRUE;
6538 cap->oap->motion_type = MCHAR;
6539 return;
6540 }
6541 }
6542 else
6543 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006544 // This is a little strange. To match what the real Vi does,
6545 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6546 // that we are not on a space or a TAB. This seems impolite
6547 // at first, but it's really more what we mean when we say
6548 // 'cw'.
6549 // Another strangeness: When standing on the end of a word
6550 // "ce" will change until the end of the next word, but "cw"
6551 // will change only one character! This is done by setting
6552 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 cap->oap->inclusive = TRUE;
6554 word_end = TRUE;
6555 flag = TRUE;
6556 }
6557 }
6558 }
6559
6560 cap->oap->motion_type = MCHAR;
6561 curwin->w_set_curswant = TRUE;
6562 if (word_end)
6563 n = end_word(cap->count1, cap->arg, flag, FALSE);
6564 else
6565 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006567 // Don't leave the cursor on the NUL past the end of line. Unless we
6568 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006569 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006570 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 if (n == FAIL && cap->oap->op_type == OP_NOP)
6573 clearopbeep(cap->oap);
6574 else
6575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#ifdef FEAT_FOLDING
6578 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6579 foldOpenCursor();
6580#endif
6581 }
6582}
6583
6584/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006585 * Used after a movement command: If the cursor ends up on the NUL after the
6586 * end of the line, may move it back to the last character and make the motion
6587 * inclusive.
6588 */
6589 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006590adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006592 // The cursor cannot remain on the NUL when:
6593 // - the column is > 0
6594 // - not in Visual mode or 'selection' is "o"
6595 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006596 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006598 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006599 {
6600 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 if (has_mbyte)
6603 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006604 oap->inclusive = TRUE;
6605 }
6606}
6607
6608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 * "0" and "^" commands.
6610 * cap->arg is the argument for beginline().
6611 */
6612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 cap->oap->motion_type = MCHAR;
6616 cap->oap->inclusive = FALSE;
6617 beginline(cap->arg);
6618#ifdef FEAT_FOLDING
6619 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6620 foldOpenCursor();
6621#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006622 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6623 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624}
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626/*
6627 * In exclusive Visual mode, may include the last character.
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006633 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (has_mbyte)
6636 inc_cursor();
6637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 ++curwin->w_cursor.col;
6639 cap->oap->inclusive = FALSE;
6640 }
6641}
6642
6643/*
6644 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6645 * Should check VIsual_mode before calling this.
6646 * Returns TRUE when backed up to the previous line.
6647 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006649unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 pos_T *pp;
6652
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006653 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006655 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 pp = &curwin->w_cursor;
6657 else
6658 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (pp->coladd > 0)
6660 --pp->coladd;
6661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (pp->col > 0)
6663 {
6664 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006665 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667 else if (pp->lnum > 1)
6668 {
6669 --pp->lnum;
6670 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6671 return TRUE;
6672 }
6673 }
6674 return FALSE;
6675}
6676
6677/*
6678 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6679 */
6680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006681nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006686 VIsual_select_reg = 0;
6687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 else if (VIsual_reselect)
6689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006690 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 cap->arg = TRUE;
6692 nv_g_cmd(cap);
6693 }
6694}
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697/*
6698 * "G", "gg", CTRL-END, CTRL-HOME.
6699 * cap->arg is TRUE for "G".
6700 */
6701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 linenr_T lnum;
6705
6706 if (cap->arg)
6707 lnum = curbuf->b_ml.ml_line_count;
6708 else
6709 lnum = 1L;
6710 cap->oap->motion_type = MLINE;
6711 setpcmark();
6712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (cap->count0 != 0)
6715 lnum = cap->count0;
6716 if (lnum < 1L)
6717 lnum = 1L;
6718 else if (lnum > curbuf->b_ml.ml_line_count)
6719 lnum = curbuf->b_ml.ml_line_count;
6720 curwin->w_cursor.lnum = lnum;
6721 beginline(BL_SOL | BL_FIX);
6722#ifdef FEAT_FOLDING
6723 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6724 foldOpenCursor();
6725#endif
6726}
6727
6728/*
6729 * CTRL-\ in Normal mode.
6730 */
6731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006732nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6735 {
6736 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006737 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006738 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (cmdwin_type != 0)
6741 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (VIsual_active)
6743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006745 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006747 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (cap->nchar == Ctrl_G && p_im)
6749 restart_edit = 'a';
6750 }
6751 else
6752 clearopbeep(cap->oap);
6753}
6754
6755/*
6756 * ESC in Normal mode: beep, but don't flush buffers.
6757 * Don't even beep if we are canceling a command.
6758 */
6759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006760nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 int no_reason;
6763
6764 no_reason = (cap->oap->op_type == OP_NOP
6765 && cap->opcount == 0
6766 && cap->count0 == 0
6767 && cap->oap->regname == 0
6768 && !p_im);
6769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006770 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006772 if (restart_edit == 0 && cmdwin_type == 0
6773 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006774 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006775 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6776
Bram Moolenaar5939c352022-09-24 12:50:45 +01006777 // The user may accidentally do "vim file | grep word" and then
6778 // CTRL-C doesn't show anything. With a changed buffer give the
6779 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006780 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006781 {
6782 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6783
6784 if (out_redir)
6785 mch_errmsg(ms);
6786 else
6787 msg(ms);
6788 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006789 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006790 {
6791 if (out_redir)
6792 {
6793 got_int = FALSE;
6794 do_cmdline_cmd((char_u *)"qa");
6795 }
6796 else
6797 msg(_("Type :qa and press <Enter> to exit Vim"));
6798 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006801 if (restart_edit != 0)
6802 redraw_mode = TRUE; // remove "-- (insert) --"
6803
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006804 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6805 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (!p_im)
6807 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (cmdwin_type != 0)
6809 {
6810 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006811 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 return;
6813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006815 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006816 {
6817 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006818 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6819 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006820 cmdwin_result = K_IGNORE;
6821 return;
6822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (VIsual_active)
6825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006826 end_visual_mode(); // stop Visual
6827 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006829 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006831 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006832 {
6833#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006834 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006835 popup_hide_message_win();
6836 else
6837#endif
6838 vim_beep(BO_ESC);
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 clearop(cap->oap);
6841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006842 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6843 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006844 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 restart_edit = 'a';
6846}
6847
6848/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006849 * Move the cursor for the "A" command.
6850 */
6851 void
6852set_cursor_for_append_to_line(void)
6853{
6854 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006855 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006856 {
6857 int save_State = State;
6858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006859 // Pretend Insert mode here to allow the cursor on the
6860 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006861 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006862 coladvance((colnr_T)MAXCOL);
6863 State = save_State;
6864 }
6865 else
6866 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6867}
6868
6869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006871 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 */
6873 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006874nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006876 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6878 cap->cmdchar = 'i';
6879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006880 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006882 {
6883#ifdef FEAT_TERMINAL
6884 if (term_in_normal_mode())
6885 {
6886 end_visual_mode();
6887 clearop(cap->oap);
6888 term_enter_job_mode();
6889 return;
6890 }
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006895 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006896 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6897 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006901#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006902 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006903 {
6904 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006905 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006906 return;
6907 }
6908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 else if (!curbuf->b_p_ma && !p_im)
6910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006911 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006912 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006914 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006915 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006916 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006918 else if (cap->cmdchar == K_PS && VIsual_active)
6919 {
6920 pos_T old_pos = curwin->w_cursor;
6921 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006922 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006924 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006925 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6926 {
6927 shift_delete_registers();
6928 cap->oap->regname = '1';
6929 }
6930 else
6931 cap->oap->regname = '-';
6932 cap->cmdchar = 'd';
6933 cap->nchar = NUL;
6934 nv_operator(cap);
6935 do_pending_operator(cap, 0, FALSE);
6936 cap->cmdchar = K_PS;
6937
zeertzjq7a732522022-03-14 20:46:41 +00006938 if (*ml_get_cursor() != NUL)
6939 {
6940 if (old_visual_mode == 'V')
6941 {
6942 // In linewise Visual mode insert before the beginning of the
6943 // next line.
6944 // When the last line in the buffer was deleted then create a
6945 // new line, otherwise there is not need to move cursor.
6946 // Detect this by checking if cursor moved above Visual area.
6947 if (curwin->w_cursor.lnum < old_pos.lnum
6948 && curwin->w_cursor.lnum < old_visual.lnum)
6949 {
6950 if (u_save_cursor() == OK)
6951 {
6952 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6953 FALSE);
6954 appended_lines(curwin->w_cursor.lnum++, 1L);
6955 }
6956 }
6957 }
6958 // When the last char in the line was deleted then append.
6959 // Detect this by checking if cursor moved before Visual area.
6960 else if (curwin->w_cursor.col < old_pos.col
6961 && curwin->w_cursor.col < old_visual.col)
6962 inc_cursor();
6963 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006965 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006966 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 else if (!checkclearopq(cap->oap))
6969 {
6970 switch (cap->cmdchar)
6971 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006973 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 break;
6975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006976 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006977 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6978 beginline(BL_WHITE);
6979 else
6980 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 break;
6982
Bram Moolenaara1891842017-02-04 21:34:31 +01006983 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006984 // Bracketed paste works like "a"ppend, unless the cursor is in
6985 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006986 if (curwin->w_cursor.col == 0)
6987 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006990 case 'a': // "a"ppend is like "i"nsert on the next character.
6991 // increment coladd when in virtual space, increment the
6992 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (virtual_active()
6994 && (curwin->w_cursor.coladd > 0
6995 || *ml_get_cursor() == NUL
6996 || *ml_get_cursor() == TAB))
6997 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006998 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 inc_cursor();
7000 break;
7001 }
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7004 {
7005 int save_State = State;
7006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 // Pretend Insert mode here to allow the cursor on the
7008 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007009 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 coladvance(getviscol());
7011 State = save_State;
7012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
7014 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7015 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007016 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007018 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019}
7020
7021/*
7022 * Invoke edit() and take care of "restart_edit" and the return value.
7023 */
7024 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007025invoke_edit(
7026 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007027 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007028 int cmd,
7029 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 int restart_edit_save = 0;
7032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007033 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7034 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7035 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 if (repl || !stuff_empty())
7037 restart_edit_save = restart_edit;
7038 else
7039 restart_edit_save = 0;
7040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007041 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 restart_edit = 0;
7043
7044 if (edit(cmd, startln, cap->count1))
7045 cap->retval |= CA_COMMAND_BUSY;
7046
7047 if (restart_edit == 0)
7048 restart_edit = restart_edit_save;
7049}
7050
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051/*
7052 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7053 */
7054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007055nv_object(
7056 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058 int flag;
7059 int include;
7060 char_u *mps_save;
7061
7062 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007063 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007065 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 mps_save = curbuf->b_p_mps;
7069 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7070
7071 switch (cap->nchar)
7072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 flag = current_word(cap->oap, cap->count1, include, FALSE);
7075 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 flag = current_word(cap->oap, cap->count1, include, TRUE);
7078 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 case '(':
7081 case ')':
7082 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7083 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 case '{':
7086 case '}':
7087 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7088 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 case ']':
7091 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7092 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 case '>':
7095 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7096 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007097#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007098 case 't': // "at" = a tag block (xml and html)
7099 // Do not adjust oap->end in do_pending_operator()
7100 // otherwise there are different results for 'dit'
7101 // (note leading whitespace in last line):
7102 // 1) <b> 2) <b>
7103 // foobar foobar
7104 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007105 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007106 flag = current_tagblock(cap->oap, cap->count1, include);
7107 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007108#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 flag = current_par(cap->oap, cap->count1, include, 'p');
7111 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 flag = current_sent(cap->oap, cap->count1, include);
7114 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007115 case '"': // "a"" = a double quoted string
7116 case '\'': // "a'" = a single quoted string
7117 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007118 flag = current_quote(cap->oap, cap->count1, include,
7119 cap->nchar);
7120 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121#if 0 // TODO
7122 case 'S': // "aS" = a section
7123 case 'f': // "af" = a filename
7124 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#endif
7126 default:
7127 flag = FAIL;
7128 break;
7129 }
7130
7131 curbuf->b_p_mps = mps_save;
7132 if (flag == FAIL)
7133 clearopbeep(cap->oap);
7134 adjust_cursor_col();
7135 curwin->w_set_curswant = TRUE;
7136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138/*
7139 * "q" command: Start/stop recording.
7140 * "q:", "q/", "q?": edit command-line in command-line window.
7141 */
7142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007143nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 if (cap->oap->op_type == OP_FORMAT)
7146 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007147 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 cap->cmdchar = 'g';
7149 cap->nchar = 'q';
7150 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007151 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007153
7154 if (checkclearop(cap->oap))
7155 return;
7156
7157 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007159 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007161 emsg(_(e_cmdline_window_already_open));
7162 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007164 stuffcharReadbuff(cap->nchar);
7165 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007167 else
7168 // (stop) recording into a named register, unless executing a
7169 // register
7170 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7171 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172}
7173
7174/*
7175 * Handle the "@r" command.
7176 */
7177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007178nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 if (checkclearop(cap->oap))
7181 return;
7182#ifdef FEAT_EVAL
7183 if (cap->nchar == '=')
7184 {
7185 if (get_expr_register() == NUL)
7186 return;
7187 }
7188#endif
7189 while (cap->count1-- && !got_int)
7190 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007191 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
7193 clearopbeep(cap->oap);
7194 break;
7195 }
7196 line_breakcheck();
7197 }
7198}
7199
7200/*
7201 * Handle the CTRL-U and CTRL-D commands.
7202 */
7203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007204nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
7206 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7207 || (cap->cmdchar == Ctrl_D
7208 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7209 clearopbeep(cap->oap);
7210 else if (!checkclearop(cap->oap))
7211 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7212}
7213
7214/*
7215 * Handle "J" or "gJ" command.
7216 */
7217 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007218nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007222 nv_operator(cap);
7223 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007225
7226 if (checkclearop(cap->oap))
7227 return;
7228
7229 if (cap->count0 <= 1)
7230 cap->count0 = 2; // default for join is two lines!
7231 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7232 curbuf->b_ml.ml_line_count)
7233 {
7234 // can't join when on the last line
7235 if (cap->count0 <= 2)
7236 {
7237 clearopbeep(cap->oap);
7238 return;
7239 }
7240 cap->count0 = curbuf->b_ml.ml_line_count
7241 - curwin->w_cursor.lnum + 1;
7242 }
7243
7244 prep_redo(cap->oap->regname, cap->count0,
7245 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7246 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247}
7248
7249/*
7250 * "P", "gP", "p" and "gp" commands.
7251 */
7252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007253nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007255 nv_put_opt(cap, FALSE);
7256}
7257
7258/*
7259 * "P", "gP", "p" and "gp" commands.
7260 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7261 */
7262 static void
7263nv_put_opt(cmdarg_T *cap, int fix_indent)
7264{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 int regname = 0;
7266 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007267 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007268 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 int dir;
7270 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007271 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273 if (cap->oap->op_type != OP_NOP)
7274 {
7275#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007276 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7278 {
7279 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007280 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282 else
7283#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007284 clearopbeep(cap->oap);
7285 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007287
Bram Moolenaarf2732452018-06-03 14:47:35 +02007288#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007289 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007290 {
7291 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007292 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007293 }
7294#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007295
7296 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007298 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7299 ? FORWARD : BACKWARD;
7300 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007302 else
7303 dir = (cap->cmdchar == 'P'
7304 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7305 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7306 prep_redo_cmd(cap);
7307 if (cap->cmdchar == 'g')
7308 flags |= PUT_CURSEND;
7309 else if (cap->cmdchar == 'z')
7310 flags |= PUT_BLOCK_INNER;
7311
7312 if (VIsual_active)
7313 {
7314 // Putting in Visual mode: The put text replaces the selected
7315 // text. First delete the selected text, then put the new text.
7316 // Need to save and restore the registers that the delete
7317 // overwrites if the old contents is being put.
7318 was_visual = TRUE;
7319 regname = cap->oap->regname;
7320 keep_registers = cap->cmdchar == 'P';
7321#ifdef FEAT_CLIPBOARD
7322 adjust_clip_reg(&regname);
7323#endif
7324 if (regname == 0 || regname == '"'
7325 || VIM_ISDIGIT(regname) || regname == '-'
7326#ifdef FEAT_CLIPBOARD
7327 || (clip_unnamed && (regname == '*' || regname == '+'))
7328#endif
7329
7330 )
7331 {
7332 // The delete is going to overwrite the register we want to
7333 // put, save it first.
7334 reg1 = get_register(regname, TRUE);
7335 }
7336
7337 // Now delete the selected text. Avoid messages here.
7338 cap->cmdchar = 'd';
7339 cap->nchar = NUL;
7340 cap->oap->regname = keep_registers ? '_' : NUL;
7341 ++msg_silent;
7342 nv_operator(cap);
7343 do_pending_operator(cap, 0, FALSE);
7344 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7345 --msg_silent;
7346
7347 // delete PUT_LINE_BACKWARD;
7348 cap->oap->regname = regname;
7349
7350 if (reg1 != NULL)
7351 {
7352 // Delete probably changed the register we want to put, save
7353 // it first. Then put back what was there before the delete.
7354 reg2 = get_register(regname, FALSE);
7355 put_register(regname, reg1);
7356 }
7357
7358 // When deleted a linewise Visual area, put the register as
7359 // lines to avoid it joined with the next line. When deletion was
7360 // characterwise, split a line when putting lines.
7361 if (VIsual_mode == 'V')
7362 flags |= PUT_LINE;
7363 else if (VIsual_mode == 'v')
7364 flags |= PUT_LINE_SPLIT;
7365 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7366 flags |= PUT_LINE_FORWARD;
7367 dir = BACKWARD;
7368 if ((VIsual_mode != 'V'
7369 && curwin->w_cursor.col < curbuf->b_op_start.col)
7370 || (VIsual_mode == 'V'
7371 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7372 // cursor is at the end of the line or end of file, put
7373 // forward.
7374 dir = FORWARD;
7375 // May have been reset in do_put().
7376 VIsual_active = TRUE;
7377 }
7378 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7379
7380 // If a register was saved, put it back now.
7381 if (reg2 != NULL)
7382 put_register(regname, reg2);
7383
7384 // What to reselect with "gv"? Selecting the just put text seems to
7385 // be the most useful, since the original text was removed.
7386 if (was_visual)
7387 {
7388 curbuf->b_visual.vi_start = curbuf->b_op_start;
7389 curbuf->b_visual.vi_end = curbuf->b_op_end;
7390 // need to adjust cursor position
7391 if (*p_sel == 'e')
7392 inc(&curbuf->b_visual.vi_end);
7393 }
7394
7395 // When all lines were selected and deleted do_put() leaves an empty
7396 // line that needs to be deleted now.
7397 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7398 {
7399 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7400 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7401
7402 // If the cursor was in that line, move it to the end of the last
7403 // line.
7404 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7405 {
7406 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7407 coladvance((colnr_T)MAXCOL);
7408 }
7409 }
7410 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411}
7412
7413/*
7414 * "o" and "O" commands.
7415 */
7416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007417nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
7419#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007420 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7422 {
7423 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007424 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426 else
7427#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007428 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007430#ifdef FEAT_JOB_CHANNEL
7431 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007432 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 n_opencmd(cap);
7436}
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438#ifdef FEAT_NETBEANS_INTG
7439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007440nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 netbeans_keycommand(cap->nchar);
7443}
7444#endif
7445
7446#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007450 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451}
7452#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007453
Bram Moolenaar3918c952005-03-15 22:34:55 +00007454/*
7455 * Trigger CursorHold event.
7456 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7457 * input buffer. "did_cursorhold" is set to avoid retriggering.
7458 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007460nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007461{
7462 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7463 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007464 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007465}