blob: e39fb772ae15c5ca7edd7d08b738d13cc1ce6e3a [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
kylo252ae6f1d82022-02-16 19:24:07 +00001566#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 Moolenaar424bcae2022-01-31 14:59:41 +00004429 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 char_u *ptr;
4432 int len;
4433
4434 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4435 clearop(cap->oap);
4436 else
4437 {
4438 find_pattern_in_path(ptr, 0, len, TRUE,
4439 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4440 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4441 cap->count1,
4442 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4443 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4444 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4445 (linenr_T)MAXLNUM);
4446 curwin->w_set_curswant = TRUE;
4447 }
4448 }
4449 else
4450#endif
4451
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004452 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4453 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4454 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4455 // "[m" or "]m" search for prev/next start of (Java) method.
4456 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if ( (cap->cmdchar == '['
4458 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4459 || (cap->cmdchar == ']'
4460 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004461 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004463 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 else if (cap->nchar == '[' || cap->nchar == ']')
4465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004466 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 flag = '{';
4468 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004469 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004472 // Imitate strange Vi behaviour: When using "]]" with an operator
4473 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004474 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 (cap->oap->op_type != OP_NOP
4476 && cap->arg == FORWARD && flag == '{')))
4477 clearopbeep(cap->oap);
4478 else
4479 {
4480 if (cap->oap->op_type == OP_NOP)
4481 beginline(BL_WHITE | BL_FIX);
4482#ifdef FEAT_FOLDING
4483 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4484 foldOpenCursor();
4485#endif
4486 }
4487 }
4488
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004489 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else if (cap->nchar == 'p' || cap->nchar == 'P')
4491 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004492 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004495 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 else if (cap->nchar == '\'' || cap->nchar == '`')
4497 {
4498 pos = &curwin->w_cursor;
4499 for (n = cap->count1; n > 0; --n)
4500 {
4501 prev_pos = *pos;
4502 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4503 cap->nchar == '\'');
4504 if (pos == NULL)
4505 break;
4506 }
4507 if (pos == NULL)
4508 pos = &prev_pos;
4509 nv_cursormark(cap, cap->nchar == '\'', pos);
4510 }
4511
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004512 // [ or ] followed by a middle mouse click: put selected text with
4513 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004514 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 (void)do_mouse(cap->oap, cap->nchar,
4517 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4518 cap->count1, PUT_FIXINDENT);
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
4521#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else if (cap->nchar == 'z')
4524 {
4525 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4526 cap->count1) == FAIL)
4527 clearopbeep(cap->oap);
4528 }
4529#endif
4530
4531#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004532 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else if (cap->nchar == 'c')
4534 {
4535 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4536 cap->count1) == FAIL)
4537 clearopbeep(cap->oap);
4538 }
4539#endif
4540
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004541#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004542 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004543 else if (cap->nchar == 's' || cap->nchar == 'S')
4544 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004545 setpcmark();
4546 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004547 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4548 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004549 {
4550 clearopbeep(cap->oap);
4551 break;
4552 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004553 else
4554 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004555# ifdef FEAT_FOLDING
4556 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4557 foldOpenCursor();
4558# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004559 }
4560#endif
4561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004562 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else
4564 clearopbeep(cap->oap);
4565}
4566
4567/*
4568 * Handle Normal mode "%" command.
4569 */
4570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004571nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572{
4573 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004574#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 linenr_T lnum = curwin->w_cursor.lnum;
4576#endif
4577
4578 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004579 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 if (cap->count0 > 100)
4582 clearopbeep(cap->oap);
4583 else
4584 {
4585 cap->oap->motion_type = MLINE;
4586 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004587 // Round up, so 'normal 100%' always jumps at the line line.
4588 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4589 // overflow on 32-bits, so use a formula with less accuracy
4590 // to avoid overflows.
4591 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4593 / 100L * cap->count0;
4594 else
4595 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4596 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004597 if (curwin->w_cursor.lnum < 1)
4598 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4600 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4601 beginline(BL_SOL | BL_FIX);
4602 }
4603 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004604 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 cap->oap->motion_type = MCHAR;
4607 cap->oap->use_reg_one = TRUE;
4608 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4609 clearopbeep(cap->oap);
4610 else
4611 {
4612 setpcmark();
4613 curwin->w_cursor = *pos;
4614 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 }
4619#ifdef FEAT_FOLDING
4620 if (cap->oap->op_type == OP_NOP
4621 && lnum != curwin->w_cursor.lnum
4622 && (fdo_flags & FDO_PERCENT)
4623 && KeyTyped)
4624 foldOpenCursor();
4625#endif
4626}
4627
4628/*
4629 * Handle "(" and ")" commands.
4630 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4631 */
4632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004633nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 cap->oap->motion_type = MCHAR;
4636 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004638 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 curwin->w_set_curswant = TRUE;
4640
4641 if (findsent(cap->arg, cap->count1) == FAIL)
4642 clearopbeep(cap->oap);
4643 else
4644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004646 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#ifdef FEAT_FOLDING
4649 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4650 foldOpenCursor();
4651#endif
4652 }
4653}
4654
4655/*
4656 * "m" command: Mark a position.
4657 */
4658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004659nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 if (!checkclearop(cap->oap))
4662 {
4663 if (setmark(cap->nchar) == FAIL)
4664 clearopbeep(cap->oap);
4665 }
4666}
4667
4668/*
4669 * "{" and "}" commands.
4670 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4671 */
4672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004673nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 cap->oap->motion_type = MCHAR;
4676 cap->oap->inclusive = FALSE;
4677 cap->oap->use_reg_one = TRUE;
4678 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004679 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 clearopbeep(cap->oap);
4681 else
4682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#ifdef FEAT_FOLDING
4685 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4686 foldOpenCursor();
4687#endif
4688 }
4689}
4690
4691/*
4692 * "u" command: Undo or make lower case.
4693 */
4694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004695nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004697 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004699 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 cap->cmdchar = 'g';
4701 cap->nchar = 'u';
4702 nv_operator(cap);
4703 }
4704 else
4705 nv_kundo(cap);
4706}
4707
4708/*
4709 * <Undo> command.
4710 */
4711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004712nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 if (!checkclearopq(cap->oap))
4715 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004716#ifdef FEAT_JOB_CHANNEL
4717 if (bt_prompt(curbuf))
4718 {
4719 clearopbeep(cap->oap);
4720 return;
4721 }
4722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 u_undo((int)cap->count1);
4724 curwin->w_set_curswant = TRUE;
4725 }
4726}
4727
4728/*
4729 * Handle the "r" command.
4730 */
4731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004732nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 char_u *ptr;
4735 int had_ctrl_v;
4736 long n;
4737
4738 if (checkclearop(cap->oap))
4739 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004740#ifdef FEAT_JOB_CHANNEL
4741 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4742 {
4743 clearopbeep(cap->oap);
4744 return;
4745 }
4746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004748 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if (cap->nchar == Ctrl_V)
4750 {
4751 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004752 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004753 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (cap->nchar > DEL)
4755 had_ctrl_v = NUL;
4756 }
4757 else
4758 had_ctrl_v = NUL;
4759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004760 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004761 if (IS_SPECIAL(cap->nchar))
4762 {
4763 clearopbeep(cap->oap);
4764 return;
4765 }
4766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004767 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (VIsual_active)
4769 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004770 if (got_int)
4771 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004772 if (had_ctrl_v)
4773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 // Use a special (negative) number to make a difference between a
4775 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004776 if (cap->nchar == CAR)
4777 cap->nchar = REPLACE_CR_NCHAR;
4778 else if (cap->nchar == NL)
4779 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 nv_operator(cap);
4782 return;
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004785 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 if (virtual_active())
4787 {
4788 if (u_save_cursor() == FAIL)
4789 return;
4790 if (gchar_cursor() == NUL)
4791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004792 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 coladvance_force((colnr_T)(getviscol() + cap->count1));
4794 curwin->w_cursor.col -= cap->count1;
4795 }
4796 else if (gchar_cursor() == TAB)
4797 coladvance_force(getviscol());
4798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004800 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004802 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004803 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 clearopbeep(cap->oap);
4806 return;
4807 }
4808
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004809 // Replacing with a TAB is done by edit() when it is complicated because
4810 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4811 // Other characters are done below to avoid problems with things like
4812 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4814 {
4815 stuffnumReadbuff(cap->count1);
4816 stuffcharReadbuff('R');
4817 stuffcharReadbuff('\t');
4818 stuffcharReadbuff(ESC);
4819 return;
4820 }
4821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004822 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (u_save_cursor() == FAIL)
4824 return;
4825
4826 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4827 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004828 // Replace character(s) by a single newline.
4829 // Strange vi behaviour: Only one newline is inserted.
4830 // Delete the characters here.
4831 // Insert the newline with an insert command, takes care of
4832 // autoindent. The insert command depends on being on the last
4833 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004834 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 stuffcharReadbuff('\r');
4836 stuffcharReadbuff(ESC);
4837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004838 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 invoke_edit(cap, TRUE, 'r', FALSE);
4840 }
4841 else
4842 {
4843 prep_redo(cap->oap->regname, cap->count1,
4844 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4845
4846 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (has_mbyte)
4848 {
4849 int old_State = State;
4850
4851 if (cap->ncharC1 != 0)
4852 AppendCharToRedobuff(cap->ncharC1);
4853 if (cap->ncharC2 != 0)
4854 AppendCharToRedobuff(cap->ncharC2);
4855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004856 // This is slow, but it handles replacing a single-byte with a
4857 // multi-byte and the other way around. Also handles adding
4858 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 for (n = cap->count1; n > 0; --n)
4860 {
4861 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004862 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4863 {
4864 int c = ins_copychar(curwin->w_cursor.lnum
4865 + (cap->nchar == Ctrl_Y ? -1 : 1));
4866 if (c != NUL)
4867 ins_char(c);
4868 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004870 ++curwin->w_cursor.col;
4871 }
4872 else
4873 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 State = old_State;
4875 if (cap->ncharC1 != 0)
4876 ins_char(cap->ncharC1);
4877 if (cap->ncharC2 != 0)
4878 ins_char(cap->ncharC2);
4879 }
4880 }
4881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004883 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 for (n = cap->count1; n > 0; --n)
4885 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004886 // Get ptr again, because u_save and/or showmatch() will have
4887 // released the line. This may also happen in ins_copychar().
4888 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004889 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4890 {
4891 int c = ins_copychar(curwin->w_cursor.lnum
4892 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004893
4894 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004895 if (c != NUL)
4896 ptr[curwin->w_cursor.col] = c;
4897 }
4898 else
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 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (p_sm && msg_silent == 0)
4904 showmatch(cap->nchar);
4905 ++curwin->w_cursor.col;
4906 }
4907#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004908 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004910 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
Bram Moolenaar009b2592004-10-24 19:18:58 +00004912 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004913 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004915 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917#endif
4918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004919 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 changed_bytes(curwin->w_cursor.lnum,
4921 (colnr_T)(curwin->w_cursor.col - cap->count1));
4922 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004923 --curwin->w_cursor.col; // cursor on the last replaced char
4924 // if the character on the left of the current cursor is a multi-byte
4925 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (has_mbyte)
4927 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 curbuf->b_op_end = curwin->w_cursor;
4929 curwin->w_set_curswant = TRUE;
4930 set_last_insert(cap->nchar);
4931 }
4932}
4933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934/*
4935 * 'o': Exchange start and end of Visual area.
4936 * 'O': same, but in block mode exchange left and right corners.
4937 */
4938 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004939v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 pos_T old_cursor;
4942 colnr_T left, right;
4943
4944 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4945 {
4946 old_cursor = curwin->w_cursor;
4947 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4948 curwin->w_cursor.lnum = VIsual.lnum;
4949 coladvance(left);
4950 VIsual = curwin->w_cursor;
4951
4952 curwin->w_cursor.lnum = old_cursor.lnum;
4953 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 // 'selection "exclusive" and cursor at right-bottom corner: move it
4955 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4957 ++curwin->w_curswant;
4958 coladvance(curwin->w_curswant);
4959 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004961 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 curwin->w_cursor.lnum = VIsual.lnum;
4964 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4965 ++right;
4966 coladvance(right);
4967 VIsual = curwin->w_cursor;
4968
4969 curwin->w_cursor.lnum = old_cursor.lnum;
4970 coladvance(left);
4971 curwin->w_curswant = left;
4972 }
4973 }
4974 else
4975 {
4976 old_cursor = curwin->w_cursor;
4977 curwin->w_cursor = VIsual;
4978 VIsual = old_cursor;
4979 curwin->w_set_curswant = TRUE;
4980 }
4981}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983/*
4984 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4985 */
4986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004989 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 cap->cmdchar = 'c';
4992 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004993 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 VIsual_mode = 'V';
4995 nv_operator(cap);
4996 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004997 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005000 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (virtual_active())
5004 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5006 }
5007 }
5008}
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * "gr".
5012 */
5013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005014nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (VIsual_active)
5017 {
5018 cap->cmdchar = 'r';
5019 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005020 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005022 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005025 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else
5027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005029 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 stuffcharReadbuff(cap->extra_char);
5031 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (virtual_active())
5033 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 invoke_edit(cap, TRUE, 'v', FALSE);
5035 }
5036 }
5037}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039/*
5040 * Swap case for "~" command, when it does not work like an operator.
5041 */
5042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005043n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 long n;
5046 pos_T startpos;
5047 int did_change = 0;
5048#ifdef FEAT_NETBEANS_INTG
5049 pos_T pos;
5050 char_u *ptr;
5051 int count;
5052#endif
5053
5054 if (checkclearopq(cap->oap))
5055 return;
5056
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005057 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 clearopbeep(cap->oap);
5060 return;
5061 }
5062
5063 prep_redo_cmd(cap);
5064
5065 if (u_save_cursor() == FAIL)
5066 return;
5067
5068 startpos = curwin->w_cursor;
5069#ifdef FEAT_NETBEANS_INTG
5070 pos = startpos;
5071#endif
5072 for (n = cap->count1; n > 0; --n)
5073 {
5074 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5075 inc_cursor();
5076 if (gchar_cursor() == NUL)
5077 {
5078 if (vim_strchr(p_ww, '~') != NULL
5079 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5080 {
5081#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005082 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 if (did_change)
5085 {
5086 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005087 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005088 netbeans_removed(curbuf, pos.lnum, pos.col,
5089 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005091 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 pos.col = 0;
5094 pos.lnum++;
5095 }
5096#endif
5097 ++curwin->w_cursor.lnum;
5098 curwin->w_cursor.col = 0;
5099 if (n > 1)
5100 {
5101 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5102 break;
5103 u_clearline();
5104 }
5105 }
5106 else
5107 break;
5108 }
5109 }
5110#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005111 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 ptr = ml_get(pos.lnum);
5114 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005115 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5116 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118#endif
5119
5120
5121 check_cursor();
5122 curwin->w_set_curswant = TRUE;
5123 if (did_change)
5124 {
5125 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5126 0L);
5127 curbuf->b_op_start = startpos;
5128 curbuf->b_op_end = curwin->w_cursor;
5129 if (curbuf->b_op_end.col > 0)
5130 --curbuf->b_op_end.col;
5131 }
5132}
5133
5134/*
5135 * Move cursor to mark.
5136 */
5137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005138nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
5140 if (check_mark(pos) == FAIL)
5141 clearop(cap->oap);
5142 else
5143 {
5144 if (cap->cmdchar == '\''
5145 || cap->cmdchar == '`'
5146 || cap->cmdchar == '['
5147 || cap->cmdchar == ']')
5148 setpcmark();
5149 curwin->w_cursor = *pos;
5150 if (flag)
5151 beginline(BL_WHITE | BL_FIX);
5152 else
5153 check_cursor();
5154 }
5155 cap->oap->motion_type = flag ? MLINE : MCHAR;
5156 if (cap->cmdchar == '`')
5157 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005158 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 curwin->w_set_curswant = TRUE;
5160}
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162/*
5163 * Handle commands that are operators in Visual mode.
5164 */
5165 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005166v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 // Uppercase means linewise, except in block mode, then "D" deletes till
5171 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (isupper(cap->cmdchar))
5173 {
5174 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005175 {
5176 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5180 curwin->w_curswant = MAXCOL;
5181 }
5182 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5183 nv_operator(cap);
5184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186/*
5187 * "s" and "S" commands.
5188 */
5189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005192#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005193 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005194 if (term_swap_diff() == OK)
5195 return;
5196#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005197#ifdef FEAT_JOB_CHANNEL
5198 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5199 {
5200 clearopbeep(cap->oap);
5201 return;
5202 }
5203#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005204 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005207 {
5208 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 cap->cmdchar = 'c';
5212 nv_operator(cap);
5213 }
5214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 nv_optrans(cap);
5216}
5217
5218/*
5219 * Abbreviated commands.
5220 */
5221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005225 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005227 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (VIsual_active)
5229 v_visop(cap);
5230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 nv_optrans(cap);
5232}
5233
5234/*
5235 * Translate a command into another command.
5236 */
5237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5241 (char_u *)"d$", (char_u *)"c$",
5242 (char_u *)"cl", (char_u *)"cc",
5243 (char_u *)"yy", (char_u *)":s\r"};
5244 static char_u *str = (char_u *)"xXDCsSY&";
5245
5246 if (!checkclearopq(cap->oap))
5247 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005248 // In Vi "2D" doesn't delete the next line. Can't translate it
5249 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005250 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5251 {
5252 cap->oap->start = curwin->w_cursor;
5253 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005254#ifdef FEAT_EVAL
5255 set_op_var(OP_DELETE);
5256#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005257 cap->count1 = 1;
5258 nv_dollar(cap);
5259 finish_op = TRUE;
5260 ResetRedobuff();
5261 AppendCharToRedobuff('D');
5262 }
5263 else
5264 {
5265 if (cap->count0)
5266 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005267 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270 cap->opcount = 0;
5271}
5272
5273/*
5274 * "'" and "`" commands. Also for "g'" and "g`".
5275 * cap->arg is TRUE for "'" and "g'".
5276 */
5277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 pos_T *pos;
5281 int c;
5282#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005283 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005284 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
5286
5287 if (cap->cmdchar == 'g')
5288 c = cap->extra_char;
5289 else
5290 c = cap->nchar;
5291 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005292 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 if (cap->arg)
5295 {
5296 check_cursor_lnum();
5297 beginline(BL_WHITE | BL_FIX);
5298 }
5299 else
5300 check_cursor();
5301 }
5302 else
5303 nv_cursormark(cap, cap->arg, pos);
5304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005305 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (!virtual_active())
5307 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005308 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309#ifdef FEAT_FOLDING
5310 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005311 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005312 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 && (fdo_flags & FDO_MARK)
5314 && old_KeyTyped)
5315 foldOpenCursor();
5316#endif
5317}
5318
5319/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005320 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 */
5322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005323nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005326#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 if (!checkclearopq(cap->oap))
5332 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005333 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5334 {
5335 if (goto_tabpage_lastused() == FAIL)
5336 clearopbeep(cap->oap);
5337 return;
5338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (cap->cmdchar == 'g')
5340 pos = movechangelist((int)cap->count1);
5341 else
5342 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
5345 curwin->w_set_curswant = TRUE;
5346 check_cursor();
5347 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005348 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 nv_cursormark(cap, FALSE, pos);
5350 else if (cap->cmdchar == 'g')
5351 {
5352 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005353 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005355 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005357 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 else
5360 clearopbeep(cap->oap);
5361# ifdef FEAT_FOLDING
5362 if (cap->oap->op_type == OP_NOP
5363 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5364 && (fdo_flags & FDO_MARK)
5365 && old_KeyTyped)
5366 foldOpenCursor();
5367# endif
5368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369}
5370
5371/*
5372 * Handle '"' command.
5373 */
5374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005375nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 if (checkclearop(cap->oap))
5378 return;
5379#ifdef FEAT_EVAL
5380 if (cap->nchar == '=')
5381 cap->nchar = get_expr_register();
5382#endif
5383 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5384 {
5385 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005386 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#ifdef FEAT_EVAL
5388 set_reg_var(cap->oap->regname);
5389#endif
5390 }
5391 else
5392 clearopbeep(cap->oap);
5393}
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395/*
5396 * Handle "v", "V" and "CTRL-V" commands.
5397 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5398 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005399 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 */
5401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005402nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005404 if (cap->cmdchar == Ctrl_Q)
5405 cap->cmdchar = Ctrl_V;
5406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005407 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5408 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 if (cap->oap->op_type != OP_NOP)
5410 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005411 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005412 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return;
5414 }
5415
5416 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005417 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005421 else // toggle char/block mode
5422 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 VIsual_mode = cap->cmdchar;
5424 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005425 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005427 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005432 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 VIsual = curwin->w_cursor;
5436
5437 VIsual_active = TRUE;
5438 VIsual_reselect = TRUE;
5439 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005443 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005445 // For V and ^V, we multiply the number of lines even if there
5446 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5448 {
5449 curwin->w_cursor.lnum +=
5450 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005451 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 VIsual_mode = resel_VIsual_mode;
5454 if (VIsual_mode == 'v')
5455 {
5456 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005457 {
5458 validate_virtcol();
5459 curwin->w_curswant = curwin->w_virtcol
5460 + resel_VIsual_vcol * cap->count0 - 1;
5461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005463 curwin->w_curswant = resel_VIsual_vcol;
5464 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005466 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
5468 curwin->w_curswant = MAXCOL;
5469 coladvance((colnr_T)MAXCOL);
5470 }
5471 else if (VIsual_mode == Ctrl_V)
5472 {
5473 validate_virtcol();
5474 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005475 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 coladvance(curwin->w_curswant);
5477 }
5478 else
5479 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005480 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 else
5483 {
5484 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005485 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 may_start_select('c');
5487 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005488 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005489 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005490 if (cap->count0 > 0 && --cap->count1 > 0)
5491 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005492 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005493 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5494 nv_right(cap);
5495 else if (VIsual_mode == 'V')
5496 nv_down(cap);
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499 }
5500}
5501
5502/*
5503 * Start selection for Shift-movement keys.
5504 */
5505 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005506start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005508 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 may_start_select('k');
5510 n_start_visual_mode('v');
5511}
5512
5513/*
5514 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5515 */
5516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005517may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 VIsual_select = (stuff_empty() && typebuf_typed()
5520 && (vim_strchr(p_slm, c) != NULL));
5521}
5522
5523/*
5524 * Start Visual mode "c".
5525 * Should set VIsual_select before calling this.
5526 */
5527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005528n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005530#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005531 int cursor_line_was_concealed = curwin->w_p_cole > 0
5532 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005533#endif
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 VIsual_mode = c;
5536 VIsual_active = TRUE;
5537 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005538
5539 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005540 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005541 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005542 {
5543 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 VIsual = curwin->w_cursor;
5547
5548#ifdef FEAT_FOLDING
5549 foldAdjustVisual();
5550#endif
5551
Bram Moolenaara0620062021-11-17 16:52:40 +00005552 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005554#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005555 // Check if redraw is needed after changing the state.
5556 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005557#endif
5558
Bram Moolenaar09df3122006-01-23 22:23:09 +00005559 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005560 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005562 // Make sure the clipboard gets updated. Needed because start and
5563 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 clip_star.vmode = NUL;
5565#endif
5566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005567 // Only need to redraw this line, unless still need to redraw an old
5568 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 if (curwin->w_redr_type < INVERTED)
5570 {
5571 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5572 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5573 }
5574}
5575
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577/*
5578 * CTRL-W: Window commands
5579 */
5580 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005583 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005585 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005586 cap->cmdchar = ':';
5587 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005588 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005589 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005590 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593
5594/*
5595 * CTRL-Z: Suspend
5596 */
5597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005598nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005602 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005603 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005607 * "gv": Reselect the previous Visual area. If Visual already active,
5608 * exchange previous and current Visual area.
5609 */
5610 static void
5611nv_gv_cmd(cmdarg_T *cap)
5612{
5613 pos_T tpos;
5614 int i;
5615
5616 if (checkclearop(cap->oap))
5617 return;
5618
5619 if (curbuf->b_visual.vi_start.lnum == 0
5620 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5621 || curbuf->b_visual.vi_end.lnum == 0)
5622 {
5623 beep_flush();
5624 return;
5625 }
5626
5627 // set w_cursor to the start of the Visual area, tpos to the end
5628 if (VIsual_active)
5629 {
5630 i = VIsual_mode;
5631 VIsual_mode = curbuf->b_visual.vi_mode;
5632 curbuf->b_visual.vi_mode = i;
5633# ifdef FEAT_EVAL
5634 curbuf->b_visual_mode_eval = i;
5635# endif
5636 i = curwin->w_curswant;
5637 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5638 curbuf->b_visual.vi_curswant = i;
5639
5640 tpos = curbuf->b_visual.vi_end;
5641 curbuf->b_visual.vi_end = curwin->w_cursor;
5642 curwin->w_cursor = curbuf->b_visual.vi_start;
5643 curbuf->b_visual.vi_start = VIsual;
5644 }
5645 else
5646 {
5647 VIsual_mode = curbuf->b_visual.vi_mode;
5648 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5649 tpos = curbuf->b_visual.vi_end;
5650 curwin->w_cursor = curbuf->b_visual.vi_start;
5651 }
5652
5653 VIsual_active = TRUE;
5654 VIsual_reselect = TRUE;
5655
5656 // Set Visual to the start and w_cursor to the end of the Visual
5657 // area. Make sure they are on an existing character.
5658 check_cursor();
5659 VIsual = curwin->w_cursor;
5660 curwin->w_cursor = tpos;
5661 check_cursor();
5662 update_topline();
5663
5664 // When called from normal "g" command: start Select mode when
5665 // 'selectmode' contains "cmd". When called for K_SELECT, always
5666 // start Select mode.
5667 if (cap->arg)
5668 {
5669 VIsual_select = TRUE;
5670 VIsual_select_reg = 0;
5671 }
5672 else
5673 may_start_select('c');
5674 setmouse();
5675#ifdef FEAT_CLIPBOARD
5676 // Make sure the clipboard gets updated. Needed because start and
5677 // end are still the same, and the selection needs to be owned
5678 clip_star.vmode = NUL;
5679#endif
5680 redraw_curbuf_later(INVERTED);
5681 showmode();
5682}
5683
5684/*
5685 * "g0", "g^" : Like "0" and "^" but for screen lines.
5686 * "gm": middle of "g0" and "g$".
5687 */
5688 static void
5689nv_g_home_m_cmd(cmdarg_T *cap)
5690{
5691 int i;
5692 int flag = FALSE;
5693
5694 if (cap->nchar == '^')
5695 flag = TRUE;
5696
5697 cap->oap->motion_type = MCHAR;
5698 cap->oap->inclusive = FALSE;
5699 if (curwin->w_p_wrap && curwin->w_width != 0)
5700 {
5701 int width1 = curwin->w_width - curwin_col_off();
5702 int width2 = width1 + curwin_col_off2();
5703
5704 validate_virtcol();
5705 i = 0;
5706 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5707 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5708 }
5709 else
5710 i = curwin->w_leftcol;
5711 // Go to the middle of the screen line. When 'number' or
5712 // 'relativenumber' is on and lines are wrapping the middle can be more
5713 // to the left.
5714 if (cap->nchar == 'm')
5715 i += (curwin->w_width - curwin_col_off()
5716 + ((curwin->w_p_wrap && i > 0)
5717 ? curwin_col_off2() : 0)) / 2;
5718 coladvance((colnr_T)i);
5719 if (flag)
5720 {
5721 do
5722 i = gchar_cursor();
5723 while (VIM_ISWHITE(i) && oneright() == OK);
5724 curwin->w_valid &= ~VALID_WCOL;
5725 }
5726 curwin->w_set_curswant = TRUE;
5727}
5728
5729/*
5730 * "g_": to the last non-blank character in the line or <count> lines
5731 * downward.
5732 */
5733 static void
5734nv_g_underscore_cmd(cmdarg_T *cap)
5735{
5736 char_u *ptr;
5737
5738 cap->oap->motion_type = MCHAR;
5739 cap->oap->inclusive = TRUE;
5740 curwin->w_curswant = MAXCOL;
5741 if (cursor_down((long)(cap->count1 - 1),
5742 cap->oap->op_type == OP_NOP) == FAIL)
5743 {
5744 clearopbeep(cap->oap);
5745 return;
5746 }
5747
5748 ptr = ml_get_curline();
5749
5750 // In Visual mode we may end up after the line.
5751 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5752 --curwin->w_cursor.col;
5753
5754 // Decrease the cursor column until it's on a non-blank.
5755 while (curwin->w_cursor.col > 0
5756 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5757 --curwin->w_cursor.col;
5758 curwin->w_set_curswant = TRUE;
5759 adjust_for_sel(cap);
5760}
5761
5762/*
5763 * "g$" : Like "$" but for screen lines.
5764 */
5765 static void
5766nv_g_dollar_cmd(cmdarg_T *cap)
5767{
5768 oparg_T *oap = cap->oap;
5769 int i;
5770 int col_off = curwin_col_off();
5771
5772 oap->motion_type = MCHAR;
5773 oap->inclusive = TRUE;
5774 if (curwin->w_p_wrap && curwin->w_width != 0)
5775 {
5776 curwin->w_curswant = MAXCOL; // so we stay at the end
5777 if (cap->count1 == 1)
5778 {
5779 int width1 = curwin->w_width - col_off;
5780 int width2 = width1 + curwin_col_off2();
5781
5782 validate_virtcol();
5783 i = width1 - 1;
5784 if (curwin->w_virtcol >= (colnr_T)width1)
5785 i += ((curwin->w_virtcol - width1) / width2 + 1)
5786 * width2;
5787 coladvance((colnr_T)i);
5788
5789 // Make sure we stick in this column.
5790 validate_virtcol();
5791 curwin->w_curswant = curwin->w_virtcol;
5792 curwin->w_set_curswant = FALSE;
5793 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5794 {
5795 // Check for landing on a character that got split at
5796 // the end of the line. We do not want to advance to
5797 // the next screen line.
5798 if (curwin->w_virtcol > (colnr_T)i)
5799 --curwin->w_cursor.col;
5800 }
5801 }
5802 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5803 clearopbeep(oap);
5804 }
5805 else
5806 {
5807 if (cap->count1 > 1)
5808 // if it fails, let the cursor still move to the last char
5809 (void)cursor_down(cap->count1 - 1, FALSE);
5810
5811 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5812 coladvance((colnr_T)i);
5813
5814 // if the character doesn't fit move one back
5815 if (curwin->w_cursor.col > 0
5816 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5817 {
5818 colnr_T vcol;
5819
5820 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5821 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5822 --curwin->w_cursor.col;
5823 }
5824
5825 // Make sure we stick in this column.
5826 validate_virtcol();
5827 curwin->w_curswant = curwin->w_virtcol;
5828 curwin->w_set_curswant = FALSE;
5829 }
5830}
5831
5832/*
5833 * "gi": start Insert at the last position.
5834 */
5835 static void
5836nv_gi_cmd(cmdarg_T *cap)
5837{
5838 int i;
5839
5840 if (curbuf->b_last_insert.lnum != 0)
5841 {
5842 curwin->w_cursor = curbuf->b_last_insert;
5843 check_cursor_lnum();
5844 i = (int)STRLEN(ml_get_curline());
5845 if (curwin->w_cursor.col > (colnr_T)i)
5846 {
5847 if (virtual_active())
5848 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5849 curwin->w_cursor.col = i;
5850 }
5851 }
5852 cap->cmdchar = 'i';
5853 nv_edit(cap);
5854}
5855
5856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 * Commands starting with "g".
5858 */
5859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005860nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865 switch (cap->nchar)
5866 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005867 case Ctrl_A:
5868 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005870 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005871 if (!VIsual_active && cap->nchar == Ctrl_A)
5872 vim_mem_profile_dump();
5873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005875 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005876 if (VIsual_active)
5877 {
5878 cap->arg = TRUE;
5879 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005880 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005881 nv_addsub(cap);
5882 }
5883 else
5884 clearopbeep(oap);
5885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005887 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 case 'R':
5889 cap->arg = TRUE;
5890 nv_Replace(cap);
5891 break;
5892
5893 case 'r':
5894 nv_vreplace(cap);
5895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 case '&':
5898 do_cmdline_cmd((char_u *)"%s//~/&");
5899 break;
5900
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005901 // "gv": Reselect the previous Visual area. If Visual already active,
5902 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005904 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005906
5907 // "gV": Don't reselect the previous Visual area after a Select mode
5908 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 case 'V':
5910 VIsual_reselect = FALSE;
5911 break;
5912
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005913 // "gh": start Select mode.
5914 // "gH": start Select line mode.
5915 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 case K_BS:
5917 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005918 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 case 'h':
5920 case 'H':
5921 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 cap->cmdchar = cap->nchar + ('v' - 'h');
5923 cap->arg = TRUE;
5924 nv_visual(cap);
5925 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005927 // "gn", "gN" visually select next/previous search match
5928 // "gn" selects next match
5929 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005930 case 'N':
5931 case 'n':
5932 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005933 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005936 // "gj" and "gk" two new funny movement keys -- up and down
5937 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 case 'j':
5939 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005940 // with 'nowrap' it works just like the normal "j" command.
5941 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 oap->motion_type = MLINE;
5944 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5945 }
5946 else
5947 i = nv_screengo(oap, FORWARD, cap->count1);
5948 if (i == FAIL)
5949 clearopbeep(oap);
5950 break;
5951
5952 case 'k':
5953 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005954 // with 'nowrap' it works just like the normal "k" command.
5955 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
5957 oap->motion_type = MLINE;
5958 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5959 }
5960 else
5961 i = nv_screengo(oap, BACKWARD, cap->count1);
5962 if (i == FAIL)
5963 clearopbeep(oap);
5964 break;
5965
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005966 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 case 'J':
5968 nv_join(cap);
5969 break;
5970
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005971 // "g0", "g^" : Like "0" and "^" but for screen lines.
5972 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 case '0':
5975 case 'm':
5976 case K_HOME:
5977 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005978 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 break;
5980
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005981 case 'M':
5982 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005983 oap->motion_type = MCHAR;
5984 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00005985 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005986 if (cap->count0 > 0 && cap->count0 <= 100)
5987 coladvance((colnr_T)(i * cap->count0 / 100));
5988 else
5989 coladvance((colnr_T)(i / 2));
5990 curwin->w_set_curswant = TRUE;
5991 }
5992 break;
5993
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005994 // "g_": to the last non-blank character in the line or <count> lines
5995 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005997 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 break;
5999
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006000 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 case '$':
6002 case K_END:
6003 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006004 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 break;
6006
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006007 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 case '*':
6009 case '#':
6010#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006011 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006013 case Ctrl_RSB: // :tag or :tselect for current identifier
6014 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 nv_ident(cap);
6016 break;
6017
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006018 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 case 'e':
6020 case 'E':
6021 oap->motion_type = MCHAR;
6022 curwin->w_set_curswant = TRUE;
6023 oap->inclusive = TRUE;
6024 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6025 clearopbeep(oap);
6026 break;
6027
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006028 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006030 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 break;
6032
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006035 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 break;
6037
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006038 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 case 'I':
6040 beginline(0);
6041 if (!checkclearopq(oap))
6042 invoke_edit(cap, FALSE, 'g', FALSE);
6043 break;
6044
6045#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006046 // "gf": goto file, edit file under cursor
6047 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006049 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 nv_gotofile(cap);
6051 break;
6052#endif
6053
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006054 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 case '\'':
6056 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006057 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 case '`':
6059 nv_gomark(cap);
6060 break;
6061
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006062 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006064 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 break;
6066
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006067 // "ga": Display the ascii value of the character under the
6068 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 case 'a':
6070 do_ascii(NULL);
6071 break;
6072
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006073 // "g8": Display the bytes used for the UTF-8 character under the
6074 // cursor. It is displayed in hex.
6075 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006077 if (cap->count0 == 8)
6078 utf_find_illegal();
6079 else
6080 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006083 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006084 case '<':
6085 show_sb_text();
6086 break;
6087
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006088 // "gg": Goto the first line in file. With a count it goes to
6089 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 case 'g':
6091 cap->arg = FALSE;
6092 nv_goto(cap);
6093 break;
6094
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006095 // Two-character operators:
6096 // "gq" Format text
6097 // "gw" Format text and keep cursor position
6098 // "g~" Toggle the case of the text.
6099 // "gu" Change text to lower case.
6100 // "gU" Change text to upper case.
6101 // "g?" rot13 encoding
6102 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 case 'q':
6104 case 'w':
6105 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 case '~':
6108 case 'u':
6109 case 'U':
6110 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006111 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 nv_operator(cap);
6113 break;
6114
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006115 // "gd": Find first occurrence of pattern under the cursor in the
6116 // current function
6117 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 case 'd':
6119 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006120 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 break;
6122
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006123 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 case K_MIDDLEMOUSE:
6125 case K_MIDDLEDRAG:
6126 case K_MIDDLERELEASE:
6127 case K_LEFTMOUSE:
6128 case K_LEFTDRAG:
6129 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006130 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 case K_RIGHTMOUSE:
6132 case K_RIGHTDRAG:
6133 case K_RIGHTRELEASE:
6134 case K_X1MOUSE:
6135 case K_X1DRAG:
6136 case K_X1RELEASE:
6137 case K_X2MOUSE:
6138 case K_X2DRAG:
6139 case K_X2RELEASE:
6140 mod_mask = MOD_MASK_CTRL;
6141 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 case K_IGNORE:
6145 break;
6146
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006147 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 case 'p':
6149 case 'P':
6150 nv_put(cap);
6151 break;
6152
6153#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006154 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 case 'o':
6156 goto_byte(cap->count0);
6157 break;
6158#endif
6159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006160 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006162 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006165 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 break;
6167 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (!checkclearopq(oap))
6170 do_exmode(TRUE);
6171 break;
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 case ',':
6174 nv_pcmark(cap);
6175 break;
6176
6177 case ';':
6178 cap->count1 = -cap->count1;
6179 nv_pcmark(cap);
6180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006183 if (!checkclearop(oap))
6184 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006186 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006187 if (!checkclearop(oap))
6188 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006189 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190
Bram Moolenaar62a23252020-08-09 14:04:42 +02006191 case TAB:
6192 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6193 clearopbeep(oap);
6194 break;
6195
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006196 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006197 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006198 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006199 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006200 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006201 break;
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 default:
6204 clearopbeep(oap);
6205 break;
6206 }
6207}
6208
6209/*
6210 * Handle "o" and "O" commands.
6211 */
6212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006213n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006215#ifdef FEAT_CONCEAL
6216 linenr_T oldline = curwin->w_cursor.lnum;
6217#endif
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (!checkclearopq(cap->oap))
6220 {
6221#ifdef FEAT_FOLDING
6222 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006223 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 (void)hasFolding(curwin->w_cursor.lnum,
6225 &curwin->w_cursor.lnum, NULL);
6226 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006227 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 (void)hasFolding(curwin->w_cursor.lnum,
6229 NULL, &curwin->w_cursor.lnum);
6230#endif
6231 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6232 (cap->cmdchar == 'O' ? 1 : 0)),
6233 (linenr_T)(curwin->w_cursor.lnum +
6234 (cap->cmdchar == 'o' ? 1 : 0))
6235 ) == OK
6236 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006237 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006238 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006240#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006241 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006242 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006243#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006244#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006245 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006246 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006247 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006248#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006249 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006250 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6251 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6253 }
6254 }
6255}
6256
6257/*
6258 * "." command: redo last change.
6259 */
6260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006261nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 if (!checkclearopq(cap->oap))
6264 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006265 // If "restart_edit" is TRUE, the last but one command is repeated
6266 // instead of the last command (inserting text). This is used for
6267 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6269 clearopbeep(cap->oap);
6270 }
6271}
6272
6273/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006274 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 */
6276 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006277nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006279 if (VIsual_select && VIsual_active)
6280 {
6281 int reg;
6282 // Get register name
6283 ++no_mapping;
6284 ++allow_keys;
6285 reg = plain_vgetc();
6286 LANGMAP_ADJUST(reg, TRUE);
6287 --no_mapping;
6288 --allow_keys;
6289
6290 if (reg == '"')
6291 // the unnamed register is 0
6292 reg = 0;
6293
6294 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6295 return;
6296 }
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (!checkclearopq(cap->oap))
6299 {
6300 u_redo((int)cap->count1);
6301 curwin->w_set_curswant = TRUE;
6302 }
6303}
6304
6305/*
6306 * Handle "U" command.
6307 */
6308 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006309nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006311 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006312 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006314 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 cap->cmdchar = 'g';
6316 cap->nchar = 'U';
6317 nv_operator(cap);
6318 }
6319 else if (!checkclearopq(cap->oap))
6320 {
6321 u_undoline();
6322 curwin->w_set_curswant = TRUE;
6323 }
6324}
6325
6326/*
6327 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6328 * single character.
6329 */
6330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006331nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006333 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006334 {
6335#ifdef FEAT_JOB_CHANNEL
6336 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6337 {
6338 clearopbeep(cap->oap);
6339 return;
6340 }
6341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 else
6345 nv_operator(cap);
6346}
6347
6348/*
6349 * Handle an operator command.
6350 * The actual work is done by do_pending_operator().
6351 */
6352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006353nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355 int op_type;
6356
6357 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006358#ifdef FEAT_JOB_CHANNEL
6359 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6360 {
6361 clearopbeep(cap->oap);
6362 return;
6363 }
6364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006366 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 nv_lineop(cap);
6368 else if (!checkclearop(cap->oap))
6369 {
6370 cap->oap->start = curwin->w_cursor;
6371 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006372#ifdef FEAT_EVAL
6373 set_op_var(op_type);
6374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376}
6377
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006378#ifdef FEAT_EVAL
6379/*
6380 * Set v:operator to the characters for "optype".
6381 */
6382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006383set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006384{
6385 char_u opchars[3];
6386
6387 if (optype == OP_NOP)
6388 set_vim_var_string(VV_OP, NULL, 0);
6389 else
6390 {
6391 opchars[0] = get_op_char(optype);
6392 opchars[1] = get_extra_op_char(optype);
6393 opchars[2] = NUL;
6394 set_vim_var_string(VV_OP, opchars, -1);
6395 }
6396}
6397#endif
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399/*
6400 * Handle linewise operator "dd", "yy", etc.
6401 *
6402 * "_" is is a strange motion command that helps make operators more logical.
6403 * It is actually implemented, but not documented in the real Vi. This motion
6404 * command actually refers to "the current line". Commands like "dd" and "yy"
6405 * are really an alternate form of "d_" and "y_". It does accept a count, so
6406 * "d3_" works to delete 3 lines.
6407 */
6408 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006409nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 cap->oap->motion_type = MLINE;
6412 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6413 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006414 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006415 && cap->oap->motion_force != 'v'
6416 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 || cap->oap->op_type == OP_LSHIFT
6418 || cap->oap->op_type == OP_RSHIFT)
6419 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006420 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 beginline(BL_WHITE | BL_FIX);
6422}
6423
6424/*
6425 * <Home> command.
6426 */
6427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006428nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006430 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006431 if (mod_mask & MOD_MASK_CTRL)
6432 nv_goto(cap);
6433 else
6434 {
6435 cap->count0 = 1;
6436 nv_pipe(cap);
6437 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6439 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
6442/*
6443 * "|" command.
6444 */
6445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006446nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 cap->oap->motion_type = MCHAR;
6449 cap->oap->inclusive = FALSE;
6450 beginline(0);
6451 if (cap->count0 > 0)
6452 {
6453 coladvance((colnr_T)(cap->count0 - 1));
6454 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6455 }
6456 else
6457 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006458 // keep curswant at the column where we wanted to go, not where
6459 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 curwin->w_set_curswant = FALSE;
6461}
6462
6463/*
6464 * Handle back-word command "b" and "B".
6465 * cap->arg is 1 for "B"
6466 */
6467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 cap->oap->motion_type = MCHAR;
6471 cap->oap->inclusive = FALSE;
6472 curwin->w_set_curswant = TRUE;
6473 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6474 clearopbeep(cap->oap);
6475#ifdef FEAT_FOLDING
6476 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6477 foldOpenCursor();
6478#endif
6479}
6480
6481/*
6482 * Handle word motion commands "e", "E", "w" and "W".
6483 * cap->arg is TRUE for "E" and "W".
6484 */
6485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006486nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 int n;
6489 int word_end;
6490 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006491 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006493 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6495 word_end = TRUE;
6496 else
6497 word_end = FALSE;
6498 cap->oap->inclusive = word_end;
6499
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006500 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if (!word_end && cap->oap->op_type == OP_CHANGE)
6502 {
6503 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006504 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006506 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006508 // Reproduce a funny Vi behaviour: "cw" on a blank only
6509 // changes one character, not all blanks until the start of
6510 // the next word. Only do this when the 'w' flag is included
6511 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6513 {
6514 cap->oap->inclusive = TRUE;
6515 cap->oap->motion_type = MCHAR;
6516 return;
6517 }
6518 }
6519 else
6520 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006521 // This is a little strange. To match what the real Vi does,
6522 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6523 // that we are not on a space or a TAB. This seems impolite
6524 // at first, but it's really more what we mean when we say
6525 // 'cw'.
6526 // Another strangeness: When standing on the end of a word
6527 // "ce" will change until the end of the next word, but "cw"
6528 // will change only one character! This is done by setting
6529 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 cap->oap->inclusive = TRUE;
6531 word_end = TRUE;
6532 flag = TRUE;
6533 }
6534 }
6535 }
6536
6537 cap->oap->motion_type = MCHAR;
6538 curwin->w_set_curswant = TRUE;
6539 if (word_end)
6540 n = end_word(cap->count1, cap->arg, flag, FALSE);
6541 else
6542 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6543
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006544 // Don't leave the cursor on the NUL past the end of line. Unless we
6545 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006546 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006547 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 if (n == FAIL && cap->oap->op_type == OP_NOP)
6550 clearopbeep(cap->oap);
6551 else
6552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554#ifdef FEAT_FOLDING
6555 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6556 foldOpenCursor();
6557#endif
6558 }
6559}
6560
6561/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006562 * Used after a movement command: If the cursor ends up on the NUL after the
6563 * end of the line, may move it back to the last character and make the motion
6564 * inclusive.
6565 */
6566 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006567adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006568{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006569 // The cursor cannot remain on the NUL when:
6570 // - the column is > 0
6571 // - not in Visual mode or 'selection' is "o"
6572 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006573 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006574 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006575 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006576 {
6577 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006578 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006579 if (has_mbyte)
6580 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006581 oap->inclusive = TRUE;
6582 }
6583}
6584
6585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 * "0" and "^" commands.
6587 * cap->arg is the argument for beginline().
6588 */
6589 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006590nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 cap->oap->motion_type = MCHAR;
6593 cap->oap->inclusive = FALSE;
6594 beginline(cap->arg);
6595#ifdef FEAT_FOLDING
6596 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6597 foldOpenCursor();
6598#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006599 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6600 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601}
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603/*
6604 * In exclusive Visual mode, may include the last character.
6605 */
6606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006607adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006610 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (has_mbyte)
6613 inc_cursor();
6614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 ++curwin->w_cursor.col;
6616 cap->oap->inclusive = FALSE;
6617 }
6618}
6619
6620/*
6621 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6622 * Should check VIsual_mode before calling this.
6623 * Returns TRUE when backed up to the previous line.
6624 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006625 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006626unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 pos_T *pp;
6629
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006630 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006632 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 pp = &curwin->w_cursor;
6634 else
6635 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (pp->coladd > 0)
6637 --pp->coladd;
6638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 if (pp->col > 0)
6640 {
6641 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006642 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644 else if (pp->lnum > 1)
6645 {
6646 --pp->lnum;
6647 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6648 return TRUE;
6649 }
6650 }
6651 return FALSE;
6652}
6653
6654/*
6655 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6656 */
6657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006658nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006663 VIsual_select_reg = 0;
6664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 else if (VIsual_reselect)
6666 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 cap->arg = TRUE;
6669 nv_g_cmd(cap);
6670 }
6671}
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674/*
6675 * "G", "gg", CTRL-END, CTRL-HOME.
6676 * cap->arg is TRUE for "G".
6677 */
6678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006679nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 linenr_T lnum;
6682
6683 if (cap->arg)
6684 lnum = curbuf->b_ml.ml_line_count;
6685 else
6686 lnum = 1L;
6687 cap->oap->motion_type = MLINE;
6688 setpcmark();
6689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006690 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (cap->count0 != 0)
6692 lnum = cap->count0;
6693 if (lnum < 1L)
6694 lnum = 1L;
6695 else if (lnum > curbuf->b_ml.ml_line_count)
6696 lnum = curbuf->b_ml.ml_line_count;
6697 curwin->w_cursor.lnum = lnum;
6698 beginline(BL_SOL | BL_FIX);
6699#ifdef FEAT_FOLDING
6700 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6701 foldOpenCursor();
6702#endif
6703}
6704
6705/*
6706 * CTRL-\ in Normal mode.
6707 */
6708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6712 {
6713 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006714 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006715 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 restart_edit = 0;
6717#ifdef FEAT_CMDWIN
6718 if (cmdwin_type != 0)
6719 cmdwin_result = Ctrl_C;
6720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (VIsual_active)
6722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006723 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 redraw_curbuf_later(INVERTED);
6725 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006726 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (cap->nchar == Ctrl_G && p_im)
6728 restart_edit = 'a';
6729 }
6730 else
6731 clearopbeep(cap->oap);
6732}
6733
6734/*
6735 * ESC in Normal mode: beep, but don't flush buffers.
6736 * Don't even beep if we are canceling a command.
6737 */
6738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006739nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 int no_reason;
6742
6743 no_reason = (cap->oap->op_type == OP_NOP
6744 && cap->opcount == 0
6745 && cap->count0 == 0
6746 && cap->oap->regname == 0
6747 && !p_im);
6748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006749 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 if (restart_edit == 0
6752#ifdef FEAT_CMDWIN
6753 && cmdwin_type == 0
6754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006757 {
6758 if (anyBufIsChanged())
6759 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6760 else
6761 msg(_("Type :qa and press <Enter> to exit Vim"));
6762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006764 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6765 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (!p_im)
6767 restart_edit = 0;
6768#ifdef FEAT_CMDWIN
6769 if (cmdwin_type != 0)
6770 {
6771 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006772 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 return;
6774 }
6775#endif
6776 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006777#ifdef FEAT_CMDWIN
6778 else if (cmdwin_type != 0 && ex_normal_busy)
6779 {
6780 // When :normal runs out of characters while in the command line window
6781 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6782 // loop.
6783 cmdwin_result = K_IGNORE;
6784 return;
6785 }
6786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (VIsual_active)
6789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 end_visual_mode(); // stop Visual
6791 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 curwin->w_set_curswant = TRUE;
6793 redraw_curbuf_later(INVERTED);
6794 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006795 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006796 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 clearop(cap->oap);
6798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6800 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006801 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 restart_edit = 'a';
6803}
6804
6805/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006806 * Move the cursor for the "A" command.
6807 */
6808 void
6809set_cursor_for_append_to_line(void)
6810{
6811 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006812 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006813 {
6814 int save_State = State;
6815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006816 // Pretend Insert mode here to allow the cursor on the
6817 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006818 State = INSERT;
6819 coladvance((colnr_T)MAXCOL);
6820 State = save_State;
6821 }
6822 else
6823 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6824}
6825
6826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006828 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 */
6830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006831nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006833 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6835 cap->cmdchar = 'i';
6836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006839 {
6840#ifdef FEAT_TERMINAL
6841 if (term_in_normal_mode())
6842 {
6843 end_visual_mode();
6844 clearop(cap->oap);
6845 term_enter_job_mode();
6846 return;
6847 }
6848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006852 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006853 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6854 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
6856#ifdef FEAT_TEXTOBJ
6857 nv_object(cap);
6858#else
6859 clearopbeep(cap->oap);
6860#endif
6861 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006862#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006863 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006864 {
6865 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006866 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006867 return;
6868 }
6869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 else if (!curbuf->b_p_ma && !p_im)
6871 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006872 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006873 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006875 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006876 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006877 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006879 else if (cap->cmdchar == K_PS && VIsual_active)
6880 {
6881 pos_T old_pos = curwin->w_cursor;
6882 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006883 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006885 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006886 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6887 {
6888 shift_delete_registers();
6889 cap->oap->regname = '1';
6890 }
6891 else
6892 cap->oap->regname = '-';
6893 cap->cmdchar = 'd';
6894 cap->nchar = NUL;
6895 nv_operator(cap);
6896 do_pending_operator(cap, 0, FALSE);
6897 cap->cmdchar = K_PS;
6898
zeertzjq7a732522022-03-14 20:46:41 +00006899 if (*ml_get_cursor() != NUL)
6900 {
6901 if (old_visual_mode == 'V')
6902 {
6903 // In linewise Visual mode insert before the beginning of the
6904 // next line.
6905 // When the last line in the buffer was deleted then create a
6906 // new line, otherwise there is not need to move cursor.
6907 // Detect this by checking if cursor moved above Visual area.
6908 if (curwin->w_cursor.lnum < old_pos.lnum
6909 && curwin->w_cursor.lnum < old_visual.lnum)
6910 {
6911 if (u_save_cursor() == OK)
6912 {
6913 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6914 FALSE);
6915 appended_lines(curwin->w_cursor.lnum++, 1L);
6916 }
6917 }
6918 }
6919 // When the last char in the line was deleted then append.
6920 // Detect this by checking if cursor moved before Visual area.
6921 else if (curwin->w_cursor.col < old_pos.col
6922 && curwin->w_cursor.col < old_visual.col)
6923 inc_cursor();
6924 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006927 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 else if (!checkclearopq(cap->oap))
6930 {
6931 switch (cap->cmdchar)
6932 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006933 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006934 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 break;
6936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006938 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6939 beginline(BL_WHITE);
6940 else
6941 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 break;
6943
Bram Moolenaara1891842017-02-04 21:34:31 +01006944 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006945 // Bracketed paste works like "a"ppend, unless the cursor is in
6946 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006947 if (curwin->w_cursor.col == 0)
6948 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006949 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006951 case 'a': // "a"ppend is like "i"nsert on the next character.
6952 // increment coladd when in virtual space, increment the
6953 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (virtual_active()
6955 && (curwin->w_cursor.coladd > 0
6956 || *ml_get_cursor() == NUL
6957 || *ml_get_cursor() == TAB))
6958 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006959 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 inc_cursor();
6961 break;
6962 }
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6965 {
6966 int save_State = State;
6967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // Pretend Insert mode here to allow the cursor on the
6969 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 State = INSERT;
6971 coladvance(getviscol());
6972 State = save_State;
6973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6976 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01006977 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006979 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980}
6981
6982/*
6983 * Invoke edit() and take care of "restart_edit" and the return value.
6984 */
6985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006986invoke_edit(
6987 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01006989 int cmd,
6990 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 int restart_edit_save = 0;
6993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
6995 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
6996 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (repl || !stuff_empty())
6998 restart_edit_save = restart_edit;
6999 else
7000 restart_edit_save = 0;
7001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007002 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 restart_edit = 0;
7004
7005 if (edit(cmd, startln, cap->count1))
7006 cap->retval |= CA_COMMAND_BUSY;
7007
7008 if (restart_edit == 0)
7009 restart_edit = restart_edit_save;
7010}
7011
7012#ifdef FEAT_TEXTOBJ
7013/*
7014 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7015 */
7016 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007017nv_object(
7018 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 int flag;
7021 int include;
7022 char_u *mps_save;
7023
7024 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007025 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007027 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007029 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 mps_save = curbuf->b_p_mps;
7031 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7032
7033 switch (cap->nchar)
7034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007035 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 flag = current_word(cap->oap, cap->count1, include, FALSE);
7037 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 flag = current_word(cap->oap, cap->count1, include, TRUE);
7040 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007041 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 case '(':
7043 case ')':
7044 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7045 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007046 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 case '{':
7048 case '}':
7049 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7050 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 case ']':
7053 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7054 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 case '>':
7057 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7058 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007059 case 't': // "at" = a tag block (xml and html)
7060 // Do not adjust oap->end in do_pending_operator()
7061 // otherwise there are different results for 'dit'
7062 // (note leading whitespace in last line):
7063 // 1) <b> 2) <b>
7064 // foobar foobar
7065 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007066 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007067 flag = current_tagblock(cap->oap, cap->count1, include);
7068 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 flag = current_par(cap->oap, cap->count1, include, 'p');
7071 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007072 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 flag = current_sent(cap->oap, cap->count1, include);
7074 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 case '"': // "a"" = a double quoted string
7076 case '\'': // "a'" = a single quoted string
7077 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007078 flag = current_quote(cap->oap, cap->count1, include,
7079 cap->nchar);
7080 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081#if 0 // TODO
7082 case 'S': // "aS" = a section
7083 case 'f': // "af" = a filename
7084 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085#endif
7086 default:
7087 flag = FAIL;
7088 break;
7089 }
7090
7091 curbuf->b_p_mps = mps_save;
7092 if (flag == FAIL)
7093 clearopbeep(cap->oap);
7094 adjust_cursor_col();
7095 curwin->w_set_curswant = TRUE;
7096}
7097#endif
7098
7099/*
7100 * "q" command: Start/stop recording.
7101 * "q:", "q/", "q?": edit command-line in command-line window.
7102 */
7103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007104nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 if (cap->oap->op_type == OP_FORMAT)
7107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 cap->cmdchar = 'g';
7110 cap->nchar = 'q';
7111 nv_operator(cap);
7112 }
7113 else if (!checkclearop(cap->oap))
7114 {
7115#ifdef FEAT_CMDWIN
7116 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7117 {
7118 stuffcharReadbuff(cap->nchar);
7119 stuffcharReadbuff(K_CMDWIN);
7120 }
7121 else
7122#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007123 // (stop) recording into a named register, unless executing a
7124 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007125 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 clearopbeep(cap->oap);
7127 }
7128}
7129
7130/*
7131 * Handle the "@r" command.
7132 */
7133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007134nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 if (checkclearop(cap->oap))
7137 return;
7138#ifdef FEAT_EVAL
7139 if (cap->nchar == '=')
7140 {
7141 if (get_expr_register() == NUL)
7142 return;
7143 }
7144#endif
7145 while (cap->count1-- && !got_int)
7146 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007147 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
7149 clearopbeep(cap->oap);
7150 break;
7151 }
7152 line_breakcheck();
7153 }
7154}
7155
7156/*
7157 * Handle the CTRL-U and CTRL-D commands.
7158 */
7159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007160nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7163 || (cap->cmdchar == Ctrl_D
7164 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7165 clearopbeep(cap->oap);
7166 else if (!checkclearop(cap->oap))
7167 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7168}
7169
7170/*
7171 * Handle "J" or "gJ" command.
7172 */
7173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007174nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007178 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7183 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007185 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007186 if (cap->count0 <= 2)
7187 {
7188 clearopbeep(cap->oap);
7189 return;
7190 }
7191 cap->count0 = curbuf->b_ml.ml_line_count
7192 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007194
7195 prep_redo(cap->oap->regname, cap->count0,
7196 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7197 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199}
7200
7201/*
7202 * "P", "gP", "p" and "gp" commands.
7203 */
7204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007205nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007207 nv_put_opt(cap, FALSE);
7208}
7209
7210/*
7211 * "P", "gP", "p" and "gp" commands.
7212 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7213 */
7214 static void
7215nv_put_opt(cmdarg_T *cap, int fix_indent)
7216{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 int regname = 0;
7218 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007219 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007220 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 int dir;
7222 int flags = 0;
Shougo Matsushitafb552072022-01-28 16:01:13 +00007223 int save_unnamed = FALSE;
7224 yankreg_T *old_y_current, *old_y_previous;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226 if (cap->oap->op_type != OP_NOP)
7227 {
7228#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007229 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7231 {
7232 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007233 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235 else
7236#endif
7237 clearopbeep(cap->oap);
7238 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007239#ifdef FEAT_JOB_CHANNEL
7240 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7241 {
7242 clearopbeep(cap->oap);
7243 }
7244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 else
7246 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007247 if (fix_indent)
7248 {
7249 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7250 ? FORWARD : BACKWARD;
7251 flags |= PUT_FIXINDENT;
7252 }
7253 else
7254 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007255 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7256 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 prep_redo_cmd(cap);
7258 if (cap->cmdchar == 'g')
7259 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007260 else if (cap->cmdchar == 'z')
7261 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (VIsual_active)
7264 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007265 // Putting in Visual mode: The put text replaces the selected
7266 // text. First delete the selected text, then put the new text.
7267 // Need to save and restore the registers that the delete
7268 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007269 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 regname = cap->oap->regname;
Shougo Matsushitafb552072022-01-28 16:01:13 +00007271 save_unnamed = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007272#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007274#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007275 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007276 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007277#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
7281 )
7282 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007283 // The delete is going to overwrite the register we want to
7284 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 reg1 = get_register(regname, TRUE);
7286 }
7287
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007288 // Now delete the selected text. Avoid messages here.
Shougo Matsushitafb552072022-01-28 16:01:13 +00007289 if (save_unnamed)
7290 {
7291 old_y_current = get_y_current();
7292 old_y_previous = get_y_previous();
7293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 cap->cmdchar = 'd';
7295 cap->nchar = NUL;
7296 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007297 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 nv_operator(cap);
7299 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007300 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007301 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
Shougo Matsushitafb552072022-01-28 16:01:13 +00007303 if (save_unnamed)
7304 {
7305 set_y_current(old_y_current);
7306 set_y_previous(old_y_previous);
7307 }
7308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 cap->oap->regname = regname;
7311
7312 if (reg1 != NULL)
7313 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007314 // Delete probably changed the register we want to put, save
7315 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 reg2 = get_register(regname, FALSE);
7317 put_register(regname, reg1);
7318 }
7319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007320 // When deleted a linewise Visual area, put the register as
7321 // lines to avoid it joined with the next line. When deletion was
7322 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (VIsual_mode == 'V')
7324 flags |= PUT_LINE;
7325 else if (VIsual_mode == 'v')
7326 flags |= PUT_LINE_SPLIT;
7327 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7328 flags |= PUT_LINE_FORWARD;
7329 dir = BACKWARD;
7330 if ((VIsual_mode != 'V'
7331 && curwin->w_cursor.col < curbuf->b_op_start.col)
7332 || (VIsual_mode == 'V'
7333 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007334 // cursor is at the end of the line or end of file, put
7335 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007337 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007338 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007340 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (reg2 != NULL)
7344 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007345
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007346 // What to reselect with "gv"? Selecting the just put text seems to
7347 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007348 if (was_visual)
7349 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007350 curbuf->b_visual.vi_start = curbuf->b_op_start;
7351 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007352 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007353 if (*p_sel == 'e')
7354 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007355 }
7356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007357 // When all lines were selected and deleted do_put() leaves an empty
7358 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007359 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007360 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007361 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007362 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007363
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 // If the cursor was in that line, move it to the end of the last
7365 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007366 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7367 {
7368 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7369 coladvance((colnr_T)MAXCOL);
7370 }
7371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 auto_format(FALSE, TRUE);
7373 }
7374}
7375
7376/*
7377 * "o" and "O" commands.
7378 */
7379 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007380nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007383 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7385 {
7386 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007387 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389 else
7390#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007391 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007393#ifdef FEAT_JOB_CHANNEL
7394 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007395 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 n_opencmd(cap);
7399}
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401#ifdef FEAT_NETBEANS_INTG
7402 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007403nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 netbeans_keycommand(cap->nchar);
7406}
7407#endif
7408
7409#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007411nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007413 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007416
Bram Moolenaar3918c952005-03-15 22:34:55 +00007417/*
7418 * Trigger CursorHold event.
7419 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7420 * input buffer. "did_cursorhold" is set to avoid retriggering.
7421 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007423nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007424{
7425 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7426 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007427 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007428}