blob: f075986551454050ac862398ed78afe8bd45e4f2 [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/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100187 * If currently editing a cmdline or text is locked: beep and give an error
188 * message, return TRUE.
189 */
190 static int
191check_text_locked(oparg_T *oap)
192{
193 if (text_locked())
194 {
195 clearopbeep(oap);
196 text_locked_msg();
197 return TRUE;
198 }
199 return FALSE;
200}
201
202/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000203 * Handle the count before a normal command and set cap->count0.
204 */
205 static int
206normal_cmd_get_count(
207 cmdarg_T *cap,
208 int c,
209 int toplevel UNUSED,
210 int set_prevcount UNUSED,
211 int *ctrl_w,
212 int *need_flushbuf UNUSED)
213{
214getcount:
215 if (!(VIsual_active && VIsual_select))
216 {
217 // Handle a count before a command and compute ca.count0.
218 // Note that '0' is a command and not the start of a count, but it's
219 // part of a count after other digits.
220 while ((c >= '1' && c <= '9')
221 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
222 || c == '0')))
223 {
224 if (c == K_DEL || c == K_KDEL)
225 {
226 cap->count0 /= 10;
227#ifdef FEAT_CMDL_INFO
228 del_from_showcmd(4); // delete the digit and ~@%
229#endif
230 }
231 else if (cap->count0 > 99999999L)
232 {
233 cap->count0 = 999999999L;
234 }
235 else
236 {
237 cap->count0 = cap->count0 * 10 + (c - '0');
238 }
239#ifdef FEAT_EVAL
240 // Set v:count here, when called from main() and not a stuffed
241 // command, so that v:count can be used in an expression mapping
242 // right after the count. Do set it for redo.
243 if (toplevel && readbuf1_empty())
244 set_vcount_ca(cap, &set_prevcount);
245#endif
246 if (*ctrl_w)
247 {
248 ++no_mapping;
249 ++allow_keys; // no mapping for nchar, but keys
250 }
251 ++no_zero_mapping; // don't map zero here
252 c = plain_vgetc();
253 LANGMAP_ADJUST(c, TRUE);
254 --no_zero_mapping;
255 if (*ctrl_w)
256 {
257 --no_mapping;
258 --allow_keys;
259 }
260#ifdef FEAT_CMDL_INFO
261 *need_flushbuf |= add_to_showcmd(c);
262#endif
263 }
264
265 // If we got CTRL-W there may be a/another count
266 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
267 {
268 *ctrl_w = TRUE;
269 cap->opcount = cap->count0; // remember first count
270 cap->count0 = 0;
271 ++no_mapping;
272 ++allow_keys; // no mapping for nchar, but keys
273 c = plain_vgetc(); // get next character
274 LANGMAP_ADJUST(c, TRUE);
275 --no_mapping;
276 --allow_keys;
277#ifdef FEAT_CMDL_INFO
278 *need_flushbuf |= add_to_showcmd(c);
279#endif
280 goto getcount; // jump back
281 }
282 }
283
284 if (c == K_CURSORHOLD)
285 {
286 // Save the count values so that ca.opcount and ca.count0 are exactly
287 // the same when coming back here after handling K_CURSORHOLD.
288 cap->oap->prev_opcount = cap->opcount;
289 cap->oap->prev_count0 = cap->count0;
290 }
291 else if (cap->opcount != 0)
292 {
293 // If we're in the middle of an operator (including after entering a
294 // yank buffer with '"') AND we had a count before the operator, then
295 // that count overrides the current value of ca.count0.
296 // What this means effectively, is that commands like "3dw" get turned
297 // into "d3w" which makes things fall into place pretty neatly.
298 // If you give a count before AND after the operator, they are
299 // multiplied.
300 if (cap->count0)
301 {
302 if (cap->opcount >= 999999999L / cap->count0)
303 cap->count0 = 999999999L;
304 else
305 cap->count0 *= cap->opcount;
306 }
307 else
308 cap->count0 = cap->opcount;
309 }
310
311 // Always remember the count. It will be set to zero (on the next call,
312 // above) when there is no pending operator.
313 // When called from main(), save the count for use by the "count" built-in
314 // variable.
315 cap->opcount = cap->count0;
316 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
317
318#ifdef FEAT_EVAL
319 // Only set v:count when called from main() and not a stuffed command.
320 // Do set it for redo.
321 if (toplevel && readbuf1_empty())
322 set_vcount(cap->count0, cap->count1, set_prevcount);
323#endif
324
325 return c;
326}
327
328/*
329 * Returns TRUE if the normal command (cap) needs a second character.
330 */
331 static int
332normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
333{
334 return ((cmd_flags & NV_NCH)
335 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
336 && cap->oap->op_type == OP_NOP)
337 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
338 || (cap->cmdchar == 'q'
339 && cap->oap->op_type == OP_NOP
340 && reg_recording == 0
341 && reg_executing == 0)
342 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
343 && (cap->oap->op_type != OP_NOP || VIsual_active))));
344}
345
346/*
347 * Get one or more additional characters for a normal command.
348 * Return the updated command index (if changed).
349 */
350 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000351normal_cmd_get_more_chars(
352 int idx_arg,
353 cmdarg_T *cap,
354 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000355{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000356 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000357 int c;
358 int *cp;
359 int repl = FALSE; // get character for replace mode
360 int lit = FALSE; // get extra character literally
361 int langmap_active = FALSE; // using :lmap mappings
362 int lang; // getting a text character
363#ifdef HAVE_INPUT_METHOD
364 int save_smd; // saved value of p_smd
365#endif
366
367 ++no_mapping;
368 ++allow_keys; // no mapping for nchar, but allow key codes
369 // Don't generate a CursorHold event here, most commands can't handle
370 // it, e.g., nv_replace(), nv_csearch().
371 did_cursorhold = TRUE;
372 if (cap->cmdchar == 'g')
373 {
374 /*
375 * For 'g' get the next character now, so that we can check for
376 * "gr", "g'" and "g`".
377 */
378 cap->nchar = plain_vgetc();
379 LANGMAP_ADJUST(cap->nchar, TRUE);
380#ifdef FEAT_CMDL_INFO
381 *need_flushbuf |= add_to_showcmd(cap->nchar);
382#endif
383 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
384 || cap->nchar == Ctrl_BSL)
385 {
386 cp = &cap->extra_char; // need to get a third character
387 if (cap->nchar != 'r')
388 lit = TRUE; // get it literally
389 else
390 repl = TRUE; // get it in replace mode
391 }
392 else
393 cp = NULL; // no third character needed
394 }
395 else
396 {
397 if (cap->cmdchar == 'r') // get it in replace mode
398 repl = TRUE;
399 cp = &cap->nchar;
400 }
401 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
402
403 /*
404 * Get a second or third character.
405 */
406 if (cp != NULL)
407 {
408 if (repl)
409 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100410 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000411#ifdef CURSOR_SHAPE
412 ui_cursor_shape(); // show different cursor shape
413#endif
414 }
415 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
416 {
417 // Allow mappings defined with ":lmap".
418 --no_mapping;
419 --allow_keys;
420 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100421 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000422 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100423 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000424 langmap_active = TRUE;
425 }
426#ifdef HAVE_INPUT_METHOD
427 save_smd = p_smd;
428 p_smd = FALSE; // Don't let the IM code show the mode here
429 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
430 im_set_active(TRUE);
431#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100432 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000433 {
434#ifdef FEAT_JOB_CHANNEL
435 ch_log_output = TRUE;
436#endif
437 // Disable bracketed paste and modifyOtherKeys here, we won't
438 // recognize the escape sequences with 'esckeys' off.
439 out_str(T_BD);
440 out_str(T_CTE);
441 }
442
443 *cp = plain_vgetc();
444
Bram Moolenaar24959102022-05-07 20:01:16 +0100445 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000446 {
447#ifdef FEAT_JOB_CHANNEL
448 ch_log_output = TRUE;
449#endif
450 // Re-enable bracketed paste mode and modifyOtherKeys
451 out_str(T_BE);
452 out_str(T_CTI);
453 }
454
455 if (langmap_active)
456 {
457 // Undo the decrement done above
458 ++no_mapping;
459 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100460 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000461 }
462#ifdef HAVE_INPUT_METHOD
463 if (lang)
464 {
465 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
466 im_save_status(&curbuf->b_p_iminsert);
467 im_set_active(FALSE);
468 }
469 p_smd = save_smd;
470#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100471 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000472#ifdef FEAT_CMDL_INFO
473 *need_flushbuf |= add_to_showcmd(*cp);
474#endif
475
476 if (!lit)
477 {
478#ifdef FEAT_DIGRAPHS
479 // Typing CTRL-K gets a digraph.
480 if (*cp == Ctrl_K
481 && ((nv_cmds[idx].cmd_flags & NV_LANG)
482 || cp == &cap->extra_char)
483 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
484 {
485 c = get_digraph(FALSE);
486 if (c > 0)
487 {
488 *cp = c;
489# ifdef FEAT_CMDL_INFO
490 // Guessing how to update showcmd here...
491 del_from_showcmd(3);
492 *need_flushbuf |= add_to_showcmd(*cp);
493# endif
494 }
495 }
496#endif
497
498 // adjust chars > 127, except after "tTfFr" commands
499 LANGMAP_ADJUST(*cp, !lang);
500#ifdef FEAT_RIGHTLEFT
501 // adjust Hebrew mapped char
502 if (p_hkmap && lang && KeyTyped)
503 *cp = hkmap(*cp);
504#endif
505 }
506
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000507 // When the next character is CTRL-\ a following CTRL-N means the
508 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000509 if (cp == &cap->extra_char
510 && cap->nchar == Ctrl_BSL
511 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
512 {
513 cap->cmdchar = Ctrl_BSL;
514 cap->nchar = cap->extra_char;
515 idx = find_command(cap->cmdchar);
516 }
517 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
518 cap->oap->op_type = get_op_type(*cp, NUL);
519 else if (*cp == Ctrl_BSL)
520 {
521 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
522
523 // There is a busy wait here when typing "f<C-\>" and then
524 // something different from CTRL-N. Can't be avoided.
525 while ((c = vpeekc()) <= 0 && towait > 0L)
526 {
527 do_sleep(towait > 50L ? 50L : towait, FALSE);
528 towait -= 50L;
529 }
530 if (c > 0)
531 {
532 c = plain_vgetc();
533 if (c != Ctrl_N && c != Ctrl_G)
534 vungetc(c);
535 else
536 {
537 cap->cmdchar = Ctrl_BSL;
538 cap->nchar = c;
539 idx = find_command(cap->cmdchar);
540 }
541 }
542 }
543
544 // When getting a text character and the next character is a
545 // multi-byte character, it could be a composing character.
546 // However, don't wait for it to arrive. Also, do enable mapping,
547 // because if it's put back with vungetc() it's too late to apply
548 // mapping.
549 --no_mapping;
550 while (enc_utf8 && lang && (c = vpeekc()) > 0
551 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
552 {
553 c = plain_vgetc();
554 if (!utf_iscomposing(c))
555 {
556 vungetc(c); // it wasn't, put it back
557 break;
558 }
559 else if (cap->ncharC1 == 0)
560 cap->ncharC1 = c;
561 else
562 cap->ncharC2 = c;
563 }
564 ++no_mapping;
565 }
566 --no_mapping;
567 --allow_keys;
568
569 return idx;
570}
571
572/*
573 * Returns TRUE if after processing a normal mode command, need to wait for a
574 * moment when a message is displayed that will be overwritten by the mode
575 * message.
576 */
577 static int
578normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
579{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000580 // In Visual mode and with "^O" in Insert mode, a short message will be
581 // overwritten by the mode message. Wait a bit, until a key is hit.
582 // In Visual mode, it's more important to keep the Visual area updated
583 // than keeping a message (e.g. from a /pat search).
584 // Only do this if the command was typed, not from a mapping.
585 // Don't wait when emsg_silent is non-zero.
586 // Also wait a bit after an error message, e.g. for "^O:".
587 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000588 return ( ((p_smd
589 && msg_silent == 0
590 && (restart_edit != 0
591 || (VIsual_active
592 && old_pos->lnum == curwin->w_cursor.lnum
593 && old_pos->col == curwin->w_cursor.col)
594 )
595 && (clear_cmdline
596 || redraw_cmdline)
597 && (msg_didout || (msg_didany && msg_scroll))
598 && !msg_nowait
599 && KeyTyped)
600 || (restart_edit != 0
601 && !VIsual_active
602 && (msg_scroll
603 || emsg_on_display)))
604 && cap->oap->regname == 0
605 && !(cap->retval & CA_COMMAND_BUSY)
606 && stuff_empty()
607 && typebuf_typed()
608 && emsg_silent == 0
609 && !in_assert_fails
610 && !did_wait_return
611 && cap->oap->op_type == OP_NOP);
612}
613
614/*
615 * After processing a normal mode command, wait for a moment when a message is
616 * displayed that will be overwritten by the mode message.
617 */
618 static void
619normal_cmd_wait_for_msg(void)
620{
621 int save_State = State;
622
623 // Draw the cursor with the right shape here
624 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100625 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000626
627 // If need to redraw, and there is a "keep_msg", redraw before the
628 // delay
629 if (must_redraw && keep_msg != NULL && !emsg_on_display)
630 {
631 char_u *kmsg;
632
633 kmsg = keep_msg;
634 keep_msg = NULL;
635 // Showmode() will clear keep_msg, but we want to use it anyway.
636 // First update w_topline.
637 setcursor();
638 update_screen(0);
639 // now reset it, otherwise it's put in the history again
640 keep_msg = kmsg;
641
642 kmsg = vim_strsave(keep_msg);
643 if (kmsg != NULL)
644 {
645 msg_attr((char *)kmsg, keep_msg_attr);
646 vim_free(kmsg);
647 }
648 }
649 setcursor();
650#ifdef CURSOR_SHAPE
651 ui_cursor_shape(); // may show different cursor shape
652#endif
653 cursor_on();
654 out_flush();
655 if (msg_scroll || emsg_on_display)
656 ui_delay(1003L, TRUE); // wait at least one second
657 ui_delay(3003L, FALSE); // wait up to three seconds
658 State = save_State;
659
660 msg_scroll = FALSE;
661 emsg_on_display = FALSE;
662}
663
664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 * Execute a command in Normal mode.
666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668normal_cmd(
669 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000676 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 static int old_mapped_len = 0;
680 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000681 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200682 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaara80faa82020-04-12 19:37:17 +0200684 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000686
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 // Use a count remembered from before entering an operator. After typing
688 // "3d" we return from normal_cmd() and come back here, the "3" is
689 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ca.opcount = opcount;
691
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000692 // If there is an operator pending, then the command we take this time
693 // will terminate it. Finish_op tells us to finish the operation before
694 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef CURSOR_SHAPE
696 c = finish_op;
697#endif
698 finish_op = (oap->op_type != OP_NOP);
699#ifdef CURSOR_SHAPE
700 if (finish_op != c)
701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703# ifdef FEAT_MOUSESHAPE
704 update_mouseshape(-1);
705# endif
706 }
707#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100708 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100710 // When not finishing an operator and no register name typed, reset the
711 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000715#ifdef FEAT_EVAL
716 set_prevcount = TRUE;
717#endif
718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // Restore counts from before receiving K_CURSORHOLD. This means after
721 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
722 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000723 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
724 {
725 ca.opcount = oap->prev_opcount;
726 ca.count0 = oap->prev_count0;
727 oap->prev_opcount = 0;
728 oap->prev_count0 = 0;
729 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar24959102022-05-07 20:01:16 +0100733 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
737
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100738#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 // Set v:count here, when called from main() and not a stuffed
740 // command, so that v:count can be used in an expression mapping
741 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100742 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100743 set_vcount_ca(&ca, &set_prevcount);
744#endif
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /*
747 * Get the command character from the user.
748 */
749 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100750 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000752 // If a mapping was started in Visual or Select mode, remember the length
753 // of the mapping. This is used below to not return to Insert mode for as
754 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (restart_edit == 0)
756 old_mapped_len = 0;
757 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000758 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 old_mapped_len = typebuf_maplen();
760
761 if (c == NUL)
762 c = K_ZERO;
763
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000764 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (VIsual_active
766 && VIsual_select
767 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
768 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000769 int len;
770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
772 // 'insertmode' is set) fake a "d"elete command, Insert mode will
773 // restart automatically.
774 // Insert the typed character in the typeahead buffer, so that it can
775 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000776 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
777
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000778 // When recording and gotchars() was called the character will be
779 // recorded again, remove the previous recording.
780 if (KeyTyped)
781 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000782
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000783 if (restart_edit != 0)
784 c = 'd';
785 else
786 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100787 msg_nowait = TRUE; // don't delay going to insert mode
788 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000791 // If the window was made so small that nothing shows, make it at least one
792 // line and one column when typing a command.
793 if (KeyTyped && !KeyStuffed)
794 win_ensure_size();
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#ifdef FEAT_CMDL_INFO
797 need_flushbuf = add_to_showcmd(c);
798#endif
799
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000800 // Get the command count
801 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
802 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000804 // Find the command character in the table of commands.
805 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (ctrl_w)
807 {
808 ca.nchar = c;
809 ca.cmdchar = Ctrl_W;
810 }
811 else
812 ca.cmdchar = c;
813 idx = find_command(ca.cmdchar);
814 if (idx < 0)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
818 goto normal_end;
819 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000820
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100821 if ((nv_cmds[idx].cmd_flags & NV_NCW)
822 && (check_text_locked(oap) || curbuf_locked()))
823 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000824 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000826 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (VIsual_active)
828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (km_stopsel
831 && (nv_cmds[idx].cmd_flags & NV_STS)
832 && !(mod_mask & MOD_MASK_SHIFT))
833 {
834 end_visual_mode();
835 redraw_curbuf_later(INVERTED);
836 }
837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (km_startsel)
840 {
841 if (nv_cmds[idx].cmd_flags & NV_SS)
842 {
843 unshift_special(&ca);
844 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000845 if (idx < 0)
846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000848 clearopbeep(oap);
849 goto normal_end;
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
853 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858#ifdef FEAT_RIGHTLEFT
859 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
860 && (nv_cmds[idx].cmd_flags & NV_RL))
861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 // Invert horizontal movements and operations. Only when typed by the
863 // user directly, not when the result of a mapping or "x" translated
864 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 switch (ca.cmdchar)
866 {
867 case 'l': ca.cmdchar = 'h'; break;
868 case K_RIGHT: ca.cmdchar = K_LEFT; break;
869 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
870 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
871 case 'h': ca.cmdchar = 'l'; break;
872 case K_LEFT: ca.cmdchar = K_RIGHT; break;
873 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
874 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
875 case '>': ca.cmdchar = '<'; break;
876 case '<': ca.cmdchar = '>'; break;
877 }
878 idx = find_command(ca.cmdchar);
879 }
880#endif
881
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000882 // Get additional characters if we need them.
883 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
884 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000887 // Flush the showcmd characters onto the screen so we can see them while
888 // the command is being executed. Only do this when the shown command was
889 // actually displayed, otherwise this will slow down a lot when executing
890 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (need_flushbuf)
892 out_flush();
893#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000894 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200895 {
896 if (ex_normal_busy)
897 did_cursorhold = save_did_cursorhold;
898 else
899 did_cursorhold = FALSE;
900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaar24959102022-05-07 20:01:16 +0100902 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904 if (ca.nchar == ESC)
905 {
906 clearop(oap);
907 if (restart_edit == 0 && goto_im())
908 restart_edit = 'a';
909 goto normal_end;
910 }
911
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000912 if (ca.cmdchar != K_IGNORE)
913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100914 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000915 msg_col = 0;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 // When 'keymodel' contains "startsel" some keys start Select/Visual
921 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (!VIsual_active && km_startsel)
923 {
924 if (nv_cmds[idx].cmd_flags & NV_SS)
925 {
926 start_selection();
927 unshift_special(&ca);
928 idx = find_command(ca.cmdchar);
929 }
930 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
931 && (mod_mask & MOD_MASK_SHIFT))
932 {
933 start_selection();
934 mod_mask &= ~MOD_MASK_SHIFT;
935 }
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000938 // Execute the command!
939 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 ca.arg = nv_cmds[idx].cmd_arg;
941 (nv_cmds[idx].cmd_func)(&ca);
942
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000943 // If we didn't start or finish an operator, reset oap->regname, unless we
944 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (!finish_op
946 && !oap->op_type
947 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
948 {
949 clearop(oap);
950#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200951 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#endif
953 }
954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100955 // Get the length of mapped chars again after typing a count, second
956 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000957 if (old_mapped_len > 0)
958 old_mapped_len = typebuf_maplen();
959
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000960 // If an operation is pending, handle it. But not for K_IGNORE or
961 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200962 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100963 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000965 // Wait for a moment when a message is displayed that will be overwritten
966 // by the mode message.
967 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
968 normal_cmd_wait_for_msg();
969
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000970 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971normal_end:
972
973 msg_nowait = FALSE;
974
Bram Moolenaarcc613032020-06-07 21:31:18 +0200975#ifdef FEAT_EVAL
976 if (finish_op)
977 reset_reg_var();
978#endif
979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100980 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#ifdef CURSOR_SHAPE
982 c = finish_op;
983#endif
984 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100985 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100987 // Redraw the cursor with another shape, if we were in Operator-pending
988 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (c || ca.cmdchar == 'r')
990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100991 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992# ifdef FEAT_MOUSESHAPE
993 update_mouseshape(-1);
994# endif
995 }
996#endif
997
998#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000999 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001000 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 clear_showcmd();
1002#endif
1003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001004 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 vim_free(ca.searchbuf);
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (has_mbyte)
1008 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (curwin->w_p_scb && toplevel)
1011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 do_check_scrollbind(TRUE);
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar860cae12010-06-05 23:22:07 +02001016 if (curwin->w_p_crb && toplevel)
1017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001019 do_check_cursorbind();
1020 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001021
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001022#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001023 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001024 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001025 restart_edit = 0;
1026#endif
1027
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001028 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1029 // if still inside a mapping that started in Visual mode).
1030 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1033 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && !(ca.retval & CA_COMMAND_BUSY)
1035 && stuff_empty()
1036 && oap->regname == 0)
1037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (restart_VIsual_select == 1)
1039 {
1040 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001041 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 showmode();
1043 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001044 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001046 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 (void)edit(restart_edit, FALSE, 1L);
1048 }
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (restart_VIsual_select == 2)
1051 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 opcount = ca.opcount;
1055}
1056
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001057#ifdef FEAT_EVAL
1058/*
1059 * Set v:count and v:count1 according to "cap".
1060 * Set v:prevcount only when "set_prevcount" is TRUE.
1061 */
1062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064{
1065 long count = cap->count0;
1066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068 if (cap->opcount != 0)
1069 count = cap->opcount * (count == 0 ? 1 : count);
1070 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001072}
1073#endif
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001076 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * if not.
1078 */
1079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 static int did_check = FALSE;
1083
1084 if (full_screen)
1085 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001086 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001087 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 did_check = TRUE;
1089 }
1090}
1091
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001092#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1093/*
1094 * Call yank_do_autocmd() for "regname".
1095 */
1096 static void
1097call_yank_do_autocmd(int regname)
1098{
1099 oparg_T oa;
1100 yankreg_T *reg;
1101
1102 clear_oparg(&oa);
1103 oa.regname = regname;
1104 oa.op_type = OP_YANK;
1105 oa.is_VIsual = TRUE;
1106 reg = get_register(regname, TRUE);
1107 yank_do_autocmd(&oa, reg);
1108 free_register(reg);
1109}
1110#endif
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001113 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114 * This function or the next should ALWAYS be called to end Visual mode, except
1115 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
1117 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001118end_visual_mode()
1119{
1120 end_visual_mode_keep_button();
1121 reset_held_button();
1122}
1123
1124 void
1125end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001128 // If we are using the clipboard, then remember what was selected in case
1129 // we need to paste it somewhere while we still own the selection.
1130 // Only do this when the clipboard is already owned. Don't want to grab
1131 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (clip_star.available && clip_star.owned)
1133 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001134
1135# if defined(FEAT_EVAL)
1136 // Emit a TextYankPost for the automatic copy of the selection into the
1137 // star and/or plus register.
1138 if (has_textyankpost())
1139 {
1140 if (clip_isautosel_star())
1141 call_yank_do_autocmd('*');
1142 if (clip_isautosel_plus())
1143 call_yank_do_autocmd('+');
1144 }
1145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#endif
1147
1148 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 setmouse();
1150 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001152 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001153 curbuf->b_visual.vi_mode = VIsual_mode;
1154 curbuf->b_visual.vi_start = VIsual;
1155 curbuf->b_visual.vi_end = curwin->w_cursor;
1156 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#ifdef FEAT_EVAL
1158 curbuf->b_visual_mode_eval = VIsual_mode;
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (!virtual_active())
1161 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001162 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001164 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001165 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166}
1167
1168/*
1169 * Reset VIsual_active and VIsual_reselect.
1170 */
1171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001172reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
1174 if (VIsual_active)
1175 {
1176 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001177 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 VIsual_reselect = FALSE;
1180}
1181
1182/*
1183 * Reset VIsual_active and VIsual_reselect if it's set.
1184 */
1185 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001186reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 if (VIsual_active)
1189 {
1190 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 VIsual_reselect = FALSE;
1193 }
1194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001196 void
1197restore_visual_mode(void)
1198{
1199 if (VIsual_mode_orig != NUL)
1200 {
1201 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1202 VIsual_mode_orig = NUL;
1203 }
1204}
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206/*
1207 * Check for a balloon-eval special item to include when searching for an
1208 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1209 * Returns TRUE if the character at "*ptr" should be included.
1210 * "dir" is FORWARD or BACKWARD, the direction of searching.
1211 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1212 * "bnp" points to a counter for square brackets.
1213 */
1214 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001215find_is_eval_item(
1216 char_u *ptr,
1217 int *colp,
1218 int *bnp,
1219 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001221 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1223 ++*bnp;
1224 if (*bnp > 0)
1225 {
1226 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1227 --*bnp;
1228 return TRUE;
1229 }
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (*ptr == '.')
1233 return TRUE;
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1237 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1238 {
1239 *colp += dir;
1240 return TRUE;
1241 }
1242 return FALSE;
1243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245/*
1246 * Find the identifier under or to the right of the cursor.
1247 * "find_type" can have one of three values:
1248 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001249 * FIND_STRING: find any non-white text
1250 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001251 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 *
1253 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001254 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * This doesn't match the real Vi but I like it a little better and it
1258 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * When FIND_IDENT isn't defined, we backup until a blank.
1261 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001262 * Returns the length of the text, or zero if no text is found.
1263 * If text is found, a pointer to the text is put in "*text". This
1264 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 */
1266 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001270 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273/*
1274 * Like find_ident_under_cursor(), but for any window and any position.
1275 * However: Uses 'iskeyword' from the current window!.
1276 */
1277 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001278find_ident_at_pos(
1279 win_T *wp,
1280 linenr_T lnum,
1281 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001282 char_u **text,
1283 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001284 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001287 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 int this_class = 0;
1290 int prev_class;
1291 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001292 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001294 // if i == 0: try to find an identifier
1295 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1297 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1298 {
1299 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001300 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 */
1302 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (has_mbyte)
1304 {
1305 while (ptr[col] != NUL)
1306 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001307 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 this_class = mb_get_class(ptr + col);
1311 if (this_class != 0 && (i == 1 || this_class != 1))
1312 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001313 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001318 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 )
1321 ++col;
1322
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001327 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (has_mbyte)
1330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1333 this_class = mb_get_class((char_u *)"a");
1334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001336 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1339 prev_class = mb_get_class(ptr + prevcol);
1340 if (this_class != prev_class
1341 && (i == 0
1342 || prev_class == 0
1343 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 && (!(find_type & FIND_EVAL)
1345 || prevcol == 0
1346 || !find_is_eval_item(ptr + prevcol, &prevcol,
1347 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 )
1349 break;
1350 col = prevcol;
1351 }
1352
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001353 // If we don't want just any old text, or we've found an
1354 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (this_class > 2)
1356 this_class = 2;
1357 if (!(find_type & FIND_STRING) || this_class == 2)
1358 break;
1359 }
1360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 while (col > 0
1363 && ((i == 0
1364 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001365 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 && (!(find_type & FIND_IDENT)
1367 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 || ((find_type & FIND_EVAL)
1369 && col > 1
1370 && find_is_eval_item(ptr + col - 1, &col,
1371 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ))
1373 --col;
1374
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001375 // If we don't want just any old text, or we've found an
1376 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1378 break;
1379 }
1380 }
1381
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001382 if (ptr[col] == NUL || (i == 0
1383 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 if ((find_type & FIND_NOERROR) == 0)
1387 {
1388 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001389 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001390 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001391 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 return 0;
1394 }
1395 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001396 *text = ptr;
1397 if (textcol != NULL)
1398 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001401 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 bn = 0;
1404 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (has_mbyte)
1407 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001408 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 this_class = mb_get_class(ptr);
1410 while (ptr[col] != NUL
1411 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1412 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 || ((find_type & FIND_EVAL)
1414 && col <= (int)startcol
1415 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001417 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001421 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 || ((find_type & FIND_EVAL)
1423 && col <= (int)startcol
1424 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 return col;
1429}
1430
1431/*
1432 * Prepare for redo of a normal command.
1433 */
1434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001435prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 prep_redo(cap->oap->regname, cap->count0,
1438 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1439}
1440
1441/*
1442 * Prepare for redo of any command.
1443 * Note that only the last argument can be a multi-byte char.
1444 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001446prep_redo(
1447 int regname,
1448 long num,
1449 int cmd1,
1450 int cmd2,
1451 int cmd3,
1452 int cmd4,
1453 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001455 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1456}
1457
1458/*
1459 * Prepare for redo of any command with extra count after "cmd2".
1460 */
1461 void
1462prep_redo_num2(
1463 int regname,
1464 long num1,
1465 int cmd1,
1466 int cmd2,
1467 long num2,
1468 int cmd3,
1469 int cmd4,
1470 int cmd5)
1471{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001473 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 AppendCharToRedobuff('"');
1476 AppendCharToRedobuff(regname);
1477 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001478 if (num1 != 0)
1479 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (cmd1 != NUL)
1481 AppendCharToRedobuff(cmd1);
1482 if (cmd2 != NUL)
1483 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001484 if (num2 != 0)
1485 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if (cmd3 != NUL)
1487 AppendCharToRedobuff(cmd3);
1488 if (cmd4 != NUL)
1489 AppendCharToRedobuff(cmd4);
1490 if (cmd5 != NUL)
1491 AppendCharToRedobuff(cmd5);
1492}
1493
1494/*
1495 * check for operator active and clear it
1496 *
1497 * return TRUE if operator was active
1498 */
1499 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001500checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 if (oap->op_type == OP_NOP)
1503 return FALSE;
1504 clearopbeep(oap);
1505 return TRUE;
1506}
1507
1508/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001509 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001511 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 */
1513 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001514checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001516 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 return FALSE;
1518 clearopbeep(oap);
1519 return TRUE;
1520}
1521
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001523clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 oap->op_type = OP_NOP;
1526 oap->regname = 0;
1527 oap->motion_force = NUL;
1528 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001529 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
1531
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001533clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535 clearop(oap);
1536 beep_flush();
1537}
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
1540 * Remove the shift modifier from a special key.
1541 */
1542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001543unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 switch (cap->cmdchar)
1546 {
1547 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1548 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1549 case K_S_UP: cap->cmdchar = K_UP; break;
1550 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1551 case K_S_HOME: cap->cmdchar = K_HOME; break;
1552 case K_S_END: cap->cmdchar = K_END; break;
1553 }
1554 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001557/*
1558 * If the mode is currently displayed clear the command line or update the
1559 * command displayed.
1560 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001561 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001562may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001563{
1564 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566#ifdef FEAT_CMDL_INFO
1567 else
1568 clear_showcmd();
1569#endif
1570}
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1573/*
1574 * Routines for displaying a partly typed command
1575 */
1576
kylo252ae6f1d82022-02-16 19:24:07 +00001577#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001579static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580static int showcmd_is_clear = TRUE;
1581static int showcmd_visual = FALSE;
1582
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001583static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001586clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 if (!p_sc)
1589 return;
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (VIsual_active && !char_avail())
1592 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001593 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 long lines;
1595 colnr_T leftcol, rightcol;
1596 linenr_T top, bot;
1597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001598 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001599 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 top = VIsual.lnum;
1602 bot = curwin->w_cursor.lnum;
1603 }
1604 else
1605 {
1606 top = curwin->w_cursor.lnum;
1607 bot = VIsual.lnum;
1608 }
1609# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001611 (void)hasFolding(top, &top, NULL);
1612 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613# endif
1614 lines = bot - top + 1;
1615
1616 if (VIsual_mode == Ctrl_V)
1617 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001618# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001620 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001623 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001624 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001627# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001628 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001629 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1632 (long)(rightcol - leftcol + 1));
1633 }
1634 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1635 sprintf((char *)showcmd_buf, "%ld", lines);
1636 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001637 {
1638 char_u *s, *e;
1639 int l;
1640 int bytes = 0;
1641 int chars = 0;
1642
1643 if (cursor_bot)
1644 {
1645 s = ml_get_pos(&VIsual);
1646 e = ml_get_cursor();
1647 }
1648 else
1649 {
1650 s = ml_get_cursor();
1651 e = ml_get_pos(&VIsual);
1652 }
1653 while ((*p_sel != 'e') ? s <= e : s < e)
1654 {
1655 l = (*mb_ptr2len)(s);
1656 if (l == 0)
1657 {
1658 ++bytes;
1659 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001661 }
1662 bytes += l;
1663 ++chars;
1664 s += l;
1665 }
1666 if (bytes == chars)
1667 sprintf((char *)showcmd_buf, "%d", chars);
1668 else
1669 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1670 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001671 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 showcmd_visual = TRUE;
1673 }
1674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 showcmd_buf[0] = NUL;
1677 showcmd_visual = FALSE;
1678
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001679 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (showcmd_is_clear)
1681 return;
1682 }
1683
1684 display_showcmd();
1685}
1686
1687/*
1688 * Add 'c' to string of shown command chars.
1689 * Return TRUE if output has been written (and setcursor() has been called).
1690 */
1691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001692add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 char_u *p;
1695 int old_len;
1696 int extra_len;
1697 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 int i;
1699 static int ignore[] =
1700 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001701#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1703 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001704#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001705 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001706 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1708 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001709 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001711 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 0
1713 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar09df3122006-01-23 22:23:09 +00001715 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return FALSE;
1717
1718 if (showcmd_visual)
1719 {
1720 showcmd_buf[0] = NUL;
1721 showcmd_visual = FALSE;
1722 }
1723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001724 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (IS_SPECIAL(c))
1726 for (i = 0; ignore[i] != 0; ++i)
1727 if (ignore[i] == c)
1728 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001731 if (*p == ' ')
1732 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 old_len = (int)STRLEN(showcmd_buf);
1734 extra_len = (int)STRLEN(p);
1735 overflow = old_len + extra_len - SHOWCMD_COLS;
1736 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001737 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1738 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 STRCAT(showcmd_buf, p);
1740
1741 if (char_avail())
1742 return FALSE;
1743
1744 display_showcmd();
1745
1746 return TRUE;
1747}
1748
1749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 if (!add_to_showcmd(c))
1753 setcursor();
1754}
1755
1756/*
1757 * Delete 'len' characters from the end of the shown command.
1758 */
1759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 int old_len;
1763
1764 if (!p_sc)
1765 return;
1766
1767 old_len = (int)STRLEN(showcmd_buf);
1768 if (len > old_len)
1769 len = old_len;
1770 showcmd_buf[old_len - len] = NUL;
1771
1772 if (!char_avail())
1773 display_showcmd();
1774}
1775
1776/*
1777 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1778 * something and there is a partial mapping.
1779 */
1780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001781push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 if (p_sc)
1784 STRCPY(old_showcmd_buf, showcmd_buf);
1785}
1786
1787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 if (!p_sc)
1791 return;
1792
1793 STRCPY(showcmd_buf, old_showcmd_buf);
1794
1795 display_showcmd();
1796}
1797
1798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int len;
1802
1803 cursor_off();
1804
1805 len = (int)STRLEN(showcmd_buf);
1806 if (len == 0)
1807 showcmd_is_clear = TRUE;
1808 else
1809 {
1810 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1811 showcmd_is_clear = FALSE;
1812 }
1813
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001814 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1815 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001818 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * When "check" is FALSE, prepare for commands that scroll the window.
1824 * When "check" is TRUE, take care of scroll-binding after the window has
1825 * scrolled. Called from normal_cmd() and edit().
1826 */
1827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001828do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 static win_T *old_curwin = NULL;
1831 static linenr_T old_topline = 0;
1832#ifdef FEAT_DIFF
1833 static int old_topfill = 0;
1834#endif
1835 static buf_T *old_buf = NULL;
1836 static colnr_T old_leftcol = 0;
1837
1838 if (check && curwin->w_p_scb)
1839 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001840 // If a ":syncbind" command was just used, don't scroll, only reset
1841 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (did_syncbind)
1843 did_syncbind = FALSE;
1844 else if (curwin == old_curwin)
1845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001846 // Synchronize other windows, as necessary according to
1847 // 'scrollbind'. Don't do this after an ":edit" command, except
1848 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if ((curwin->w_buffer == old_buf
1850#ifdef FEAT_DIFF
1851 || curwin->w_p_diff
1852#endif
1853 )
1854 && (curwin->w_topline != old_topline
1855#ifdef FEAT_DIFF
1856 || curwin->w_topfill != old_topfill
1857#endif
1858 || curwin->w_leftcol != old_leftcol))
1859 {
1860 check_scrollbind(curwin->w_topline - old_topline,
1861 (long)(curwin->w_leftcol - old_leftcol));
1862 }
1863 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001866 // When switching between windows, make sure that the relative
1867 // vertical offset is valid for the new window. The relative
1868 // offset is invalid whenever another 'scrollbind' window has
1869 // scrolled to a point that would force the current window to
1870 // scroll past the beginning or end of its buffer. When the
1871 // resync is performed, some of the other 'scrollbind' windows may
1872 // need to jump so that the current window's relative position is
1873 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1875 }
1876 curwin->w_scbind_pos = curwin->w_topline;
1877 }
1878
1879 old_curwin = curwin;
1880 old_topline = curwin->w_topline;
1881#ifdef FEAT_DIFF
1882 old_topfill = curwin->w_topfill;
1883#endif
1884 old_buf = curwin->w_buffer;
1885 old_leftcol = curwin->w_leftcol;
1886}
1887
1888/*
1889 * Synchronize any windows that have "scrollbind" set, based on the
1890 * number of rows by which the current window has changed
1891 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1892 */
1893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001894check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 int want_ver;
1897 int want_hor;
1898 win_T *old_curwin = curwin;
1899 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int old_VIsual_select = VIsual_select;
1901 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 colnr_T tgt_leftcol = curwin->w_leftcol;
1903 long topline;
1904 long y;
1905
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001906 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1908#ifdef FEAT_DIFF
1909 want_ver |= old_curwin->w_p_diff;
1910#endif
1911 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1912
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001913 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001915 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (curwin != old_curwin && curwin->w_p_scb)
1920 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001921 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (want_ver)
1923 {
1924#ifdef FEAT_DIFF
1925 if (old_curwin->w_p_diff && curwin->w_p_diff)
1926 {
1927 diff_set_topline(old_curwin, curwin);
1928 }
1929 else
1930#endif
1931 {
1932 curwin->w_scbind_pos += topline_diff;
1933 topline = curwin->w_scbind_pos;
1934 if (topline > curbuf->b_ml.ml_line_count)
1935 topline = curbuf->b_ml.ml_line_count;
1936 if (topline < 1)
1937 topline = 1;
1938
1939 y = topline - curwin->w_topline;
1940 if (y > 0)
1941 scrollup(y, FALSE);
1942 else
1943 scrolldown(-y, FALSE);
1944 }
1945
1946 redraw_later(VALID);
1947 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001951 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1953 {
1954 curwin->w_leftcol = tgt_leftcol;
1955 leftcol_changed();
1956 }
1957 }
1958 }
1959
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001960 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 VIsual_select = old_VIsual_select;
1962 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 curwin = old_curwin;
1964 curbuf = old_curbuf;
1965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967/*
1968 * Command character that's ignored.
1969 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001970 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001975 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976}
1977
1978/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001979 * Command character that doesn't do anything, but unlike nv_ignore() does
1980 * start edit(). Used for "startinsert" executed while starting up.
1981 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001983nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001984{
1985}
1986
1987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * Command character doesn't exist.
1989 */
1990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001991nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 clearopbeep(cap->oap);
1994}
1995
1996/*
1997 * <Help> and <F1> commands.
1998 */
1999 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002000nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 if (!checkclearopq(cap->oap))
2003 ex_help(NULL);
2004}
2005
2006/*
2007 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2008 */
2009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002012#ifdef FEAT_JOB_CHANNEL
2013 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2014 clearopbeep(cap->oap);
2015 else
2016#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002017 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002018 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002019 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002020 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2021 op_addsub(cap->oap, cap->count1, cap->arg);
2022 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002023 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002024 else if (VIsual_active)
2025 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002026 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002027 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028}
2029
2030/*
2031 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2032 */
2033 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002038 if (mod_mask & MOD_MASK_CTRL)
2039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 if (cap->arg == BACKWARD)
2042 goto_tabpage(-(int)cap->count1);
2043 else
2044 goto_tabpage((int)cap->count0);
2045 }
2046 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002047 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049}
2050
2051/*
2052 * Implementation of "gd" and "gD" command.
2053 */
2054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002055nv_gd(
2056 oparg_T *oap,
2057 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 char_u *ptr;
2062
Bram Moolenaard9d30582005-05-18 22:10:28 +00002063 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002064 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002065 == FAIL)
2066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002068 }
2069 else
2070 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002071#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002072 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2073 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002074#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002075 // clear any search statistics
2076 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2077 clear_cmdline = TRUE;
2078 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002079}
2080
2081/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002082 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2083 * otherwise.
2084 */
2085 static int
2086is_ident(char_u *line, int offset)
2087{
2088 int i;
2089 int incomment = FALSE;
2090 int instring = 0;
2091 int prev = 0;
2092
2093 for (i = 0; i < offset && line[i] != NUL; i++)
2094 {
2095 if (instring != 0)
2096 {
2097 if (prev != '\\' && line[i] == instring)
2098 instring = 0;
2099 }
2100 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2101 {
2102 instring = line[i];
2103 }
2104 else
2105 {
2106 if (incomment)
2107 {
2108 if (prev == '*' && line[i] == '/')
2109 incomment = FALSE;
2110 }
2111 else if (prev == '/' && line[i] == '*')
2112 {
2113 incomment = TRUE;
2114 }
2115 else if (prev == '/' && line[i] == '/')
2116 {
2117 return FALSE;
2118 }
2119 }
2120
2121 prev = line[i];
2122 }
2123
2124 return incomment == FALSE && instring == 0;
2125}
2126
2127/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002128 * Search for variable declaration of "ptr[len]".
2129 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2130 * current file ("gD").
2131 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002132 * Return FAIL when not found.
2133 */
2134 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002135find_decl(
2136 char_u *ptr,
2137 int len,
2138 int locally,
2139 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002140 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002141{
2142 char_u *pat;
2143 pos_T old_pos;
2144 pos_T par_pos;
2145 pos_T found_pos;
2146 int t;
2147 int save_p_ws;
2148 int save_p_scs;
2149 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002150 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002151 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002152 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002153
2154 if ((pat = alloc(len + 7)) == NULL)
2155 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002157 // Put "\V" before the pattern to avoid that the special meaning of "."
2158 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002159 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2160 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 old_pos = curwin->w_cursor;
2162 save_p_ws = p_ws;
2163 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002164 p_ws = FALSE; // don't wrap around end of file now
2165 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002167 // With "gD" go to line 1.
2168 // With "gd" Search back for the start of the current function, then go
2169 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002170 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002172 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002174 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 else
2177 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002178 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2180 --curwin->w_cursor.lnum;
2181 }
2182 curwin->w_cursor.col = 0;
2183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002184 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002185 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002186 for (;;)
2187 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002188 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002189 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002190 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002192
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002193 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002194 {
2195 pos_T *pos;
2196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002197 // Check that the block the match is in doesn't end before the
2198 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002199 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2200 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2201 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002202 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002203 // There can't be a useful match before the end of this block.
2204 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002205 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002206 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002207 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002208 }
2209
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 if (t == FAIL)
2211 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002213 if (found_pos.lnum != 0)
2214 {
2215 curwin->w_cursor = found_pos;
2216 t = OK;
2217 }
2218 break;
2219 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002220 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 ++curwin->w_cursor.lnum;
2224 curwin->w_cursor.col = 0;
2225 continue;
2226 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002229 // If the current position is not a valid identifier and a previous
2230 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 if (!valid && found_pos.lnum != 0)
2232 {
2233 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002234 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002235 }
2236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002238 if (valid && !locally)
2239 break;
2240 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002242 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002243 if (found_pos.lnum != 0)
2244 curwin->w_cursor = found_pos;
2245 break;
2246 }
2247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 // For finding a local variable and the match is before the "{" or
2249 // inside a comment, continue searching. For K&R style function
2250 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002251 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002252 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002253 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002254 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 // Remove SEARCH_START from flags to avoid getting stuck at one
2256 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002257 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002259
2260 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002262 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 curwin->w_cursor = old_pos;
2264 }
2265 else
2266 {
2267 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002268 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 reset_search_dir();
2270 }
2271
2272 vim_free(pat);
2273 p_ws = save_p_ws;
2274 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002275
2276 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277}
2278
2279/*
2280 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2281 * lines rather than lines in the file.
2282 * 'dist' must be positive.
2283 *
2284 * Return OK if able to move cursor, FAIL otherwise.
2285 */
2286 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int linelen = linetabsize(ml_get_curline());
2290 int retval = OK;
2291 int atend = FALSE;
2292 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 int col_off1; // margin offset for first screen line
2294 int col_off2; // margin offset for wrapped screen line
2295 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002296 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002299 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 col_off1 = curwin_col_off();
2302 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002303 width1 = curwin->w_width - col_off1;
2304 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002305 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002309 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002310 // Instead of sticking at the last character of the buffer line we
2311 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (curwin->w_curswant == MAXCOL)
2313 {
2314 atend = TRUE;
2315 validate_virtcol();
2316 if (width1 <= 0)
2317 curwin->w_curswant = 0;
2318 else
2319 {
2320 curwin->w_curswant = width1 - 1;
2321 if (curwin->w_virtcol > curwin->w_curswant)
2322 curwin->w_curswant += ((curwin->w_virtcol
2323 - curwin->w_curswant - 1) / width2 + 1) * width2;
2324 }
2325 }
2326 else
2327 {
2328 if (linelen > width1)
2329 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2330 else
2331 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002332 if (curwin->w_curswant >= (colnr_T)n)
2333 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335
2336 while (dist--)
2337 {
2338 if (dir == BACKWARD)
2339 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002340 if ((long)curwin->w_curswant >= width1
2341#ifdef FEAT_FOLDING
2342 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2343#endif
2344 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002345 // Move back within the line. This can give a negative value
2346 // for w_curswant if width1 < width2 (with cpoptions+=n),
2347 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 curwin->w_curswant -= width2;
2349 else
2350 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002351 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 // Move to the start of a closed fold. Don't do that when
2354 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (!(fdo_flags & FDO_ALL))
2356 (void)hasFolding(curwin->w_cursor.lnum,
2357 &curwin->w_cursor.lnum, NULL);
2358#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002359 if (curwin->w_cursor.lnum == 1)
2360 {
2361 retval = FAIL;
2362 break;
2363 }
2364 --curwin->w_cursor.lnum;
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 linelen = linetabsize(ml_get_curline());
2367 if (linelen > width1)
2368 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2369 + 1) * width2;
2370 }
2371 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 if (linelen > width1)
2375 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2376 else
2377 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002378 if (curwin->w_curswant + width2 < (colnr_T)n
2379#ifdef FEAT_FOLDING
2380 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2381#endif
2382 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 curwin->w_curswant += width2;
2385 else
2386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2391 &curwin->w_cursor.lnum);
2392#endif
2393 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2394 {
2395 retval = FAIL;
2396 break;
2397 }
2398 curwin->w_cursor.lnum++;
2399 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002400 // Check if the cursor has moved below the number display
2401 // when width1 < width2 (with cpoptions+=n). Subtract width2
2402 // to get a negative value for w_curswant, which will get
2403 // clipped to column 0.
2404 if (curwin->w_curswant >= width1)
2405 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002406 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 }
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002412 if (virtual_active() && atend)
2413 coladvance(MAXCOL);
2414 else
2415 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2418 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002419 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002420 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002421
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002422 // Check for landing on a character that got split at the end of the
2423 // last line. We want to advance a screenline, not end up in the same
2424 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002426 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002427#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002428 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2429 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002430#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002431
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002432 c = (*mb_ptr2char)(ml_get_cursor());
2433 if (dir == FORWARD && virtcol < curwin->w_curswant
2434 && (curwin->w_curswant <= (colnr_T)width1)
2435 && !vim_isprintc(c) && c > 255)
2436 oneright();
2437
Bram Moolenaar773b1582014-08-29 14:20:51 +02002438 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 && (curwin->w_curswant < (colnr_T)width1
2440 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2441 : ((curwin->w_curswant - width1) % width2
2442 > (colnr_T)width2 / 2)))
2443 --curwin->w_cursor.col;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 return retval;
2450}
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452/*
2453 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2454 * cap->arg must be TRUE for CTRL-E.
2455 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 if (!checkclearop(cap->oap))
2460 scroll_redraw(cap->arg, cap->count1);
2461}
2462
2463/*
2464 * Scroll "count" lines up or down, and redraw.
2465 */
2466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002467scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 linenr_T prev_topline = curwin->w_topline;
2470#ifdef FEAT_DIFF
2471 int prev_topfill = curwin->w_topfill;
2472#endif
2473 linenr_T prev_lnum = curwin->w_cursor.lnum;
2474
2475 if (up)
2476 scrollup(count, TRUE);
2477 else
2478 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002479 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2482 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 cursor_correct();
2484 check_cursor_moved(curwin);
2485 curwin->w_valid |= VALID_TOPLINE;
2486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 // If moved back to where we were, at least move the cursor, otherwise
2488 // we get stuck at one position. Don't move the cursor up if the
2489 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 while (curwin->w_topline == prev_topline
2491#ifdef FEAT_DIFF
2492 && curwin->w_topfill == prev_topfill
2493#endif
2494 )
2495 {
2496 if (up)
2497 {
2498 if (curwin->w_cursor.lnum > prev_lnum
2499 || cursor_down(1L, FALSE) == FAIL)
2500 break;
2501 }
2502 else
2503 {
2504 if (curwin->w_cursor.lnum < prev_lnum
2505 || prev_topline == 1L
2506 || cursor_up(1L, FALSE) == FAIL)
2507 break;
2508 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002509 // Mark w_topline as valid, otherwise the screen jumps back at the
2510 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 check_cursor_moved(curwin);
2512 curwin->w_valid |= VALID_TOPLINE;
2513 }
2514 }
2515 if (curwin->w_cursor.lnum != prev_lnum)
2516 coladvance(curwin->w_curswant);
2517 redraw_later(VALID);
2518}
2519
2520/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002521 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2522 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2523 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002524 */
2525 static int
2526nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2527{
2528 int nchar = *nchar_arg;
2529 long n;
2530
2531 // "z123{nchar}": edit the count before obtaining {nchar}
2532 if (checkclearop(cap->oap))
2533 return FALSE;
2534 n = nchar - '0';
2535
2536 for (;;)
2537 {
2538#ifdef USE_ON_FLY_SCROLL
2539 dont_scroll = TRUE; // disallow scrolling here
2540#endif
2541 ++no_mapping;
2542 ++allow_keys; // no mapping for nchar, but allow key codes
2543 nchar = plain_vgetc();
2544 LANGMAP_ADJUST(nchar, TRUE);
2545 --no_mapping;
2546 --allow_keys;
2547#ifdef FEAT_CMDL_INFO
2548 (void)add_to_showcmd(nchar);
2549#endif
2550 if (nchar == K_DEL || nchar == K_KDEL)
2551 n /= 10;
2552 else if (VIM_ISDIGIT(nchar))
2553 n = n * 10 + (nchar - '0');
2554 else if (nchar == CAR)
2555 {
2556#ifdef FEAT_GUI
2557 need_mouse_correct = TRUE;
2558#endif
2559 win_setheight((int)n);
2560 break;
2561 }
2562 else if (nchar == 'l'
2563 || nchar == 'h'
2564 || nchar == K_LEFT
2565 || nchar == K_RIGHT)
2566 {
2567 cap->count1 = n ? n * cap->count1 : cap->count1;
2568 *nchar_arg = nchar;
2569 return TRUE;
2570 }
2571 else
2572 {
2573 clearopbeep(cap->oap);
2574 break;
2575 }
2576 }
2577 cap->oap->op_type = OP_NOP;
2578 return FALSE;
2579}
2580
2581#ifdef FEAT_SPELL
2582/*
2583 * "zug" and "zuw": undo "zg" and "zw"
2584 * "zg": add good word to word list
2585 * "zw": add wrong word to word list
2586 * "zG": add good word to temp word list
2587 * "zW": add wrong word to temp word list
2588 */
2589 static int
2590nv_zg_zw(cmdarg_T *cap, int nchar)
2591{
2592 char_u *ptr = NULL;
2593 int len;
2594 int undo = FALSE;
2595
2596 if (nchar == 'u')
2597 {
2598 ++no_mapping;
2599 ++allow_keys; // no mapping for nchar, but allow key codes
2600 nchar = plain_vgetc();
2601 LANGMAP_ADJUST(nchar, TRUE);
2602 --no_mapping;
2603 --allow_keys;
2604#ifdef FEAT_CMDL_INFO
2605 (void)add_to_showcmd(nchar);
2606#endif
2607 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2608 {
2609 clearopbeep(cap->oap);
2610 return OK;
2611 }
2612 undo = TRUE;
2613 }
2614
2615 if (checkclearop(cap->oap))
2616 return OK;
2617 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2618 return FAIL;
2619 if (ptr == NULL)
2620 {
2621 pos_T pos = curwin->w_cursor;
2622
2623 // Find bad word under the cursor. When 'spell' is
2624 // off this fails and find_ident_under_cursor() is
2625 // used below.
2626 emsg_off++;
2627 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2628 emsg_off--;
2629 if (len != 0 && curwin->w_cursor.col <= pos.col)
2630 ptr = ml_get_pos(&curwin->w_cursor);
2631 curwin->w_cursor = pos;
2632 }
2633
2634 if (ptr == NULL
2635 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2636 return FAIL;
2637 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2638 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2639 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2640
2641 return OK;
2642}
2643#endif
2644
2645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 * Commands that start with "z".
2647 */
2648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002649nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 long n;
2652 colnr_T col;
2653 int nchar = cap->nchar;
2654#ifdef FEAT_FOLDING
2655 long old_fdl = curwin->w_p_fdl;
2656 int old_fen = curwin->w_p_fen;
2657#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002658 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002660 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (
2664#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002665 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2666 // and "zC" only in Visual mode. "zj" and "zk" are motion
2667 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 cap->nchar != 'f' && cap->nchar != 'F'
2669 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2670 && cap->nchar != 'j' && cap->nchar != 'k'
2671 &&
2672#endif
2673 checkclearop(cap->oap))
2674 return;
2675
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002676 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2677 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2679 && cap->count0
2680 && cap->count0 != curwin->w_cursor.lnum)
2681 {
2682 setpcmark();
2683 if (cap->count0 > curbuf->b_ml.ml_line_count)
2684 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2685 else
2686 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002687 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 switch (nchar)
2691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 case '+':
2694 if (cap->count0 == 0)
2695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // No count given: put cursor at the line below screen
2697 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2699 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2700 else
2701 curwin->w_cursor.lnum = curwin->w_botline;
2702 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 case NL:
2705 case CAR:
2706 case K_KENTER:
2707 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 case 't': scroll_cursor_top(0, TRUE);
2711 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002712 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 break;
2714
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 case 'z': scroll_cursor_halfway(TRUE);
2720 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002721 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 break;
2723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 // "z^", "z-" and "zb": put cursor at bottom of screen
2725 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2726 // when <count> is at bottom of window, and puts that one at
2727 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (cap->count0 != 0)
2729 {
2730 scroll_cursor_bot(0, TRUE);
2731 curwin->w_cursor.lnum = curwin->w_topline;
2732 }
2733 else if (curwin->w_topline == 1)
2734 curwin->w_cursor.lnum = 1;
2735 else
2736 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 case '-':
2739 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 case 'b': scroll_cursor_bot(0, TRUE);
2743 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002744 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 break;
2746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002749 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 case 'h':
2754 case K_LEFT:
2755 if (!curwin->w_p_wrap)
2756 {
2757 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2758 curwin->w_leftcol = 0;
2759 else
2760 curwin->w_leftcol -= (colnr_T)cap->count1;
2761 leftcol_changed();
2762 }
2763 break;
2764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002766 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case 'l':
2771 case K_RIGHT:
2772 if (!curwin->w_p_wrap)
2773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 curwin->w_leftcol += (colnr_T)cap->count1;
2776 leftcol_changed();
2777 }
2778 break;
2779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case 's': if (!curwin->w_p_wrap)
2782 {
2783#ifdef FEAT_FOLDING
2784 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
2787#endif
2788 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002789 if ((long)col > siso)
2790 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792 col = 0;
2793 if (curwin->w_leftcol != col)
2794 {
2795 curwin->w_leftcol = col;
2796 redraw_later(NOT_VALID);
2797 }
2798 }
2799 break;
2800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case 'e': if (!curwin->w_p_wrap)
2803 {
2804#ifdef FEAT_FOLDING
2805 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
2808#endif
2809 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002810 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002811 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 col = 0;
2813 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002814 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (curwin->w_leftcol != col)
2816 {
2817 curwin->w_leftcol = col;
2818 redraw_later(NOT_VALID);
2819 }
2820 }
2821 break;
2822
Christian Brabandt2fa93842021-05-30 22:17:25 +02002823 // "zp", "zP" in block mode put without addind trailing spaces
2824 case 'P':
2825 case 'p': nv_put(cap);
2826 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002827 // "zy" Yank without trailing spaces
2828 case 'y': nv_operator(cap);
2829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 // "zF": create fold command
2832 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 case 'F':
2834 case 'f': if (foldManualAllowed(TRUE))
2835 {
2836 cap->nchar = 'f';
2837 nv_operator(cap);
2838 curwin->w_p_fen = TRUE;
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2842 {
2843 nv_operator(cap);
2844 finish_op = TRUE;
2845 }
2846 }
2847 else
2848 clearopbeep(cap->oap);
2849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zd": delete fold at cursor
2852 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 case 'd':
2854 case 'D': if (foldManualAllowed(FALSE))
2855 {
2856 if (VIsual_active)
2857 nv_operator(cap);
2858 else
2859 deleteFold(curwin->w_cursor.lnum,
2860 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2861 }
2862 break;
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case 'E': if (foldmethodIsManual(curwin))
2866 {
2867 clearFolding(curwin);
2868 changed_window_setting();
2869 }
2870 else if (foldmethodIsMarker(curwin))
2871 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2872 TRUE, FALSE);
2873 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002874 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'n': curwin->w_p_fen = FALSE;
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'N': curwin->w_p_fen = TRUE;
2883 break;
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2891 openFold(curwin->w_cursor.lnum, cap->count1);
2892 else
2893 {
2894 closeFold(curwin->w_cursor.lnum, cap->count1);
2895 curwin->w_p_fen = TRUE;
2896 }
2897 break;
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2901 openFoldRecurse(curwin->w_cursor.lnum);
2902 else
2903 {
2904 closeFoldRecurse(curwin->w_cursor.lnum);
2905 curwin->w_p_fen = TRUE;
2906 }
2907 break;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case 'o': if (VIsual_active)
2911 nv_operator(cap);
2912 else
2913 openFold(curwin->w_cursor.lnum, cap->count1);
2914 break;
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case 'O': if (VIsual_active)
2918 nv_operator(cap);
2919 else
2920 openFoldRecurse(curwin->w_cursor.lnum);
2921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'c': if (VIsual_active)
2925 nv_operator(cap);
2926 else
2927 closeFold(curwin->w_cursor.lnum, cap->count1);
2928 curwin->w_p_fen = TRUE;
2929 break;
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case 'C': if (VIsual_active)
2933 nv_operator(cap);
2934 else
2935 closeFoldRecurse(curwin->w_cursor.lnum);
2936 curwin->w_p_fen = TRUE;
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'v': foldOpenCursor();
2941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 curwin->w_foldinvalid = TRUE; // recompute folds
2946 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 foldOpenCursor();
2948 break;
2949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 curwin->w_foldinvalid = TRUE; // recompute folds
2953 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002958 {
2959 curwin->w_p_fdl -= cap->count1;
2960 if (curwin->w_p_fdl < 0)
2961 curwin->w_p_fdl = 0;
2962 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 curwin->w_p_fen = TRUE;
2965 break;
2966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 curwin->w_p_fen = TRUE;
2971 break;
2972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002974 case 'r': curwin->w_p_fdl += cap->count1;
2975 {
2976 int d = getDeepestNesting();
2977
2978 if (curwin->w_p_fdl >= d)
2979 curwin->w_p_fdl = d;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 break;
2987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case 'j': // "zj" move to next fold downwards
2989 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2991 cap->count1) == FAIL)
2992 clearopbeep(cap->oap);
2993 break;
2994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002997#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 case 'g': // "zg": add good word to word list
3000 case 'w': // "zw": add wrong word to word list
3001 case 'G': // "zG": add good word to temp word list
3002 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003003 if (nv_zg_zw(cap, nchar) == FAIL)
3004 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003005 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003007 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003008 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003009 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003010 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003011#endif
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 default: clearopbeep(cap->oap);
3014 }
3015
3016#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (old_fen != curwin->w_p_fen)
3019 {
3020# ifdef FEAT_DIFF
3021 win_T *wp;
3022
3023 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 FOR_ALL_WINDOWS(wp)
3027 {
3028 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3029 {
3030 wp->w_p_fen = curwin->w_p_fen;
3031 changed_window_setting_win(wp);
3032 }
3033 }
3034 }
3035# endif
3036 changed_window_setting();
3037 }
3038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (old_fdl != curwin->w_p_fdl)
3041 newFoldLevel();
3042#endif
3043}
3044
3045#ifdef FEAT_GUI
3046/*
3047 * Vertical scrollbar movement.
3048 */
3049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (cap->oap->op_type != OP_NOP)
3053 clearopbeep(cap->oap);
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 gui_do_scroll();
3057}
3058
3059/*
3060 * Horizontal scrollbar movement.
3061 */
3062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003063nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 if (cap->oap->op_type != OP_NOP)
3066 clearopbeep(cap->oap);
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003069 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070}
3071#endif
3072
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003073#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003074/*
3075 * Click in GUI tab.
3076 */
3077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003079{
3080 if (cap->oap->op_type != OP_NOP)
3081 clearopbeep(cap->oap);
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003084 goto_tabpage(current_tab);
3085}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003086
3087/*
3088 * Selected item in tab line menu.
3089 */
3090 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003091nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003092{
3093 if (cap->oap->op_type != OP_NOP)
3094 clearopbeep(cap->oap);
3095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003097 handle_tabmenu();
3098}
3099
3100/*
3101 * Handle selecting an item of the GUI tab line menu.
3102 * Used in Normal and Insert mode.
3103 */
3104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003105handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003106{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003107 switch (current_tabmenu)
3108 {
3109 case TABLINE_MENU_CLOSE:
3110 if (current_tab == 0)
3111 do_cmdline_cmd((char_u *)"tabclose");
3112 else
3113 {
3114 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3115 current_tab);
3116 do_cmdline_cmd(IObuff);
3117 }
3118 break;
3119
3120 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003121 if (current_tab == 0)
3122 do_cmdline_cmd((char_u *)"$tabnew");
3123 else
3124 {
3125 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3126 current_tab - 1);
3127 do_cmdline_cmd(IObuff);
3128 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003129 break;
3130
3131 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003132 if (current_tab == 0)
3133 do_cmdline_cmd((char_u *)"browse $tabnew");
3134 else
3135 {
3136 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3137 current_tab - 1);
3138 do_cmdline_cmd(IObuff);
3139 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003140 break;
3141 }
3142}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003143#endif
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * "Q" command.
3147 */
3148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003151 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003153 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003154 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 do_exmode(FALSE);
3156}
3157
3158/*
3159 * Handle a ":" command.
3160 */
3161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003162nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003164 int old_p_im;
3165 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003166 int is_cmdkey = cap->cmdchar == K_COMMAND
3167 || cap->cmdchar == K_SCRIPT_COMMAND;
3168 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaar957cf672020-11-12 14:21:06 +01003170 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 nv_operator(cap);
3172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 if (cap->oap->op_type != OP_NOP)
3175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003176 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 cap->oap->motion_type = MCHAR;
3178 cap->oap->inclusive = FALSE;
3179 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003180 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 stuffcharReadbuff('.');
3184 if (cap->count0 > 1)
3185 {
3186 stuffReadbuff((char_u *)",.+");
3187 stuffnumReadbuff((long)cap->count0 - 1L);
3188 }
3189 }
3190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003191 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (KeyTyped)
3193 compute_cmdrow();
3194
3195 old_p_im = p_im;
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003198 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3199 if (is_cmdkey)
3200 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3201 else
3202 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003204 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (p_im != old_p_im)
3206 {
3207 if (p_im)
3208 restart_edit = 'i';
3209 else
3210 restart_edit = 0;
3211 }
3212
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003213 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003215 clearop(cap->oap);
3216 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3218 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003219 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3220 || did_emsg
3221 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 clearopbeep(cap->oap);
3224 }
3225}
3226
3227/*
3228 * Handle CTRL-G command.
3229 */
3230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003231nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003236 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 showmode();
3238 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003239 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 fileinfo((int)cap->count0, FALSE, TRUE);
3242}
3243
3244/*
3245 * Handle CTRL-H <Backspace> command.
3246 */
3247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003248nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (VIsual_active && VIsual_select)
3251 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 v_visop(cap);
3254 }
3255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 nv_left(cap);
3257}
3258
3259/*
3260 * CTRL-L: clear screen and redraw.
3261 */
3262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003263nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 if (!checkclearop(cap->oap))
3266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003268 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003269 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003270# ifdef FEAT_RELTIME
3271 {
3272 win_T *wp;
3273
3274 FOR_ALL_WINDOWS(wp)
3275 wp->w_s->b_syn_slow = FALSE;
3276 }
3277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#endif
3279 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003280#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3281# ifdef VIMDLL
3282 if (!gui.in_use)
3283# endif
3284 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287}
3288
3289/*
3290 * CTRL-O: In Select mode: switch to Visual mode for one command.
3291 * Otherwise: Go to older pcmark.
3292 */
3293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003294nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (VIsual_active && VIsual_select)
3297 {
3298 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003299 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003301 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 cap->count1 = -cap->count1;
3306 nv_pcmark(cap);
3307 }
3308}
3309
3310/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003311 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3312 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (!checkclearopq(cap->oap))
3318 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3319 GETF_SETMARK|GETF_ALT, FALSE);
3320}
3321
3322/*
3323 * "Z" commands.
3324 */
3325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003326nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 if (!checkclearopq(cap->oap))
3329 {
3330 switch (cap->nchar)
3331 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 case 'Z': do_cmdline_cmd((char_u *)"x");
3334 break;
3335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003336 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 case 'Q': do_cmdline_cmd((char_u *)"q!");
3338 break;
3339
3340 default: clearopbeep(cap->oap);
3341 }
3342 }
3343}
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345/*
3346 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3347 */
3348 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003349do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 oparg_T oa;
3352 cmdarg_T ca;
3353
3354 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003355 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 ca.oap = &oa;
3357 ca.cmdchar = c1;
3358 ca.nchar = c2;
3359 nv_ident(&ca);
3360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
3362/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003363 * 'K' normal-mode command. Get the command to lookup the keyword under the
3364 * cursor.
3365 */
3366 static int
3367nv_K_getcmd(
3368 cmdarg_T *cap,
3369 char_u *kp,
3370 int kp_help,
3371 int kp_ex,
3372 char_u **ptr_arg,
3373 int n,
3374 char_u *buf,
3375 unsigned buflen)
3376{
3377 char_u *ptr = *ptr_arg;
3378 int isman;
3379 int isman_s;
3380
3381 if (kp_help)
3382 {
3383 // in the help buffer
3384 STRCPY(buf, "he! ");
3385 return n;
3386 }
3387
3388 if (kp_ex)
3389 {
3390 // 'keywordprog' is an ex command
3391 if (cap->count0 != 0)
3392 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3393 else
3394 STRCPY(buf, kp);
3395 STRCAT(buf, " ");
3396 return n;
3397 }
3398
3399 // An external command will probably use an argument starting
3400 // with "-" as an option. To avoid trouble we skip the "-".
3401 while (*ptr == '-' && n > 0)
3402 {
3403 ++ptr;
3404 --n;
3405 }
3406 if (n == 0)
3407 {
3408 // found dashes only
3409 emsg(_(e_no_identifier_under_cursor));
3410 vim_free(buf);
3411 *ptr_arg = ptr;
3412 return 0;
3413 }
3414
3415 // When a count is given, turn it into a range. Is this
3416 // really what we want?
3417 isman = (STRCMP(kp, "man") == 0);
3418 isman_s = (STRCMP(kp, "man -s") == 0);
3419 if (cap->count0 != 0 && !(isman || isman_s))
3420 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3421
3422 STRCAT(buf, "! ");
3423 if (cap->count0 == 0 && isman_s)
3424 STRCAT(buf, "man");
3425 else
3426 STRCAT(buf, kp);
3427 STRCAT(buf, " ");
3428 if (cap->count0 != 0 && (isman || isman_s))
3429 {
3430 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3431 STRCAT(buf, " ");
3432 }
3433
3434 *ptr_arg = ptr;
3435 return n;
3436}
3437
3438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Handle the commands that use the word under the cursor.
3440 * [g] CTRL-] :ta to current identifier
3441 * [g] 'K' run program for current identifier
3442 * [g] '*' / to current identifier or string
3443 * [g] '#' ? to current identifier or string
3444 * g ']' :tselect for current identifier
3445 */
3446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003447nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 char_u *ptr = NULL;
3450 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003451 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003452 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 char_u *kp; // value of 'keywordprg'
3455 int kp_help; // 'keywordprg' is ":he"
3456 int kp_ex; // 'keywordprg' starts with ":"
3457 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003460 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 cmdchar = cap->nchar;
3466 g_cmd = TRUE;
3467 }
3468 else
3469 {
3470 cmdchar = cap->cmdchar;
3471 g_cmd = FALSE;
3472 }
3473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003474 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 cmdchar = '#';
3476
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003477 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3481 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (checkclearopq(cap->oap))
3483 return;
3484 }
3485
3486 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3487 (cmdchar == '*' || cmdchar == '#')
3488 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3489 {
3490 clearop(cap->oap);
3491 return;
3492 }
3493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // Allocate buffer to put the command in. Inserting backslashes can
3495 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3496 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3498 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3499 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003500 if (kp_help && *skipwhite(ptr) == NUL)
3501 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003502 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003503 return;
3504 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003505 kp_ex = (*kp == ':');
3506 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3507 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (buf == NULL)
3509 return;
3510 buf[0] = NUL;
3511
3512 switch (cmdchar)
3513 {
3514 case '*':
3515 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003516 // Put cursor at start of word, makes search skip the word
3517 // under the cursor.
3518 // Call setpcmark() first, so "*``" puts the cursor back where
3519 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 setpcmark();
3521 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3522
3523 if (!g_cmd && vim_iswordp(ptr))
3524 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003525 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 break;
3527
3528 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003529 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3530 if (n == 0)
3531 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533
3534 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003535 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#ifdef FEAT_CSCOPE
3537 if (p_cst)
3538 STRCPY(buf, "cstag ");
3539 else
3540#endif
3541 STRCPY(buf, "ts ");
3542 break;
3543
3544 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003545 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (curbuf->b_help)
3547 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003549 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003550 if (g_cmd)
3551 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003552 else if (cap->count0 == 0)
3553 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003554 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003555 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003559 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003560 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003562 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003563 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003564 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003565 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003566 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003568 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003569 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003570 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003572 vim_free(buf);
3573 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003575 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003576 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003577 {
3578 vim_free(buf);
3579 vim_free(p);
3580 return;
3581 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003582 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003583 STRCAT(buf, p);
3584 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003586 else
3587 {
3588 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003589 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003590 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003591 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003592 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003593 {
3594 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003595 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003596 aux_ptr = (char_u *)"";
3597 else
3598 aux_ptr = (char_u *)"\\|\"\n[";
3599 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003600 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003601 aux_ptr = (char_u *)"\\|\"\n*?[";
3602
3603 p = buf + STRLEN(buf);
3604 while (n-- > 0)
3605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003606 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 if (vim_strchr(aux_ptr, *ptr) != NULL)
3608 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003609 // When current byte is a part of multibyte character, copy all
3610 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003611 if (has_mbyte)
3612 {
3613 int i;
3614 int len = (*mb_ptr2len)(ptr) - 1;
3615
3616 for (i = 0; i < len && n >= 1; ++i, --n)
3617 *p++ = *ptr++;
3618 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003619 *p++ = *ptr++;
3620 }
3621 *p = NUL;
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003624 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (cmdchar == '*' || cmdchar == '#')
3626 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003627 if (!g_cmd && (has_mbyte
3628 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3629 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003631
3632 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003633 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003635
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003636 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003639 {
3640 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003642 g_tag_at_cursor = FALSE;
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 vim_free(buf);
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648/*
3649 * Get visually selected text, within one line only.
3650 * Returns FAIL if more than one line selected.
3651 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003652 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003653get_visual_text(
3654 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003655 char_u **pp, // return: start of selected text
3656 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (VIsual_mode != 'V')
3659 unadjust_for_sel();
3660 if (VIsual.lnum != curwin->w_cursor.lnum)
3661 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003662 if (cap != NULL)
3663 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 return FAIL;
3665 }
3666 if (VIsual_mode == 'V')
3667 {
3668 *pp = ml_get_curline();
3669 *lenp = (int)STRLEN(*pp);
3670 }
3671 else
3672 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003673 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 *pp = ml_get_pos(&curwin->w_cursor);
3676 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3677 }
3678 else
3679 {
3680 *pp = ml_get_pos(&VIsual);
3681 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3682 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003683 if (**pp == NUL)
3684 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003685 if (*lenp > 0)
3686 {
3687 if (has_mbyte)
3688 // Correct the length to include all bytes of the last
3689 // character.
3690 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3691 else if ((*pp)[*lenp - 1] == NUL)
3692 // Do not include a trailing NUL.
3693 *lenp -= 1;
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 reset_VIsual_and_resel();
3697 return OK;
3698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700/*
3701 * CTRL-T: backwards in tag stack
3702 */
3703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003704nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 if (!checkclearopq(cap->oap))
3707 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3708}
3709
3710/*
3711 * Handle scrolling command 'H', 'L' and 'M'.
3712 */
3713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003714nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 int used = 0;
3717 long n;
3718#ifdef FEAT_FOLDING
3719 linenr_T lnum;
3720#endif
3721 int half;
3722
3723 cap->oap->motion_type = MLINE;
3724 setpcmark();
3725
3726 if (cap->cmdchar == 'L')
3727 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003728 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 curwin->w_cursor.lnum = curwin->w_botline - 1;
3730 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3731 curwin->w_cursor.lnum = 1;
3732 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003733 {
3734#ifdef FEAT_FOLDING
3735 if (hasAnyFolding(curwin))
3736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003737 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003738 for (n = cap->count1 - 1; n > 0
3739 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3740 {
3741 (void)hasFolding(curwin->w_cursor.lnum,
3742 &curwin->w_cursor.lnum, NULL);
3743 --curwin->w_cursor.lnum;
3744 }
3745 }
3746 else
3747#endif
3748 curwin->w_cursor.lnum -= cap->count1 - 1;
3749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 else
3752 {
3753 if (cap->cmdchar == 'M')
3754 {
3755#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003756 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 used -= diff_check_fill(curwin, curwin->w_topline)
3758 - curwin->w_topfill;
3759#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003760 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3762 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3763 {
3764#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003765 // Count half he number of filler lines to be "below this
3766 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3768 + n) / 2 >= half)
3769 {
3770 --n;
3771 break;
3772 }
3773#endif
3774 used += plines(curwin->w_topline + n);
3775 if (used >= half)
3776 break;
3777#ifdef FEAT_FOLDING
3778 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3779 n = lnum - curwin->w_topline;
3780#endif
3781 }
3782 if (n > 0 && used > curwin->w_height)
3783 --n;
3784 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003788#ifdef FEAT_FOLDING
3789 if (hasAnyFolding(curwin))
3790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003791 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003792 lnum = curwin->w_topline;
3793 while (n-- > 0 && lnum < curwin->w_botline - 1)
3794 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003795 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003796 ++lnum;
3797 }
3798 n = lnum - curwin->w_topline;
3799 }
3800#endif
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 curwin->w_cursor.lnum = curwin->w_topline + n;
3803 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3804 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3805 }
3806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003807 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003808 if (cap->oap->op_type == OP_NOP)
3809 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 beginline(BL_SOL | BL_FIX);
3811}
3812
3813/*
3814 * Cursor right commands.
3815 */
3816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003817nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003820 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003822 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003824 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003825 if (mod_mask & MOD_MASK_CTRL)
3826 cap->arg = TRUE;
3827 nv_wordcmd(cap);
3828 return;
3829 }
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 cap->oap->motion_type = MCHAR;
3832 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003833 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003835 // In virtual edit mode, there's no such thing as "past_line", as lines
3836 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003838 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840 for (n = cap->count1; n > 0; --n)
3841 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003842 if ((!past_line && oneright() == FAIL)
3843 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003844 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003846 // <Space> wraps to next line if 'whichwrap' has 's'.
3847 // 'l' wraps to next line if 'whichwrap' has 'l'.
3848 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if ( ((cap->cmdchar == ' '
3850 && vim_strchr(p_ww, 's') != NULL)
3851 || (cap->cmdchar == 'l'
3852 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003853 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 && vim_strchr(p_ww, '>') != NULL))
3855 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003857 // When deleting we also count the NL as a character.
3858 // Set cap->oap->inclusive when last char in the line is
3859 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003860 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003862 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 cap->oap->inclusive = TRUE;
3864 else
3865 {
3866 ++curwin->w_cursor.lnum;
3867 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 curwin->w_set_curswant = TRUE;
3870 cap->oap->inclusive = FALSE;
3871 }
3872 continue;
3873 }
3874 if (cap->oap->op_type == OP_NOP)
3875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003876 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (n == cap->count1)
3878 beep_flush();
3879 }
3880 else
3881 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003882 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 cap->oap->inclusive = TRUE;
3884 }
3885 break;
3886 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003887 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
3889 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (virtual_active())
3891 oneright();
3892 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003895 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ++curwin->w_cursor.col;
3898 }
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901#ifdef FEAT_FOLDING
3902 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3903 && cap->oap->op_type == OP_NOP)
3904 foldOpenCursor();
3905#endif
3906}
3907
3908/*
3909 * Cursor left commands.
3910 *
3911 * Returns TRUE when operator end should not be adjusted.
3912 */
3913 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003914nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 long n;
3917
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003920 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003921 if (mod_mask & MOD_MASK_CTRL)
3922 cap->arg = 1;
3923 nv_bck_word(cap);
3924 return;
3925 }
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 cap->oap->motion_type = MCHAR;
3928 cap->oap->inclusive = FALSE;
3929 for (n = cap->count1; n > 0; --n)
3930 {
3931 if (oneleft() == FAIL)
3932 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003933 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3934 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3935 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if ( (((cap->cmdchar == K_BS
3937 || cap->cmdchar == Ctrl_H)
3938 && vim_strchr(p_ww, 'b') != NULL)
3939 || (cap->cmdchar == 'h'
3940 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003941 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 && vim_strchr(p_ww, '<') != NULL))
3943 && curwin->w_cursor.lnum > 1)
3944 {
3945 --(curwin->w_cursor.lnum);
3946 coladvance((colnr_T)MAXCOL);
3947 curwin->w_set_curswant = TRUE;
3948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003949 // When the NL before the first char has to be deleted we
3950 // put the cursor on the NUL after the previous line.
3951 // This is a very special case, be careful!
3952 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if ( (cap->oap->op_type == OP_DELETE
3954 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003955 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003957 char_u *cp = ml_get_cursor();
3958
3959 if (*cp != NUL)
3960 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003961 if (has_mbyte)
3962 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3963 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003964 ++curwin->w_cursor.col;
3965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 cap->retval |= CA_NO_ADJ_OP_END;
3967 }
3968 continue;
3969 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003970 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3972 beep_flush();
3973 break;
3974 }
3975 }
3976#ifdef FEAT_FOLDING
3977 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3978 && cap->oap->op_type == OP_NOP)
3979 foldOpenCursor();
3980#endif
3981}
3982
3983/*
3984 * Cursor up commands.
3985 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3986 */
3987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003988nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003990 if (mod_mask & MOD_MASK_SHIFT)
3991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003992 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993 cap->arg = BACKWARD;
3994 nv_page(cap);
3995 }
3996 else
3997 {
3998 cap->oap->motion_type = MLINE;
3999 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4000 clearopbeep(cap->oap);
4001 else if (cap->arg)
4002 beginline(BL_WHITE | BL_FIX);
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004}
4005
4006/*
4007 * Cursor down commands.
4008 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4009 */
4010 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004011nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004013 if (mod_mask & MOD_MASK_SHIFT)
4014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004015 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 cap->arg = FORWARD;
4017 nv_page(cap);
4018 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004019#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004021 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4022 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004028 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 cmdwin_result = CAR;
4030 else
4031#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004032#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004033 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004034 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4035 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4036 {
4037 invoke_prompt_callback();
4038 if (restart_edit == 0)
4039 restart_edit = 'a';
4040 }
4041 else
4042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
4044 cap->oap->motion_type = MLINE;
4045 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4046 clearopbeep(cap->oap);
4047 else if (cap->arg)
4048 beginline(BL_WHITE | BL_FIX);
4049 }
4050 }
4051}
4052
4053#ifdef FEAT_SEARCHPATH
4054/*
4055 * Grab the file name under the cursor and edit it.
4056 */
4057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004061 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004063 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 if (curbuf_locked())
4066 {
4067 clearop(cap->oap);
4068 return;
4069 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004070#ifdef FEAT_PROP_POPUP
4071 if (ERROR_IF_TERM_POPUP_WINDOW)
4072 return;
4073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004075 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 if (ptr != NULL)
4078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004079 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004080 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004081 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004083 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004084 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004085 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004086 {
4087 curwin->w_cursor.lnum = lnum;
4088 check_cursor_lnum();
4089 beginline(BL_SOL | BL_FIX);
4090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 vim_free(ptr);
4092 }
4093 else
4094 clearop(cap->oap);
4095}
4096#endif
4097
4098/*
4099 * <End> command: to end of current line or last line.
4100 */
4101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004102nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004104 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004108 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 nv_dollar(cap);
4111}
4112
4113/*
4114 * Handle the "$" command.
4115 */
4116 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004117nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 cap->oap->motion_type = MCHAR;
4120 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004121 // In virtual mode when off the edge of a line and an operator
4122 // is pending (whew!) keep the cursor where it is.
4123 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (!virtual_active() || gchar_cursor() != NUL
4125 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (cursor_down((long)(cap->count1 - 1),
4128 cap->oap->op_type == OP_NOP) == FAIL)
4129 clearopbeep(cap->oap);
4130#ifdef FEAT_FOLDING
4131 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4132 foldOpenCursor();
4133#endif
4134}
4135
4136/*
4137 * Implementation of '?' and '/' commands.
4138 * If cap->arg is TRUE don't set PC mark.
4139 */
4140 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004141nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004144 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004148 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 cap->cmdchar = 'g';
4150 cap->nchar = '?';
4151 nv_operator(cap);
4152 return;
4153 }
4154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004155 // When using 'incsearch' the cursor may be moved to set a different search
4156 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004157 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 if (cap->searchbuf == NULL)
4160 {
4161 clearop(oap);
4162 return;
4163 }
4164
Bram Moolenaar46539112015-02-17 15:43:57 +01004165 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004166 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004167 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168}
4169
LemonBoya4399382022-04-09 21:04:08 +01004170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171/*
4172 * Handle "N" and "n" commands.
4173 * cap->arg is SEARCH_REV for "N", 0 for "n".
4174 */
4175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004176nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004178 pos_T old = curwin->w_cursor;
4179 int wrapped = FALSE;
4180 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004181
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004182 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004183 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004184 // Avoid getting stuck on the current cursor position, which can
4185 // happen when an offset is given and the cursor is on the last char
4186 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004187 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004188 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004189 cap->count1 -= 1;
4190 }
LemonBoya4399382022-04-09 21:04:08 +01004191
4192#ifdef FEAT_SEARCH_EXTRA
4193 // Redraw the window to refresh the highlighted matches.
4194 if (i > 0 && p_hls && !no_hlsearch)
4195 redraw_later(SOME_VALID);
4196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197}
4198
4199/*
4200 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4201 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004202 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004204 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004205normal_search(
4206 cmdarg_T *cap,
4207 int dir,
4208 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004209 int opt, // extra flags for do_search()
4210 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004213 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004214#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004215 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 cap->oap->motion_type = MCHAR;
4219 cap->oap->inclusive = FALSE;
4220 cap->oap->use_reg_one = TRUE;
4221 curwin->w_set_curswant = TRUE;
4222
Bram Moolenaara80faa82020-04-12 19:37:17 +02004223 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004224 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004225 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4226 if (wrapped != NULL)
4227 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (i == 0)
4229 clearop(cap->oap);
4230 else
4231 {
4232 if (i == 2)
4233 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#ifdef FEAT_FOLDING
4236 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4237 foldOpenCursor();
4238#endif
4239 }
LemonBoya4399382022-04-09 21:04:08 +01004240#ifdef FEAT_SEARCH_EXTRA
4241 // Redraw the window to refresh the highlighted matches.
4242 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
4243 redraw_later(SOME_VALID);
4244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004246 // "/$" will put the cursor after the end of the line, may need to
4247 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004249 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250}
4251
4252/*
4253 * Character search commands.
4254 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4255 * ',' and FALSE for ';'.
4256 * cap->nchar is NUL for ',' and ';' (repeat the search)
4257 */
4258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004259nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260{
4261 int t_cmd;
4262
4263 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4264 t_cmd = TRUE;
4265 else
4266 t_cmd = FALSE;
4267
4268 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4270 clearopbeep(cap->oap);
4271 else
4272 {
4273 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004274 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4276 && (t_cmd || cap->oap->op_type != OP_NOP))
4277 {
4278 colnr_T scol, ecol;
4279
4280 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4281 curwin->w_cursor.coladd = ecol - scol;
4282 }
4283 else
4284 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef FEAT_FOLDING
4287 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4288 foldOpenCursor();
4289#endif
4290 }
4291}
4292
4293/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004294 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4295 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4296 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4297 * "[m" or "]m" search for prev/next start of (Java) method.
4298 * "[M" or "]M" search for prev/next end of (Java) method.
4299 */
4300 static void
4301nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4302{
4303 pos_T new_pos = {0, 0, 0};
4304 pos_T *pos = NULL; // init for GCC
4305 pos_T prev_pos;
4306 long n;
4307 int findc;
4308 int c;
4309
4310 if (cap->nchar == '*')
4311 cap->nchar = '/';
4312 prev_pos.lnum = 0;
4313 if (cap->nchar == 'm' || cap->nchar == 'M')
4314 {
4315 if (cap->cmdchar == '[')
4316 findc = '{';
4317 else
4318 findc = '}';
4319 n = 9999;
4320 }
4321 else
4322 {
4323 findc = cap->nchar;
4324 n = cap->count1;
4325 }
4326 for ( ; n > 0; --n)
4327 {
4328 if ((pos = findmatchlimit(cap->oap, findc,
4329 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4330 {
4331 if (new_pos.lnum == 0) // nothing found
4332 {
4333 if (cap->nchar != 'm' && cap->nchar != 'M')
4334 clearopbeep(cap->oap);
4335 }
4336 else
4337 pos = &new_pos; // use last one found
4338 break;
4339 }
4340 prev_pos = new_pos;
4341 curwin->w_cursor = *pos;
4342 new_pos = *pos;
4343 }
4344 curwin->w_cursor = *old_pos;
4345
4346 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4347 // brought us to the match for "[m" and "]M" when inside a method.
4348 // Try finding the '{' or '}' we want to be at.
4349 // Also repeat for the given count.
4350 if (cap->nchar == 'm' || cap->nchar == 'M')
4351 {
4352 // norm is TRUE for "]M" and "[m"
4353 int norm = ((findc == '{') == (cap->nchar == 'm'));
4354
4355 n = cap->count1;
4356 // found a match: we were inside a method
4357 if (prev_pos.lnum != 0)
4358 {
4359 pos = &prev_pos;
4360 curwin->w_cursor = prev_pos;
4361 if (norm)
4362 --n;
4363 }
4364 else
4365 pos = NULL;
4366 while (n > 0)
4367 {
4368 for (;;)
4369 {
4370 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4371 {
4372 // if not found anything, that's an error
4373 if (pos == NULL)
4374 clearopbeep(cap->oap);
4375 n = 0;
4376 break;
4377 }
4378 c = gchar_cursor();
4379 if (c == '{' || c == '}')
4380 {
4381 // Must have found end/start of class: use it.
4382 // Or found the place to be at.
4383 if ((c == findc && norm) || (n == 1 && !norm))
4384 {
4385 new_pos = curwin->w_cursor;
4386 pos = &new_pos;
4387 n = 0;
4388 }
4389 // if no match found at all, we started outside of the
4390 // class and we're inside now. Just go on.
4391 else if (new_pos.lnum == 0)
4392 {
4393 new_pos = curwin->w_cursor;
4394 pos = &new_pos;
4395 }
4396 // found start/end of other method: go to match
4397 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004398 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4399 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004400 n = 0;
4401 else
4402 curwin->w_cursor = *pos;
4403 break;
4404 }
4405 }
4406 --n;
4407 }
4408 curwin->w_cursor = *old_pos;
4409 if (pos == NULL && new_pos.lnum != 0)
4410 clearopbeep(cap->oap);
4411 }
4412 if (pos != NULL)
4413 {
4414 setpcmark();
4415 curwin->w_cursor = *pos;
4416 curwin->w_set_curswant = TRUE;
4417#ifdef FEAT_FOLDING
4418 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4419 && cap->oap->op_type == OP_NOP)
4420 foldOpenCursor();
4421#endif
4422 }
4423}
4424
4425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 * "[" and "]" commands.
4427 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4428 */
4429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004430nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004433 pos_T *pos = NULL; // init for GCC
4434 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 int flag;
4436 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 cap->oap->motion_type = MCHAR;
4439 cap->oap->inclusive = FALSE;
4440 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004441 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
4443#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004444 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (cap->nchar == 'f')
4446 nv_gotofile(cap);
4447 else
4448#endif
4449
4450#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004451 // Find the occurrence(s) of the identifier or define under cursor
4452 // in current and included files or jump to the first occurrence.
4453 //
4454 // search list jump
4455 // fwd bwd fwd bwd fwd bwd
4456 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4457 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004458 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
4460 char_u *ptr;
4461 int len;
4462
4463 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4464 clearop(cap->oap);
4465 else
4466 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004467 // Make a copy, if the line was changed it will be freed.
4468 ptr = vim_strnsave(ptr, len);
4469 if (ptr == NULL)
4470 return;
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 find_pattern_in_path(ptr, 0, len, TRUE,
4473 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4474 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4475 cap->count1,
4476 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4477 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4478 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4479 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004480 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 curwin->w_set_curswant = TRUE;
4482 }
4483 }
4484 else
4485#endif
4486
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004487 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4488 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4489 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4490 // "[m" or "]m" search for prev/next start of (Java) method.
4491 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if ( (cap->cmdchar == '['
4493 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4494 || (cap->cmdchar == ']'
4495 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004496 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004498 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 else if (cap->nchar == '[' || cap->nchar == ']')
4500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004501 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 flag = '{';
4503 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004504 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004507 // Imitate strange Vi behaviour: When using "]]" with an operator
4508 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004509 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 (cap->oap->op_type != OP_NOP
4511 && cap->arg == FORWARD && flag == '{')))
4512 clearopbeep(cap->oap);
4513 else
4514 {
4515 if (cap->oap->op_type == OP_NOP)
4516 beginline(BL_WHITE | BL_FIX);
4517#ifdef FEAT_FOLDING
4518 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4519 foldOpenCursor();
4520#endif
4521 }
4522 }
4523
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004524 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 else if (cap->nchar == 'p' || cap->nchar == 'P')
4526 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004527 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004530 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 else if (cap->nchar == '\'' || cap->nchar == '`')
4532 {
4533 pos = &curwin->w_cursor;
4534 for (n = cap->count1; n > 0; --n)
4535 {
4536 prev_pos = *pos;
4537 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4538 cap->nchar == '\'');
4539 if (pos == NULL)
4540 break;
4541 }
4542 if (pos == NULL)
4543 pos = &prev_pos;
4544 nv_cursormark(cap, cap->nchar == '\'', pos);
4545 }
4546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004547 // [ or ] followed by a middle mouse click: put selected text with
4548 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004549 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 (void)do_mouse(cap->oap, cap->nchar,
4552 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4553 cap->count1, PUT_FIXINDENT);
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004557 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 else if (cap->nchar == 'z')
4559 {
4560 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4561 cap->count1) == FAIL)
4562 clearopbeep(cap->oap);
4563 }
4564#endif
4565
4566#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004567 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 else if (cap->nchar == 'c')
4569 {
4570 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4571 cap->count1) == FAIL)
4572 clearopbeep(cap->oap);
4573 }
4574#endif
4575
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004576#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004577 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004578 else if (cap->nchar == 's' || cap->nchar == 'S')
4579 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004580 setpcmark();
4581 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004582 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4583 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004584 {
4585 clearopbeep(cap->oap);
4586 break;
4587 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004588 else
4589 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004590# ifdef FEAT_FOLDING
4591 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4592 foldOpenCursor();
4593# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594 }
4595#endif
4596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 clearopbeep(cap->oap);
4600}
4601
4602/*
4603 * Handle Normal mode "%" command.
4604 */
4605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004606nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
4608 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004609#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 linenr_T lnum = curwin->w_cursor.lnum;
4611#endif
4612
4613 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004614 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 if (cap->count0 > 100)
4617 clearopbeep(cap->oap);
4618 else
4619 {
4620 cap->oap->motion_type = MLINE;
4621 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004622 // Round up, so 'normal 100%' always jumps at the line line.
4623 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4624 // overflow on 32-bits, so use a formula with less accuracy
4625 // to avoid overflows.
4626 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4628 / 100L * cap->count0;
4629 else
4630 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4631 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004632 if (curwin->w_cursor.lnum < 1)
4633 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4635 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4636 beginline(BL_SOL | BL_FIX);
4637 }
4638 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004639 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 cap->oap->motion_type = MCHAR;
4642 cap->oap->use_reg_one = TRUE;
4643 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4644 clearopbeep(cap->oap);
4645 else
4646 {
4647 setpcmark();
4648 curwin->w_cursor = *pos;
4649 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 }
4654#ifdef FEAT_FOLDING
4655 if (cap->oap->op_type == OP_NOP
4656 && lnum != curwin->w_cursor.lnum
4657 && (fdo_flags & FDO_PERCENT)
4658 && KeyTyped)
4659 foldOpenCursor();
4660#endif
4661}
4662
4663/*
4664 * Handle "(" and ")" commands.
4665 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4666 */
4667 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004668nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670 cap->oap->motion_type = MCHAR;
4671 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004673 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 curwin->w_set_curswant = TRUE;
4675
4676 if (findsent(cap->arg, cap->count1) == FAIL)
4677 clearopbeep(cap->oap);
4678 else
4679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004680 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004681 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#ifdef FEAT_FOLDING
4684 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4685 foldOpenCursor();
4686#endif
4687 }
4688}
4689
4690/*
4691 * "m" command: Mark a position.
4692 */
4693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004694nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 if (!checkclearop(cap->oap))
4697 {
4698 if (setmark(cap->nchar) == FAIL)
4699 clearopbeep(cap->oap);
4700 }
4701}
4702
4703/*
4704 * "{" and "}" commands.
4705 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4706 */
4707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 cap->oap->motion_type = MCHAR;
4711 cap->oap->inclusive = FALSE;
4712 cap->oap->use_reg_one = TRUE;
4713 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004714 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 clearopbeep(cap->oap);
4716 else
4717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719#ifdef FEAT_FOLDING
4720 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4721 foldOpenCursor();
4722#endif
4723 }
4724}
4725
4726/*
4727 * "u" command: Undo or make lower case.
4728 */
4729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004730nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004732 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004734 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 cap->cmdchar = 'g';
4736 cap->nchar = 'u';
4737 nv_operator(cap);
4738 }
4739 else
4740 nv_kundo(cap);
4741}
4742
4743/*
4744 * <Undo> command.
4745 */
4746 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004747nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 if (!checkclearopq(cap->oap))
4750 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004751#ifdef FEAT_JOB_CHANNEL
4752 if (bt_prompt(curbuf))
4753 {
4754 clearopbeep(cap->oap);
4755 return;
4756 }
4757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 u_undo((int)cap->count1);
4759 curwin->w_set_curswant = TRUE;
4760 }
4761}
4762
4763/*
4764 * Handle the "r" command.
4765 */
4766 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004767nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 char_u *ptr;
4770 int had_ctrl_v;
4771 long n;
4772
4773 if (checkclearop(cap->oap))
4774 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004775#ifdef FEAT_JOB_CHANNEL
4776 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4777 {
4778 clearopbeep(cap->oap);
4779 return;
4780 }
4781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (cap->nchar == Ctrl_V)
4785 {
4786 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004787 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (cap->nchar > DEL)
4790 had_ctrl_v = NUL;
4791 }
4792 else
4793 had_ctrl_v = NUL;
4794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004795 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004796 if (IS_SPECIAL(cap->nchar))
4797 {
4798 clearopbeep(cap->oap);
4799 return;
4800 }
4801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (VIsual_active)
4804 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004805 if (got_int)
4806 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004807 if (had_ctrl_v)
4808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004809 // Use a special (negative) number to make a difference between a
4810 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004811 if (cap->nchar == CAR)
4812 cap->nchar = REPLACE_CR_NCHAR;
4813 else if (cap->nchar == NL)
4814 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 nv_operator(cap);
4817 return;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004820 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (virtual_active())
4822 {
4823 if (u_save_cursor() == FAIL)
4824 return;
4825 if (gchar_cursor() == NUL)
4826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 coladvance_force((colnr_T)(getviscol() + cap->count1));
4829 curwin->w_cursor.col -= cap->count1;
4830 }
4831 else if (gchar_cursor() == TAB)
4832 coladvance_force(getviscol());
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004837 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004838 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 clearopbeep(cap->oap);
4841 return;
4842 }
4843
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004844 // Replacing with a TAB is done by edit() when it is complicated because
4845 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4846 // Other characters are done below to avoid problems with things like
4847 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4849 {
4850 stuffnumReadbuff(cap->count1);
4851 stuffcharReadbuff('R');
4852 stuffcharReadbuff('\t');
4853 stuffcharReadbuff(ESC);
4854 return;
4855 }
4856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004857 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (u_save_cursor() == FAIL)
4859 return;
4860
4861 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4862 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004863 // Replace character(s) by a single newline.
4864 // Strange vi behaviour: Only one newline is inserted.
4865 // Delete the characters here.
4866 // Insert the newline with an insert command, takes care of
4867 // autoindent. The insert command depends on being on the last
4868 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 stuffcharReadbuff('\r');
4871 stuffcharReadbuff(ESC);
4872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004873 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 invoke_edit(cap, TRUE, 'r', FALSE);
4875 }
4876 else
4877 {
4878 prep_redo(cap->oap->regname, cap->count1,
4879 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4880
4881 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (has_mbyte)
4883 {
4884 int old_State = State;
4885
4886 if (cap->ncharC1 != 0)
4887 AppendCharToRedobuff(cap->ncharC1);
4888 if (cap->ncharC2 != 0)
4889 AppendCharToRedobuff(cap->ncharC2);
4890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004891 // This is slow, but it handles replacing a single-byte with a
4892 // multi-byte and the other way around. Also handles adding
4893 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 for (n = cap->count1; n > 0; --n)
4895 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004896 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004897 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4898 {
4899 int c = ins_copychar(curwin->w_cursor.lnum
4900 + (cap->nchar == Ctrl_Y ? -1 : 1));
4901 if (c != NUL)
4902 ins_char(c);
4903 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004905 ++curwin->w_cursor.col;
4906 }
4907 else
4908 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 State = old_State;
4910 if (cap->ncharC1 != 0)
4911 ins_char(cap->ncharC1);
4912 if (cap->ncharC2 != 0)
4913 ins_char(cap->ncharC2);
4914 }
4915 }
4916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004918 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 for (n = cap->count1; n > 0; --n)
4920 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004921 // Get ptr again, because u_save and/or showmatch() will have
4922 // released the line. This may also happen in ins_copychar().
4923 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004924 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4925 {
4926 int c = ins_copychar(curwin->w_cursor.lnum
4927 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004928
4929 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004930 if (c != NUL)
4931 ptr[curwin->w_cursor.col] = c;
4932 }
4933 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004934 {
4935 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004936 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (p_sm && msg_silent == 0)
4939 showmatch(cap->nchar);
4940 ++curwin->w_cursor.col;
4941 }
4942#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004943 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004945 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaar009b2592004-10-24 19:18:58 +00004947 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004948 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004950 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952#endif
4953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 changed_bytes(curwin->w_cursor.lnum,
4956 (colnr_T)(curwin->w_cursor.col - cap->count1));
4957 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004958 --curwin->w_cursor.col; // cursor on the last replaced char
4959 // if the character on the left of the current cursor is a multi-byte
4960 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (has_mbyte)
4962 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 curbuf->b_op_end = curwin->w_cursor;
4964 curwin->w_set_curswant = TRUE;
4965 set_last_insert(cap->nchar);
4966 }
4967}
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969/*
4970 * 'o': Exchange start and end of Visual area.
4971 * 'O': same, but in block mode exchange left and right corners.
4972 */
4973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004974v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
4976 pos_T old_cursor;
4977 colnr_T left, right;
4978
4979 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4980 {
4981 old_cursor = curwin->w_cursor;
4982 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4983 curwin->w_cursor.lnum = VIsual.lnum;
4984 coladvance(left);
4985 VIsual = curwin->w_cursor;
4986
4987 curwin->w_cursor.lnum = old_cursor.lnum;
4988 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004989 // 'selection "exclusive" and cursor at right-bottom corner: move it
4990 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4992 ++curwin->w_curswant;
4993 coladvance(curwin->w_curswant);
4994 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004996 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 curwin->w_cursor.lnum = VIsual.lnum;
4999 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5000 ++right;
5001 coladvance(right);
5002 VIsual = curwin->w_cursor;
5003
5004 curwin->w_cursor.lnum = old_cursor.lnum;
5005 coladvance(left);
5006 curwin->w_curswant = left;
5007 }
5008 }
5009 else
5010 {
5011 old_cursor = curwin->w_cursor;
5012 curwin->w_cursor = VIsual;
5013 VIsual = old_cursor;
5014 curwin->w_set_curswant = TRUE;
5015 }
5016}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018/*
5019 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5020 */
5021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005022nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005024 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
5026 cap->cmdchar = 'c';
5027 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 VIsual_mode = 'V';
5030 nv_operator(cap);
5031 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005032 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005035 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
5037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (virtual_active())
5039 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5041 }
5042 }
5043}
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045/*
5046 * "gr".
5047 */
5048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005049nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (VIsual_active)
5052 {
5053 cap->cmdchar = 'r';
5054 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005055 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005057 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005060 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
5062 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005063 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005064 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 stuffcharReadbuff(cap->extra_char);
5066 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (virtual_active())
5068 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 invoke_edit(cap, TRUE, 'v', FALSE);
5070 }
5071 }
5072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074/*
5075 * Swap case for "~" command, when it does not work like an operator.
5076 */
5077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005078n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 long n;
5081 pos_T startpos;
5082 int did_change = 0;
5083#ifdef FEAT_NETBEANS_INTG
5084 pos_T pos;
5085 char_u *ptr;
5086 int count;
5087#endif
5088
5089 if (checkclearopq(cap->oap))
5090 return;
5091
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005092 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 clearopbeep(cap->oap);
5095 return;
5096 }
5097
5098 prep_redo_cmd(cap);
5099
5100 if (u_save_cursor() == FAIL)
5101 return;
5102
5103 startpos = curwin->w_cursor;
5104#ifdef FEAT_NETBEANS_INTG
5105 pos = startpos;
5106#endif
5107 for (n = cap->count1; n > 0; --n)
5108 {
5109 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5110 inc_cursor();
5111 if (gchar_cursor() == NUL)
5112 {
5113 if (vim_strchr(p_ww, '~') != NULL
5114 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5115 {
5116#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005117 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 if (did_change)
5120 {
5121 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005122 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005123 netbeans_removed(curbuf, pos.lnum, pos.col,
5124 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005126 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 pos.col = 0;
5129 pos.lnum++;
5130 }
5131#endif
5132 ++curwin->w_cursor.lnum;
5133 curwin->w_cursor.col = 0;
5134 if (n > 1)
5135 {
5136 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5137 break;
5138 u_clearline();
5139 }
5140 }
5141 else
5142 break;
5143 }
5144 }
5145#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005146 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 ptr = ml_get(pos.lnum);
5149 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005150 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5151 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153#endif
5154
5155
5156 check_cursor();
5157 curwin->w_set_curswant = TRUE;
5158 if (did_change)
5159 {
5160 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5161 0L);
5162 curbuf->b_op_start = startpos;
5163 curbuf->b_op_end = curwin->w_cursor;
5164 if (curbuf->b_op_end.col > 0)
5165 --curbuf->b_op_end.col;
5166 }
5167}
5168
5169/*
5170 * Move cursor to mark.
5171 */
5172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005173nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 if (check_mark(pos) == FAIL)
5176 clearop(cap->oap);
5177 else
5178 {
5179 if (cap->cmdchar == '\''
5180 || cap->cmdchar == '`'
5181 || cap->cmdchar == '['
5182 || cap->cmdchar == ']')
5183 setpcmark();
5184 curwin->w_cursor = *pos;
5185 if (flag)
5186 beginline(BL_WHITE | BL_FIX);
5187 else
5188 check_cursor();
5189 }
5190 cap->oap->motion_type = flag ? MLINE : MCHAR;
5191 if (cap->cmdchar == '`')
5192 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005193 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 curwin->w_set_curswant = TRUE;
5195}
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197/*
5198 * Handle commands that are operators in Visual mode.
5199 */
5200 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005201v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005205 // Uppercase means linewise, except in block mode, then "D" deletes till
5206 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (isupper(cap->cmdchar))
5208 {
5209 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005210 {
5211 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5215 curwin->w_curswant = MAXCOL;
5216 }
5217 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5218 nv_operator(cap);
5219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221/*
5222 * "s" and "S" commands.
5223 */
5224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005225nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005227#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005228 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005229 if (term_swap_diff() == OK)
5230 return;
5231#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005232#ifdef FEAT_JOB_CHANNEL
5233 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5234 {
5235 clearopbeep(cap->oap);
5236 return;
5237 }
5238#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005239 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005242 {
5243 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 cap->cmdchar = 'c';
5247 nv_operator(cap);
5248 }
5249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 nv_optrans(cap);
5251}
5252
5253/*
5254 * Abbreviated commands.
5255 */
5256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005260 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005262 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (VIsual_active)
5264 v_visop(cap);
5265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 nv_optrans(cap);
5267}
5268
5269/*
5270 * Translate a command into another command.
5271 */
5272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5276 (char_u *)"d$", (char_u *)"c$",
5277 (char_u *)"cl", (char_u *)"cc",
5278 (char_u *)"yy", (char_u *)":s\r"};
5279 static char_u *str = (char_u *)"xXDCsSY&";
5280
5281 if (!checkclearopq(cap->oap))
5282 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005283 // In Vi "2D" doesn't delete the next line. Can't translate it
5284 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005285 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5286 {
5287 cap->oap->start = curwin->w_cursor;
5288 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005289#ifdef FEAT_EVAL
5290 set_op_var(OP_DELETE);
5291#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005292 cap->count1 = 1;
5293 nv_dollar(cap);
5294 finish_op = TRUE;
5295 ResetRedobuff();
5296 AppendCharToRedobuff('D');
5297 }
5298 else
5299 {
5300 if (cap->count0)
5301 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005302 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 cap->opcount = 0;
5306}
5307
5308/*
5309 * "'" and "`" commands. Also for "g'" and "g`".
5310 * cap->arg is TRUE for "'" and "g'".
5311 */
5312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 pos_T *pos;
5316 int c;
5317#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005318 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005319 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#endif
5321
5322 if (cap->cmdchar == 'g')
5323 c = cap->extra_char;
5324 else
5325 c = cap->nchar;
5326 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005327 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
5329 if (cap->arg)
5330 {
5331 check_cursor_lnum();
5332 beginline(BL_WHITE | BL_FIX);
5333 }
5334 else
5335 check_cursor();
5336 }
5337 else
5338 nv_cursormark(cap, cap->arg, pos);
5339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (!virtual_active())
5342 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005343 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#ifdef FEAT_FOLDING
5345 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005346 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005347 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 && (fdo_flags & FDO_MARK)
5349 && old_KeyTyped)
5350 foldOpenCursor();
5351#endif
5352}
5353
5354/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005355 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 */
5357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005358nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005361#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005363 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 if (!checkclearopq(cap->oap))
5367 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005368 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5369 {
5370 if (goto_tabpage_lastused() == FAIL)
5371 clearopbeep(cap->oap);
5372 return;
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (cap->cmdchar == 'g')
5375 pos = movechangelist((int)cap->count1);
5376 else
5377 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005378 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 curwin->w_set_curswant = TRUE;
5381 check_cursor();
5382 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005383 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 nv_cursormark(cap, FALSE, pos);
5385 else if (cap->cmdchar == 'g')
5386 {
5387 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005388 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005390 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005392 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 else
5395 clearopbeep(cap->oap);
5396# ifdef FEAT_FOLDING
5397 if (cap->oap->op_type == OP_NOP
5398 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5399 && (fdo_flags & FDO_MARK)
5400 && old_KeyTyped)
5401 foldOpenCursor();
5402# endif
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404}
5405
5406/*
5407 * Handle '"' command.
5408 */
5409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005410nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 if (checkclearop(cap->oap))
5413 return;
5414#ifdef FEAT_EVAL
5415 if (cap->nchar == '=')
5416 cap->nchar = get_expr_register();
5417#endif
5418 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5419 {
5420 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005421 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#ifdef FEAT_EVAL
5423 set_reg_var(cap->oap->regname);
5424#endif
5425 }
5426 else
5427 clearopbeep(cap->oap);
5428}
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430/*
5431 * Handle "v", "V" and "CTRL-V" commands.
5432 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5433 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005434 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 */
5436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005437nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005439 if (cap->cmdchar == Ctrl_Q)
5440 cap->cmdchar = Ctrl_V;
5441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5443 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (cap->oap->op_type != OP_NOP)
5445 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005446 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 return;
5449 }
5450
5451 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005454 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 else // toggle char/block mode
5457 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 VIsual_mode = cap->cmdchar;
5459 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005460 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005462 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005464 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005467 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 VIsual = curwin->w_cursor;
5471
5472 VIsual_active = TRUE;
5473 VIsual_reselect = TRUE;
5474 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005478 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005479 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005480 // For V and ^V, we multiply the number of lines even if there
5481 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5483 {
5484 curwin->w_cursor.lnum +=
5485 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005486 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 VIsual_mode = resel_VIsual_mode;
5489 if (VIsual_mode == 'v')
5490 {
5491 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005492 {
5493 validate_virtcol();
5494 curwin->w_curswant = curwin->w_virtcol
5495 + resel_VIsual_vcol * cap->count0 - 1;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005498 curwin->w_curswant = resel_VIsual_vcol;
5499 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005501 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 curwin->w_curswant = MAXCOL;
5504 coladvance((colnr_T)MAXCOL);
5505 }
5506 else if (VIsual_mode == Ctrl_V)
5507 {
5508 validate_virtcol();
5509 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005510 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 coladvance(curwin->w_curswant);
5512 }
5513 else
5514 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005515 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517 else
5518 {
5519 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 may_start_select('c');
5522 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005523 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005525 if (cap->count0 > 0 && --cap->count1 > 0)
5526 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005528 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5529 nv_right(cap);
5530 else if (VIsual_mode == 'V')
5531 nv_down(cap);
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 }
5535}
5536
5537/*
5538 * Start selection for Shift-movement keys.
5539 */
5540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005541start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 may_start_select('k');
5545 n_start_visual_mode('v');
5546}
5547
5548/*
5549 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005550 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 */
5552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005553may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005555 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5556 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558
5559/*
5560 * Start Visual mode "c".
5561 * Should set VIsual_select before calling this.
5562 */
5563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005564n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005566#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005567 int cursor_line_was_concealed = curwin->w_p_cole > 0
5568 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005569#endif
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 VIsual_mode = c;
5572 VIsual_active = TRUE;
5573 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005574
5575 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005576 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005577 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005578 {
5579 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 VIsual = curwin->w_cursor;
5583
5584#ifdef FEAT_FOLDING
5585 foldAdjustVisual();
5586#endif
5587
LemonBoy2bf52dd2022-04-09 18:17:34 +01005588 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005590#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005591 // Check if redraw is needed after changing the state.
5592 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005593#endif
5594
Bram Moolenaar09df3122006-01-23 22:23:09 +00005595 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 // Make sure the clipboard gets updated. Needed because start and
5599 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 clip_star.vmode = NUL;
5601#endif
5602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 // Only need to redraw this line, unless still need to redraw an old
5604 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (curwin->w_redr_type < INVERTED)
5606 {
5607 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5608 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5609 }
5610}
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613/*
5614 * CTRL-W: Window commands
5615 */
5616 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005617nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005619 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005622 cap->cmdchar = ':';
5623 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005624 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005625 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005626 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630/*
5631 * CTRL-Z: Suspend
5632 */
5633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005634nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005638 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005639 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005643 * "gv": Reselect the previous Visual area. If Visual already active,
5644 * exchange previous and current Visual area.
5645 */
5646 static void
5647nv_gv_cmd(cmdarg_T *cap)
5648{
5649 pos_T tpos;
5650 int i;
5651
5652 if (checkclearop(cap->oap))
5653 return;
5654
5655 if (curbuf->b_visual.vi_start.lnum == 0
5656 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5657 || curbuf->b_visual.vi_end.lnum == 0)
5658 {
5659 beep_flush();
5660 return;
5661 }
5662
5663 // set w_cursor to the start of the Visual area, tpos to the end
5664 if (VIsual_active)
5665 {
5666 i = VIsual_mode;
5667 VIsual_mode = curbuf->b_visual.vi_mode;
5668 curbuf->b_visual.vi_mode = i;
5669# ifdef FEAT_EVAL
5670 curbuf->b_visual_mode_eval = i;
5671# endif
5672 i = curwin->w_curswant;
5673 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5674 curbuf->b_visual.vi_curswant = i;
5675
5676 tpos = curbuf->b_visual.vi_end;
5677 curbuf->b_visual.vi_end = curwin->w_cursor;
5678 curwin->w_cursor = curbuf->b_visual.vi_start;
5679 curbuf->b_visual.vi_start = VIsual;
5680 }
5681 else
5682 {
5683 VIsual_mode = curbuf->b_visual.vi_mode;
5684 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5685 tpos = curbuf->b_visual.vi_end;
5686 curwin->w_cursor = curbuf->b_visual.vi_start;
5687 }
5688
5689 VIsual_active = TRUE;
5690 VIsual_reselect = TRUE;
5691
5692 // Set Visual to the start and w_cursor to the end of the Visual
5693 // area. Make sure they are on an existing character.
5694 check_cursor();
5695 VIsual = curwin->w_cursor;
5696 curwin->w_cursor = tpos;
5697 check_cursor();
5698 update_topline();
5699
5700 // When called from normal "g" command: start Select mode when
5701 // 'selectmode' contains "cmd". When called for K_SELECT, always
5702 // start Select mode.
5703 if (cap->arg)
5704 {
5705 VIsual_select = TRUE;
5706 VIsual_select_reg = 0;
5707 }
5708 else
5709 may_start_select('c');
5710 setmouse();
5711#ifdef FEAT_CLIPBOARD
5712 // Make sure the clipboard gets updated. Needed because start and
5713 // end are still the same, and the selection needs to be owned
5714 clip_star.vmode = NUL;
5715#endif
5716 redraw_curbuf_later(INVERTED);
5717 showmode();
5718}
5719
5720/*
5721 * "g0", "g^" : Like "0" and "^" but for screen lines.
5722 * "gm": middle of "g0" and "g$".
5723 */
5724 static void
5725nv_g_home_m_cmd(cmdarg_T *cap)
5726{
5727 int i;
5728 int flag = FALSE;
5729
5730 if (cap->nchar == '^')
5731 flag = TRUE;
5732
5733 cap->oap->motion_type = MCHAR;
5734 cap->oap->inclusive = FALSE;
5735 if (curwin->w_p_wrap && curwin->w_width != 0)
5736 {
5737 int width1 = curwin->w_width - curwin_col_off();
5738 int width2 = width1 + curwin_col_off2();
5739
5740 validate_virtcol();
5741 i = 0;
5742 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5743 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5744 }
5745 else
5746 i = curwin->w_leftcol;
5747 // Go to the middle of the screen line. When 'number' or
5748 // 'relativenumber' is on and lines are wrapping the middle can be more
5749 // to the left.
5750 if (cap->nchar == 'm')
5751 i += (curwin->w_width - curwin_col_off()
5752 + ((curwin->w_p_wrap && i > 0)
5753 ? curwin_col_off2() : 0)) / 2;
5754 coladvance((colnr_T)i);
5755 if (flag)
5756 {
5757 do
5758 i = gchar_cursor();
5759 while (VIM_ISWHITE(i) && oneright() == OK);
5760 curwin->w_valid &= ~VALID_WCOL;
5761 }
5762 curwin->w_set_curswant = TRUE;
5763}
5764
5765/*
5766 * "g_": to the last non-blank character in the line or <count> lines
5767 * downward.
5768 */
5769 static void
5770nv_g_underscore_cmd(cmdarg_T *cap)
5771{
5772 char_u *ptr;
5773
5774 cap->oap->motion_type = MCHAR;
5775 cap->oap->inclusive = TRUE;
5776 curwin->w_curswant = MAXCOL;
5777 if (cursor_down((long)(cap->count1 - 1),
5778 cap->oap->op_type == OP_NOP) == FAIL)
5779 {
5780 clearopbeep(cap->oap);
5781 return;
5782 }
5783
5784 ptr = ml_get_curline();
5785
5786 // In Visual mode we may end up after the line.
5787 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5788 --curwin->w_cursor.col;
5789
5790 // Decrease the cursor column until it's on a non-blank.
5791 while (curwin->w_cursor.col > 0
5792 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5793 --curwin->w_cursor.col;
5794 curwin->w_set_curswant = TRUE;
5795 adjust_for_sel(cap);
5796}
5797
5798/*
5799 * "g$" : Like "$" but for screen lines.
5800 */
5801 static void
5802nv_g_dollar_cmd(cmdarg_T *cap)
5803{
5804 oparg_T *oap = cap->oap;
5805 int i;
5806 int col_off = curwin_col_off();
5807
5808 oap->motion_type = MCHAR;
5809 oap->inclusive = TRUE;
5810 if (curwin->w_p_wrap && curwin->w_width != 0)
5811 {
5812 curwin->w_curswant = MAXCOL; // so we stay at the end
5813 if (cap->count1 == 1)
5814 {
5815 int width1 = curwin->w_width - col_off;
5816 int width2 = width1 + curwin_col_off2();
5817
5818 validate_virtcol();
5819 i = width1 - 1;
5820 if (curwin->w_virtcol >= (colnr_T)width1)
5821 i += ((curwin->w_virtcol - width1) / width2 + 1)
5822 * width2;
5823 coladvance((colnr_T)i);
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 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5830 {
5831 // Check for landing on a character that got split at
5832 // the end of the line. We do not want to advance to
5833 // the next screen line.
5834 if (curwin->w_virtcol > (colnr_T)i)
5835 --curwin->w_cursor.col;
5836 }
5837 }
5838 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5839 clearopbeep(oap);
5840 }
5841 else
5842 {
5843 if (cap->count1 > 1)
5844 // if it fails, let the cursor still move to the last char
5845 (void)cursor_down(cap->count1 - 1, FALSE);
5846
5847 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5848 coladvance((colnr_T)i);
5849
5850 // if the character doesn't fit move one back
5851 if (curwin->w_cursor.col > 0
5852 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5853 {
5854 colnr_T vcol;
5855
5856 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5857 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5858 --curwin->w_cursor.col;
5859 }
5860
5861 // Make sure we stick in this column.
5862 validate_virtcol();
5863 curwin->w_curswant = curwin->w_virtcol;
5864 curwin->w_set_curswant = FALSE;
5865 }
5866}
5867
5868/*
5869 * "gi": start Insert at the last position.
5870 */
5871 static void
5872nv_gi_cmd(cmdarg_T *cap)
5873{
5874 int i;
5875
5876 if (curbuf->b_last_insert.lnum != 0)
5877 {
5878 curwin->w_cursor = curbuf->b_last_insert;
5879 check_cursor_lnum();
5880 i = (int)STRLEN(ml_get_curline());
5881 if (curwin->w_cursor.col > (colnr_T)i)
5882 {
5883 if (virtual_active())
5884 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5885 curwin->w_cursor.col = i;
5886 }
5887 }
5888 cap->cmdchar = 'i';
5889 nv_edit(cap);
5890}
5891
5892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 * Commands starting with "g".
5894 */
5895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005896nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 switch (cap->nchar)
5902 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005903 case Ctrl_A:
5904 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005906 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005907 if (!VIsual_active && cap->nchar == Ctrl_A)
5908 vim_mem_profile_dump();
5909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005911 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005912 if (VIsual_active)
5913 {
5914 cap->arg = TRUE;
5915 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005916 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005917 nv_addsub(cap);
5918 }
5919 else
5920 clearopbeep(oap);
5921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005923 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 case 'R':
5925 cap->arg = TRUE;
5926 nv_Replace(cap);
5927 break;
5928
5929 case 'r':
5930 nv_vreplace(cap);
5931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 case '&':
5934 do_cmdline_cmd((char_u *)"%s//~/&");
5935 break;
5936
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005937 // "gv": Reselect the previous Visual area. If Visual already active,
5938 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005940 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005942
5943 // "gV": Don't reselect the previous Visual area after a Select mode
5944 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 case 'V':
5946 VIsual_reselect = FALSE;
5947 break;
5948
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005949 // "gh": start Select mode.
5950 // "gH": start Select line mode.
5951 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 case K_BS:
5953 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005954 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 case 'h':
5956 case 'H':
5957 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 cap->cmdchar = cap->nchar + ('v' - 'h');
5959 cap->arg = TRUE;
5960 nv_visual(cap);
5961 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005963 // "gn", "gN" visually select next/previous search match
5964 // "gn" selects next match
5965 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005966 case 'N':
5967 case 'n':
5968 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005969 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005972 // "gj" and "gk" two new funny movement keys -- up and down
5973 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 case 'j':
5975 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005976 // with 'nowrap' it works just like the normal "j" command.
5977 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
5979 oap->motion_type = MLINE;
5980 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5981 }
5982 else
5983 i = nv_screengo(oap, FORWARD, cap->count1);
5984 if (i == FAIL)
5985 clearopbeep(oap);
5986 break;
5987
5988 case 'k':
5989 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005990 // with 'nowrap' it works just like the normal "k" command.
5991 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
5993 oap->motion_type = MLINE;
5994 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5995 }
5996 else
5997 i = nv_screengo(oap, BACKWARD, cap->count1);
5998 if (i == FAIL)
5999 clearopbeep(oap);
6000 break;
6001
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006002 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case 'J':
6004 nv_join(cap);
6005 break;
6006
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006007 // "g0", "g^" : Like "0" and "^" but for screen lines.
6008 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 case '0':
6011 case 'm':
6012 case K_HOME:
6013 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006014 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 break;
6016
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006017 case 'M':
6018 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006019 oap->motion_type = MCHAR;
6020 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006021 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006022 if (cap->count0 > 0 && cap->count0 <= 100)
6023 coladvance((colnr_T)(i * cap->count0 / 100));
6024 else
6025 coladvance((colnr_T)(i / 2));
6026 curwin->w_set_curswant = TRUE;
6027 }
6028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // "g_": to the last non-blank character in the line or <count> lines
6031 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 break;
6035
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006036 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 case '$':
6038 case K_END:
6039 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006040 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 break;
6042
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006043 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 case '*':
6045 case '#':
6046#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006047 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006049 case Ctrl_RSB: // :tag or :tselect for current identifier
6050 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 nv_ident(cap);
6052 break;
6053
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006054 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 case 'e':
6056 case 'E':
6057 oap->motion_type = MCHAR;
6058 curwin->w_set_curswant = TRUE;
6059 oap->inclusive = TRUE;
6060 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6061 clearopbeep(oap);
6062 break;
6063
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006064 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006066 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 break;
6068
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006069 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006071 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 break;
6073
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 case 'I':
6076 beginline(0);
6077 if (!checkclearopq(oap))
6078 invoke_edit(cap, FALSE, 'g', FALSE);
6079 break;
6080
6081#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "gf": goto file, edit file under cursor
6083 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006085 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 nv_gotofile(cap);
6087 break;
6088#endif
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 case '\'':
6092 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006093 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 case '`':
6095 nv_gomark(cap);
6096 break;
6097
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006098 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006100 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 break;
6102
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006103 // "ga": Display the ascii value of the character under the
6104 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 case 'a':
6106 do_ascii(NULL);
6107 break;
6108
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006109 // "g8": Display the bytes used for the UTF-8 character under the
6110 // cursor. It is displayed in hex.
6111 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006113 if (cap->count0 == 8)
6114 utf_find_illegal();
6115 else
6116 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006119 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006120 case '<':
6121 show_sb_text();
6122 break;
6123
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006124 // "gg": Goto the first line in file. With a count it goes to
6125 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 case 'g':
6127 cap->arg = FALSE;
6128 nv_goto(cap);
6129 break;
6130
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006131 // Two-character operators:
6132 // "gq" Format text
6133 // "gw" Format text and keep cursor position
6134 // "g~" Toggle the case of the text.
6135 // "gu" Change text to lower case.
6136 // "gU" Change text to upper case.
6137 // "g?" rot13 encoding
6138 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 case 'q':
6140 case 'w':
6141 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006142 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 case '~':
6144 case 'u':
6145 case 'U':
6146 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006147 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 nv_operator(cap);
6149 break;
6150
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006151 // "gd": Find first occurrence of pattern under the cursor in the
6152 // current function
6153 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 case 'd':
6155 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006156 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 break;
6158
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006159 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 case K_MIDDLEMOUSE:
6161 case K_MIDDLEDRAG:
6162 case K_MIDDLERELEASE:
6163 case K_LEFTMOUSE:
6164 case K_LEFTDRAG:
6165 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006166 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 case K_RIGHTMOUSE:
6168 case K_RIGHTDRAG:
6169 case K_RIGHTRELEASE:
6170 case K_X1MOUSE:
6171 case K_X1DRAG:
6172 case K_X1RELEASE:
6173 case K_X2MOUSE:
6174 case K_X2DRAG:
6175 case K_X2RELEASE:
6176 mod_mask = MOD_MASK_CTRL;
6177 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 case K_IGNORE:
6181 break;
6182
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006183 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case 'p':
6185 case 'P':
6186 nv_put(cap);
6187 break;
6188
6189#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006190 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 case 'o':
6192 goto_byte(cap->count0);
6193 break;
6194#endif
6195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006196 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006198 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 do_exmode(TRUE);
6200 break;
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 case ',':
6203 nv_pcmark(cap);
6204 break;
6205
6206 case ';':
6207 cap->count1 = -cap->count1;
6208 nv_pcmark(cap);
6209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006212 if (!checkclearop(oap))
6213 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006215 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006216 if (!checkclearop(oap))
6217 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006218 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219
Bram Moolenaar62a23252020-08-09 14:04:42 +02006220 case TAB:
6221 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6222 clearopbeep(oap);
6223 break;
6224
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006225 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006226 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006227 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006228 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006229 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006230 break;
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 default:
6233 clearopbeep(oap);
6234 break;
6235 }
6236}
6237
6238/*
6239 * Handle "o" and "O" commands.
6240 */
6241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006242n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006244#ifdef FEAT_CONCEAL
6245 linenr_T oldline = curwin->w_cursor.lnum;
6246#endif
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 if (!checkclearopq(cap->oap))
6249 {
6250#ifdef FEAT_FOLDING
6251 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006252 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 (void)hasFolding(curwin->w_cursor.lnum,
6254 &curwin->w_cursor.lnum, NULL);
6255 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006256 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 (void)hasFolding(curwin->w_cursor.lnum,
6258 NULL, &curwin->w_cursor.lnum);
6259#endif
6260 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6261 (cap->cmdchar == 'O' ? 1 : 0)),
6262 (linenr_T)(curwin->w_cursor.lnum +
6263 (cap->cmdchar == 'o' ? 1 : 0))
6264 ) == OK
6265 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006266 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006267 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006269#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006270 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006271 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006272#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006273#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006274 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006275 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006276 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006277#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006279 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6280 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6282 }
6283 }
6284}
6285
6286/*
6287 * "." command: redo last change.
6288 */
6289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006290nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 if (!checkclearopq(cap->oap))
6293 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006294 // If "restart_edit" is TRUE, the last but one command is repeated
6295 // instead of the last command (inserting text). This is used for
6296 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6298 clearopbeep(cap->oap);
6299 }
6300}
6301
6302/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006303 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 */
6305 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006306nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006308 if (VIsual_select && VIsual_active)
6309 {
6310 int reg;
6311 // Get register name
6312 ++no_mapping;
6313 ++allow_keys;
6314 reg = plain_vgetc();
6315 LANGMAP_ADJUST(reg, TRUE);
6316 --no_mapping;
6317 --allow_keys;
6318
6319 if (reg == '"')
6320 // the unnamed register is 0
6321 reg = 0;
6322
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006323 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006324 return;
6325 }
6326
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (!checkclearopq(cap->oap))
6328 {
6329 u_redo((int)cap->count1);
6330 curwin->w_set_curswant = TRUE;
6331 }
6332}
6333
6334/*
6335 * Handle "U" command.
6336 */
6337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006338nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006340 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006341 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 cap->cmdchar = 'g';
6345 cap->nchar = 'U';
6346 nv_operator(cap);
6347 }
6348 else if (!checkclearopq(cap->oap))
6349 {
6350 u_undoline();
6351 curwin->w_set_curswant = TRUE;
6352 }
6353}
6354
6355/*
6356 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6357 * single character.
6358 */
6359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006360nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006362 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006363 {
6364#ifdef FEAT_JOB_CHANNEL
6365 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6366 {
6367 clearopbeep(cap->oap);
6368 return;
6369 }
6370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 else
6374 nv_operator(cap);
6375}
6376
6377/*
6378 * Handle an operator command.
6379 * The actual work is done by do_pending_operator().
6380 */
6381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006382nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int op_type;
6385
6386 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006387#ifdef FEAT_JOB_CHANNEL
6388 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6389 {
6390 clearopbeep(cap->oap);
6391 return;
6392 }
6393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006395 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 nv_lineop(cap);
6397 else if (!checkclearop(cap->oap))
6398 {
6399 cap->oap->start = curwin->w_cursor;
6400 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006401#ifdef FEAT_EVAL
6402 set_op_var(op_type);
6403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405}
6406
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006407#ifdef FEAT_EVAL
6408/*
6409 * Set v:operator to the characters for "optype".
6410 */
6411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006412set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006413{
6414 char_u opchars[3];
6415
6416 if (optype == OP_NOP)
6417 set_vim_var_string(VV_OP, NULL, 0);
6418 else
6419 {
6420 opchars[0] = get_op_char(optype);
6421 opchars[1] = get_extra_op_char(optype);
6422 opchars[2] = NUL;
6423 set_vim_var_string(VV_OP, opchars, -1);
6424 }
6425}
6426#endif
6427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428/*
6429 * Handle linewise operator "dd", "yy", etc.
6430 *
6431 * "_" is is a strange motion command that helps make operators more logical.
6432 * It is actually implemented, but not documented in the real Vi. This motion
6433 * command actually refers to "the current line". Commands like "dd" and "yy"
6434 * are really an alternate form of "d_" and "y_". It does accept a count, so
6435 * "d3_" works to delete 3 lines.
6436 */
6437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006438nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 cap->oap->motion_type = MLINE;
6441 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6442 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006444 && cap->oap->motion_force != 'v'
6445 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 || cap->oap->op_type == OP_LSHIFT
6447 || cap->oap->op_type == OP_RSHIFT)
6448 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006449 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 beginline(BL_WHITE | BL_FIX);
6451}
6452
6453/*
6454 * <Home> command.
6455 */
6456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006457nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006460 if (mod_mask & MOD_MASK_CTRL)
6461 nv_goto(cap);
6462 else
6463 {
6464 cap->count0 = 1;
6465 nv_pipe(cap);
6466 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6468 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469}
6470
6471/*
6472 * "|" command.
6473 */
6474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006475nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 cap->oap->motion_type = MCHAR;
6478 cap->oap->inclusive = FALSE;
6479 beginline(0);
6480 if (cap->count0 > 0)
6481 {
6482 coladvance((colnr_T)(cap->count0 - 1));
6483 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6484 }
6485 else
6486 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006487 // keep curswant at the column where we wanted to go, not where
6488 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 curwin->w_set_curswant = FALSE;
6490}
6491
6492/*
6493 * Handle back-word command "b" and "B".
6494 * cap->arg is 1 for "B"
6495 */
6496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006497nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 cap->oap->motion_type = MCHAR;
6500 cap->oap->inclusive = FALSE;
6501 curwin->w_set_curswant = TRUE;
6502 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6503 clearopbeep(cap->oap);
6504#ifdef FEAT_FOLDING
6505 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6506 foldOpenCursor();
6507#endif
6508}
6509
6510/*
6511 * Handle word motion commands "e", "E", "w" and "W".
6512 * cap->arg is TRUE for "E" and "W".
6513 */
6514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006515nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 int n;
6518 int word_end;
6519 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006520 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006522 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6524 word_end = TRUE;
6525 else
6526 word_end = FALSE;
6527 cap->oap->inclusive = word_end;
6528
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006529 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (!word_end && cap->oap->op_type == OP_CHANGE)
6531 {
6532 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006533 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006535 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006537 // Reproduce a funny Vi behaviour: "cw" on a blank only
6538 // changes one character, not all blanks until the start of
6539 // the next word. Only do this when the 'w' flag is included
6540 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6542 {
6543 cap->oap->inclusive = TRUE;
6544 cap->oap->motion_type = MCHAR;
6545 return;
6546 }
6547 }
6548 else
6549 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006550 // This is a little strange. To match what the real Vi does,
6551 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6552 // that we are not on a space or a TAB. This seems impolite
6553 // at first, but it's really more what we mean when we say
6554 // 'cw'.
6555 // Another strangeness: When standing on the end of a word
6556 // "ce" will change until the end of the next word, but "cw"
6557 // will change only one character! This is done by setting
6558 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 cap->oap->inclusive = TRUE;
6560 word_end = TRUE;
6561 flag = TRUE;
6562 }
6563 }
6564 }
6565
6566 cap->oap->motion_type = MCHAR;
6567 curwin->w_set_curswant = TRUE;
6568 if (word_end)
6569 n = end_word(cap->count1, cap->arg, flag, FALSE);
6570 else
6571 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006573 // Don't leave the cursor on the NUL past the end of line. Unless we
6574 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006575 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006576 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 if (n == FAIL && cap->oap->op_type == OP_NOP)
6579 clearopbeep(cap->oap);
6580 else
6581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583#ifdef FEAT_FOLDING
6584 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6585 foldOpenCursor();
6586#endif
6587 }
6588}
6589
6590/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591 * Used after a movement command: If the cursor ends up on the NUL after the
6592 * end of the line, may move it back to the last character and make the motion
6593 * inclusive.
6594 */
6595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006596adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006598 // The cursor cannot remain on the NUL when:
6599 // - the column is > 0
6600 // - not in Visual mode or 'selection' is "o"
6601 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006603 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006604 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006605 {
6606 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006607 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006608 if (has_mbyte)
6609 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006610 oap->inclusive = TRUE;
6611 }
6612}
6613
6614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 * "0" and "^" commands.
6616 * cap->arg is the argument for beginline().
6617 */
6618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006619nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 cap->oap->motion_type = MCHAR;
6622 cap->oap->inclusive = FALSE;
6623 beginline(cap->arg);
6624#ifdef FEAT_FOLDING
6625 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6626 foldOpenCursor();
6627#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006628 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6629 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630}
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632/*
6633 * In exclusive Visual mode, may include the last character.
6634 */
6635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006639 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (has_mbyte)
6642 inc_cursor();
6643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 ++curwin->w_cursor.col;
6645 cap->oap->inclusive = FALSE;
6646 }
6647}
6648
6649/*
6650 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6651 * Should check VIsual_mode before calling this.
6652 * Returns TRUE when backed up to the previous line.
6653 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006654 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006655unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 pos_T *pp;
6658
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006659 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006661 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 pp = &curwin->w_cursor;
6663 else
6664 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (pp->coladd > 0)
6666 --pp->coladd;
6667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (pp->col > 0)
6669 {
6670 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006671 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 else if (pp->lnum > 1)
6674 {
6675 --pp->lnum;
6676 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6677 return TRUE;
6678 }
6679 }
6680 return FALSE;
6681}
6682
6683/*
6684 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6685 */
6686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006687nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006692 VIsual_select_reg = 0;
6693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 else if (VIsual_reselect)
6695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006696 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 cap->arg = TRUE;
6698 nv_g_cmd(cap);
6699 }
6700}
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703/*
6704 * "G", "gg", CTRL-END, CTRL-HOME.
6705 * cap->arg is TRUE for "G".
6706 */
6707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006708nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 linenr_T lnum;
6711
6712 if (cap->arg)
6713 lnum = curbuf->b_ml.ml_line_count;
6714 else
6715 lnum = 1L;
6716 cap->oap->motion_type = MLINE;
6717 setpcmark();
6718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006719 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (cap->count0 != 0)
6721 lnum = cap->count0;
6722 if (lnum < 1L)
6723 lnum = 1L;
6724 else if (lnum > curbuf->b_ml.ml_line_count)
6725 lnum = curbuf->b_ml.ml_line_count;
6726 curwin->w_cursor.lnum = lnum;
6727 beginline(BL_SOL | BL_FIX);
6728#ifdef FEAT_FOLDING
6729 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6730 foldOpenCursor();
6731#endif
6732}
6733
6734/*
6735 * CTRL-\ in Normal mode.
6736 */
6737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006738nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6741 {
6742 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006743 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 restart_edit = 0;
6746#ifdef FEAT_CMDWIN
6747 if (cmdwin_type != 0)
6748 cmdwin_result = Ctrl_C;
6749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 if (VIsual_active)
6751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 redraw_curbuf_later(INVERTED);
6754 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (cap->nchar == Ctrl_G && p_im)
6757 restart_edit = 'a';
6758 }
6759 else
6760 clearopbeep(cap->oap);
6761}
6762
6763/*
6764 * ESC in Normal mode: beep, but don't flush buffers.
6765 * Don't even beep if we are canceling a command.
6766 */
6767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006768nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770 int no_reason;
6771
6772 no_reason = (cap->oap->op_type == OP_NOP
6773 && cap->opcount == 0
6774 && cap->count0 == 0
6775 && cap->oap->regname == 0
6776 && !p_im);
6777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006778 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 if (restart_edit == 0
6781#ifdef FEAT_CMDWIN
6782 && cmdwin_type == 0
6783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006786 {
6787 if (anyBufIsChanged())
6788 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6789 else
6790 msg(_("Type :qa and press <Enter> to exit Vim"));
6791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006793 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6794 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (!p_im)
6796 restart_edit = 0;
6797#ifdef FEAT_CMDWIN
6798 if (cmdwin_type != 0)
6799 {
6800 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006801 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 return;
6803 }
6804#endif
6805 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006806#ifdef FEAT_CMDWIN
6807 else if (cmdwin_type != 0 && ex_normal_busy)
6808 {
6809 // When :normal runs out of characters while in the command line window
6810 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6811 // loop.
6812 cmdwin_result = K_IGNORE;
6813 return;
6814 }
6815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (VIsual_active)
6818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006819 end_visual_mode(); // stop Visual
6820 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 curwin->w_set_curswant = TRUE;
6822 redraw_curbuf_later(INVERTED);
6823 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006824 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006825 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 clearop(cap->oap);
6827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006828 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6829 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006830 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 restart_edit = 'a';
6832}
6833
6834/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006835 * Move the cursor for the "A" command.
6836 */
6837 void
6838set_cursor_for_append_to_line(void)
6839{
6840 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006841 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006842 {
6843 int save_State = State;
6844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006845 // Pretend Insert mode here to allow the cursor on the
6846 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006847 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006848 coladvance((colnr_T)MAXCOL);
6849 State = save_State;
6850 }
6851 else
6852 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6853}
6854
6855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006857 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 */
6859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006860nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006862 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6864 cap->cmdchar = 'i';
6865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006868 {
6869#ifdef FEAT_TERMINAL
6870 if (term_in_normal_mode())
6871 {
6872 end_visual_mode();
6873 clearop(cap->oap);
6874 term_enter_job_mode();
6875 return;
6876 }
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006881 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006882 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6883 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
6885#ifdef FEAT_TEXTOBJ
6886 nv_object(cap);
6887#else
6888 clearopbeep(cap->oap);
6889#endif
6890 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006891#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006892 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006893 {
6894 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006895 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006896 return;
6897 }
6898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 else if (!curbuf->b_p_ma && !p_im)
6900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006902 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006904 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006906 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006908 else if (cap->cmdchar == K_PS && VIsual_active)
6909 {
6910 pos_T old_pos = curwin->w_cursor;
6911 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006912 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006914 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006915 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6916 {
6917 shift_delete_registers();
6918 cap->oap->regname = '1';
6919 }
6920 else
6921 cap->oap->regname = '-';
6922 cap->cmdchar = 'd';
6923 cap->nchar = NUL;
6924 nv_operator(cap);
6925 do_pending_operator(cap, 0, FALSE);
6926 cap->cmdchar = K_PS;
6927
zeertzjq7a732522022-03-14 20:46:41 +00006928 if (*ml_get_cursor() != NUL)
6929 {
6930 if (old_visual_mode == 'V')
6931 {
6932 // In linewise Visual mode insert before the beginning of the
6933 // next line.
6934 // When the last line in the buffer was deleted then create a
6935 // new line, otherwise there is not need to move cursor.
6936 // Detect this by checking if cursor moved above Visual area.
6937 if (curwin->w_cursor.lnum < old_pos.lnum
6938 && curwin->w_cursor.lnum < old_visual.lnum)
6939 {
6940 if (u_save_cursor() == OK)
6941 {
6942 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6943 FALSE);
6944 appended_lines(curwin->w_cursor.lnum++, 1L);
6945 }
6946 }
6947 }
6948 // When the last char in the line was deleted then append.
6949 // Detect this by checking if cursor moved before Visual area.
6950 else if (curwin->w_cursor.col < old_pos.col
6951 && curwin->w_cursor.col < old_visual.col)
6952 inc_cursor();
6953 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006956 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 else if (!checkclearopq(cap->oap))
6959 {
6960 switch (cap->cmdchar)
6961 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006962 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006963 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 break;
6965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006967 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6968 beginline(BL_WHITE);
6969 else
6970 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 break;
6972
Bram Moolenaara1891842017-02-04 21:34:31 +01006973 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 // Bracketed paste works like "a"ppend, unless the cursor is in
6975 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006976 if (curwin->w_cursor.col == 0)
6977 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 case 'a': // "a"ppend is like "i"nsert on the next character.
6981 // increment coladd when in virtual space, increment the
6982 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (virtual_active()
6984 && (curwin->w_cursor.coladd > 0
6985 || *ml_get_cursor() == NUL
6986 || *ml_get_cursor() == TAB))
6987 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006988 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 inc_cursor();
6990 break;
6991 }
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6994 {
6995 int save_State = State;
6996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006997 // Pretend Insert mode here to allow the cursor on the
6998 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006999 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 coladvance(getviscol());
7001 State = save_State;
7002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7005 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007006 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007008 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009}
7010
7011/*
7012 * Invoke edit() and take care of "restart_edit" and the return value.
7013 */
7014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007015invoke_edit(
7016 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007018 int cmd,
7019 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 int restart_edit_save = 0;
7022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007023 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7024 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7025 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (repl || !stuff_empty())
7027 restart_edit_save = restart_edit;
7028 else
7029 restart_edit_save = 0;
7030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007031 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 restart_edit = 0;
7033
7034 if (edit(cmd, startln, cap->count1))
7035 cap->retval |= CA_COMMAND_BUSY;
7036
7037 if (restart_edit == 0)
7038 restart_edit = restart_edit_save;
7039}
7040
7041#ifdef FEAT_TEXTOBJ
7042/*
7043 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7044 */
7045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007046nv_object(
7047 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 int flag;
7050 int include;
7051 char_u *mps_save;
7052
7053 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 mps_save = curbuf->b_p_mps;
7060 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7061
7062 switch (cap->nchar)
7063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 flag = current_word(cap->oap, cap->count1, include, FALSE);
7066 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 flag = current_word(cap->oap, cap->count1, include, TRUE);
7069 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 case '(':
7072 case ')':
7073 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7074 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 case '{':
7077 case '}':
7078 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7079 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007080 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 case ']':
7082 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7083 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 case '>':
7086 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7087 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case 't': // "at" = a tag block (xml and html)
7089 // Do not adjust oap->end in do_pending_operator()
7090 // otherwise there are different results for 'dit'
7091 // (note leading whitespace in last line):
7092 // 1) <b> 2) <b>
7093 // foobar foobar
7094 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007095 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007096 flag = current_tagblock(cap->oap, cap->count1, include);
7097 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007098 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 flag = current_par(cap->oap, cap->count1, include, 'p');
7100 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 flag = current_sent(cap->oap, cap->count1, include);
7103 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 case '"': // "a"" = a double quoted string
7105 case '\'': // "a'" = a single quoted string
7106 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007107 flag = current_quote(cap->oap, cap->count1, include,
7108 cap->nchar);
7109 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110#if 0 // TODO
7111 case 'S': // "aS" = a section
7112 case 'f': // "af" = a filename
7113 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#endif
7115 default:
7116 flag = FAIL;
7117 break;
7118 }
7119
7120 curbuf->b_p_mps = mps_save;
7121 if (flag == FAIL)
7122 clearopbeep(cap->oap);
7123 adjust_cursor_col();
7124 curwin->w_set_curswant = TRUE;
7125}
7126#endif
7127
7128/*
7129 * "q" command: Start/stop recording.
7130 * "q:", "q/", "q?": edit command-line in command-line window.
7131 */
7132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007133nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
7135 if (cap->oap->op_type == OP_FORMAT)
7136 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007137 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 cap->cmdchar = 'g';
7139 cap->nchar = 'q';
7140 nv_operator(cap);
7141 }
7142 else if (!checkclearop(cap->oap))
7143 {
7144#ifdef FEAT_CMDWIN
7145 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7146 {
7147 stuffcharReadbuff(cap->nchar);
7148 stuffcharReadbuff(K_CMDWIN);
7149 }
7150 else
7151#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007152 // (stop) recording into a named register, unless executing a
7153 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007154 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 clearopbeep(cap->oap);
7156 }
7157}
7158
7159/*
7160 * Handle the "@r" command.
7161 */
7162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007163nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 if (checkclearop(cap->oap))
7166 return;
7167#ifdef FEAT_EVAL
7168 if (cap->nchar == '=')
7169 {
7170 if (get_expr_register() == NUL)
7171 return;
7172 }
7173#endif
7174 while (cap->count1-- && !got_int)
7175 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007176 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
7178 clearopbeep(cap->oap);
7179 break;
7180 }
7181 line_breakcheck();
7182 }
7183}
7184
7185/*
7186 * Handle the CTRL-U and CTRL-D commands.
7187 */
7188 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007189nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7192 || (cap->cmdchar == Ctrl_D
7193 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7194 clearopbeep(cap->oap);
7195 else if (!checkclearop(cap->oap))
7196 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7197}
7198
7199/*
7200 * Handle "J" or "gJ" command.
7201 */
7202 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007203nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007205 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007207 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
7209 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007210 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7212 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007214 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007215 if (cap->count0 <= 2)
7216 {
7217 clearopbeep(cap->oap);
7218 return;
7219 }
7220 cap->count0 = curbuf->b_ml.ml_line_count
7221 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007223
7224 prep_redo(cap->oap->regname, cap->count0,
7225 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7226 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
7228}
7229
7230/*
7231 * "P", "gP", "p" and "gp" commands.
7232 */
7233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007234nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007236 nv_put_opt(cap, FALSE);
7237}
7238
7239/*
7240 * "P", "gP", "p" and "gp" commands.
7241 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7242 */
7243 static void
7244nv_put_opt(cmdarg_T *cap, int fix_indent)
7245{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 int regname = 0;
7247 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007248 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007249 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 int dir;
7251 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007252 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254 if (cap->oap->op_type != OP_NOP)
7255 {
7256#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7259 {
7260 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007261 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263 else
7264#endif
7265 clearopbeep(cap->oap);
7266 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007267#ifdef FEAT_JOB_CHANNEL
7268 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7269 {
7270 clearopbeep(cap->oap);
7271 }
7272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 else
7274 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007275 if (fix_indent)
7276 {
7277 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7278 ? FORWARD : BACKWARD;
7279 flags |= PUT_FIXINDENT;
7280 }
7281 else
7282 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007283 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7284 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 prep_redo_cmd(cap);
7286 if (cap->cmdchar == 'g')
7287 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007288 else if (cap->cmdchar == 'z')
7289 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 if (VIsual_active)
7292 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007293 // Putting in Visual mode: The put text replaces the selected
7294 // text. First delete the selected text, then put the new text.
7295 // Need to save and restore the registers that the delete
7296 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007297 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007299 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007300#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007302#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007303 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007304 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007305#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 )
7310 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311 // The delete is going to overwrite the register we want to
7312 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 reg1 = get_register(regname, TRUE);
7314 }
7315
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007316 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 cap->cmdchar = 'd';
7318 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007319 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007320 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 nv_operator(cap);
7322 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007323 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007324 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007326 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 cap->oap->regname = regname;
7328
7329 if (reg1 != NULL)
7330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007331 // Delete probably changed the register we want to put, save
7332 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 reg2 = get_register(regname, FALSE);
7334 put_register(regname, reg1);
7335 }
7336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007337 // When deleted a linewise Visual area, put the register as
7338 // lines to avoid it joined with the next line. When deletion was
7339 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (VIsual_mode == 'V')
7341 flags |= PUT_LINE;
7342 else if (VIsual_mode == 'v')
7343 flags |= PUT_LINE_SPLIT;
7344 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7345 flags |= PUT_LINE_FORWARD;
7346 dir = BACKWARD;
7347 if ((VIsual_mode != 'V'
7348 && curwin->w_cursor.col < curbuf->b_op_start.col)
7349 || (VIsual_mode == 'V'
7350 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007351 // cursor is at the end of the line or end of file, put
7352 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007355 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007357 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007359 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (reg2 != NULL)
7361 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 // What to reselect with "gv"? Selecting the just put text seems to
7364 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007365 if (was_visual)
7366 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007367 curbuf->b_visual.vi_start = curbuf->b_op_start;
7368 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007369 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007370 if (*p_sel == 'e')
7371 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007372 }
7373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007374 // When all lines were selected and deleted do_put() leaves an empty
7375 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007376 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007377 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007378 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007379 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007381 // If the cursor was in that line, move it to the end of the last
7382 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007383 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7384 {
7385 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7386 coladvance((colnr_T)MAXCOL);
7387 }
7388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 auto_format(FALSE, TRUE);
7390 }
7391}
7392
7393/*
7394 * "o" and "O" commands.
7395 */
7396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007397nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007400 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7402 {
7403 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007404 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 else
7407#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007408 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007410#ifdef FEAT_JOB_CHANNEL
7411 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007412 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 n_opencmd(cap);
7416}
7417
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418#ifdef FEAT_NETBEANS_INTG
7419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007420nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 netbeans_keycommand(cap->nchar);
7423}
7424#endif
7425
7426#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007428nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007430 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007433
Bram Moolenaar3918c952005-03-15 22:34:55 +00007434/*
7435 * Trigger CursorHold event.
7436 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7437 * input buffer. "did_cursorhold" is set to avoid retriggering.
7438 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007440nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007441{
7442 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7443 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007444 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007445}