blob: fd6218472eab16a6766f2a62bb1ad3f0b8c2b919 [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 Moolenaar071d4272004-06-13 20:20:40 +000023#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
26
27/*
28 * nv_*(): functions called to handle Normal and Visual mode commands.
29 * n_*(): functions called to handle Normal mode commands.
30 * v_*(): functions called to handle Visual mode commands.
31 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static void nv_ignore(cmdarg_T *cap);
33static void nv_nop(cmdarg_T *cap);
34static void nv_error(cmdarg_T *cap);
35static void nv_help(cmdarg_T *cap);
36static void nv_addsub(cmdarg_T *cap);
37static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_ver_scrollbar(cmdarg_T *cap);
41static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000043#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void nv_tabline(cmdarg_T *cap);
45static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000046#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010047static void nv_exmode(cmdarg_T *cap);
48static void nv_colon(cmdarg_T *cap);
49static void nv_ctrlg(cmdarg_T *cap);
50static void nv_ctrlh(cmdarg_T *cap);
51static void nv_clear(cmdarg_T *cap);
52static void nv_ctrlo(cmdarg_T *cap);
53static void nv_hat(cmdarg_T *cap);
54static void nv_Zet(cmdarg_T *cap);
55static void nv_ident(cmdarg_T *cap);
56static void nv_tagpop(cmdarg_T *cap);
57static void nv_scroll(cmdarg_T *cap);
58static void nv_right(cmdarg_T *cap);
59static void nv_left(cmdarg_T *cap);
60static void nv_up(cmdarg_T *cap);
61static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void nv_end(cmdarg_T *cap);
63static void nv_dollar(cmdarg_T *cap);
64static void nv_search(cmdarg_T *cap);
65static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020066static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010067static void nv_csearch(cmdarg_T *cap);
68static void nv_brackets(cmdarg_T *cap);
69static void nv_percent(cmdarg_T *cap);
70static void nv_brace(cmdarg_T *cap);
71static void nv_mark(cmdarg_T *cap);
72static void nv_findpar(cmdarg_T *cap);
73static void nv_undo(cmdarg_T *cap);
74static void nv_kundo(cmdarg_T *cap);
75static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010077static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
78static void v_visop(cmdarg_T *cap);
79static void nv_subst(cmdarg_T *cap);
80static void nv_abbrev(cmdarg_T *cap);
81static void nv_optrans(cmdarg_T *cap);
82static void nv_gomark(cmdarg_T *cap);
83static void nv_pcmark(cmdarg_T *cap);
84static void nv_regname(cmdarg_T *cap);
85static void nv_visual(cmdarg_T *cap);
86static void n_start_visual_mode(int c);
87static void nv_window(cmdarg_T *cap);
88static void nv_suspend(cmdarg_T *cap);
89static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000091static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_Undo(cmdarg_T *cap);
93static void nv_tilde(cmdarg_T *cap);
94static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_lineop(cmdarg_T *cap);
99static void nv_home(cmdarg_T *cap);
100static void nv_pipe(cmdarg_T *cap);
101static void nv_bck_word(cmdarg_T *cap);
102static void nv_wordcmd(cmdarg_T *cap);
103static void nv_beginline(cmdarg_T *cap);
104static void adjust_cursor(oparg_T *oap);
105static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_select(cmdarg_T *cap);
107static void nv_goto(cmdarg_T *cap);
108static void nv_normal(cmdarg_T *cap);
109static void nv_esc(cmdarg_T *oap);
110static void nv_edit(cmdarg_T *cap);
111static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_record(cmdarg_T *cap);
116static void nv_at(cmdarg_T *cap);
117static void nv_halfpage(cmdarg_T *cap);
118static void nv_join(cmdarg_T *cap);
119static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200120static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
ichizok672776d2022-01-31 12:27:18 +0000130// Declare nv_cmds[].
131#define DO_DECLARE_NVCMD
132#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000134// Include the lookuptable generated by create_nvcmdidx.vim.
135#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
138 * Search for a command in the commands table.
139 * Returns -1 for invalid command.
140 */
141 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100142find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
144 int i;
145 int idx;
146 int top, bot;
147 int c;
148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 if (cmdchar >= 0x100)
151 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100153 // We use the absolute value of the character. Special keys have a
154 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (cmdchar < 0)
156 cmdchar = -cmdchar;
157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100158 // If the character is in the first part: The character is the index into
159 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (cmdchar <= nv_max_linear)
161 return nv_cmd_idx[cmdchar];
162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 bot = nv_max_linear + 1;
165 top = NV_CMDS_SIZE - 1;
166 idx = -1;
167 while (bot <= top)
168 {
169 i = (top + bot) / 2;
170 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
171 if (c < 0)
172 c = -c;
173 if (cmdchar == c)
174 {
175 idx = nv_cmd_idx[i];
176 break;
177 }
178 if (cmdchar > c)
179 bot = i + 1;
180 else
181 top = i - 1;
182 }
183 return idx;
184}
185
186/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000187 * Handle the count before a normal command and set cap->count0.
188 */
189 static int
190normal_cmd_get_count(
191 cmdarg_T *cap,
192 int c,
193 int toplevel UNUSED,
194 int set_prevcount UNUSED,
195 int *ctrl_w,
196 int *need_flushbuf UNUSED)
197{
198getcount:
199 if (!(VIsual_active && VIsual_select))
200 {
201 // Handle a count before a command and compute ca.count0.
202 // Note that '0' is a command and not the start of a count, but it's
203 // part of a count after other digits.
204 while ((c >= '1' && c <= '9')
205 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
206 || c == '0')))
207 {
208 if (c == K_DEL || c == K_KDEL)
209 {
210 cap->count0 /= 10;
211#ifdef FEAT_CMDL_INFO
212 del_from_showcmd(4); // delete the digit and ~@%
213#endif
214 }
215 else if (cap->count0 > 99999999L)
216 {
217 cap->count0 = 999999999L;
218 }
219 else
220 {
221 cap->count0 = cap->count0 * 10 + (c - '0');
222 }
223#ifdef FEAT_EVAL
224 // Set v:count here, when called from main() and not a stuffed
225 // command, so that v:count can be used in an expression mapping
226 // right after the count. Do set it for redo.
227 if (toplevel && readbuf1_empty())
228 set_vcount_ca(cap, &set_prevcount);
229#endif
230 if (*ctrl_w)
231 {
232 ++no_mapping;
233 ++allow_keys; // no mapping for nchar, but keys
234 }
235 ++no_zero_mapping; // don't map zero here
236 c = plain_vgetc();
237 LANGMAP_ADJUST(c, TRUE);
238 --no_zero_mapping;
239 if (*ctrl_w)
240 {
241 --no_mapping;
242 --allow_keys;
243 }
244#ifdef FEAT_CMDL_INFO
245 *need_flushbuf |= add_to_showcmd(c);
246#endif
247 }
248
249 // If we got CTRL-W there may be a/another count
250 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
251 {
252 *ctrl_w = TRUE;
253 cap->opcount = cap->count0; // remember first count
254 cap->count0 = 0;
255 ++no_mapping;
256 ++allow_keys; // no mapping for nchar, but keys
257 c = plain_vgetc(); // get next character
258 LANGMAP_ADJUST(c, TRUE);
259 --no_mapping;
260 --allow_keys;
261#ifdef FEAT_CMDL_INFO
262 *need_flushbuf |= add_to_showcmd(c);
263#endif
264 goto getcount; // jump back
265 }
266 }
267
268 if (c == K_CURSORHOLD)
269 {
270 // Save the count values so that ca.opcount and ca.count0 are exactly
271 // the same when coming back here after handling K_CURSORHOLD.
272 cap->oap->prev_opcount = cap->opcount;
273 cap->oap->prev_count0 = cap->count0;
274 }
275 else if (cap->opcount != 0)
276 {
277 // If we're in the middle of an operator (including after entering a
278 // yank buffer with '"') AND we had a count before the operator, then
279 // that count overrides the current value of ca.count0.
280 // What this means effectively, is that commands like "3dw" get turned
281 // into "d3w" which makes things fall into place pretty neatly.
282 // If you give a count before AND after the operator, they are
283 // multiplied.
284 if (cap->count0)
285 {
286 if (cap->opcount >= 999999999L / cap->count0)
287 cap->count0 = 999999999L;
288 else
289 cap->count0 *= cap->opcount;
290 }
291 else
292 cap->count0 = cap->opcount;
293 }
294
295 // Always remember the count. It will be set to zero (on the next call,
296 // above) when there is no pending operator.
297 // When called from main(), save the count for use by the "count" built-in
298 // variable.
299 cap->opcount = cap->count0;
300 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
301
302#ifdef FEAT_EVAL
303 // Only set v:count when called from main() and not a stuffed command.
304 // Do set it for redo.
305 if (toplevel && readbuf1_empty())
306 set_vcount(cap->count0, cap->count1, set_prevcount);
307#endif
308
309 return c;
310}
311
312/*
313 * Returns TRUE if the normal command (cap) needs a second character.
314 */
315 static int
316normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
317{
318 return ((cmd_flags & NV_NCH)
319 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
320 && cap->oap->op_type == OP_NOP)
321 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
322 || (cap->cmdchar == 'q'
323 && cap->oap->op_type == OP_NOP
324 && reg_recording == 0
325 && reg_executing == 0)
326 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
327 && (cap->oap->op_type != OP_NOP || VIsual_active))));
328}
329
330/*
331 * Get one or more additional characters for a normal command.
332 * Return the updated command index (if changed).
333 */
334 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000335normal_cmd_get_more_chars(
336 int idx_arg,
337 cmdarg_T *cap,
338 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000339{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000340 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000341 int c;
342 int *cp;
343 int repl = FALSE; // get character for replace mode
344 int lit = FALSE; // get extra character literally
345 int langmap_active = FALSE; // using :lmap mappings
346 int lang; // getting a text character
347#ifdef HAVE_INPUT_METHOD
348 int save_smd; // saved value of p_smd
349#endif
350
351 ++no_mapping;
352 ++allow_keys; // no mapping for nchar, but allow key codes
353 // Don't generate a CursorHold event here, most commands can't handle
354 // it, e.g., nv_replace(), nv_csearch().
355 did_cursorhold = TRUE;
356 if (cap->cmdchar == 'g')
357 {
358 /*
359 * For 'g' get the next character now, so that we can check for
360 * "gr", "g'" and "g`".
361 */
362 cap->nchar = plain_vgetc();
363 LANGMAP_ADJUST(cap->nchar, TRUE);
364#ifdef FEAT_CMDL_INFO
365 *need_flushbuf |= add_to_showcmd(cap->nchar);
366#endif
367 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
368 || cap->nchar == Ctrl_BSL)
369 {
370 cp = &cap->extra_char; // need to get a third character
371 if (cap->nchar != 'r')
372 lit = TRUE; // get it literally
373 else
374 repl = TRUE; // get it in replace mode
375 }
376 else
377 cp = NULL; // no third character needed
378 }
379 else
380 {
381 if (cap->cmdchar == 'r') // get it in replace mode
382 repl = TRUE;
383 cp = &cap->nchar;
384 }
385 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
386
387 /*
388 * Get a second or third character.
389 */
390 if (cp != NULL)
391 {
392 if (repl)
393 {
394 State = REPLACE; // pretend Replace mode
395#ifdef CURSOR_SHAPE
396 ui_cursor_shape(); // show different cursor shape
397#endif
398 }
399 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
400 {
401 // Allow mappings defined with ":lmap".
402 --no_mapping;
403 --allow_keys;
404 if (repl)
405 State = LREPLACE;
406 else
407 State = LANGMAP;
408 langmap_active = TRUE;
409 }
410#ifdef HAVE_INPUT_METHOD
411 save_smd = p_smd;
412 p_smd = FALSE; // Don't let the IM code show the mode here
413 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
414 im_set_active(TRUE);
415#endif
416 if ((State & INSERT) && !p_ek)
417 {
418#ifdef FEAT_JOB_CHANNEL
419 ch_log_output = TRUE;
420#endif
421 // Disable bracketed paste and modifyOtherKeys here, we won't
422 // recognize the escape sequences with 'esckeys' off.
423 out_str(T_BD);
424 out_str(T_CTE);
425 }
426
427 *cp = plain_vgetc();
428
429 if ((State & INSERT) && !p_ek)
430 {
431#ifdef FEAT_JOB_CHANNEL
432 ch_log_output = TRUE;
433#endif
434 // Re-enable bracketed paste mode and modifyOtherKeys
435 out_str(T_BE);
436 out_str(T_CTI);
437 }
438
439 if (langmap_active)
440 {
441 // Undo the decrement done above
442 ++no_mapping;
443 ++allow_keys;
444 State = NORMAL_BUSY;
445 }
446#ifdef HAVE_INPUT_METHOD
447 if (lang)
448 {
449 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
450 im_save_status(&curbuf->b_p_iminsert);
451 im_set_active(FALSE);
452 }
453 p_smd = save_smd;
454#endif
455 State = NORMAL_BUSY;
456#ifdef FEAT_CMDL_INFO
457 *need_flushbuf |= add_to_showcmd(*cp);
458#endif
459
460 if (!lit)
461 {
462#ifdef FEAT_DIGRAPHS
463 // Typing CTRL-K gets a digraph.
464 if (*cp == Ctrl_K
465 && ((nv_cmds[idx].cmd_flags & NV_LANG)
466 || cp == &cap->extra_char)
467 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
468 {
469 c = get_digraph(FALSE);
470 if (c > 0)
471 {
472 *cp = c;
473# ifdef FEAT_CMDL_INFO
474 // Guessing how to update showcmd here...
475 del_from_showcmd(3);
476 *need_flushbuf |= add_to_showcmd(*cp);
477# endif
478 }
479 }
480#endif
481
482 // adjust chars > 127, except after "tTfFr" commands
483 LANGMAP_ADJUST(*cp, !lang);
484#ifdef FEAT_RIGHTLEFT
485 // adjust Hebrew mapped char
486 if (p_hkmap && lang && KeyTyped)
487 *cp = hkmap(*cp);
488#endif
489 }
490
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000491 // When the next character is CTRL-\ a following CTRL-N means the
492 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000493 if (cp == &cap->extra_char
494 && cap->nchar == Ctrl_BSL
495 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
496 {
497 cap->cmdchar = Ctrl_BSL;
498 cap->nchar = cap->extra_char;
499 idx = find_command(cap->cmdchar);
500 }
501 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
502 cap->oap->op_type = get_op_type(*cp, NUL);
503 else if (*cp == Ctrl_BSL)
504 {
505 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
506
507 // There is a busy wait here when typing "f<C-\>" and then
508 // something different from CTRL-N. Can't be avoided.
509 while ((c = vpeekc()) <= 0 && towait > 0L)
510 {
511 do_sleep(towait > 50L ? 50L : towait, FALSE);
512 towait -= 50L;
513 }
514 if (c > 0)
515 {
516 c = plain_vgetc();
517 if (c != Ctrl_N && c != Ctrl_G)
518 vungetc(c);
519 else
520 {
521 cap->cmdchar = Ctrl_BSL;
522 cap->nchar = c;
523 idx = find_command(cap->cmdchar);
524 }
525 }
526 }
527
528 // When getting a text character and the next character is a
529 // multi-byte character, it could be a composing character.
530 // However, don't wait for it to arrive. Also, do enable mapping,
531 // because if it's put back with vungetc() it's too late to apply
532 // mapping.
533 --no_mapping;
534 while (enc_utf8 && lang && (c = vpeekc()) > 0
535 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
536 {
537 c = plain_vgetc();
538 if (!utf_iscomposing(c))
539 {
540 vungetc(c); // it wasn't, put it back
541 break;
542 }
543 else if (cap->ncharC1 == 0)
544 cap->ncharC1 = c;
545 else
546 cap->ncharC2 = c;
547 }
548 ++no_mapping;
549 }
550 --no_mapping;
551 --allow_keys;
552
553 return idx;
554}
555
556/*
557 * Returns TRUE if after processing a normal mode command, need to wait for a
558 * moment when a message is displayed that will be overwritten by the mode
559 * message.
560 */
561 static int
562normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
563{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000564 // In Visual mode and with "^O" in Insert mode, a short message will be
565 // overwritten by the mode message. Wait a bit, until a key is hit.
566 // In Visual mode, it's more important to keep the Visual area updated
567 // than keeping a message (e.g. from a /pat search).
568 // Only do this if the command was typed, not from a mapping.
569 // Don't wait when emsg_silent is non-zero.
570 // Also wait a bit after an error message, e.g. for "^O:".
571 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000572 return ( ((p_smd
573 && msg_silent == 0
574 && (restart_edit != 0
575 || (VIsual_active
576 && old_pos->lnum == curwin->w_cursor.lnum
577 && old_pos->col == curwin->w_cursor.col)
578 )
579 && (clear_cmdline
580 || redraw_cmdline)
581 && (msg_didout || (msg_didany && msg_scroll))
582 && !msg_nowait
583 && KeyTyped)
584 || (restart_edit != 0
585 && !VIsual_active
586 && (msg_scroll
587 || emsg_on_display)))
588 && cap->oap->regname == 0
589 && !(cap->retval & CA_COMMAND_BUSY)
590 && stuff_empty()
591 && typebuf_typed()
592 && emsg_silent == 0
593 && !in_assert_fails
594 && !did_wait_return
595 && cap->oap->op_type == OP_NOP);
596}
597
598/*
599 * After processing a normal mode command, wait for a moment when a message is
600 * displayed that will be overwritten by the mode message.
601 */
602 static void
603normal_cmd_wait_for_msg(void)
604{
605 int save_State = State;
606
607 // Draw the cursor with the right shape here
608 if (restart_edit != 0)
609 State = INSERT;
610
611 // If need to redraw, and there is a "keep_msg", redraw before the
612 // delay
613 if (must_redraw && keep_msg != NULL && !emsg_on_display)
614 {
615 char_u *kmsg;
616
617 kmsg = keep_msg;
618 keep_msg = NULL;
619 // Showmode() will clear keep_msg, but we want to use it anyway.
620 // First update w_topline.
621 setcursor();
622 update_screen(0);
623 // now reset it, otherwise it's put in the history again
624 keep_msg = kmsg;
625
626 kmsg = vim_strsave(keep_msg);
627 if (kmsg != NULL)
628 {
629 msg_attr((char *)kmsg, keep_msg_attr);
630 vim_free(kmsg);
631 }
632 }
633 setcursor();
634#ifdef CURSOR_SHAPE
635 ui_cursor_shape(); // may show different cursor shape
636#endif
637 cursor_on();
638 out_flush();
639 if (msg_scroll || emsg_on_display)
640 ui_delay(1003L, TRUE); // wait at least one second
641 ui_delay(3003L, FALSE); // wait up to three seconds
642 State = save_State;
643
644 msg_scroll = FALSE;
645 emsg_on_display = FALSE;
646}
647
648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * Execute a command in Normal mode.
650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652normal_cmd(
653 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100654 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100658 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000660 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100661 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 static int old_mapped_len = 0;
664 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000665 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200666 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaara80faa82020-04-12 19:37:17 +0200668 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100671 // Use a count remembered from before entering an operator. After typing
672 // "3d" we return from normal_cmd() and come back here, the "3" is
673 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 ca.opcount = opcount;
675
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000676 // If there is an operator pending, then the command we take this time
677 // will terminate it. Finish_op tells us to finish the operation before
678 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef CURSOR_SHAPE
680 c = finish_op;
681#endif
682 finish_op = (oap->op_type != OP_NOP);
683#ifdef CURSOR_SHAPE
684 if (finish_op != c)
685 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687# ifdef FEAT_MOUSESHAPE
688 update_mouseshape(-1);
689# endif
690 }
691#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100692 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100694 // When not finishing an operator and no register name typed, reset the
695 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000699#ifdef FEAT_EVAL
700 set_prevcount = TRUE;
701#endif
702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // Restore counts from before receiving K_CURSORHOLD. This means after
705 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
706 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000707 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
708 {
709 ca.opcount = oap->prev_opcount;
710 ca.count0 = oap->prev_count0;
711 oap->prev_opcount = 0;
712 oap->prev_count0 = 0;
713 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
717 State = NORMAL_BUSY;
718#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#endif
721
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100722#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100723 // Set v:count here, when called from main() and not a stuffed
724 // command, so that v:count can be used in an expression mapping
725 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100726 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100727 set_vcount_ca(&ca, &set_prevcount);
728#endif
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 /*
731 * Get the command character from the user.
732 */
733 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100734 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000736 // If a mapping was started in Visual or Select mode, remember the length
737 // of the mapping. This is used below to not return to Insert mode for as
738 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (restart_edit == 0)
740 old_mapped_len = 0;
741 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000742 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 old_mapped_len = typebuf_maplen();
744
745 if (c == NUL)
746 c = K_ZERO;
747
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000748 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 if (VIsual_active
750 && VIsual_select
751 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
752 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000753 int len;
754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100755 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
756 // 'insertmode' is set) fake a "d"elete command, Insert mode will
757 // restart automatically.
758 // Insert the typed character in the typeahead buffer, so that it can
759 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000760 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
761
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000762 // When recording and gotchars() was called the character will be
763 // recorded again, remove the previous recording.
764 if (KeyTyped)
765 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000766
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000767 if (restart_edit != 0)
768 c = 'd';
769 else
770 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 msg_nowait = TRUE; // don't delay going to insert mode
772 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000775 // If the window was made so small that nothing shows, make it at least one
776 // line and one column when typing a command.
777 if (KeyTyped && !KeyStuffed)
778 win_ensure_size();
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef FEAT_CMDL_INFO
781 need_flushbuf = add_to_showcmd(c);
782#endif
783
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000784 // Get the command count
785 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
786 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000788 // Find the command character in the table of commands.
789 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (ctrl_w)
791 {
792 ca.nchar = c;
793 ca.cmdchar = Ctrl_W;
794 }
795 else
796 ca.cmdchar = c;
797 idx = find_command(ca.cmdchar);
798 if (idx < 0)
799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100800 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 clearopbeep(oap);
802 goto normal_end;
803 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000804
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000805 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000809 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 goto normal_end;
811 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000812 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
813 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000815 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (VIsual_active)
817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (km_stopsel
820 && (nv_cmds[idx].cmd_flags & NV_STS)
821 && !(mod_mask & MOD_MASK_SHIFT))
822 {
823 end_visual_mode();
824 redraw_curbuf_later(INVERTED);
825 }
826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (km_startsel)
829 {
830 if (nv_cmds[idx].cmd_flags & NV_SS)
831 {
832 unshift_special(&ca);
833 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000834 if (idx < 0)
835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000837 clearopbeep(oap);
838 goto normal_end;
839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
842 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 }
845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847#ifdef FEAT_RIGHTLEFT
848 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
849 && (nv_cmds[idx].cmd_flags & NV_RL))
850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 // Invert horizontal movements and operations. Only when typed by the
852 // user directly, not when the result of a mapping or "x" translated
853 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 switch (ca.cmdchar)
855 {
856 case 'l': ca.cmdchar = 'h'; break;
857 case K_RIGHT: ca.cmdchar = K_LEFT; break;
858 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
859 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
860 case 'h': ca.cmdchar = 'l'; break;
861 case K_LEFT: ca.cmdchar = K_RIGHT; break;
862 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
863 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
864 case '>': ca.cmdchar = '<'; break;
865 case '<': ca.cmdchar = '>'; break;
866 }
867 idx = find_command(ca.cmdchar);
868 }
869#endif
870
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000871 // Get additional characters if we need them.
872 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
873 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000876 // Flush the showcmd characters onto the screen so we can see them while
877 // the command is being executed. Only do this when the shown command was
878 // actually displayed, otherwise this will slow down a lot when executing
879 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (need_flushbuf)
881 out_flush();
882#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000883 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200884 {
885 if (ex_normal_busy)
886 did_cursorhold = save_did_cursorhold;
887 else
888 did_cursorhold = FALSE;
889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 State = NORMAL;
892
893 if (ca.nchar == ESC)
894 {
895 clearop(oap);
896 if (restart_edit == 0 && goto_im())
897 restart_edit = 'a';
898 goto normal_end;
899 }
900
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000901 if (ca.cmdchar != K_IGNORE)
902 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100903 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000904 msg_col = 0;
905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100907 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 // When 'keymodel' contains "startsel" some keys start Select/Visual
910 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (!VIsual_active && km_startsel)
912 {
913 if (nv_cmds[idx].cmd_flags & NV_SS)
914 {
915 start_selection();
916 unshift_special(&ca);
917 idx = find_command(ca.cmdchar);
918 }
919 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
920 && (mod_mask & MOD_MASK_SHIFT))
921 {
922 start_selection();
923 mod_mask &= ~MOD_MASK_SHIFT;
924 }
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000927 // Execute the command!
928 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 ca.arg = nv_cmds[idx].cmd_arg;
930 (nv_cmds[idx].cmd_func)(&ca);
931
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000932 // If we didn't start or finish an operator, reset oap->regname, unless we
933 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (!finish_op
935 && !oap->op_type
936 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
937 {
938 clearop(oap);
939#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200940 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#endif
942 }
943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100944 // Get the length of mapped chars again after typing a count, second
945 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000946 if (old_mapped_len > 0)
947 old_mapped_len = typebuf_maplen();
948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000949 // If an operation is pending, handle it. But not for K_IGNORE or
950 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200951 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100952 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000954 // Wait for a moment when a message is displayed that will be overwritten
955 // by the mode message.
956 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
957 normal_cmd_wait_for_msg();
958
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000959 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960normal_end:
961
962 msg_nowait = FALSE;
963
Bram Moolenaarcc613032020-06-07 21:31:18 +0200964#ifdef FEAT_EVAL
965 if (finish_op)
966 reset_reg_var();
967#endif
968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#ifdef CURSOR_SHAPE
971 c = finish_op;
972#endif
973 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100974 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Redraw the cursor with another shape, if we were in Operator-pending
977 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (c || ca.cmdchar == 'r')
979 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100980 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# ifdef FEAT_MOUSESHAPE
982 update_mouseshape(-1);
983# endif
984 }
985#endif
986
987#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000988 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100989 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 clear_showcmd();
991#endif
992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100993 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 vim_free(ca.searchbuf);
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (has_mbyte)
997 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (curwin->w_p_scb && toplevel)
1000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 do_check_scrollbind(TRUE);
1003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar860cae12010-06-05 23:22:07 +02001005 if (curwin->w_p_crb && toplevel)
1006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001007 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001008 do_check_cursorbind();
1009 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001010
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001011#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001013 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001014 restart_edit = 0;
1015#endif
1016
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001017 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1018 // if still inside a mapping that started in Visual mode).
1019 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1022 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !(ca.retval & CA_COMMAND_BUSY)
1024 && stuff_empty()
1025 && oap->regname == 0)
1026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (restart_VIsual_select == 1)
1028 {
1029 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001030 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 showmode();
1032 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001033 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001035 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 (void)edit(restart_edit, FALSE, 1L);
1037 }
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (restart_VIsual_select == 2)
1040 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 opcount = ca.opcount;
1044}
1045
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001046#ifdef FEAT_EVAL
1047/*
1048 * Set v:count and v:count1 according to "cap".
1049 * Set v:prevcount only when "set_prevcount" is TRUE.
1050 */
1051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001052set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053{
1054 long count = cap->count0;
1055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001056 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001057 if (cap->opcount != 0)
1058 count = cap->opcount * (count == 0 ? 1 : count);
1059 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001060 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001061}
1062#endif
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001065 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * if not.
1067 */
1068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001069check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
1071 static int did_check = FALSE;
1072
1073 if (full_screen)
1074 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001075 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001076 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 did_check = TRUE;
1078 }
1079}
1080
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001081#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1082/*
1083 * Call yank_do_autocmd() for "regname".
1084 */
1085 static void
1086call_yank_do_autocmd(int regname)
1087{
1088 oparg_T oa;
1089 yankreg_T *reg;
1090
1091 clear_oparg(&oa);
1092 oa.regname = regname;
1093 oa.op_type = OP_YANK;
1094 oa.is_VIsual = TRUE;
1095 reg = get_register(regname, TRUE);
1096 yank_do_autocmd(&oa, reg);
1097 free_register(reg);
1098}
1099#endif
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001102 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001103 * This function or the next should ALWAYS be called to end Visual mode, except
1104 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
1106 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001107end_visual_mode()
1108{
1109 end_visual_mode_keep_button();
1110 reset_held_button();
1111}
1112
1113 void
1114end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001117 // If we are using the clipboard, then remember what was selected in case
1118 // we need to paste it somewhere while we still own the selection.
1119 // Only do this when the clipboard is already owned. Don't want to grab
1120 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (clip_star.available && clip_star.owned)
1122 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001123
1124# if defined(FEAT_EVAL)
1125 // Emit a TextYankPost for the automatic copy of the selection into the
1126 // star and/or plus register.
1127 if (has_textyankpost())
1128 {
1129 if (clip_isautosel_star())
1130 call_yank_do_autocmd('*');
1131 if (clip_isautosel_plus())
1132 call_yank_do_autocmd('+');
1133 }
1134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 setmouse();
1139 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001141 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001142 curbuf->b_visual.vi_mode = VIsual_mode;
1143 curbuf->b_visual.vi_start = VIsual;
1144 curbuf->b_visual.vi_end = curwin->w_cursor;
1145 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#ifdef FEAT_EVAL
1147 curbuf->b_visual_mode_eval = VIsual_mode;
1148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (!virtual_active())
1150 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001151 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001153 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001154 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155}
1156
1157/*
1158 * Reset VIsual_active and VIsual_reselect.
1159 */
1160 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001161reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
1163 if (VIsual_active)
1164 {
1165 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 VIsual_reselect = FALSE;
1169}
1170
1171/*
1172 * Reset VIsual_active and VIsual_reselect if it's set.
1173 */
1174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001175reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 if (VIsual_active)
1178 {
1179 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001180 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 VIsual_reselect = FALSE;
1182 }
1183}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001185 void
1186restore_visual_mode(void)
1187{
1188 if (VIsual_mode_orig != NUL)
1189 {
1190 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1191 VIsual_mode_orig = NUL;
1192 }
1193}
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195/*
1196 * Check for a balloon-eval special item to include when searching for an
1197 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1198 * Returns TRUE if the character at "*ptr" should be included.
1199 * "dir" is FORWARD or BACKWARD, the direction of searching.
1200 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1201 * "bnp" points to a counter for square brackets.
1202 */
1203 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001204find_is_eval_item(
1205 char_u *ptr,
1206 int *colp,
1207 int *bnp,
1208 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1212 ++*bnp;
1213 if (*bnp > 0)
1214 {
1215 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1216 --*bnp;
1217 return TRUE;
1218 }
1219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (*ptr == '.')
1222 return TRUE;
1223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001224 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1226 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1227 {
1228 *colp += dir;
1229 return TRUE;
1230 }
1231 return FALSE;
1232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234/*
1235 * Find the identifier under or to the right of the cursor.
1236 * "find_type" can have one of three values:
1237 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001238 * FIND_STRING: find any non-white text
1239 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001240 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 *
1242 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001243 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 * This doesn't match the real Vi but I like it a little better and it
1247 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001248 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 * When FIND_IDENT isn't defined, we backup until a blank.
1250 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001251 * Returns the length of the text, or zero if no text is found.
1252 * If text is found, a pointer to the text is put in "*text". This
1253 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 */
1255 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260}
1261
1262/*
1263 * Like find_ident_under_cursor(), but for any window and any position.
1264 * However: Uses 'iskeyword' from the current window!.
1265 */
1266 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001267find_ident_at_pos(
1268 win_T *wp,
1269 linenr_T lnum,
1270 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001271 char_u **text,
1272 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001273 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001276 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int this_class = 0;
1279 int prev_class;
1280 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001281 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001283 // if i == 0: try to find an identifier
1284 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1286 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1287 {
1288 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001289 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
1291 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (has_mbyte)
1293 {
1294 while (ptr[col] != NUL)
1295 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1298 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 this_class = mb_get_class(ptr + col);
1300 if (this_class != 0 && (i == 1 || this_class != 1))
1301 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001302 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 }
1305 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001307 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 )
1310 ++col;
1311
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001312 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001316 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (has_mbyte)
1319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1322 this_class = mb_get_class((char_u *)"a");
1323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001325 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
1327 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1328 prev_class = mb_get_class(ptr + prevcol);
1329 if (this_class != prev_class
1330 && (i == 0
1331 || prev_class == 0
1332 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && (!(find_type & FIND_EVAL)
1334 || prevcol == 0
1335 || !find_is_eval_item(ptr + prevcol, &prevcol,
1336 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 )
1338 break;
1339 col = prevcol;
1340 }
1341
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001342 // If we don't want just any old text, or we've found an
1343 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (this_class > 2)
1345 this_class = 2;
1346 if (!(find_type & FIND_STRING) || this_class == 2)
1347 break;
1348 }
1349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 while (col > 0
1352 && ((i == 0
1353 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001354 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 && (!(find_type & FIND_IDENT)
1356 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 || ((find_type & FIND_EVAL)
1358 && col > 1
1359 && find_is_eval_item(ptr + col - 1, &col,
1360 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 ))
1362 --col;
1363
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001364 // If we don't want just any old text, or we've found an
1365 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1367 break;
1368 }
1369 }
1370
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001371 if (ptr[col] == NUL || (i == 0
1372 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001374 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001375 if ((find_type & FIND_NOERROR) == 0)
1376 {
1377 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001378 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001379 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001380 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return 0;
1383 }
1384 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 *text = ptr;
1386 if (textcol != NULL)
1387 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001390 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 bn = 0;
1393 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (has_mbyte)
1396 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 this_class = mb_get_class(ptr);
1399 while (ptr[col] != NUL
1400 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1401 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 || ((find_type & FIND_EVAL)
1403 && col <= (int)startcol
1404 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001406 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001410 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 || ((find_type & FIND_EVAL)
1412 && col <= (int)startcol
1413 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 return col;
1418}
1419
1420/*
1421 * Prepare for redo of a normal command.
1422 */
1423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001424prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 prep_redo(cap->oap->regname, cap->count0,
1427 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1428}
1429
1430/*
1431 * Prepare for redo of any command.
1432 * Note that only the last argument can be a multi-byte char.
1433 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001435prep_redo(
1436 int regname,
1437 long num,
1438 int cmd1,
1439 int cmd2,
1440 int cmd3,
1441 int cmd4,
1442 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001444 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1445}
1446
1447/*
1448 * Prepare for redo of any command with extra count after "cmd2".
1449 */
1450 void
1451prep_redo_num2(
1452 int regname,
1453 long num1,
1454 int cmd1,
1455 int cmd2,
1456 long num2,
1457 int cmd3,
1458 int cmd4,
1459 int cmd5)
1460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001462 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 AppendCharToRedobuff('"');
1465 AppendCharToRedobuff(regname);
1466 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001467 if (num1 != 0)
1468 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (cmd1 != NUL)
1470 AppendCharToRedobuff(cmd1);
1471 if (cmd2 != NUL)
1472 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001473 if (num2 != 0)
1474 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (cmd3 != NUL)
1476 AppendCharToRedobuff(cmd3);
1477 if (cmd4 != NUL)
1478 AppendCharToRedobuff(cmd4);
1479 if (cmd5 != NUL)
1480 AppendCharToRedobuff(cmd5);
1481}
1482
1483/*
1484 * check for operator active and clear it
1485 *
1486 * return TRUE if operator was active
1487 */
1488 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001489checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 if (oap->op_type == OP_NOP)
1492 return FALSE;
1493 clearopbeep(oap);
1494 return TRUE;
1495}
1496
1497/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001498 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001500 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001505 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001511 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 oap->op_type = OP_NOP;
1515 oap->regname = 0;
1516 oap->motion_force = NUL;
1517 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001518 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519}
1520
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001522clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 clearop(oap);
1525 beep_flush();
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
1529 * Remove the shift modifier from a special key.
1530 */
1531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001532unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 switch (cap->cmdchar)
1535 {
1536 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1537 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1538 case K_S_UP: cap->cmdchar = K_UP; break;
1539 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1540 case K_S_HOME: cap->cmdchar = K_HOME; break;
1541 case K_S_END: cap->cmdchar = K_END; break;
1542 }
1543 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001546/*
1547 * If the mode is currently displayed clear the command line or update the
1548 * command displayed.
1549 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001551may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001552{
1553 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001554 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001555#ifdef FEAT_CMDL_INFO
1556 else
1557 clear_showcmd();
1558#endif
1559}
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1562/*
1563 * Routines for displaying a partly typed command
1564 */
1565
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001566#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569static int showcmd_is_clear = TRUE;
1570static int showcmd_visual = FALSE;
1571
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001572static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001575clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 if (!p_sc)
1578 return;
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (VIsual_active && !char_avail())
1581 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001582 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long lines;
1584 colnr_T leftcol, rightcol;
1585 linenr_T top, bot;
1586
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001587 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001588 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 top = VIsual.lnum;
1591 bot = curwin->w_cursor.lnum;
1592 }
1593 else
1594 {
1595 top = curwin->w_cursor.lnum;
1596 bot = VIsual.lnum;
1597 }
1598# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001599 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001600 (void)hasFolding(top, &top, NULL);
1601 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602# endif
1603 lines = bot - top + 1;
1604
1605 if (VIsual_mode == Ctrl_V)
1606 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001607# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001608 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001609 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001611 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001612 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001613 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001616# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001618 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1621 (long)(rightcol - leftcol + 1));
1622 }
1623 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1624 sprintf((char *)showcmd_buf, "%ld", lines);
1625 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001626 {
1627 char_u *s, *e;
1628 int l;
1629 int bytes = 0;
1630 int chars = 0;
1631
1632 if (cursor_bot)
1633 {
1634 s = ml_get_pos(&VIsual);
1635 e = ml_get_cursor();
1636 }
1637 else
1638 {
1639 s = ml_get_cursor();
1640 e = ml_get_pos(&VIsual);
1641 }
1642 while ((*p_sel != 'e') ? s <= e : s < e)
1643 {
1644 l = (*mb_ptr2len)(s);
1645 if (l == 0)
1646 {
1647 ++bytes;
1648 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001649 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001650 }
1651 bytes += l;
1652 ++chars;
1653 s += l;
1654 }
1655 if (bytes == chars)
1656 sprintf((char *)showcmd_buf, "%d", chars);
1657 else
1658 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1659 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 showcmd_visual = TRUE;
1662 }
1663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665 showcmd_buf[0] = NUL;
1666 showcmd_visual = FALSE;
1667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001668 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (showcmd_is_clear)
1670 return;
1671 }
1672
1673 display_showcmd();
1674}
1675
1676/*
1677 * Add 'c' to string of shown command chars.
1678 * Return TRUE if output has been written (and setcursor() has been called).
1679 */
1680 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 char_u *p;
1684 int old_len;
1685 int extra_len;
1686 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 int i;
1688 static int ignore[] =
1689 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001690#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1692 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001693#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001694 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001695 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1697 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001698 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001700 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 0
1702 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar09df3122006-01-23 22:23:09 +00001704 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 return FALSE;
1706
1707 if (showcmd_visual)
1708 {
1709 showcmd_buf[0] = NUL;
1710 showcmd_visual = FALSE;
1711 }
1712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001713 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (IS_SPECIAL(c))
1715 for (i = 0; ignore[i] != 0; ++i)
1716 if (ignore[i] == c)
1717 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001720 if (*p == ' ')
1721 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 old_len = (int)STRLEN(showcmd_buf);
1723 extra_len = (int)STRLEN(p);
1724 overflow = old_len + extra_len - SHOWCMD_COLS;
1725 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001726 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1727 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 STRCAT(showcmd_buf, p);
1729
1730 if (char_avail())
1731 return FALSE;
1732
1733 display_showcmd();
1734
1735 return TRUE;
1736}
1737
1738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741 if (!add_to_showcmd(c))
1742 setcursor();
1743}
1744
1745/*
1746 * Delete 'len' characters from the end of the shown command.
1747 */
1748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 int old_len;
1752
1753 if (!p_sc)
1754 return;
1755
1756 old_len = (int)STRLEN(showcmd_buf);
1757 if (len > old_len)
1758 len = old_len;
1759 showcmd_buf[old_len - len] = NUL;
1760
1761 if (!char_avail())
1762 display_showcmd();
1763}
1764
1765/*
1766 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1767 * something and there is a partial mapping.
1768 */
1769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001770push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 if (p_sc)
1773 STRCPY(old_showcmd_buf, showcmd_buf);
1774}
1775
1776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001777pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 if (!p_sc)
1780 return;
1781
1782 STRCPY(showcmd_buf, old_showcmd_buf);
1783
1784 display_showcmd();
1785}
1786
1787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 int len;
1791
1792 cursor_off();
1793
1794 len = (int)STRLEN(showcmd_buf);
1795 if (len == 0)
1796 showcmd_is_clear = TRUE;
1797 else
1798 {
1799 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1800 showcmd_is_clear = FALSE;
1801 }
1802
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001803 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1804 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001807 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808}
1809#endif
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
1812 * When "check" is FALSE, prepare for commands that scroll the window.
1813 * When "check" is TRUE, take care of scroll-binding after the window has
1814 * scrolled. Called from normal_cmd() and edit().
1815 */
1816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001817do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 static win_T *old_curwin = NULL;
1820 static linenr_T old_topline = 0;
1821#ifdef FEAT_DIFF
1822 static int old_topfill = 0;
1823#endif
1824 static buf_T *old_buf = NULL;
1825 static colnr_T old_leftcol = 0;
1826
1827 if (check && curwin->w_p_scb)
1828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001829 // If a ":syncbind" command was just used, don't scroll, only reset
1830 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (did_syncbind)
1832 did_syncbind = FALSE;
1833 else if (curwin == old_curwin)
1834 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001835 // Synchronize other windows, as necessary according to
1836 // 'scrollbind'. Don't do this after an ":edit" command, except
1837 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if ((curwin->w_buffer == old_buf
1839#ifdef FEAT_DIFF
1840 || curwin->w_p_diff
1841#endif
1842 )
1843 && (curwin->w_topline != old_topline
1844#ifdef FEAT_DIFF
1845 || curwin->w_topfill != old_topfill
1846#endif
1847 || curwin->w_leftcol != old_leftcol))
1848 {
1849 check_scrollbind(curwin->w_topline - old_topline,
1850 (long)(curwin->w_leftcol - old_leftcol));
1851 }
1852 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001853 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001855 // When switching between windows, make sure that the relative
1856 // vertical offset is valid for the new window. The relative
1857 // offset is invalid whenever another 'scrollbind' window has
1858 // scrolled to a point that would force the current window to
1859 // scroll past the beginning or end of its buffer. When the
1860 // resync is performed, some of the other 'scrollbind' windows may
1861 // need to jump so that the current window's relative position is
1862 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1864 }
1865 curwin->w_scbind_pos = curwin->w_topline;
1866 }
1867
1868 old_curwin = curwin;
1869 old_topline = curwin->w_topline;
1870#ifdef FEAT_DIFF
1871 old_topfill = curwin->w_topfill;
1872#endif
1873 old_buf = curwin->w_buffer;
1874 old_leftcol = curwin->w_leftcol;
1875}
1876
1877/*
1878 * Synchronize any windows that have "scrollbind" set, based on the
1879 * number of rows by which the current window has changed
1880 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1881 */
1882 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001883check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
1885 int want_ver;
1886 int want_hor;
1887 win_T *old_curwin = curwin;
1888 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 int old_VIsual_select = VIsual_select;
1890 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 colnr_T tgt_leftcol = curwin->w_leftcol;
1892 long topline;
1893 long y;
1894
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001895 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1897#ifdef FEAT_DIFF
1898 want_ver |= old_curwin->w_p_diff;
1899#endif
1900 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1901
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001902 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001904 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001907 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (curwin != old_curwin && curwin->w_p_scb)
1909 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001910 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (want_ver)
1912 {
1913#ifdef FEAT_DIFF
1914 if (old_curwin->w_p_diff && curwin->w_p_diff)
1915 {
1916 diff_set_topline(old_curwin, curwin);
1917 }
1918 else
1919#endif
1920 {
1921 curwin->w_scbind_pos += topline_diff;
1922 topline = curwin->w_scbind_pos;
1923 if (topline > curbuf->b_ml.ml_line_count)
1924 topline = curbuf->b_ml.ml_line_count;
1925 if (topline < 1)
1926 topline = 1;
1927
1928 y = topline - curwin->w_topline;
1929 if (y > 0)
1930 scrollup(y, FALSE);
1931 else
1932 scrolldown(-y, FALSE);
1933 }
1934
1935 redraw_later(VALID);
1936 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001940 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1942 {
1943 curwin->w_leftcol = tgt_leftcol;
1944 leftcol_changed();
1945 }
1946 }
1947 }
1948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001949 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 VIsual_select = old_VIsual_select;
1951 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 curwin = old_curwin;
1953 curbuf = old_curbuf;
1954}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956/*
1957 * Command character that's ignored.
1958 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001959 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001962nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965}
1966
1967/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001968 * Command character that doesn't do anything, but unlike nv_ignore() does
1969 * start edit(). Used for "startinsert" executed while starting up.
1970 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001972nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001973{
1974}
1975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Command character doesn't exist.
1978 */
1979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 clearopbeep(cap->oap);
1983}
1984
1985/*
1986 * <Help> and <F1> commands.
1987 */
1988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001989nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 if (!checkclearopq(cap->oap))
1992 ex_help(NULL);
1993}
1994
1995/*
1996 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
1997 */
1998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002001#ifdef FEAT_JOB_CHANNEL
2002 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2003 clearopbeep(cap->oap);
2004 else
2005#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002006 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002007 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002008 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002009 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2010 op_addsub(cap->oap, cap->count1, cap->arg);
2011 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002012 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002013 else if (VIsual_active)
2014 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002015 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002016 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017}
2018
2019/*
2020 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2021 */
2022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002023nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002026 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002027 if (mod_mask & MOD_MASK_CTRL)
2028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002030 if (cap->arg == BACKWARD)
2031 goto_tabpage(-(int)cap->count1);
2032 else
2033 goto_tabpage((int)cap->count0);
2034 }
2035 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002036 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038}
2039
2040/*
2041 * Implementation of "gd" and "gD" command.
2042 */
2043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044nv_gd(
2045 oparg_T *oap,
2046 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002047 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 char_u *ptr;
2051
Bram Moolenaard9d30582005-05-18 22:10:28 +00002052 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002053 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002054 == FAIL)
2055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002057 }
2058 else
2059 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002060#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002061 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2062 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002063#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002064 // clear any search statistics
2065 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2066 clear_cmdline = TRUE;
2067 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002068}
2069
2070/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002071 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2072 * otherwise.
2073 */
2074 static int
2075is_ident(char_u *line, int offset)
2076{
2077 int i;
2078 int incomment = FALSE;
2079 int instring = 0;
2080 int prev = 0;
2081
2082 for (i = 0; i < offset && line[i] != NUL; i++)
2083 {
2084 if (instring != 0)
2085 {
2086 if (prev != '\\' && line[i] == instring)
2087 instring = 0;
2088 }
2089 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2090 {
2091 instring = line[i];
2092 }
2093 else
2094 {
2095 if (incomment)
2096 {
2097 if (prev == '*' && line[i] == '/')
2098 incomment = FALSE;
2099 }
2100 else if (prev == '/' && line[i] == '*')
2101 {
2102 incomment = TRUE;
2103 }
2104 else if (prev == '/' && line[i] == '/')
2105 {
2106 return FALSE;
2107 }
2108 }
2109
2110 prev = line[i];
2111 }
2112
2113 return incomment == FALSE && instring == 0;
2114}
2115
2116/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002117 * Search for variable declaration of "ptr[len]".
2118 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2119 * current file ("gD").
2120 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002121 * Return FAIL when not found.
2122 */
2123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124find_decl(
2125 char_u *ptr,
2126 int len,
2127 int locally,
2128 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002129 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002130{
2131 char_u *pat;
2132 pos_T old_pos;
2133 pos_T par_pos;
2134 pos_T found_pos;
2135 int t;
2136 int save_p_ws;
2137 int save_p_scs;
2138 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002139 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002140 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002141 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002142
2143 if ((pat = alloc(len + 7)) == NULL)
2144 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002146 // Put "\V" before the pattern to avoid that the special meaning of "."
2147 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002148 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2149 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 old_pos = curwin->w_cursor;
2151 save_p_ws = p_ws;
2152 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002153 p_ws = FALSE; // don't wrap around end of file now
2154 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002156 // With "gD" go to line 1.
2157 // With "gd" Search back for the start of the current function, then go
2158 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002159 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002161 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002163 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 else
2166 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002167 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2169 --curwin->w_cursor.lnum;
2170 }
2171 curwin->w_cursor.col = 0;
2172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002173 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002174 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002175 for (;;)
2176 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002177 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002178 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002179 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002181
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002182 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002183 {
2184 pos_T *pos;
2185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002186 // Check that the block the match is in doesn't end before the
2187 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002188 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2189 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2190 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 // There can't be a useful match before the end of this block.
2193 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002194 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002196 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002197 }
2198
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002199 if (t == FAIL)
2200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002202 if (found_pos.lnum != 0)
2203 {
2204 curwin->w_cursor = found_pos;
2205 t = OK;
2206 }
2207 break;
2208 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002209 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002211 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002212 ++curwin->w_cursor.lnum;
2213 curwin->w_cursor.col = 0;
2214 continue;
2215 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002216 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // If the current position is not a valid identifier and a previous
2219 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002220 if (!valid && found_pos.lnum != 0)
2221 {
2222 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002224 }
2225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002226 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 if (valid && !locally)
2228 break;
2229 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002232 if (found_pos.lnum != 0)
2233 curwin->w_cursor = found_pos;
2234 break;
2235 }
2236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // For finding a local variable and the match is before the "{" or
2238 // inside a comment, continue searching. For K&R style function
2239 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002240 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002241 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002242 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002243 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // Remove SEARCH_START from flags to avoid getting stuck at one
2245 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002246 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002248
2249 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002251 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 curwin->w_cursor = old_pos;
2253 }
2254 else
2255 {
2256 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002257 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 reset_search_dir();
2259 }
2260
2261 vim_free(pat);
2262 p_ws = save_p_ws;
2263 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002264
2265 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266}
2267
2268/*
2269 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2270 * lines rather than lines in the file.
2271 * 'dist' must be positive.
2272 *
2273 * Return OK if able to move cursor, FAIL otherwise.
2274 */
2275 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002276nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277{
2278 int linelen = linetabsize(ml_get_curline());
2279 int retval = OK;
2280 int atend = FALSE;
2281 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002282 int col_off1; // margin offset for first screen line
2283 int col_off2; // margin offset for wrapped screen line
2284 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002285 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002288 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 col_off1 = curwin_col_off();
2291 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002292 width1 = curwin->w_width - col_off1;
2293 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002294 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002295 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002298 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002299 // Instead of sticking at the last character of the buffer line we
2300 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (curwin->w_curswant == MAXCOL)
2302 {
2303 atend = TRUE;
2304 validate_virtcol();
2305 if (width1 <= 0)
2306 curwin->w_curswant = 0;
2307 else
2308 {
2309 curwin->w_curswant = width1 - 1;
2310 if (curwin->w_virtcol > curwin->w_curswant)
2311 curwin->w_curswant += ((curwin->w_virtcol
2312 - curwin->w_curswant - 1) / width2 + 1) * width2;
2313 }
2314 }
2315 else
2316 {
2317 if (linelen > width1)
2318 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2319 else
2320 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002321 if (curwin->w_curswant >= (colnr_T)n)
2322 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
2325 while (dist--)
2326 {
2327 if (dir == BACKWARD)
2328 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002329 if ((long)curwin->w_curswant >= width1
2330#ifdef FEAT_FOLDING
2331 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2332#endif
2333 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002334 // Move back within the line. This can give a negative value
2335 // for w_curswant if width1 < width2 (with cpoptions+=n),
2336 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 curwin->w_curswant -= width2;
2338 else
2339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 // Move to the start of a closed fold. Don't do that when
2343 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (!(fdo_flags & FDO_ALL))
2345 (void)hasFolding(curwin->w_cursor.lnum,
2346 &curwin->w_cursor.lnum, NULL);
2347#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002348 if (curwin->w_cursor.lnum == 1)
2349 {
2350 retval = FAIL;
2351 break;
2352 }
2353 --curwin->w_cursor.lnum;
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 linelen = linetabsize(ml_get_curline());
2356 if (linelen > width1)
2357 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2358 + 1) * width2;
2359 }
2360 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002361 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 if (linelen > width1)
2364 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2365 else
2366 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002367 if (curwin->w_curswant + width2 < (colnr_T)n
2368#ifdef FEAT_FOLDING
2369 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2370#endif
2371 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 curwin->w_curswant += width2;
2374 else
2375 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002376 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002378 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2380 &curwin->w_cursor.lnum);
2381#endif
2382 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2383 {
2384 retval = FAIL;
2385 break;
2386 }
2387 curwin->w_cursor.lnum++;
2388 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002389 // Check if the cursor has moved below the number display
2390 // when width1 < width2 (with cpoptions+=n). Subtract width2
2391 // to get a negative value for w_curswant, which will get
2392 // clipped to column 0.
2393 if (curwin->w_curswant >= width1)
2394 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002395 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 }
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002401 if (virtual_active() && atend)
2402 coladvance(MAXCOL);
2403 else
2404 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2407 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002408 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002409 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002410
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002411 // Check for landing on a character that got split at the end of the
2412 // last line. We want to advance a screenline, not end up in the same
2413 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002415 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002416#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002417 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2418 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002419#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002420
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002421 c = (*mb_ptr2char)(ml_get_cursor());
2422 if (dir == FORWARD && virtcol < curwin->w_curswant
2423 && (curwin->w_curswant <= (colnr_T)width1)
2424 && !vim_isprintc(c) && c > 255)
2425 oneright();
2426
Bram Moolenaar773b1582014-08-29 14:20:51 +02002427 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 && (curwin->w_curswant < (colnr_T)width1
2429 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2430 : ((curwin->w_curswant - width1) % width2
2431 > (colnr_T)width2 / 2)))
2432 --curwin->w_cursor.col;
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 return retval;
2439}
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441/*
2442 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2443 * cap->arg must be TRUE for CTRL-E.
2444 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002446nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 if (!checkclearop(cap->oap))
2449 scroll_redraw(cap->arg, cap->count1);
2450}
2451
2452/*
2453 * Scroll "count" lines up or down, and redraw.
2454 */
2455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 linenr_T prev_topline = curwin->w_topline;
2459#ifdef FEAT_DIFF
2460 int prev_topfill = curwin->w_topfill;
2461#endif
2462 linenr_T prev_lnum = curwin->w_cursor.lnum;
2463
2464 if (up)
2465 scrollup(count, TRUE);
2466 else
2467 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002468 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002470 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2471 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 cursor_correct();
2473 check_cursor_moved(curwin);
2474 curwin->w_valid |= VALID_TOPLINE;
2475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002476 // If moved back to where we were, at least move the cursor, otherwise
2477 // we get stuck at one position. Don't move the cursor up if the
2478 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 while (curwin->w_topline == prev_topline
2480#ifdef FEAT_DIFF
2481 && curwin->w_topfill == prev_topfill
2482#endif
2483 )
2484 {
2485 if (up)
2486 {
2487 if (curwin->w_cursor.lnum > prev_lnum
2488 || cursor_down(1L, FALSE) == FAIL)
2489 break;
2490 }
2491 else
2492 {
2493 if (curwin->w_cursor.lnum < prev_lnum
2494 || prev_topline == 1L
2495 || cursor_up(1L, FALSE) == FAIL)
2496 break;
2497 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002498 // Mark w_topline as valid, otherwise the screen jumps back at the
2499 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 check_cursor_moved(curwin);
2501 curwin->w_valid |= VALID_TOPLINE;
2502 }
2503 }
2504 if (curwin->w_cursor.lnum != prev_lnum)
2505 coladvance(curwin->w_curswant);
2506 redraw_later(VALID);
2507}
2508
2509/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002510 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2511 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2512 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002513 */
2514 static int
2515nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2516{
2517 int nchar = *nchar_arg;
2518 long n;
2519
2520 // "z123{nchar}": edit the count before obtaining {nchar}
2521 if (checkclearop(cap->oap))
2522 return FALSE;
2523 n = nchar - '0';
2524
2525 for (;;)
2526 {
2527#ifdef USE_ON_FLY_SCROLL
2528 dont_scroll = TRUE; // disallow scrolling here
2529#endif
2530 ++no_mapping;
2531 ++allow_keys; // no mapping for nchar, but allow key codes
2532 nchar = plain_vgetc();
2533 LANGMAP_ADJUST(nchar, TRUE);
2534 --no_mapping;
2535 --allow_keys;
2536#ifdef FEAT_CMDL_INFO
2537 (void)add_to_showcmd(nchar);
2538#endif
2539 if (nchar == K_DEL || nchar == K_KDEL)
2540 n /= 10;
2541 else if (VIM_ISDIGIT(nchar))
2542 n = n * 10 + (nchar - '0');
2543 else if (nchar == CAR)
2544 {
2545#ifdef FEAT_GUI
2546 need_mouse_correct = TRUE;
2547#endif
2548 win_setheight((int)n);
2549 break;
2550 }
2551 else if (nchar == 'l'
2552 || nchar == 'h'
2553 || nchar == K_LEFT
2554 || nchar == K_RIGHT)
2555 {
2556 cap->count1 = n ? n * cap->count1 : cap->count1;
2557 *nchar_arg = nchar;
2558 return TRUE;
2559 }
2560 else
2561 {
2562 clearopbeep(cap->oap);
2563 break;
2564 }
2565 }
2566 cap->oap->op_type = OP_NOP;
2567 return FALSE;
2568}
2569
2570#ifdef FEAT_SPELL
2571/*
2572 * "zug" and "zuw": undo "zg" and "zw"
2573 * "zg": add good word to word list
2574 * "zw": add wrong word to word list
2575 * "zG": add good word to temp word list
2576 * "zW": add wrong word to temp word list
2577 */
2578 static int
2579nv_zg_zw(cmdarg_T *cap, int nchar)
2580{
2581 char_u *ptr = NULL;
2582 int len;
2583 int undo = FALSE;
2584
2585 if (nchar == 'u')
2586 {
2587 ++no_mapping;
2588 ++allow_keys; // no mapping for nchar, but allow key codes
2589 nchar = plain_vgetc();
2590 LANGMAP_ADJUST(nchar, TRUE);
2591 --no_mapping;
2592 --allow_keys;
2593#ifdef FEAT_CMDL_INFO
2594 (void)add_to_showcmd(nchar);
2595#endif
2596 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2597 {
2598 clearopbeep(cap->oap);
2599 return OK;
2600 }
2601 undo = TRUE;
2602 }
2603
2604 if (checkclearop(cap->oap))
2605 return OK;
2606 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2607 return FAIL;
2608 if (ptr == NULL)
2609 {
2610 pos_T pos = curwin->w_cursor;
2611
2612 // Find bad word under the cursor. When 'spell' is
2613 // off this fails and find_ident_under_cursor() is
2614 // used below.
2615 emsg_off++;
2616 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2617 emsg_off--;
2618 if (len != 0 && curwin->w_cursor.col <= pos.col)
2619 ptr = ml_get_pos(&curwin->w_cursor);
2620 curwin->w_cursor = pos;
2621 }
2622
2623 if (ptr == NULL
2624 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2625 return FAIL;
2626 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2627 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2628 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2629
2630 return OK;
2631}
2632#endif
2633
2634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * Commands that start with "z".
2636 */
2637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002638nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 long n;
2641 colnr_T col;
2642 int nchar = cap->nchar;
2643#ifdef FEAT_FOLDING
2644 long old_fdl = curwin->w_p_fdl;
2645 int old_fen = curwin->w_p_fen;
2646#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002647 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002649 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (
2653#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002654 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2655 // and "zC" only in Visual mode. "zj" and "zk" are motion
2656 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 cap->nchar != 'f' && cap->nchar != 'F'
2658 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2659 && cap->nchar != 'j' && cap->nchar != 'k'
2660 &&
2661#endif
2662 checkclearop(cap->oap))
2663 return;
2664
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002665 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2666 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2668 && cap->count0
2669 && cap->count0 != curwin->w_cursor.lnum)
2670 {
2671 setpcmark();
2672 if (cap->count0 > curbuf->b_ml.ml_line_count)
2673 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2674 else
2675 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002676 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 switch (nchar)
2680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002681 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 case '+':
2683 if (cap->count0 == 0)
2684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002685 // No count given: put cursor at the line below screen
2686 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2688 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2689 else
2690 curwin->w_cursor.lnum = curwin->w_botline;
2691 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 case NL:
2694 case CAR:
2695 case K_KENTER:
2696 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 case 't': scroll_cursor_top(0, TRUE);
2700 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002701 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 break;
2703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 case 'z': scroll_cursor_halfway(TRUE);
2709 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002710 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // "z^", "z-" and "zb": put cursor at bottom of screen
2714 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2715 // when <count> is at bottom of window, and puts that one at
2716 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (cap->count0 != 0)
2718 {
2719 scroll_cursor_bot(0, TRUE);
2720 curwin->w_cursor.lnum = curwin->w_topline;
2721 }
2722 else if (curwin->w_topline == 1)
2723 curwin->w_cursor.lnum = 1;
2724 else
2725 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 case '-':
2728 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 case 'b': scroll_cursor_bot(0, TRUE);
2732 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002733 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 break;
2735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002738 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 case 'h':
2743 case K_LEFT:
2744 if (!curwin->w_p_wrap)
2745 {
2746 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2747 curwin->w_leftcol = 0;
2748 else
2749 curwin->w_leftcol -= (colnr_T)cap->count1;
2750 leftcol_changed();
2751 }
2752 break;
2753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002755 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 case 'l':
2760 case K_RIGHT:
2761 if (!curwin->w_p_wrap)
2762 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 curwin->w_leftcol += (colnr_T)cap->count1;
2765 leftcol_changed();
2766 }
2767 break;
2768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case 's': if (!curwin->w_p_wrap)
2771 {
2772#ifdef FEAT_FOLDING
2773 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
2776#endif
2777 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002778 if ((long)col > siso)
2779 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781 col = 0;
2782 if (curwin->w_leftcol != col)
2783 {
2784 curwin->w_leftcol = col;
2785 redraw_later(NOT_VALID);
2786 }
2787 }
2788 break;
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 case 'e': if (!curwin->w_p_wrap)
2792 {
2793#ifdef FEAT_FOLDING
2794 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else
2797#endif
2798 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002800 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 col = 0;
2802 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002803 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (curwin->w_leftcol != col)
2805 {
2806 curwin->w_leftcol = col;
2807 redraw_later(NOT_VALID);
2808 }
2809 }
2810 break;
2811
Christian Brabandt2fa93842021-05-30 22:17:25 +02002812 // "zp", "zP" in block mode put without addind trailing spaces
2813 case 'P':
2814 case 'p': nv_put(cap);
2815 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002816 // "zy" Yank without trailing spaces
2817 case 'y': nv_operator(cap);
2818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // "zF": create fold command
2821 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case 'F':
2823 case 'f': if (foldManualAllowed(TRUE))
2824 {
2825 cap->nchar = 'f';
2826 nv_operator(cap);
2827 curwin->w_p_fen = TRUE;
2828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2831 {
2832 nv_operator(cap);
2833 finish_op = TRUE;
2834 }
2835 }
2836 else
2837 clearopbeep(cap->oap);
2838 break;
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // "zd": delete fold at cursor
2841 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case 'd':
2843 case 'D': if (foldManualAllowed(FALSE))
2844 {
2845 if (VIsual_active)
2846 nv_operator(cap);
2847 else
2848 deleteFold(curwin->w_cursor.lnum,
2849 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2850 }
2851 break;
2852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case 'E': if (foldmethodIsManual(curwin))
2855 {
2856 clearFolding(curwin);
2857 changed_window_setting();
2858 }
2859 else if (foldmethodIsMarker(curwin))
2860 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2861 TRUE, FALSE);
2862 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002863 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case 'n': curwin->w_p_fen = FALSE;
2868 break;
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case 'N': curwin->w_p_fen = TRUE;
2872 break;
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2876 break;
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2880 openFold(curwin->w_cursor.lnum, cap->count1);
2881 else
2882 {
2883 closeFold(curwin->w_cursor.lnum, cap->count1);
2884 curwin->w_p_fen = TRUE;
2885 }
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2890 openFoldRecurse(curwin->w_cursor.lnum);
2891 else
2892 {
2893 closeFoldRecurse(curwin->w_cursor.lnum);
2894 curwin->w_p_fen = TRUE;
2895 }
2896 break;
2897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 case 'o': if (VIsual_active)
2900 nv_operator(cap);
2901 else
2902 openFold(curwin->w_cursor.lnum, cap->count1);
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'O': if (VIsual_active)
2907 nv_operator(cap);
2908 else
2909 openFoldRecurse(curwin->w_cursor.lnum);
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'c': if (VIsual_active)
2914 nv_operator(cap);
2915 else
2916 closeFold(curwin->w_cursor.lnum, cap->count1);
2917 curwin->w_p_fen = TRUE;
2918 break;
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 case 'C': if (VIsual_active)
2922 nv_operator(cap);
2923 else
2924 closeFoldRecurse(curwin->w_cursor.lnum);
2925 curwin->w_p_fen = TRUE;
2926 break;
2927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 case 'v': foldOpenCursor();
2930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 curwin->w_foldinvalid = TRUE; // recompute folds
2935 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 foldOpenCursor();
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 curwin->w_foldinvalid = TRUE; // recompute folds
2942 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002947 {
2948 curwin->w_p_fdl -= cap->count1;
2949 if (curwin->w_p_fdl < 0)
2950 curwin->w_p_fdl = 0;
2951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 curwin->w_p_fen = TRUE;
2954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 curwin->w_p_fen = TRUE;
2960 break;
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002963 case 'r': curwin->w_p_fdl += cap->count1;
2964 {
2965 int d = getDeepestNesting();
2966
2967 if (curwin->w_p_fdl >= d)
2968 curwin->w_p_fdl = d;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 break;
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 case 'j': // "zj" move to next fold downwards
2978 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2980 cap->count1) == FAIL)
2981 clearopbeep(cap->oap);
2982 break;
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002986#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case 'g': // "zg": add good word to word list
2989 case 'w': // "zw": add wrong word to word list
2990 case 'G': // "zG": add good word to temp word list
2991 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002992 if (nv_zg_zw(cap, nchar) == FAIL)
2993 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002994 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002997 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002998 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 default: clearopbeep(cap->oap);
3003 }
3004
3005#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (old_fen != curwin->w_p_fen)
3008 {
3009# ifdef FEAT_DIFF
3010 win_T *wp;
3011
3012 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003014 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 FOR_ALL_WINDOWS(wp)
3016 {
3017 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3018 {
3019 wp->w_p_fen = curwin->w_p_fen;
3020 changed_window_setting_win(wp);
3021 }
3022 }
3023 }
3024# endif
3025 changed_window_setting();
3026 }
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (old_fdl != curwin->w_p_fdl)
3030 newFoldLevel();
3031#endif
3032}
3033
3034#ifdef FEAT_GUI
3035/*
3036 * Vertical scrollbar movement.
3037 */
3038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003039nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 if (cap->oap->op_type != OP_NOP)
3042 clearopbeep(cap->oap);
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 gui_do_scroll();
3046}
3047
3048/*
3049 * Horizontal scrollbar movement.
3050 */
3051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 if (cap->oap->op_type != OP_NOP)
3055 clearopbeep(cap->oap);
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003058 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059}
3060#endif
3061
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003062#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003063/*
3064 * Click in GUI tab.
3065 */
3066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003068{
3069 if (cap->oap->op_type != OP_NOP)
3070 clearopbeep(cap->oap);
3071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073 goto_tabpage(current_tab);
3074}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003075
3076/*
3077 * Selected item in tab line menu.
3078 */
3079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003081{
3082 if (cap->oap->op_type != OP_NOP)
3083 clearopbeep(cap->oap);
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003086 handle_tabmenu();
3087}
3088
3089/*
3090 * Handle selecting an item of the GUI tab line menu.
3091 * Used in Normal and Insert mode.
3092 */
3093 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003094handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003095{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003096 switch (current_tabmenu)
3097 {
3098 case TABLINE_MENU_CLOSE:
3099 if (current_tab == 0)
3100 do_cmdline_cmd((char_u *)"tabclose");
3101 else
3102 {
3103 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3104 current_tab);
3105 do_cmdline_cmd(IObuff);
3106 }
3107 break;
3108
3109 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003110 if (current_tab == 0)
3111 do_cmdline_cmd((char_u *)"$tabnew");
3112 else
3113 {
3114 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3115 current_tab - 1);
3116 do_cmdline_cmd(IObuff);
3117 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003118 break;
3119
3120 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003121 if (current_tab == 0)
3122 do_cmdline_cmd((char_u *)"browse $tabnew");
3123 else
3124 {
3125 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3126 current_tab - 1);
3127 do_cmdline_cmd(IObuff);
3128 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003129 break;
3130 }
3131}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003132#endif
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * "Q" command.
3136 */
3137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003138nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003140 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003142 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003143 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 do_exmode(FALSE);
3145}
3146
3147/*
3148 * Handle a ":" command.
3149 */
3150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003151nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003153 int old_p_im;
3154 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003155 int is_cmdkey = cap->cmdchar == K_COMMAND
3156 || cap->cmdchar == K_SCRIPT_COMMAND;
3157 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar957cf672020-11-12 14:21:06 +01003159 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 nv_operator(cap);
3161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 if (cap->oap->op_type != OP_NOP)
3164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 cap->oap->motion_type = MCHAR;
3167 cap->oap->inclusive = FALSE;
3168 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003169 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 stuffcharReadbuff('.');
3173 if (cap->count0 > 1)
3174 {
3175 stuffReadbuff((char_u *)",.+");
3176 stuffnumReadbuff((long)cap->count0 - 1L);
3177 }
3178 }
3179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (KeyTyped)
3182 compute_cmdrow();
3183
3184 old_p_im = p_im;
3185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003186 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003187 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3188 if (is_cmdkey)
3189 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3190 else
3191 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (p_im != old_p_im)
3195 {
3196 if (p_im)
3197 restart_edit = 'i';
3198 else
3199 restart_edit = 0;
3200 }
3201
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003202 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003204 clearop(cap->oap);
3205 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3207 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003208 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3209 || did_emsg
3210 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 clearopbeep(cap->oap);
3213 }
3214}
3215
3216/*
3217 * Handle CTRL-G command.
3218 */
3219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003220nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003225 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 showmode();
3227 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003228 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 fileinfo((int)cap->count0, FALSE, TRUE);
3231}
3232
3233/*
3234 * Handle CTRL-H <Backspace> command.
3235 */
3236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003237nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (VIsual_active && VIsual_select)
3240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003241 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 v_visop(cap);
3243 }
3244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 nv_left(cap);
3246}
3247
3248/*
3249 * CTRL-L: clear screen and redraw.
3250 */
3251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003252nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 if (!checkclearop(cap->oap))
3255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003258 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003259# ifdef FEAT_RELTIME
3260 {
3261 win_T *wp;
3262
3263 FOR_ALL_WINDOWS(wp)
3264 wp->w_s->b_syn_slow = FALSE;
3265 }
3266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
3268 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003269#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3270# ifdef VIMDLL
3271 if (!gui.in_use)
3272# endif
3273 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276}
3277
3278/*
3279 * CTRL-O: In Select mode: switch to Visual mode for one command.
3280 * Otherwise: Go to older pcmark.
3281 */
3282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (VIsual_active && VIsual_select)
3286 {
3287 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003288 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 cap->count1 = -cap->count1;
3295 nv_pcmark(cap);
3296 }
3297}
3298
3299/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003300 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3301 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
3303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 if (!checkclearopq(cap->oap))
3307 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3308 GETF_SETMARK|GETF_ALT, FALSE);
3309}
3310
3311/*
3312 * "Z" commands.
3313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (!checkclearopq(cap->oap))
3318 {
3319 switch (cap->nchar)
3320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 case 'Z': do_cmdline_cmd((char_u *)"x");
3323 break;
3324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 case 'Q': do_cmdline_cmd((char_u *)"q!");
3327 break;
3328
3329 default: clearopbeep(cap->oap);
3330 }
3331 }
3332}
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3336 */
3337 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003338do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 oparg_T oa;
3341 cmdarg_T ca;
3342
3343 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003344 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 ca.oap = &oa;
3346 ca.cmdchar = c1;
3347 ca.nchar = c2;
3348 nv_ident(&ca);
3349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003352 * 'K' normal-mode command. Get the command to lookup the keyword under the
3353 * cursor.
3354 */
3355 static int
3356nv_K_getcmd(
3357 cmdarg_T *cap,
3358 char_u *kp,
3359 int kp_help,
3360 int kp_ex,
3361 char_u **ptr_arg,
3362 int n,
3363 char_u *buf,
3364 unsigned buflen)
3365{
3366 char_u *ptr = *ptr_arg;
3367 int isman;
3368 int isman_s;
3369
3370 if (kp_help)
3371 {
3372 // in the help buffer
3373 STRCPY(buf, "he! ");
3374 return n;
3375 }
3376
3377 if (kp_ex)
3378 {
3379 // 'keywordprog' is an ex command
3380 if (cap->count0 != 0)
3381 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3382 else
3383 STRCPY(buf, kp);
3384 STRCAT(buf, " ");
3385 return n;
3386 }
3387
3388 // An external command will probably use an argument starting
3389 // with "-" as an option. To avoid trouble we skip the "-".
3390 while (*ptr == '-' && n > 0)
3391 {
3392 ++ptr;
3393 --n;
3394 }
3395 if (n == 0)
3396 {
3397 // found dashes only
3398 emsg(_(e_no_identifier_under_cursor));
3399 vim_free(buf);
3400 *ptr_arg = ptr;
3401 return 0;
3402 }
3403
3404 // When a count is given, turn it into a range. Is this
3405 // really what we want?
3406 isman = (STRCMP(kp, "man") == 0);
3407 isman_s = (STRCMP(kp, "man -s") == 0);
3408 if (cap->count0 != 0 && !(isman || isman_s))
3409 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3410
3411 STRCAT(buf, "! ");
3412 if (cap->count0 == 0 && isman_s)
3413 STRCAT(buf, "man");
3414 else
3415 STRCAT(buf, kp);
3416 STRCAT(buf, " ");
3417 if (cap->count0 != 0 && (isman || isman_s))
3418 {
3419 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3420 STRCAT(buf, " ");
3421 }
3422
3423 *ptr_arg = ptr;
3424 return n;
3425}
3426
3427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 * Handle the commands that use the word under the cursor.
3429 * [g] CTRL-] :ta to current identifier
3430 * [g] 'K' run program for current identifier
3431 * [g] '*' / to current identifier or string
3432 * [g] '#' ? to current identifier or string
3433 * g ']' :tselect for current identifier
3434 */
3435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003436nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 char_u *ptr = NULL;
3439 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003440 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003441 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 char_u *kp; // value of 'keywordprg'
3444 int kp_help; // 'keywordprg' is ":he"
3445 int kp_ex; // 'keywordprg' starts with ":"
3446 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003449 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 cmdchar = cap->nchar;
3455 g_cmd = TRUE;
3456 }
3457 else
3458 {
3459 cmdchar = cap->cmdchar;
3460 g_cmd = FALSE;
3461 }
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 cmdchar = '#';
3465
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003466 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (checkclearopq(cap->oap))
3472 return;
3473 }
3474
3475 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3476 (cmdchar == '*' || cmdchar == '#')
3477 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3478 {
3479 clearop(cap->oap);
3480 return;
3481 }
3482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // Allocate buffer to put the command in. Inserting backslashes can
3484 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3485 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3487 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3488 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003489 if (kp_help && *skipwhite(ptr) == NUL)
3490 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003491 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003492 return;
3493 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003494 kp_ex = (*kp == ':');
3495 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3496 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (buf == NULL)
3498 return;
3499 buf[0] = NUL;
3500
3501 switch (cmdchar)
3502 {
3503 case '*':
3504 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003505 // Put cursor at start of word, makes search skip the word
3506 // under the cursor.
3507 // Call setpcmark() first, so "*``" puts the cursor back where
3508 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 setpcmark();
3510 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3511
3512 if (!g_cmd && vim_iswordp(ptr))
3513 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
3516
3517 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003518 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3519 if (n == 0)
3520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 break;
3522
3523 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003524 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#ifdef FEAT_CSCOPE
3526 if (p_cst)
3527 STRCPY(buf, "cstag ");
3528 else
3529#endif
3530 STRCPY(buf, "ts ");
3531 break;
3532
3533 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003534 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (curbuf->b_help)
3536 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003539 if (g_cmd)
3540 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003541 else if (cap->count0 == 0)
3542 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003543 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003544 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003548 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003549 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003551 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003552 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003554 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003555 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003556 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003557 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003559 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 vim_free(buf);
3562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003564 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003565 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003566 {
3567 vim_free(buf);
3568 vim_free(p);
3569 return;
3570 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003571 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003572 STRCAT(buf, p);
3573 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 else
3576 {
3577 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003578 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003579 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003580 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003581 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003582 {
3583 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003585 aux_ptr = (char_u *)"";
3586 else
3587 aux_ptr = (char_u *)"\\|\"\n[";
3588 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003589 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003590 aux_ptr = (char_u *)"\\|\"\n*?[";
3591
3592 p = buf + STRLEN(buf);
3593 while (n-- > 0)
3594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003595 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596 if (vim_strchr(aux_ptr, *ptr) != NULL)
3597 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 // When current byte is a part of multibyte character, copy all
3599 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 if (has_mbyte)
3601 {
3602 int i;
3603 int len = (*mb_ptr2len)(ptr) - 1;
3604
3605 for (i = 0; i < len && n >= 1; ++i, --n)
3606 *p++ = *ptr++;
3607 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608 *p++ = *ptr++;
3609 }
3610 *p = NUL;
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003613 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (cmdchar == '*' || cmdchar == '#')
3615 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003616 if (!g_cmd && (has_mbyte
3617 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3618 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003620
3621 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003622 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003624
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003625 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003628 {
3629 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003631 g_tag_at_cursor = FALSE;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 vim_free(buf);
3635}
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637/*
3638 * Get visually selected text, within one line only.
3639 * Returns FAIL if more than one line selected.
3640 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003641 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003642get_visual_text(
3643 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 char_u **pp, // return: start of selected text
3645 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 if (VIsual_mode != 'V')
3648 unadjust_for_sel();
3649 if (VIsual.lnum != curwin->w_cursor.lnum)
3650 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003651 if (cap != NULL)
3652 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return FAIL;
3654 }
3655 if (VIsual_mode == 'V')
3656 {
3657 *pp = ml_get_curline();
3658 *lenp = (int)STRLEN(*pp);
3659 }
3660 else
3661 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003662 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 *pp = ml_get_pos(&curwin->w_cursor);
3665 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3666 }
3667 else
3668 {
3669 *pp = ml_get_pos(&VIsual);
3670 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3671 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003672 if (**pp == NUL)
3673 *lenp = 0;
3674 if (has_mbyte && *lenp > 0)
3675 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003676 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 reset_VIsual_and_resel();
3679 return OK;
3680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682/*
3683 * CTRL-T: backwards in tag stack
3684 */
3685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003686nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 if (!checkclearopq(cap->oap))
3689 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3690}
3691
3692/*
3693 * Handle scrolling command 'H', 'L' and 'M'.
3694 */
3695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003696nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 int used = 0;
3699 long n;
3700#ifdef FEAT_FOLDING
3701 linenr_T lnum;
3702#endif
3703 int half;
3704
3705 cap->oap->motion_type = MLINE;
3706 setpcmark();
3707
3708 if (cap->cmdchar == 'L')
3709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003710 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 curwin->w_cursor.lnum = curwin->w_botline - 1;
3712 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3713 curwin->w_cursor.lnum = 1;
3714 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003715 {
3716#ifdef FEAT_FOLDING
3717 if (hasAnyFolding(curwin))
3718 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003719 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003720 for (n = cap->count1 - 1; n > 0
3721 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3722 {
3723 (void)hasFolding(curwin->w_cursor.lnum,
3724 &curwin->w_cursor.lnum, NULL);
3725 --curwin->w_cursor.lnum;
3726 }
3727 }
3728 else
3729#endif
3730 curwin->w_cursor.lnum -= cap->count1 - 1;
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 else
3734 {
3735 if (cap->cmdchar == 'M')
3736 {
3737#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 used -= diff_check_fill(curwin, curwin->w_topline)
3740 - curwin->w_topfill;
3741#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3744 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3745 {
3746#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 // Count half he number of filler lines to be "below this
3748 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3750 + n) / 2 >= half)
3751 {
3752 --n;
3753 break;
3754 }
3755#endif
3756 used += plines(curwin->w_topline + n);
3757 if (used >= half)
3758 break;
3759#ifdef FEAT_FOLDING
3760 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3761 n = lnum - curwin->w_topline;
3762#endif
3763 }
3764 if (n > 0 && used > curwin->w_height)
3765 --n;
3766 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003767 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003770#ifdef FEAT_FOLDING
3771 if (hasAnyFolding(curwin))
3772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003774 lnum = curwin->w_topline;
3775 while (n-- > 0 && lnum < curwin->w_botline - 1)
3776 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003777 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003778 ++lnum;
3779 }
3780 n = lnum - curwin->w_topline;
3781 }
3782#endif
3783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 curwin->w_cursor.lnum = curwin->w_topline + n;
3785 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3786 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3787 }
3788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003789 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003790 if (cap->oap->op_type == OP_NOP)
3791 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 beginline(BL_SOL | BL_FIX);
3793}
3794
3795/*
3796 * Cursor right commands.
3797 */
3798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003799nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003802 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003804 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003806 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003807 if (mod_mask & MOD_MASK_CTRL)
3808 cap->arg = TRUE;
3809 nv_wordcmd(cap);
3810 return;
3811 }
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 cap->oap->motion_type = MCHAR;
3814 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003815 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003817 // In virtual edit mode, there's no such thing as "past_line", as lines
3818 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003820 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
3822 for (n = cap->count1; n > 0; --n)
3823 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003824 if ((!past_line && oneright() == FAIL)
3825 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003826 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003828 // <Space> wraps to next line if 'whichwrap' has 's'.
3829 // 'l' wraps to next line if 'whichwrap' has 'l'.
3830 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if ( ((cap->cmdchar == ' '
3832 && vim_strchr(p_ww, 's') != NULL)
3833 || (cap->cmdchar == 'l'
3834 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003835 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 && vim_strchr(p_ww, '>') != NULL))
3837 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 // When deleting we also count the NL as a character.
3840 // Set cap->oap->inclusive when last char in the line is
3841 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003842 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003844 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 cap->oap->inclusive = TRUE;
3846 else
3847 {
3848 ++curwin->w_cursor.lnum;
3849 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 curwin->w_set_curswant = TRUE;
3852 cap->oap->inclusive = FALSE;
3853 }
3854 continue;
3855 }
3856 if (cap->oap->op_type == OP_NOP)
3857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003858 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (n == cap->count1)
3860 beep_flush();
3861 }
3862 else
3863 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003864 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 cap->oap->inclusive = TRUE;
3866 }
3867 break;
3868 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003869 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (virtual_active())
3873 oneright();
3874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003877 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 ++curwin->w_cursor.col;
3880 }
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883#ifdef FEAT_FOLDING
3884 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3885 && cap->oap->op_type == OP_NOP)
3886 foldOpenCursor();
3887#endif
3888}
3889
3890/*
3891 * Cursor left commands.
3892 *
3893 * Returns TRUE when operator end should not be adjusted.
3894 */
3895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003896nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 long n;
3899
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003900 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003903 if (mod_mask & MOD_MASK_CTRL)
3904 cap->arg = 1;
3905 nv_bck_word(cap);
3906 return;
3907 }
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 cap->oap->motion_type = MCHAR;
3910 cap->oap->inclusive = FALSE;
3911 for (n = cap->count1; n > 0; --n)
3912 {
3913 if (oneleft() == FAIL)
3914 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003915 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3916 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3917 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if ( (((cap->cmdchar == K_BS
3919 || cap->cmdchar == Ctrl_H)
3920 && vim_strchr(p_ww, 'b') != NULL)
3921 || (cap->cmdchar == 'h'
3922 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003923 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 && vim_strchr(p_ww, '<') != NULL))
3925 && curwin->w_cursor.lnum > 1)
3926 {
3927 --(curwin->w_cursor.lnum);
3928 coladvance((colnr_T)MAXCOL);
3929 curwin->w_set_curswant = TRUE;
3930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 // When the NL before the first char has to be deleted we
3932 // put the cursor on the NUL after the previous line.
3933 // This is a very special case, be careful!
3934 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if ( (cap->oap->op_type == OP_DELETE
3936 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003937 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003939 char_u *cp = ml_get_cursor();
3940
3941 if (*cp != NUL)
3942 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003943 if (has_mbyte)
3944 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3945 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003946 ++curwin->w_cursor.col;
3947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 cap->retval |= CA_NO_ADJ_OP_END;
3949 }
3950 continue;
3951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003952 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3954 beep_flush();
3955 break;
3956 }
3957 }
3958#ifdef FEAT_FOLDING
3959 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3960 && cap->oap->op_type == OP_NOP)
3961 foldOpenCursor();
3962#endif
3963}
3964
3965/*
3966 * Cursor up commands.
3967 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3968 */
3969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003970nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 if (mod_mask & MOD_MASK_SHIFT)
3973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003974 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 cap->arg = BACKWARD;
3976 nv_page(cap);
3977 }
3978 else
3979 {
3980 cap->oap->motion_type = MLINE;
3981 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3982 clearopbeep(cap->oap);
3983 else if (cap->arg)
3984 beginline(BL_WHITE | BL_FIX);
3985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986}
3987
3988/*
3989 * Cursor down commands.
3990 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3991 */
3992 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003993nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 if (mod_mask & MOD_MASK_SHIFT)
3996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003997 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 cap->arg = FORWARD;
3999 nv_page(cap);
4000 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004001#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004002 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004003 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4004 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004010 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 cmdwin_result = CAR;
4012 else
4013#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004014#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004015 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004016 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4017 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4018 {
4019 invoke_prompt_callback();
4020 if (restart_edit == 0)
4021 restart_edit = 'a';
4022 }
4023 else
4024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026 cap->oap->motion_type = MLINE;
4027 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4028 clearopbeep(cap->oap);
4029 else if (cap->arg)
4030 beginline(BL_WHITE | BL_FIX);
4031 }
4032 }
4033}
4034
4035#ifdef FEAT_SEARCHPATH
4036/*
4037 * Grab the file name under the cursor and edit it.
4038 */
4039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004040nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
4042 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004043 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004045 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004048 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return;
4050 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 if (curbuf_locked())
4052 {
4053 clearop(cap->oap);
4054 return;
4055 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004056#ifdef FEAT_PROP_POPUP
4057 if (ERROR_IF_TERM_POPUP_WINDOW)
4058 return;
4059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004061 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 if (ptr != NULL)
4064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004065 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004066 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004067 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004069 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004070 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004071 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004072 {
4073 curwin->w_cursor.lnum = lnum;
4074 check_cursor_lnum();
4075 beginline(BL_SOL | BL_FIX);
4076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 vim_free(ptr);
4078 }
4079 else
4080 clearop(cap->oap);
4081}
4082#endif
4083
4084/*
4085 * <End> command: to end of current line or last line.
4086 */
4087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004088nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004094 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 nv_dollar(cap);
4097}
4098
4099/*
4100 * Handle the "$" command.
4101 */
4102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 cap->oap->motion_type = MCHAR;
4106 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004107 // In virtual mode when off the edge of a line and an operator
4108 // is pending (whew!) keep the cursor where it is.
4109 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (!virtual_active() || gchar_cursor() != NUL
4111 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004112 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (cursor_down((long)(cap->count1 - 1),
4114 cap->oap->op_type == OP_NOP) == FAIL)
4115 clearopbeep(cap->oap);
4116#ifdef FEAT_FOLDING
4117 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4118 foldOpenCursor();
4119#endif
4120}
4121
4122/*
4123 * Implementation of '?' and '/' commands.
4124 * If cap->arg is TRUE don't set PC mark.
4125 */
4126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004127nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004130 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4133 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004134 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 cap->cmdchar = 'g';
4136 cap->nchar = '?';
4137 nv_operator(cap);
4138 return;
4139 }
4140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004141 // When using 'incsearch' the cursor may be moved to set a different search
4142 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004143 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 if (cap->searchbuf == NULL)
4146 {
4147 clearop(oap);
4148 return;
4149 }
4150
Bram Moolenaar46539112015-02-17 15:43:57 +01004151 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004152 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004153 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
4155
4156/*
4157 * Handle "N" and "n" commands.
4158 * cap->arg is SEARCH_REV for "N", 0 for "n".
4159 */
4160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004161nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004163 pos_T old = curwin->w_cursor;
4164 int wrapped = FALSE;
4165 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004166
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004167 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004169 // Avoid getting stuck on the current cursor position, which can
4170 // happen when an offset is given and the cursor is on the last char
4171 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004172 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004173 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004174 cap->count1 -= 1;
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176}
4177
4178/*
4179 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4180 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004181 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004183 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004184normal_search(
4185 cmdarg_T *cap,
4186 int dir,
4187 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004188 int opt, // extra flags for do_search()
4189 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
4191 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004192 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
4194 cap->oap->motion_type = MCHAR;
4195 cap->oap->inclusive = FALSE;
4196 cap->oap->use_reg_one = TRUE;
4197 curwin->w_set_curswant = TRUE;
4198
Bram Moolenaara80faa82020-04-12 19:37:17 +02004199 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004200 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004201 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4202 if (wrapped != NULL)
4203 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (i == 0)
4205 clearop(cap->oap);
4206 else
4207 {
4208 if (i == 2)
4209 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#ifdef FEAT_FOLDING
4212 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4213 foldOpenCursor();
4214#endif
4215 }
4216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004217 // "/$" will put the cursor after the end of the line, may need to
4218 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004220 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
4223/*
4224 * Character search commands.
4225 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4226 * ',' and FALSE for ';'.
4227 * cap->nchar is NUL for ',' and ';' (repeat the search)
4228 */
4229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004230nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 int t_cmd;
4233
4234 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4235 t_cmd = TRUE;
4236 else
4237 t_cmd = FALSE;
4238
4239 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4241 clearopbeep(cap->oap);
4242 else
4243 {
4244 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004245 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4247 && (t_cmd || cap->oap->op_type != OP_NOP))
4248 {
4249 colnr_T scol, ecol;
4250
4251 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4252 curwin->w_cursor.coladd = ecol - scol;
4253 }
4254 else
4255 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#ifdef FEAT_FOLDING
4258 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4259 foldOpenCursor();
4260#endif
4261 }
4262}
4263
4264/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004265 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4266 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4267 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4268 * "[m" or "]m" search for prev/next start of (Java) method.
4269 * "[M" or "]M" search for prev/next end of (Java) method.
4270 */
4271 static void
4272nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4273{
4274 pos_T new_pos = {0, 0, 0};
4275 pos_T *pos = NULL; // init for GCC
4276 pos_T prev_pos;
4277 long n;
4278 int findc;
4279 int c;
4280
4281 if (cap->nchar == '*')
4282 cap->nchar = '/';
4283 prev_pos.lnum = 0;
4284 if (cap->nchar == 'm' || cap->nchar == 'M')
4285 {
4286 if (cap->cmdchar == '[')
4287 findc = '{';
4288 else
4289 findc = '}';
4290 n = 9999;
4291 }
4292 else
4293 {
4294 findc = cap->nchar;
4295 n = cap->count1;
4296 }
4297 for ( ; n > 0; --n)
4298 {
4299 if ((pos = findmatchlimit(cap->oap, findc,
4300 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4301 {
4302 if (new_pos.lnum == 0) // nothing found
4303 {
4304 if (cap->nchar != 'm' && cap->nchar != 'M')
4305 clearopbeep(cap->oap);
4306 }
4307 else
4308 pos = &new_pos; // use last one found
4309 break;
4310 }
4311 prev_pos = new_pos;
4312 curwin->w_cursor = *pos;
4313 new_pos = *pos;
4314 }
4315 curwin->w_cursor = *old_pos;
4316
4317 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4318 // brought us to the match for "[m" and "]M" when inside a method.
4319 // Try finding the '{' or '}' we want to be at.
4320 // Also repeat for the given count.
4321 if (cap->nchar == 'm' || cap->nchar == 'M')
4322 {
4323 // norm is TRUE for "]M" and "[m"
4324 int norm = ((findc == '{') == (cap->nchar == 'm'));
4325
4326 n = cap->count1;
4327 // found a match: we were inside a method
4328 if (prev_pos.lnum != 0)
4329 {
4330 pos = &prev_pos;
4331 curwin->w_cursor = prev_pos;
4332 if (norm)
4333 --n;
4334 }
4335 else
4336 pos = NULL;
4337 while (n > 0)
4338 {
4339 for (;;)
4340 {
4341 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4342 {
4343 // if not found anything, that's an error
4344 if (pos == NULL)
4345 clearopbeep(cap->oap);
4346 n = 0;
4347 break;
4348 }
4349 c = gchar_cursor();
4350 if (c == '{' || c == '}')
4351 {
4352 // Must have found end/start of class: use it.
4353 // Or found the place to be at.
4354 if ((c == findc && norm) || (n == 1 && !norm))
4355 {
4356 new_pos = curwin->w_cursor;
4357 pos = &new_pos;
4358 n = 0;
4359 }
4360 // if no match found at all, we started outside of the
4361 // class and we're inside now. Just go on.
4362 else if (new_pos.lnum == 0)
4363 {
4364 new_pos = curwin->w_cursor;
4365 pos = &new_pos;
4366 }
4367 // found start/end of other method: go to match
4368 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004369 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4370 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004371 n = 0;
4372 else
4373 curwin->w_cursor = *pos;
4374 break;
4375 }
4376 }
4377 --n;
4378 }
4379 curwin->w_cursor = *old_pos;
4380 if (pos == NULL && new_pos.lnum != 0)
4381 clearopbeep(cap->oap);
4382 }
4383 if (pos != NULL)
4384 {
4385 setpcmark();
4386 curwin->w_cursor = *pos;
4387 curwin->w_set_curswant = TRUE;
4388#ifdef FEAT_FOLDING
4389 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4390 && cap->oap->op_type == OP_NOP)
4391 foldOpenCursor();
4392#endif
4393 }
4394}
4395
4396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 * "[" and "]" commands.
4398 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4399 */
4400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004401nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004404 pos_T *pos = NULL; // init for GCC
4405 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 int flag;
4407 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409 cap->oap->motion_type = MCHAR;
4410 cap->oap->inclusive = FALSE;
4411 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004412 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004415 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (cap->nchar == 'f')
4417 nv_gotofile(cap);
4418 else
4419#endif
4420
4421#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004422 // Find the occurrence(s) of the identifier or define under cursor
4423 // in current and included files or jump to the first occurrence.
4424 //
4425 // search list jump
4426 // fwd bwd fwd bwd fwd bwd
4427 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4428 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004430# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004432# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 cap->nchar) != NULL)
4436 {
4437 char_u *ptr;
4438 int len;
4439
4440 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4441 clearop(cap->oap);
4442 else
4443 {
4444 find_pattern_in_path(ptr, 0, len, TRUE,
4445 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4446 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4447 cap->count1,
4448 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4449 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4450 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4451 (linenr_T)MAXLNUM);
4452 curwin->w_set_curswant = TRUE;
4453 }
4454 }
4455 else
4456#endif
4457
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004458 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4459 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4460 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4461 // "[m" or "]m" search for prev/next start of (Java) method.
4462 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 if ( (cap->cmdchar == '['
4464 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4465 || (cap->cmdchar == ']'
4466 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004467 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004469 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 else if (cap->nchar == '[' || cap->nchar == ']')
4471 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004472 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 flag = '{';
4474 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004475 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004478 // Imitate strange Vi behaviour: When using "]]" with an operator
4479 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004480 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 (cap->oap->op_type != OP_NOP
4482 && cap->arg == FORWARD && flag == '{')))
4483 clearopbeep(cap->oap);
4484 else
4485 {
4486 if (cap->oap->op_type == OP_NOP)
4487 beginline(BL_WHITE | BL_FIX);
4488#ifdef FEAT_FOLDING
4489 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4490 foldOpenCursor();
4491#endif
4492 }
4493 }
4494
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004495 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 else if (cap->nchar == 'p' || cap->nchar == 'P')
4497 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004498 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004501 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 else if (cap->nchar == '\'' || cap->nchar == '`')
4503 {
4504 pos = &curwin->w_cursor;
4505 for (n = cap->count1; n > 0; --n)
4506 {
4507 prev_pos = *pos;
4508 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4509 cap->nchar == '\'');
4510 if (pos == NULL)
4511 break;
4512 }
4513 if (pos == NULL)
4514 pos = &prev_pos;
4515 nv_cursormark(cap, cap->nchar == '\'', pos);
4516 }
4517
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004518 // [ or ] followed by a middle mouse click: put selected text with
4519 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004520 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 (void)do_mouse(cap->oap, cap->nchar,
4523 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4524 cap->count1, PUT_FIXINDENT);
4525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004528 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 else if (cap->nchar == 'z')
4530 {
4531 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4532 cap->count1) == FAIL)
4533 clearopbeep(cap->oap);
4534 }
4535#endif
4536
4537#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004538 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else if (cap->nchar == 'c')
4540 {
4541 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4542 cap->count1) == FAIL)
4543 clearopbeep(cap->oap);
4544 }
4545#endif
4546
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004547#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004548 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004549 else if (cap->nchar == 's' || cap->nchar == 'S')
4550 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004551 setpcmark();
4552 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004553 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4554 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004555 {
4556 clearopbeep(cap->oap);
4557 break;
4558 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004559 else
4560 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004561# ifdef FEAT_FOLDING
4562 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4563 foldOpenCursor();
4564# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004565 }
4566#endif
4567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004568 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
4570 clearopbeep(cap->oap);
4571}
4572
4573/*
4574 * Handle Normal mode "%" command.
4575 */
4576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004577nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
4579 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004580#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 linenr_T lnum = curwin->w_cursor.lnum;
4582#endif
4583
4584 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004585 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
4587 if (cap->count0 > 100)
4588 clearopbeep(cap->oap);
4589 else
4590 {
4591 cap->oap->motion_type = MLINE;
4592 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004593 // Round up, so 'normal 100%' always jumps at the line line.
4594 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4595 // overflow on 32-bits, so use a formula with less accuracy
4596 // to avoid overflows.
4597 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4599 / 100L * cap->count0;
4600 else
4601 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4602 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004603 if (curwin->w_cursor.lnum < 1)
4604 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4606 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4607 beginline(BL_SOL | BL_FIX);
4608 }
4609 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004610 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612 cap->oap->motion_type = MCHAR;
4613 cap->oap->use_reg_one = TRUE;
4614 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4615 clearopbeep(cap->oap);
4616 else
4617 {
4618 setpcmark();
4619 curwin->w_cursor = *pos;
4620 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 }
4625#ifdef FEAT_FOLDING
4626 if (cap->oap->op_type == OP_NOP
4627 && lnum != curwin->w_cursor.lnum
4628 && (fdo_flags & FDO_PERCENT)
4629 && KeyTyped)
4630 foldOpenCursor();
4631#endif
4632}
4633
4634/*
4635 * Handle "(" and ")" commands.
4636 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4637 */
4638 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004639nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 cap->oap->motion_type = MCHAR;
4642 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004644 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 curwin->w_set_curswant = TRUE;
4646
4647 if (findsent(cap->arg, cap->count1) == FAIL)
4648 clearopbeep(cap->oap);
4649 else
4650 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004652 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#ifdef FEAT_FOLDING
4655 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4656 foldOpenCursor();
4657#endif
4658 }
4659}
4660
4661/*
4662 * "m" command: Mark a position.
4663 */
4664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004665nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
4667 if (!checkclearop(cap->oap))
4668 {
4669 if (setmark(cap->nchar) == FAIL)
4670 clearopbeep(cap->oap);
4671 }
4672}
4673
4674/*
4675 * "{" and "}" commands.
4676 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4677 */
4678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004679nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
4681 cap->oap->motion_type = MCHAR;
4682 cap->oap->inclusive = FALSE;
4683 cap->oap->use_reg_one = TRUE;
4684 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004685 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 clearopbeep(cap->oap);
4687 else
4688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#ifdef FEAT_FOLDING
4691 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4692 foldOpenCursor();
4693#endif
4694 }
4695}
4696
4697/*
4698 * "u" command: Undo or make lower case.
4699 */
4700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004701nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004703 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004705 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 cap->cmdchar = 'g';
4707 cap->nchar = 'u';
4708 nv_operator(cap);
4709 }
4710 else
4711 nv_kundo(cap);
4712}
4713
4714/*
4715 * <Undo> command.
4716 */
4717 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004718nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 if (!checkclearopq(cap->oap))
4721 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004722#ifdef FEAT_JOB_CHANNEL
4723 if (bt_prompt(curbuf))
4724 {
4725 clearopbeep(cap->oap);
4726 return;
4727 }
4728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 u_undo((int)cap->count1);
4730 curwin->w_set_curswant = TRUE;
4731 }
4732}
4733
4734/*
4735 * Handle the "r" command.
4736 */
4737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004738nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 char_u *ptr;
4741 int had_ctrl_v;
4742 long n;
4743
4744 if (checkclearop(cap->oap))
4745 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004746#ifdef FEAT_JOB_CHANNEL
4747 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4748 {
4749 clearopbeep(cap->oap);
4750 return;
4751 }
4752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004754 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (cap->nchar == Ctrl_V)
4756 {
4757 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004758 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004759 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (cap->nchar > DEL)
4761 had_ctrl_v = NUL;
4762 }
4763 else
4764 had_ctrl_v = NUL;
4765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004766 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004767 if (IS_SPECIAL(cap->nchar))
4768 {
4769 clearopbeep(cap->oap);
4770 return;
4771 }
4772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (VIsual_active)
4775 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004776 if (got_int)
4777 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004778 if (had_ctrl_v)
4779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 // Use a special (negative) number to make a difference between a
4781 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004782 if (cap->nchar == CAR)
4783 cap->nchar = REPLACE_CR_NCHAR;
4784 else if (cap->nchar == NL)
4785 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 nv_operator(cap);
4788 return;
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004791 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (virtual_active())
4793 {
4794 if (u_save_cursor() == FAIL)
4795 return;
4796 if (gchar_cursor() == NUL)
4797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 coladvance_force((colnr_T)(getviscol() + cap->count1));
4800 curwin->w_cursor.col -= cap->count1;
4801 }
4802 else if (gchar_cursor() == TAB)
4803 coladvance_force(getviscol());
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004806 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004808 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004809 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 clearopbeep(cap->oap);
4812 return;
4813 }
4814
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004815 // Replacing with a TAB is done by edit() when it is complicated because
4816 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4817 // Other characters are done below to avoid problems with things like
4818 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4820 {
4821 stuffnumReadbuff(cap->count1);
4822 stuffcharReadbuff('R');
4823 stuffcharReadbuff('\t');
4824 stuffcharReadbuff(ESC);
4825 return;
4826 }
4827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004828 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (u_save_cursor() == FAIL)
4830 return;
4831
4832 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4833 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004834 // Replace character(s) by a single newline.
4835 // Strange vi behaviour: Only one newline is inserted.
4836 // Delete the characters here.
4837 // Insert the newline with an insert command, takes care of
4838 // autoindent. The insert command depends on being on the last
4839 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004840 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 stuffcharReadbuff('\r');
4842 stuffcharReadbuff(ESC);
4843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004844 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 invoke_edit(cap, TRUE, 'r', FALSE);
4846 }
4847 else
4848 {
4849 prep_redo(cap->oap->regname, cap->count1,
4850 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4851
4852 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (has_mbyte)
4854 {
4855 int old_State = State;
4856
4857 if (cap->ncharC1 != 0)
4858 AppendCharToRedobuff(cap->ncharC1);
4859 if (cap->ncharC2 != 0)
4860 AppendCharToRedobuff(cap->ncharC2);
4861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 // This is slow, but it handles replacing a single-byte with a
4863 // multi-byte and the other way around. Also handles adding
4864 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 for (n = cap->count1; n > 0; --n)
4866 {
4867 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004868 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4869 {
4870 int c = ins_copychar(curwin->w_cursor.lnum
4871 + (cap->nchar == Ctrl_Y ? -1 : 1));
4872 if (c != NUL)
4873 ins_char(c);
4874 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004875 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004876 ++curwin->w_cursor.col;
4877 }
4878 else
4879 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 State = old_State;
4881 if (cap->ncharC1 != 0)
4882 ins_char(cap->ncharC1);
4883 if (cap->ncharC2 != 0)
4884 ins_char(cap->ncharC2);
4885 }
4886 }
4887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004889 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 for (n = cap->count1; n > 0; --n)
4891 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004892 // Get ptr again, because u_save and/or showmatch() will have
4893 // released the line. This may also happen in ins_copychar().
4894 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004895 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4896 {
4897 int c = ins_copychar(curwin->w_cursor.lnum
4898 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004899
4900 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004901 if (c != NUL)
4902 ptr[curwin->w_cursor.col] = c;
4903 }
4904 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004905 {
4906 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004907 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (p_sm && msg_silent == 0)
4910 showmatch(cap->nchar);
4911 ++curwin->w_cursor.col;
4912 }
4913#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004914 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004916 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
Bram Moolenaar009b2592004-10-24 19:18:58 +00004918 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004919 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004921 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923#endif
4924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004925 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 changed_bytes(curwin->w_cursor.lnum,
4927 (colnr_T)(curwin->w_cursor.col - cap->count1));
4928 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 --curwin->w_cursor.col; // cursor on the last replaced char
4930 // if the character on the left of the current cursor is a multi-byte
4931 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (has_mbyte)
4933 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 curbuf->b_op_end = curwin->w_cursor;
4935 curwin->w_set_curswant = TRUE;
4936 set_last_insert(cap->nchar);
4937 }
4938}
4939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940/*
4941 * 'o': Exchange start and end of Visual area.
4942 * 'O': same, but in block mode exchange left and right corners.
4943 */
4944 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004945v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 pos_T old_cursor;
4948 colnr_T left, right;
4949
4950 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4951 {
4952 old_cursor = curwin->w_cursor;
4953 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4954 curwin->w_cursor.lnum = VIsual.lnum;
4955 coladvance(left);
4956 VIsual = curwin->w_cursor;
4957
4958 curwin->w_cursor.lnum = old_cursor.lnum;
4959 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004960 // 'selection "exclusive" and cursor at right-bottom corner: move it
4961 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4963 ++curwin->w_curswant;
4964 coladvance(curwin->w_curswant);
4965 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004967 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969 curwin->w_cursor.lnum = VIsual.lnum;
4970 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4971 ++right;
4972 coladvance(right);
4973 VIsual = curwin->w_cursor;
4974
4975 curwin->w_cursor.lnum = old_cursor.lnum;
4976 coladvance(left);
4977 curwin->w_curswant = left;
4978 }
4979 }
4980 else
4981 {
4982 old_cursor = curwin->w_cursor;
4983 curwin->w_cursor = VIsual;
4984 VIsual = old_cursor;
4985 curwin->w_set_curswant = TRUE;
4986 }
4987}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989/*
4990 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4991 */
4992 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004993nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004995 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 cap->cmdchar = 'c';
4998 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 VIsual_mode = 'V';
5001 nv_operator(cap);
5002 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005003 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005006 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 else
5008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 if (virtual_active())
5010 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5012 }
5013 }
5014}
5015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016/*
5017 * "gr".
5018 */
5019 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005020nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (VIsual_active)
5023 {
5024 cap->cmdchar = 'r';
5025 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005026 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005028 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005031 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 else
5033 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005034 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005035 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 stuffcharReadbuff(cap->extra_char);
5037 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (virtual_active())
5039 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 invoke_edit(cap, TRUE, 'v', FALSE);
5041 }
5042 }
5043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045/*
5046 * Swap case for "~" command, when it does not work like an operator.
5047 */
5048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005049n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 long n;
5052 pos_T startpos;
5053 int did_change = 0;
5054#ifdef FEAT_NETBEANS_INTG
5055 pos_T pos;
5056 char_u *ptr;
5057 int count;
5058#endif
5059
5060 if (checkclearopq(cap->oap))
5061 return;
5062
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005063 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 clearopbeep(cap->oap);
5066 return;
5067 }
5068
5069 prep_redo_cmd(cap);
5070
5071 if (u_save_cursor() == FAIL)
5072 return;
5073
5074 startpos = curwin->w_cursor;
5075#ifdef FEAT_NETBEANS_INTG
5076 pos = startpos;
5077#endif
5078 for (n = cap->count1; n > 0; --n)
5079 {
5080 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5081 inc_cursor();
5082 if (gchar_cursor() == NUL)
5083 {
5084 if (vim_strchr(p_ww, '~') != NULL
5085 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5086 {
5087#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005088 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 if (did_change)
5091 {
5092 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005093 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005094 netbeans_removed(curbuf, pos.lnum, pos.col,
5095 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005097 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099 pos.col = 0;
5100 pos.lnum++;
5101 }
5102#endif
5103 ++curwin->w_cursor.lnum;
5104 curwin->w_cursor.col = 0;
5105 if (n > 1)
5106 {
5107 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5108 break;
5109 u_clearline();
5110 }
5111 }
5112 else
5113 break;
5114 }
5115 }
5116#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005117 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 ptr = ml_get(pos.lnum);
5120 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005121 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5122 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124#endif
5125
5126
5127 check_cursor();
5128 curwin->w_set_curswant = TRUE;
5129 if (did_change)
5130 {
5131 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5132 0L);
5133 curbuf->b_op_start = startpos;
5134 curbuf->b_op_end = curwin->w_cursor;
5135 if (curbuf->b_op_end.col > 0)
5136 --curbuf->b_op_end.col;
5137 }
5138}
5139
5140/*
5141 * Move cursor to mark.
5142 */
5143 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005144nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 if (check_mark(pos) == FAIL)
5147 clearop(cap->oap);
5148 else
5149 {
5150 if (cap->cmdchar == '\''
5151 || cap->cmdchar == '`'
5152 || cap->cmdchar == '['
5153 || cap->cmdchar == ']')
5154 setpcmark();
5155 curwin->w_cursor = *pos;
5156 if (flag)
5157 beginline(BL_WHITE | BL_FIX);
5158 else
5159 check_cursor();
5160 }
5161 cap->oap->motion_type = flag ? MLINE : MCHAR;
5162 if (cap->cmdchar == '`')
5163 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005164 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 curwin->w_set_curswant = TRUE;
5166}
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168/*
5169 * Handle commands that are operators in Visual mode.
5170 */
5171 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005172v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005176 // Uppercase means linewise, except in block mode, then "D" deletes till
5177 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (isupper(cap->cmdchar))
5179 {
5180 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005181 {
5182 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5186 curwin->w_curswant = MAXCOL;
5187 }
5188 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5189 nv_operator(cap);
5190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192/*
5193 * "s" and "S" commands.
5194 */
5195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005196nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005198#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005199 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 if (term_swap_diff() == OK)
5201 return;
5202#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005203#ifdef FEAT_JOB_CHANNEL
5204 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5205 {
5206 clearopbeep(cap->oap);
5207 return;
5208 }
5209#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
5212 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005213 {
5214 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 cap->cmdchar = 'c';
5218 nv_operator(cap);
5219 }
5220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 nv_optrans(cap);
5222}
5223
5224/*
5225 * Abbreviated commands.
5226 */
5227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005228nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005231 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005233 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if (VIsual_active)
5235 v_visop(cap);
5236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 nv_optrans(cap);
5238}
5239
5240/*
5241 * Translate a command into another command.
5242 */
5243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005244nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5247 (char_u *)"d$", (char_u *)"c$",
5248 (char_u *)"cl", (char_u *)"cc",
5249 (char_u *)"yy", (char_u *)":s\r"};
5250 static char_u *str = (char_u *)"xXDCsSY&";
5251
5252 if (!checkclearopq(cap->oap))
5253 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005254 // In Vi "2D" doesn't delete the next line. Can't translate it
5255 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005256 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5257 {
5258 cap->oap->start = curwin->w_cursor;
5259 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005260#ifdef FEAT_EVAL
5261 set_op_var(OP_DELETE);
5262#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005263 cap->count1 = 1;
5264 nv_dollar(cap);
5265 finish_op = TRUE;
5266 ResetRedobuff();
5267 AppendCharToRedobuff('D');
5268 }
5269 else
5270 {
5271 if (cap->count0)
5272 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005273 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 cap->opcount = 0;
5277}
5278
5279/*
5280 * "'" and "`" commands. Also for "g'" and "g`".
5281 * cap->arg is TRUE for "'" and "g'".
5282 */
5283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 pos_T *pos;
5287 int c;
5288#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005289 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005290 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#endif
5292
5293 if (cap->cmdchar == 'g')
5294 c = cap->extra_char;
5295 else
5296 c = cap->nchar;
5297 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005298 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
5300 if (cap->arg)
5301 {
5302 check_cursor_lnum();
5303 beginline(BL_WHITE | BL_FIX);
5304 }
5305 else
5306 check_cursor();
5307 }
5308 else
5309 nv_cursormark(cap, cap->arg, pos);
5310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005311 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (!virtual_active())
5313 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005314 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_FOLDING
5316 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005317 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005318 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 && (fdo_flags & FDO_MARK)
5320 && old_KeyTyped)
5321 foldOpenCursor();
5322#endif
5323}
5324
5325/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005326 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 */
5328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005329nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005332#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005334 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 if (!checkclearopq(cap->oap))
5338 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005339 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5340 {
5341 if (goto_tabpage_lastused() == FAIL)
5342 clearopbeep(cap->oap);
5343 return;
5344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (cap->cmdchar == 'g')
5346 pos = movechangelist((int)cap->count1);
5347 else
5348 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005349 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
5351 curwin->w_set_curswant = TRUE;
5352 check_cursor();
5353 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005354 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 nv_cursormark(cap, FALSE, pos);
5356 else if (cap->cmdchar == 'g')
5357 {
5358 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005359 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005361 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005363 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
5365 else
5366 clearopbeep(cap->oap);
5367# ifdef FEAT_FOLDING
5368 if (cap->oap->op_type == OP_NOP
5369 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5370 && (fdo_flags & FDO_MARK)
5371 && old_KeyTyped)
5372 foldOpenCursor();
5373# endif
5374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375}
5376
5377/*
5378 * Handle '"' command.
5379 */
5380 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005381nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 if (checkclearop(cap->oap))
5384 return;
5385#ifdef FEAT_EVAL
5386 if (cap->nchar == '=')
5387 cap->nchar = get_expr_register();
5388#endif
5389 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5390 {
5391 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005392 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_EVAL
5394 set_reg_var(cap->oap->regname);
5395#endif
5396 }
5397 else
5398 clearopbeep(cap->oap);
5399}
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401/*
5402 * Handle "v", "V" and "CTRL-V" commands.
5403 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5404 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005405 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 */
5407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005408nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005410 if (cap->cmdchar == Ctrl_Q)
5411 cap->cmdchar = Ctrl_V;
5412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005413 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5414 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (cap->oap->op_type != OP_NOP)
5416 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005417 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005418 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return;
5420 }
5421
5422 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005423 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005425 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005427 else // toggle char/block mode
5428 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 VIsual_mode = cap->cmdchar;
5430 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005431 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005435 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
5437 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005438 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 VIsual = curwin->w_cursor;
5442
5443 VIsual_active = TRUE;
5444 VIsual_reselect = TRUE;
5445 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005449 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005451 // For V and ^V, we multiply the number of lines even if there
5452 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5454 {
5455 curwin->w_cursor.lnum +=
5456 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005457 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 VIsual_mode = resel_VIsual_mode;
5460 if (VIsual_mode == 'v')
5461 {
5462 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005463 {
5464 validate_virtcol();
5465 curwin->w_curswant = curwin->w_virtcol
5466 + resel_VIsual_vcol * cap->count0 - 1;
5467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005469 curwin->w_curswant = resel_VIsual_vcol;
5470 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005472 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
5474 curwin->w_curswant = MAXCOL;
5475 coladvance((colnr_T)MAXCOL);
5476 }
5477 else if (VIsual_mode == Ctrl_V)
5478 {
5479 validate_virtcol();
5480 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005481 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 coladvance(curwin->w_curswant);
5483 }
5484 else
5485 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005486 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 else
5489 {
5490 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005491 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 may_start_select('c');
5493 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005494 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005496 if (cap->count0 > 0 && --cap->count1 > 0)
5497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005498 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005499 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5500 nv_right(cap);
5501 else if (VIsual_mode == 'V')
5502 nv_down(cap);
5503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
5505 }
5506}
5507
5508/*
5509 * Start selection for Shift-movement keys.
5510 */
5511 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005512start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005514 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 may_start_select('k');
5516 n_start_visual_mode('v');
5517}
5518
5519/*
5520 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5521 */
5522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 VIsual_select = (stuff_empty() && typebuf_typed()
5526 && (vim_strchr(p_slm, c) != NULL));
5527}
5528
5529/*
5530 * Start Visual mode "c".
5531 * Should set VIsual_select before calling this.
5532 */
5533 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005534n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005536#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005537 int cursor_line_was_concealed = curwin->w_p_cole > 0
5538 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005539#endif
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 VIsual_mode = c;
5542 VIsual_active = TRUE;
5543 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005544
5545 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005546 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005547 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005548 {
5549 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 VIsual = curwin->w_cursor;
5553
5554#ifdef FEAT_FOLDING
5555 foldAdjustVisual();
5556#endif
5557
Bram Moolenaara0620062021-11-17 16:52:40 +00005558 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005560#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005561 // Check if redraw is needed after changing the state.
5562 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005563#endif
5564
Bram Moolenaar09df3122006-01-23 22:23:09 +00005565 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005568 // Make sure the clipboard gets updated. Needed because start and
5569 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 clip_star.vmode = NUL;
5571#endif
5572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 // Only need to redraw this line, unless still need to redraw an old
5574 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 if (curwin->w_redr_type < INVERTED)
5576 {
5577 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5578 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5579 }
5580}
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583/*
5584 * CTRL-W: Window commands
5585 */
5586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005587nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005589 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005590 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005592 cap->cmdchar = ':';
5593 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005594 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005595 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005596 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598}
5599
5600/*
5601 * CTRL-Z: Suspend
5602 */
5603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005604nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005609 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610}
5611
5612/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005613 * "gv": Reselect the previous Visual area. If Visual already active,
5614 * exchange previous and current Visual area.
5615 */
5616 static void
5617nv_gv_cmd(cmdarg_T *cap)
5618{
5619 pos_T tpos;
5620 int i;
5621
5622 if (checkclearop(cap->oap))
5623 return;
5624
5625 if (curbuf->b_visual.vi_start.lnum == 0
5626 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5627 || curbuf->b_visual.vi_end.lnum == 0)
5628 {
5629 beep_flush();
5630 return;
5631 }
5632
5633 // set w_cursor to the start of the Visual area, tpos to the end
5634 if (VIsual_active)
5635 {
5636 i = VIsual_mode;
5637 VIsual_mode = curbuf->b_visual.vi_mode;
5638 curbuf->b_visual.vi_mode = i;
5639# ifdef FEAT_EVAL
5640 curbuf->b_visual_mode_eval = i;
5641# endif
5642 i = curwin->w_curswant;
5643 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5644 curbuf->b_visual.vi_curswant = i;
5645
5646 tpos = curbuf->b_visual.vi_end;
5647 curbuf->b_visual.vi_end = curwin->w_cursor;
5648 curwin->w_cursor = curbuf->b_visual.vi_start;
5649 curbuf->b_visual.vi_start = VIsual;
5650 }
5651 else
5652 {
5653 VIsual_mode = curbuf->b_visual.vi_mode;
5654 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5655 tpos = curbuf->b_visual.vi_end;
5656 curwin->w_cursor = curbuf->b_visual.vi_start;
5657 }
5658
5659 VIsual_active = TRUE;
5660 VIsual_reselect = TRUE;
5661
5662 // Set Visual to the start and w_cursor to the end of the Visual
5663 // area. Make sure they are on an existing character.
5664 check_cursor();
5665 VIsual = curwin->w_cursor;
5666 curwin->w_cursor = tpos;
5667 check_cursor();
5668 update_topline();
5669
5670 // When called from normal "g" command: start Select mode when
5671 // 'selectmode' contains "cmd". When called for K_SELECT, always
5672 // start Select mode.
5673 if (cap->arg)
5674 {
5675 VIsual_select = TRUE;
5676 VIsual_select_reg = 0;
5677 }
5678 else
5679 may_start_select('c');
5680 setmouse();
5681#ifdef FEAT_CLIPBOARD
5682 // Make sure the clipboard gets updated. Needed because start and
5683 // end are still the same, and the selection needs to be owned
5684 clip_star.vmode = NUL;
5685#endif
5686 redraw_curbuf_later(INVERTED);
5687 showmode();
5688}
5689
5690/*
5691 * "g0", "g^" : Like "0" and "^" but for screen lines.
5692 * "gm": middle of "g0" and "g$".
5693 */
5694 static void
5695nv_g_home_m_cmd(cmdarg_T *cap)
5696{
5697 int i;
5698 int flag = FALSE;
5699
5700 if (cap->nchar == '^')
5701 flag = TRUE;
5702
5703 cap->oap->motion_type = MCHAR;
5704 cap->oap->inclusive = FALSE;
5705 if (curwin->w_p_wrap && curwin->w_width != 0)
5706 {
5707 int width1 = curwin->w_width - curwin_col_off();
5708 int width2 = width1 + curwin_col_off2();
5709
5710 validate_virtcol();
5711 i = 0;
5712 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5713 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5714 }
5715 else
5716 i = curwin->w_leftcol;
5717 // Go to the middle of the screen line. When 'number' or
5718 // 'relativenumber' is on and lines are wrapping the middle can be more
5719 // to the left.
5720 if (cap->nchar == 'm')
5721 i += (curwin->w_width - curwin_col_off()
5722 + ((curwin->w_p_wrap && i > 0)
5723 ? curwin_col_off2() : 0)) / 2;
5724 coladvance((colnr_T)i);
5725 if (flag)
5726 {
5727 do
5728 i = gchar_cursor();
5729 while (VIM_ISWHITE(i) && oneright() == OK);
5730 curwin->w_valid &= ~VALID_WCOL;
5731 }
5732 curwin->w_set_curswant = TRUE;
5733}
5734
5735/*
5736 * "g_": to the last non-blank character in the line or <count> lines
5737 * downward.
5738 */
5739 static void
5740nv_g_underscore_cmd(cmdarg_T *cap)
5741{
5742 char_u *ptr;
5743
5744 cap->oap->motion_type = MCHAR;
5745 cap->oap->inclusive = TRUE;
5746 curwin->w_curswant = MAXCOL;
5747 if (cursor_down((long)(cap->count1 - 1),
5748 cap->oap->op_type == OP_NOP) == FAIL)
5749 {
5750 clearopbeep(cap->oap);
5751 return;
5752 }
5753
5754 ptr = ml_get_curline();
5755
5756 // In Visual mode we may end up after the line.
5757 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5758 --curwin->w_cursor.col;
5759
5760 // Decrease the cursor column until it's on a non-blank.
5761 while (curwin->w_cursor.col > 0
5762 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5763 --curwin->w_cursor.col;
5764 curwin->w_set_curswant = TRUE;
5765 adjust_for_sel(cap);
5766}
5767
5768/*
5769 * "g$" : Like "$" but for screen lines.
5770 */
5771 static void
5772nv_g_dollar_cmd(cmdarg_T *cap)
5773{
5774 oparg_T *oap = cap->oap;
5775 int i;
5776 int col_off = curwin_col_off();
5777
5778 oap->motion_type = MCHAR;
5779 oap->inclusive = TRUE;
5780 if (curwin->w_p_wrap && curwin->w_width != 0)
5781 {
5782 curwin->w_curswant = MAXCOL; // so we stay at the end
5783 if (cap->count1 == 1)
5784 {
5785 int width1 = curwin->w_width - col_off;
5786 int width2 = width1 + curwin_col_off2();
5787
5788 validate_virtcol();
5789 i = width1 - 1;
5790 if (curwin->w_virtcol >= (colnr_T)width1)
5791 i += ((curwin->w_virtcol - width1) / width2 + 1)
5792 * width2;
5793 coladvance((colnr_T)i);
5794
5795 // Make sure we stick in this column.
5796 validate_virtcol();
5797 curwin->w_curswant = curwin->w_virtcol;
5798 curwin->w_set_curswant = FALSE;
5799 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5800 {
5801 // Check for landing on a character that got split at
5802 // the end of the line. We do not want to advance to
5803 // the next screen line.
5804 if (curwin->w_virtcol > (colnr_T)i)
5805 --curwin->w_cursor.col;
5806 }
5807 }
5808 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5809 clearopbeep(oap);
5810 }
5811 else
5812 {
5813 if (cap->count1 > 1)
5814 // if it fails, let the cursor still move to the last char
5815 (void)cursor_down(cap->count1 - 1, FALSE);
5816
5817 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5818 coladvance((colnr_T)i);
5819
5820 // if the character doesn't fit move one back
5821 if (curwin->w_cursor.col > 0
5822 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5823 {
5824 colnr_T vcol;
5825
5826 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5827 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5828 --curwin->w_cursor.col;
5829 }
5830
5831 // Make sure we stick in this column.
5832 validate_virtcol();
5833 curwin->w_curswant = curwin->w_virtcol;
5834 curwin->w_set_curswant = FALSE;
5835 }
5836}
5837
5838/*
5839 * "gi": start Insert at the last position.
5840 */
5841 static void
5842nv_gi_cmd(cmdarg_T *cap)
5843{
5844 int i;
5845
5846 if (curbuf->b_last_insert.lnum != 0)
5847 {
5848 curwin->w_cursor = curbuf->b_last_insert;
5849 check_cursor_lnum();
5850 i = (int)STRLEN(ml_get_curline());
5851 if (curwin->w_cursor.col > (colnr_T)i)
5852 {
5853 if (virtual_active())
5854 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5855 curwin->w_cursor.col = i;
5856 }
5857 }
5858 cap->cmdchar = 'i';
5859 nv_edit(cap);
5860}
5861
5862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 * Commands starting with "g".
5864 */
5865 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005866nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
5868 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871 switch (cap->nchar)
5872 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005873 case Ctrl_A:
5874 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005876 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005877 if (!VIsual_active && cap->nchar == Ctrl_A)
5878 vim_mem_profile_dump();
5879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005881 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005882 if (VIsual_active)
5883 {
5884 cap->arg = TRUE;
5885 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005886 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005887 nv_addsub(cap);
5888 }
5889 else
5890 clearopbeep(oap);
5891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005893 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 case 'R':
5895 cap->arg = TRUE;
5896 nv_Replace(cap);
5897 break;
5898
5899 case 'r':
5900 nv_vreplace(cap);
5901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 case '&':
5904 do_cmdline_cmd((char_u *)"%s//~/&");
5905 break;
5906
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005907 // "gv": Reselect the previous Visual area. If Visual already active,
5908 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005910 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005912
5913 // "gV": Don't reselect the previous Visual area after a Select mode
5914 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 case 'V':
5916 VIsual_reselect = FALSE;
5917 break;
5918
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005919 // "gh": start Select mode.
5920 // "gH": start Select line mode.
5921 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 case K_BS:
5923 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005924 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 case 'h':
5926 case 'H':
5927 case Ctrl_H:
5928# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005929 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 if (cap->nchar == Ctrl_H)
5931 cap->cmdchar = Ctrl_V;
5932 else
5933# endif
5934 cap->cmdchar = cap->nchar + ('v' - 'h');
5935 cap->arg = TRUE;
5936 nv_visual(cap);
5937 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005939 // "gn", "gN" visually select next/previous search match
5940 // "gn" selects next match
5941 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005942 case 'N':
5943 case 'n':
5944 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005945 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005948 // "gj" and "gk" two new funny movement keys -- up and down
5949 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 case 'j':
5951 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005952 // with 'nowrap' it works just like the normal "j" command.
5953 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
5955 oap->motion_type = MLINE;
5956 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5957 }
5958 else
5959 i = nv_screengo(oap, FORWARD, cap->count1);
5960 if (i == FAIL)
5961 clearopbeep(oap);
5962 break;
5963
5964 case 'k':
5965 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005966 // with 'nowrap' it works just like the normal "k" command.
5967 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
5969 oap->motion_type = MLINE;
5970 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5971 }
5972 else
5973 i = nv_screengo(oap, BACKWARD, cap->count1);
5974 if (i == FAIL)
5975 clearopbeep(oap);
5976 break;
5977
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005978 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 case 'J':
5980 nv_join(cap);
5981 break;
5982
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005983 // "g0", "g^" : Like "0" and "^" but for screen lines.
5984 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 case '0':
5987 case 'm':
5988 case K_HOME:
5989 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005990 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 break;
5992
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005993 case 'M':
5994 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005995 oap->motion_type = MCHAR;
5996 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00005997 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005998 if (cap->count0 > 0 && cap->count0 <= 100)
5999 coladvance((colnr_T)(i * cap->count0 / 100));
6000 else
6001 coladvance((colnr_T)(i / 2));
6002 curwin->w_set_curswant = TRUE;
6003 }
6004 break;
6005
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006006 // "g_": to the last non-blank character in the line or <count> lines
6007 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006009 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 break;
6011
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006012 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 case '$':
6014 case K_END:
6015 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006016 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 break;
6018
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006019 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 case '*':
6021 case '#':
6022#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006023 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006025 case Ctrl_RSB: // :tag or :tselect for current identifier
6026 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 nv_ident(cap);
6028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 case 'e':
6032 case 'E':
6033 oap->motion_type = MCHAR;
6034 curwin->w_set_curswant = TRUE;
6035 oap->inclusive = TRUE;
6036 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6037 clearopbeep(oap);
6038 break;
6039
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006040 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006042 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 break;
6044
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006045 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006047 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 break;
6049
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006050 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 case 'I':
6052 beginline(0);
6053 if (!checkclearopq(oap))
6054 invoke_edit(cap, FALSE, 'g', FALSE);
6055 break;
6056
6057#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // "gf": goto file, edit file under cursor
6059 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006061 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 nv_gotofile(cap);
6063 break;
6064#endif
6065
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006066 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 case '\'':
6068 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006069 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case '`':
6071 nv_gomark(cap);
6072 break;
6073
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006076 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 break;
6078
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006079 // "ga": Display the ascii value of the character under the
6080 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 case 'a':
6082 do_ascii(NULL);
6083 break;
6084
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006085 // "g8": Display the bytes used for the UTF-8 character under the
6086 // cursor. It is displayed in hex.
6087 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006089 if (cap->count0 == 8)
6090 utf_find_illegal();
6091 else
6092 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006095 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006096 case '<':
6097 show_sb_text();
6098 break;
6099
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006100 // "gg": Goto the first line in file. With a count it goes to
6101 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 case 'g':
6103 cap->arg = FALSE;
6104 nv_goto(cap);
6105 break;
6106
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006107 // Two-character operators:
6108 // "gq" Format text
6109 // "gw" Format text and keep cursor position
6110 // "g~" Toggle the case of the text.
6111 // "gu" Change text to lower case.
6112 // "gU" Change text to upper case.
6113 // "g?" rot13 encoding
6114 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 case 'q':
6116 case 'w':
6117 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006118 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 case '~':
6120 case 'u':
6121 case 'U':
6122 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006123 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 nv_operator(cap);
6125 break;
6126
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006127 // "gd": Find first occurrence of pattern under the cursor in the
6128 // current function
6129 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 case 'd':
6131 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006132 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 break;
6134
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006135 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 case K_MIDDLEMOUSE:
6137 case K_MIDDLEDRAG:
6138 case K_MIDDLERELEASE:
6139 case K_LEFTMOUSE:
6140 case K_LEFTDRAG:
6141 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006142 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 case K_RIGHTMOUSE:
6144 case K_RIGHTDRAG:
6145 case K_RIGHTRELEASE:
6146 case K_X1MOUSE:
6147 case K_X1DRAG:
6148 case K_X1RELEASE:
6149 case K_X2MOUSE:
6150 case K_X2DRAG:
6151 case K_X2RELEASE:
6152 mod_mask = MOD_MASK_CTRL;
6153 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156 case K_IGNORE:
6157 break;
6158
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006159 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 case 'p':
6161 case 'P':
6162 nv_put(cap);
6163 break;
6164
6165#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006166 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 case 'o':
6168 goto_byte(cap->count0);
6169 break;
6170#endif
6171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006172 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006174 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
6176 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006177 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 break;
6179 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006180
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (!checkclearopq(oap))
6182 do_exmode(TRUE);
6183 break;
6184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 case ',':
6186 nv_pcmark(cap);
6187 break;
6188
6189 case ';':
6190 cap->count1 = -cap->count1;
6191 nv_pcmark(cap);
6192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006195 if (!checkclearop(oap))
6196 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006197 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006198 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006199 if (!checkclearop(oap))
6200 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006201 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202
Bram Moolenaar62a23252020-08-09 14:04:42 +02006203 case TAB:
6204 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6205 clearopbeep(oap);
6206 break;
6207
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006208 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006209 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006210 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006211 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006212 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006213 break;
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 default:
6216 clearopbeep(oap);
6217 break;
6218 }
6219}
6220
6221/*
6222 * Handle "o" and "O" commands.
6223 */
6224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006225n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006227#ifdef FEAT_CONCEAL
6228 linenr_T oldline = curwin->w_cursor.lnum;
6229#endif
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 if (!checkclearopq(cap->oap))
6232 {
6233#ifdef FEAT_FOLDING
6234 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006235 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 (void)hasFolding(curwin->w_cursor.lnum,
6237 &curwin->w_cursor.lnum, NULL);
6238 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006239 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 (void)hasFolding(curwin->w_cursor.lnum,
6241 NULL, &curwin->w_cursor.lnum);
6242#endif
6243 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6244 (cap->cmdchar == 'O' ? 1 : 0)),
6245 (linenr_T)(curwin->w_cursor.lnum +
6246 (cap->cmdchar == 'o' ? 1 : 0))
6247 ) == OK
6248 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006249 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006250 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006252#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006253 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006254 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006255#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006256#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006257 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006258 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006259 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006260#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006262 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6263 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6265 }
6266 }
6267}
6268
6269/*
6270 * "." command: redo last change.
6271 */
6272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006273nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 if (!checkclearopq(cap->oap))
6276 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006277 // If "restart_edit" is TRUE, the last but one command is repeated
6278 // instead of the last command (inserting text). This is used for
6279 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6281 clearopbeep(cap->oap);
6282 }
6283}
6284
6285/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006286 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 */
6288 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006289nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006291 if (VIsual_select && VIsual_active)
6292 {
6293 int reg;
6294 // Get register name
6295 ++no_mapping;
6296 ++allow_keys;
6297 reg = plain_vgetc();
6298 LANGMAP_ADJUST(reg, TRUE);
6299 --no_mapping;
6300 --allow_keys;
6301
6302 if (reg == '"')
6303 // the unnamed register is 0
6304 reg = 0;
6305
6306 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6307 return;
6308 }
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (!checkclearopq(cap->oap))
6311 {
6312 u_redo((int)cap->count1);
6313 curwin->w_set_curswant = TRUE;
6314 }
6315}
6316
6317/*
6318 * Handle "U" command.
6319 */
6320 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006321nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006323 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006324 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006326 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 cap->cmdchar = 'g';
6328 cap->nchar = 'U';
6329 nv_operator(cap);
6330 }
6331 else if (!checkclearopq(cap->oap))
6332 {
6333 u_undoline();
6334 curwin->w_set_curswant = TRUE;
6335 }
6336}
6337
6338/*
6339 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6340 * single character.
6341 */
6342 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006343nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006345 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006346 {
6347#ifdef FEAT_JOB_CHANNEL
6348 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6349 {
6350 clearopbeep(cap->oap);
6351 return;
6352 }
6353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 else
6357 nv_operator(cap);
6358}
6359
6360/*
6361 * Handle an operator command.
6362 * The actual work is done by do_pending_operator().
6363 */
6364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 int op_type;
6368
6369 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006370#ifdef FEAT_JOB_CHANNEL
6371 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6372 {
6373 clearopbeep(cap->oap);
6374 return;
6375 }
6376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006378 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 nv_lineop(cap);
6380 else if (!checkclearop(cap->oap))
6381 {
6382 cap->oap->start = curwin->w_cursor;
6383 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006384#ifdef FEAT_EVAL
6385 set_op_var(op_type);
6386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 }
6388}
6389
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006390#ifdef FEAT_EVAL
6391/*
6392 * Set v:operator to the characters for "optype".
6393 */
6394 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006395set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006396{
6397 char_u opchars[3];
6398
6399 if (optype == OP_NOP)
6400 set_vim_var_string(VV_OP, NULL, 0);
6401 else
6402 {
6403 opchars[0] = get_op_char(optype);
6404 opchars[1] = get_extra_op_char(optype);
6405 opchars[2] = NUL;
6406 set_vim_var_string(VV_OP, opchars, -1);
6407 }
6408}
6409#endif
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411/*
6412 * Handle linewise operator "dd", "yy", etc.
6413 *
6414 * "_" is is a strange motion command that helps make operators more logical.
6415 * It is actually implemented, but not documented in the real Vi. This motion
6416 * command actually refers to "the current line". Commands like "dd" and "yy"
6417 * are really an alternate form of "d_" and "y_". It does accept a count, so
6418 * "d3_" works to delete 3 lines.
6419 */
6420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006421nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
6423 cap->oap->motion_type = MLINE;
6424 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6425 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006426 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006427 && cap->oap->motion_force != 'v'
6428 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 || cap->oap->op_type == OP_LSHIFT
6430 || cap->oap->op_type == OP_RSHIFT)
6431 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006432 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 beginline(BL_WHITE | BL_FIX);
6434}
6435
6436/*
6437 * <Home> command.
6438 */
6439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006440nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006442 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006443 if (mod_mask & MOD_MASK_CTRL)
6444 nv_goto(cap);
6445 else
6446 {
6447 cap->count0 = 1;
6448 nv_pipe(cap);
6449 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6451 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452}
6453
6454/*
6455 * "|" command.
6456 */
6457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006458nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 cap->oap->motion_type = MCHAR;
6461 cap->oap->inclusive = FALSE;
6462 beginline(0);
6463 if (cap->count0 > 0)
6464 {
6465 coladvance((colnr_T)(cap->count0 - 1));
6466 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6467 }
6468 else
6469 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 // keep curswant at the column where we wanted to go, not where
6471 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 curwin->w_set_curswant = FALSE;
6473}
6474
6475/*
6476 * Handle back-word command "b" and "B".
6477 * cap->arg is 1 for "B"
6478 */
6479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006480nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 cap->oap->motion_type = MCHAR;
6483 cap->oap->inclusive = FALSE;
6484 curwin->w_set_curswant = TRUE;
6485 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6486 clearopbeep(cap->oap);
6487#ifdef FEAT_FOLDING
6488 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6489 foldOpenCursor();
6490#endif
6491}
6492
6493/*
6494 * Handle word motion commands "e", "E", "w" and "W".
6495 * cap->arg is TRUE for "E" and "W".
6496 */
6497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006498nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 int n;
6501 int word_end;
6502 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006503 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006505 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6507 word_end = TRUE;
6508 else
6509 word_end = FALSE;
6510 cap->oap->inclusive = word_end;
6511
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006512 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 if (!word_end && cap->oap->op_type == OP_CHANGE)
6514 {
6515 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006516 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006518 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006520 // Reproduce a funny Vi behaviour: "cw" on a blank only
6521 // changes one character, not all blanks until the start of
6522 // the next word. Only do this when the 'w' flag is included
6523 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6525 {
6526 cap->oap->inclusive = TRUE;
6527 cap->oap->motion_type = MCHAR;
6528 return;
6529 }
6530 }
6531 else
6532 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006533 // This is a little strange. To match what the real Vi does,
6534 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6535 // that we are not on a space or a TAB. This seems impolite
6536 // at first, but it's really more what we mean when we say
6537 // 'cw'.
6538 // Another strangeness: When standing on the end of a word
6539 // "ce" will change until the end of the next word, but "cw"
6540 // will change only one character! This is done by setting
6541 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 cap->oap->inclusive = TRUE;
6543 word_end = TRUE;
6544 flag = TRUE;
6545 }
6546 }
6547 }
6548
6549 cap->oap->motion_type = MCHAR;
6550 curwin->w_set_curswant = TRUE;
6551 if (word_end)
6552 n = end_word(cap->count1, cap->arg, flag, FALSE);
6553 else
6554 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6555
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006556 // Don't leave the cursor on the NUL past the end of line. Unless we
6557 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006558 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006559 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 if (n == FAIL && cap->oap->op_type == OP_NOP)
6562 clearopbeep(cap->oap);
6563 else
6564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566#ifdef FEAT_FOLDING
6567 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6568 foldOpenCursor();
6569#endif
6570 }
6571}
6572
6573/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006574 * Used after a movement command: If the cursor ends up on the NUL after the
6575 * end of the line, may move it back to the last character and make the motion
6576 * inclusive.
6577 */
6578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006579adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006580{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006581 // The cursor cannot remain on the NUL when:
6582 // - the column is > 0
6583 // - not in Visual mode or 'selection' is "o"
6584 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006585 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006586 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006587 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006588 {
6589 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006590 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591 if (has_mbyte)
6592 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006593 oap->inclusive = TRUE;
6594 }
6595}
6596
6597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 * "0" and "^" commands.
6599 * cap->arg is the argument for beginline().
6600 */
6601 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006602nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 cap->oap->motion_type = MCHAR;
6605 cap->oap->inclusive = FALSE;
6606 beginline(cap->arg);
6607#ifdef FEAT_FOLDING
6608 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6609 foldOpenCursor();
6610#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006611 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6612 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613}
6614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615/*
6616 * In exclusive Visual mode, may include the last character.
6617 */
6618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006619adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006622 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (has_mbyte)
6625 inc_cursor();
6626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 ++curwin->w_cursor.col;
6628 cap->oap->inclusive = FALSE;
6629 }
6630}
6631
6632/*
6633 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6634 * Should check VIsual_mode before calling this.
6635 * Returns TRUE when backed up to the previous line.
6636 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006637 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006638unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 pos_T *pp;
6641
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006642 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006644 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 pp = &curwin->w_cursor;
6646 else
6647 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (pp->coladd > 0)
6649 --pp->coladd;
6650 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (pp->col > 0)
6652 {
6653 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006654 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 }
6656 else if (pp->lnum > 1)
6657 {
6658 --pp->lnum;
6659 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6660 return TRUE;
6661 }
6662 }
6663 return FALSE;
6664}
6665
6666/*
6667 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6668 */
6669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006670nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006675 VIsual_select_reg = 0;
6676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 else if (VIsual_reselect)
6678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006679 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 cap->arg = TRUE;
6681 nv_g_cmd(cap);
6682 }
6683}
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686/*
6687 * "G", "gg", CTRL-END, CTRL-HOME.
6688 * cap->arg is TRUE for "G".
6689 */
6690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 linenr_T lnum;
6694
6695 if (cap->arg)
6696 lnum = curbuf->b_ml.ml_line_count;
6697 else
6698 lnum = 1L;
6699 cap->oap->motion_type = MLINE;
6700 setpcmark();
6701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006702 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (cap->count0 != 0)
6704 lnum = cap->count0;
6705 if (lnum < 1L)
6706 lnum = 1L;
6707 else if (lnum > curbuf->b_ml.ml_line_count)
6708 lnum = curbuf->b_ml.ml_line_count;
6709 curwin->w_cursor.lnum = lnum;
6710 beginline(BL_SOL | BL_FIX);
6711#ifdef FEAT_FOLDING
6712 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6713 foldOpenCursor();
6714#endif
6715}
6716
6717/*
6718 * CTRL-\ in Normal mode.
6719 */
6720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6724 {
6725 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006726 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006727 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 restart_edit = 0;
6729#ifdef FEAT_CMDWIN
6730 if (cmdwin_type != 0)
6731 cmdwin_result = Ctrl_C;
6732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (VIsual_active)
6734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006735 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 redraw_curbuf_later(INVERTED);
6737 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006738 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (cap->nchar == Ctrl_G && p_im)
6740 restart_edit = 'a';
6741 }
6742 else
6743 clearopbeep(cap->oap);
6744}
6745
6746/*
6747 * ESC in Normal mode: beep, but don't flush buffers.
6748 * Don't even beep if we are canceling a command.
6749 */
6750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int no_reason;
6754
6755 no_reason = (cap->oap->op_type == OP_NOP
6756 && cap->opcount == 0
6757 && cap->count0 == 0
6758 && cap->oap->regname == 0
6759 && !p_im);
6760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006761 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 if (restart_edit == 0
6764#ifdef FEAT_CMDWIN
6765 && cmdwin_type == 0
6766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006769 {
6770 if (anyBufIsChanged())
6771 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6772 else
6773 msg(_("Type :qa and press <Enter> to exit Vim"));
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006776 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6777 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (!p_im)
6779 restart_edit = 0;
6780#ifdef FEAT_CMDWIN
6781 if (cmdwin_type != 0)
6782 {
6783 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006784 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 return;
6786 }
6787#endif
6788 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006789#ifdef FEAT_CMDWIN
6790 else if (cmdwin_type != 0 && ex_normal_busy)
6791 {
6792 // When :normal runs out of characters while in the command line window
6793 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6794 // loop.
6795 cmdwin_result = K_IGNORE;
6796 return;
6797 }
6798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (VIsual_active)
6801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 end_visual_mode(); // stop Visual
6803 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 curwin->w_set_curswant = TRUE;
6805 redraw_curbuf_later(INVERTED);
6806 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006807 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006808 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 clearop(cap->oap);
6810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006811 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6812 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006813 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 restart_edit = 'a';
6815}
6816
6817/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006818 * Move the cursor for the "A" command.
6819 */
6820 void
6821set_cursor_for_append_to_line(void)
6822{
6823 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006824 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006825 {
6826 int save_State = State;
6827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006828 // Pretend Insert mode here to allow the cursor on the
6829 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006830 State = INSERT;
6831 coladvance((colnr_T)MAXCOL);
6832 State = save_State;
6833 }
6834 else
6835 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6836}
6837
6838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006840 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 */
6842 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006843nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006845 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6847 cap->cmdchar = 'i';
6848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006849 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006851 {
6852#ifdef FEAT_TERMINAL
6853 if (term_in_normal_mode())
6854 {
6855 end_visual_mode();
6856 clearop(cap->oap);
6857 term_enter_job_mode();
6858 return;
6859 }
6860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006864 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006865 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6866 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {
6868#ifdef FEAT_TEXTOBJ
6869 nv_object(cap);
6870#else
6871 clearopbeep(cap->oap);
6872#endif
6873 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006874#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006875 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006876 {
6877 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006878 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006879 return;
6880 }
6881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 else if (!curbuf->b_p_ma && !p_im)
6883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006884 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006885 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006887 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006888 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006889 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006891 else if (cap->cmdchar == K_PS && VIsual_active)
6892 {
6893 pos_T old_pos = curwin->w_cursor;
6894 pos_T old_visual = VIsual;
6895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006896 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006897 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6898 {
6899 shift_delete_registers();
6900 cap->oap->regname = '1';
6901 }
6902 else
6903 cap->oap->regname = '-';
6904 cap->cmdchar = 'd';
6905 cap->nchar = NUL;
6906 nv_operator(cap);
6907 do_pending_operator(cap, 0, FALSE);
6908 cap->cmdchar = K_PS;
6909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 // When the last char in the line was deleted then append. Detect this
6911 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006912 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
6913 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01006914 inc_cursor();
6915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006917 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 else if (!checkclearopq(cap->oap))
6920 {
6921 switch (cap->cmdchar)
6922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006923 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006924 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 break;
6926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006928 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6929 beginline(BL_WHITE);
6930 else
6931 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 break;
6933
Bram Moolenaara1891842017-02-04 21:34:31 +01006934 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006935 // Bracketed paste works like "a"ppend, unless the cursor is in
6936 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006937 if (curwin->w_cursor.col == 0)
6938 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006939 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006941 case 'a': // "a"ppend is like "i"nsert on the next character.
6942 // increment coladd when in virtual space, increment the
6943 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (virtual_active()
6945 && (curwin->w_cursor.coladd > 0
6946 || *ml_get_cursor() == NUL
6947 || *ml_get_cursor() == TAB))
6948 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006949 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 inc_cursor();
6951 break;
6952 }
6953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6955 {
6956 int save_State = State;
6957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006958 // Pretend Insert mode here to allow the cursor on the
6959 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 State = INSERT;
6961 coladvance(getviscol());
6962 State = save_State;
6963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
6965 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6966 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01006967 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006969 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970}
6971
6972/*
6973 * Invoke edit() and take care of "restart_edit" and the return value.
6974 */
6975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006976invoke_edit(
6977 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01006979 int cmd,
6980 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 int restart_edit_save = 0;
6983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006984 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
6985 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
6986 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (repl || !stuff_empty())
6988 restart_edit_save = restart_edit;
6989 else
6990 restart_edit_save = 0;
6991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006992 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 restart_edit = 0;
6994
6995 if (edit(cmd, startln, cap->count1))
6996 cap->retval |= CA_COMMAND_BUSY;
6997
6998 if (restart_edit == 0)
6999 restart_edit = restart_edit_save;
7000}
7001
7002#ifdef FEAT_TEXTOBJ
7003/*
7004 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7005 */
7006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007007nv_object(
7008 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 int flag;
7011 int include;
7012 char_u *mps_save;
7013
7014 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007019 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 mps_save = curbuf->b_p_mps;
7021 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7022
7023 switch (cap->nchar)
7024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007025 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 flag = current_word(cap->oap, cap->count1, include, FALSE);
7027 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 flag = current_word(cap->oap, cap->count1, include, TRUE);
7030 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007031 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 case '(':
7033 case ')':
7034 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7035 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007036 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 case '{':
7038 case '}':
7039 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7040 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007041 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 case ']':
7043 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7044 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007045 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 case '>':
7047 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7048 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007049 case 't': // "at" = a tag block (xml and html)
7050 // Do not adjust oap->end in do_pending_operator()
7051 // otherwise there are different results for 'dit'
7052 // (note leading whitespace in last line):
7053 // 1) <b> 2) <b>
7054 // foobar foobar
7055 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007056 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007057 flag = current_tagblock(cap->oap, cap->count1, include);
7058 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007059 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 flag = current_par(cap->oap, cap->count1, include, 'p');
7061 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 flag = current_sent(cap->oap, cap->count1, include);
7064 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007065 case '"': // "a"" = a double quoted string
7066 case '\'': // "a'" = a single quoted string
7067 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007068 flag = current_quote(cap->oap, cap->count1, include,
7069 cap->nchar);
7070 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007071#if 0 // TODO
7072 case 'S': // "aS" = a section
7073 case 'f': // "af" = a filename
7074 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#endif
7076 default:
7077 flag = FAIL;
7078 break;
7079 }
7080
7081 curbuf->b_p_mps = mps_save;
7082 if (flag == FAIL)
7083 clearopbeep(cap->oap);
7084 adjust_cursor_col();
7085 curwin->w_set_curswant = TRUE;
7086}
7087#endif
7088
7089/*
7090 * "q" command: Start/stop recording.
7091 * "q:", "q/", "q?": edit command-line in command-line window.
7092 */
7093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007094nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 if (cap->oap->op_type == OP_FORMAT)
7097 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007098 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 cap->cmdchar = 'g';
7100 cap->nchar = 'q';
7101 nv_operator(cap);
7102 }
7103 else if (!checkclearop(cap->oap))
7104 {
7105#ifdef FEAT_CMDWIN
7106 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7107 {
7108 stuffcharReadbuff(cap->nchar);
7109 stuffcharReadbuff(K_CMDWIN);
7110 }
7111 else
7112#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007113 // (stop) recording into a named register, unless executing a
7114 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007115 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 clearopbeep(cap->oap);
7117 }
7118}
7119
7120/*
7121 * Handle the "@r" command.
7122 */
7123 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007124nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 if (checkclearop(cap->oap))
7127 return;
7128#ifdef FEAT_EVAL
7129 if (cap->nchar == '=')
7130 {
7131 if (get_expr_register() == NUL)
7132 return;
7133 }
7134#endif
7135 while (cap->count1-- && !got_int)
7136 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007137 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {
7139 clearopbeep(cap->oap);
7140 break;
7141 }
7142 line_breakcheck();
7143 }
7144}
7145
7146/*
7147 * Handle the CTRL-U and CTRL-D commands.
7148 */
7149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007150nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7153 || (cap->cmdchar == Ctrl_D
7154 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7155 clearopbeep(cap->oap);
7156 else if (!checkclearop(cap->oap))
7157 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7158}
7159
7160/*
7161 * Handle "J" or "gJ" command.
7162 */
7163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007164nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007166 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007168 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
7170 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007171 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7173 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007175 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007176 if (cap->count0 <= 2)
7177 {
7178 clearopbeep(cap->oap);
7179 return;
7180 }
7181 cap->count0 = curbuf->b_ml.ml_line_count
7182 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007184
7185 prep_redo(cap->oap->regname, cap->count0,
7186 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7187 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189}
7190
7191/*
7192 * "P", "gP", "p" and "gp" commands.
7193 */
7194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007195nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007197 nv_put_opt(cap, FALSE);
7198}
7199
7200/*
7201 * "P", "gP", "p" and "gp" commands.
7202 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7203 */
7204 static void
7205nv_put_opt(cmdarg_T *cap, int fix_indent)
7206{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 int regname = 0;
7208 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007209 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007210 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 int dir;
7212 int flags = 0;
Shougo Matsushitafb552072022-01-28 16:01:13 +00007213 int save_unnamed = FALSE;
7214 yankreg_T *old_y_current, *old_y_previous;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
7216 if (cap->oap->op_type != OP_NOP)
7217 {
7218#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007219 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7221 {
7222 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007223 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
7225 else
7226#endif
7227 clearopbeep(cap->oap);
7228 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007229#ifdef FEAT_JOB_CHANNEL
7230 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7231 {
7232 clearopbeep(cap->oap);
7233 }
7234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 else
7236 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007237 if (fix_indent)
7238 {
7239 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7240 ? FORWARD : BACKWARD;
7241 flags |= PUT_FIXINDENT;
7242 }
7243 else
7244 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007245 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7246 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 prep_redo_cmd(cap);
7248 if (cap->cmdchar == 'g')
7249 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007250 else if (cap->cmdchar == 'z')
7251 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (VIsual_active)
7254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 // Putting in Visual mode: The put text replaces the selected
7256 // text. First delete the selected text, then put the new text.
7257 // Need to save and restore the registers that the delete
7258 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007259 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 regname = cap->oap->regname;
Shougo Matsushitafb552072022-01-28 16:01:13 +00007261 save_unnamed = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007262#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007264#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007265 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007266 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007267#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
7271 )
7272 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007273 // The delete is going to overwrite the register we want to
7274 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 reg1 = get_register(regname, TRUE);
7276 }
7277
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007278 // Now delete the selected text. Avoid messages here.
Shougo Matsushitafb552072022-01-28 16:01:13 +00007279 if (save_unnamed)
7280 {
7281 old_y_current = get_y_current();
7282 old_y_previous = get_y_previous();
7283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 cap->cmdchar = 'd';
7285 cap->nchar = NUL;
7286 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007287 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 nv_operator(cap);
7289 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007290 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007291 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Shougo Matsushitafb552072022-01-28 16:01:13 +00007293 if (save_unnamed)
7294 {
7295 set_y_current(old_y_current);
7296 set_y_previous(old_y_previous);
7297 }
7298
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007299 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 cap->oap->regname = regname;
7301
7302 if (reg1 != NULL)
7303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007304 // Delete probably changed the register we want to put, save
7305 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 reg2 = get_register(regname, FALSE);
7307 put_register(regname, reg1);
7308 }
7309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007310 // When deleted a linewise Visual area, put the register as
7311 // lines to avoid it joined with the next line. When deletion was
7312 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 if (VIsual_mode == 'V')
7314 flags |= PUT_LINE;
7315 else if (VIsual_mode == 'v')
7316 flags |= PUT_LINE_SPLIT;
7317 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7318 flags |= PUT_LINE_FORWARD;
7319 dir = BACKWARD;
7320 if ((VIsual_mode != 'V'
7321 && curwin->w_cursor.col < curbuf->b_op_start.col)
7322 || (VIsual_mode == 'V'
7323 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007324 // cursor is at the end of the line or end of file, put
7325 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007327 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007328 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007330 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007332 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (reg2 != NULL)
7334 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // What to reselect with "gv"? Selecting the just put text seems to
7337 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007338 if (was_visual)
7339 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007340 curbuf->b_visual.vi_start = curbuf->b_op_start;
7341 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007343 if (*p_sel == 'e')
7344 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007345 }
7346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007347 // When all lines were selected and deleted do_put() leaves an empty
7348 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007349 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007350 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007351 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007352 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007353
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 // If the cursor was in that line, move it to the end of the last
7355 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007356 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7357 {
7358 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7359 coladvance((colnr_T)MAXCOL);
7360 }
7361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 auto_format(FALSE, TRUE);
7363 }
7364}
7365
7366/*
7367 * "o" and "O" commands.
7368 */
7369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007370nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7375 {
7376 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007377 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379 else
7380#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007381 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007383#ifdef FEAT_JOB_CHANNEL
7384 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007385 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 n_opencmd(cap);
7389}
7390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391#ifdef FEAT_NETBEANS_INTG
7392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007393nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 netbeans_keycommand(cap->nchar);
7396}
7397#endif
7398
7399#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007401nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007403 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404}
7405#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007406
Bram Moolenaar3918c952005-03-15 22:34:55 +00007407/*
7408 * Trigger CursorHold event.
7409 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7410 * input buffer. "did_cursorhold" is set to avoid retriggering.
7411 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007412 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007413nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007414{
7415 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7416 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007417 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007418}