blob: 63423d070b7e46744c31f305b0084410f206aa09 [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 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100434 MAY_WANT_TO_LOG_THIS;
435
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000436 // Disable bracketed paste and modifyOtherKeys here, we won't
437 // recognize the escape sequences with 'esckeys' off.
438 out_str(T_BD);
439 out_str(T_CTE);
440 }
441
442 *cp = plain_vgetc();
443
Bram Moolenaar24959102022-05-07 20:01:16 +0100444 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000445 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100446 MAY_WANT_TO_LOG_THIS;
447
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000448 // Re-enable bracketed paste mode and modifyOtherKeys
449 out_str(T_BE);
450 out_str(T_CTI);
451 }
452
453 if (langmap_active)
454 {
455 // Undo the decrement done above
456 ++no_mapping;
457 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100458 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000459 }
460#ifdef HAVE_INPUT_METHOD
461 if (lang)
462 {
463 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
464 im_save_status(&curbuf->b_p_iminsert);
465 im_set_active(FALSE);
466 }
467 p_smd = save_smd;
468#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100469 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000470#ifdef FEAT_CMDL_INFO
471 *need_flushbuf |= add_to_showcmd(*cp);
472#endif
473
474 if (!lit)
475 {
476#ifdef FEAT_DIGRAPHS
477 // Typing CTRL-K gets a digraph.
478 if (*cp == Ctrl_K
479 && ((nv_cmds[idx].cmd_flags & NV_LANG)
480 || cp == &cap->extra_char)
481 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
482 {
483 c = get_digraph(FALSE);
484 if (c > 0)
485 {
486 *cp = c;
487# ifdef FEAT_CMDL_INFO
488 // Guessing how to update showcmd here...
489 del_from_showcmd(3);
490 *need_flushbuf |= add_to_showcmd(*cp);
491# endif
492 }
493 }
494#endif
495
496 // adjust chars > 127, except after "tTfFr" commands
497 LANGMAP_ADJUST(*cp, !lang);
498#ifdef FEAT_RIGHTLEFT
499 // adjust Hebrew mapped char
500 if (p_hkmap && lang && KeyTyped)
501 *cp = hkmap(*cp);
502#endif
503 }
504
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000505 // When the next character is CTRL-\ a following CTRL-N means the
506 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000507 if (cp == &cap->extra_char
508 && cap->nchar == Ctrl_BSL
509 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
510 {
511 cap->cmdchar = Ctrl_BSL;
512 cap->nchar = cap->extra_char;
513 idx = find_command(cap->cmdchar);
514 }
515 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
516 cap->oap->op_type = get_op_type(*cp, NUL);
517 else if (*cp == Ctrl_BSL)
518 {
519 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
520
521 // There is a busy wait here when typing "f<C-\>" and then
522 // something different from CTRL-N. Can't be avoided.
523 while ((c = vpeekc()) <= 0 && towait > 0L)
524 {
525 do_sleep(towait > 50L ? 50L : towait, FALSE);
526 towait -= 50L;
527 }
528 if (c > 0)
529 {
530 c = plain_vgetc();
531 if (c != Ctrl_N && c != Ctrl_G)
532 vungetc(c);
533 else
534 {
535 cap->cmdchar = Ctrl_BSL;
536 cap->nchar = c;
537 idx = find_command(cap->cmdchar);
538 }
539 }
540 }
541
542 // When getting a text character and the next character is a
543 // multi-byte character, it could be a composing character.
544 // However, don't wait for it to arrive. Also, do enable mapping,
545 // because if it's put back with vungetc() it's too late to apply
546 // mapping.
547 --no_mapping;
548 while (enc_utf8 && lang && (c = vpeekc()) > 0
549 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
550 {
551 c = plain_vgetc();
552 if (!utf_iscomposing(c))
553 {
554 vungetc(c); // it wasn't, put it back
555 break;
556 }
557 else if (cap->ncharC1 == 0)
558 cap->ncharC1 = c;
559 else
560 cap->ncharC2 = c;
561 }
562 ++no_mapping;
563 }
564 --no_mapping;
565 --allow_keys;
566
567 return idx;
568}
569
570/*
571 * Returns TRUE if after processing a normal mode command, need to wait for a
572 * moment when a message is displayed that will be overwritten by the mode
573 * message.
574 */
575 static int
576normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
577{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000578 // In Visual mode and with "^O" in Insert mode, a short message will be
579 // overwritten by the mode message. Wait a bit, until a key is hit.
580 // In Visual mode, it's more important to keep the Visual area updated
581 // than keeping a message (e.g. from a /pat search).
582 // Only do this if the command was typed, not from a mapping.
583 // Don't wait when emsg_silent is non-zero.
584 // Also wait a bit after an error message, e.g. for "^O:".
585 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000586 return ( ((p_smd
587 && msg_silent == 0
588 && (restart_edit != 0
589 || (VIsual_active
590 && old_pos->lnum == curwin->w_cursor.lnum
591 && old_pos->col == curwin->w_cursor.col)
592 )
593 && (clear_cmdline
594 || redraw_cmdline)
595 && (msg_didout || (msg_didany && msg_scroll))
596 && !msg_nowait
597 && KeyTyped)
598 || (restart_edit != 0
599 && !VIsual_active
600 && (msg_scroll
601 || emsg_on_display)))
602 && cap->oap->regname == 0
603 && !(cap->retval & CA_COMMAND_BUSY)
604 && stuff_empty()
605 && typebuf_typed()
606 && emsg_silent == 0
607 && !in_assert_fails
608 && !did_wait_return
609 && cap->oap->op_type == OP_NOP);
610}
611
612/*
613 * After processing a normal mode command, wait for a moment when a message is
614 * displayed that will be overwritten by the mode message.
615 */
616 static void
617normal_cmd_wait_for_msg(void)
618{
619 int save_State = State;
620
621 // Draw the cursor with the right shape here
622 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100623 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000624
625 // If need to redraw, and there is a "keep_msg", redraw before the
626 // delay
627 if (must_redraw && keep_msg != NULL && !emsg_on_display)
628 {
629 char_u *kmsg;
630
631 kmsg = keep_msg;
632 keep_msg = NULL;
633 // Showmode() will clear keep_msg, but we want to use it anyway.
634 // First update w_topline.
635 setcursor();
636 update_screen(0);
637 // now reset it, otherwise it's put in the history again
638 keep_msg = kmsg;
639
640 kmsg = vim_strsave(keep_msg);
641 if (kmsg != NULL)
642 {
643 msg_attr((char *)kmsg, keep_msg_attr);
644 vim_free(kmsg);
645 }
646 }
647 setcursor();
648#ifdef CURSOR_SHAPE
649 ui_cursor_shape(); // may show different cursor shape
650#endif
651 cursor_on();
652 out_flush();
653 if (msg_scroll || emsg_on_display)
654 ui_delay(1003L, TRUE); // wait at least one second
655 ui_delay(3003L, FALSE); // wait up to three seconds
656 State = save_State;
657
658 msg_scroll = FALSE;
659 emsg_on_display = FALSE;
660}
661
662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 * Execute a command in Normal mode.
664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100666normal_cmd(
667 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000674 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100675 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 static int old_mapped_len = 0;
678 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000679 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200680 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
Bram Moolenaara80faa82020-04-12 19:37:17 +0200682 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000684
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 // Use a count remembered from before entering an operator. After typing
686 // "3d" we return from normal_cmd() and come back here, the "3" is
687 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ca.opcount = opcount;
689
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000690 // If there is an operator pending, then the command we take this time
691 // will terminate it. Finish_op tells us to finish the operation before
692 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef CURSOR_SHAPE
694 c = finish_op;
695#endif
696 finish_op = (oap->op_type != OP_NOP);
697#ifdef CURSOR_SHAPE
698 if (finish_op != c)
699 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100700 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701# ifdef FEAT_MOUSESHAPE
702 update_mouseshape(-1);
703# endif
704 }
705#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100706 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100708 // When not finishing an operator and no register name typed, reset the
709 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000713#ifdef FEAT_EVAL
714 set_prevcount = TRUE;
715#endif
716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 // Restore counts from before receiving K_CURSORHOLD. This means after
719 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
720 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000721 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
722 {
723 ca.opcount = oap->prev_opcount;
724 ca.count0 = oap->prev_count0;
725 oap->prev_opcount = 0;
726 oap->prev_count0 = 0;
727 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaar24959102022-05-07 20:01:16 +0100731 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100733 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
735
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100736#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 // Set v:count here, when called from main() and not a stuffed
738 // command, so that v:count can be used in an expression mapping
739 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100740 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100741 set_vcount_ca(&ca, &set_prevcount);
742#endif
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 /*
745 * Get the command character from the user.
746 */
747 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100748 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000750 // If a mapping was started in Visual or Select mode, remember the length
751 // of the mapping. This is used below to not return to Insert mode for as
752 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (restart_edit == 0)
754 old_mapped_len = 0;
755 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000756 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 old_mapped_len = typebuf_maplen();
758
759 if (c == NUL)
760 c = K_ZERO;
761
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000762 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (VIsual_active
764 && VIsual_select
765 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
766 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000767 int len;
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
770 // 'insertmode' is set) fake a "d"elete command, Insert mode will
771 // restart automatically.
772 // Insert the typed character in the typeahead buffer, so that it can
773 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000774 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
775
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000776 // When recording and gotchars() was called the character will be
777 // recorded again, remove the previous recording.
778 if (KeyTyped)
779 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000780
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000781 if (restart_edit != 0)
782 c = 'd';
783 else
784 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 msg_nowait = TRUE; // don't delay going to insert mode
786 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000789 // If the window was made so small that nothing shows, make it at least one
790 // line and one column when typing a command.
791 if (KeyTyped && !KeyStuffed)
792 win_ensure_size();
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794#ifdef FEAT_CMDL_INFO
795 need_flushbuf = add_to_showcmd(c);
796#endif
797
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000798 // Get the command count
799 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
800 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000802 // Find the command character in the table of commands.
803 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (ctrl_w)
805 {
806 ca.nchar = c;
807 ca.cmdchar = Ctrl_W;
808 }
809 else
810 ca.cmdchar = c;
811 idx = find_command(ca.cmdchar);
812 if (idx < 0)
813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100814 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 clearopbeep(oap);
816 goto normal_end;
817 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000818
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100819 if ((nv_cmds[idx].cmd_flags & NV_NCW)
820 && (check_text_locked(oap) || curbuf_locked()))
821 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000822 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000824 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (VIsual_active)
826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (km_stopsel
829 && (nv_cmds[idx].cmd_flags & NV_STS)
830 && !(mod_mask & MOD_MASK_SHIFT))
831 {
832 end_visual_mode();
833 redraw_curbuf_later(INVERTED);
834 }
835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (km_startsel)
838 {
839 if (nv_cmds[idx].cmd_flags & NV_SS)
840 {
841 unshift_special(&ca);
842 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000843 if (idx < 0)
844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000846 clearopbeep(oap);
847 goto normal_end;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
851 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856#ifdef FEAT_RIGHTLEFT
857 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
858 && (nv_cmds[idx].cmd_flags & NV_RL))
859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 // Invert horizontal movements and operations. Only when typed by the
861 // user directly, not when the result of a mapping or "x" translated
862 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 switch (ca.cmdchar)
864 {
865 case 'l': ca.cmdchar = 'h'; break;
866 case K_RIGHT: ca.cmdchar = K_LEFT; break;
867 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
868 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
869 case 'h': ca.cmdchar = 'l'; break;
870 case K_LEFT: ca.cmdchar = K_RIGHT; break;
871 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
872 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
873 case '>': ca.cmdchar = '<'; break;
874 case '<': ca.cmdchar = '>'; break;
875 }
876 idx = find_command(ca.cmdchar);
877 }
878#endif
879
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000880 // Get additional characters if we need them.
881 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
882 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000885 // Flush the showcmd characters onto the screen so we can see them while
886 // the command is being executed. Only do this when the shown command was
887 // actually displayed, otherwise this will slow down a lot when executing
888 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (need_flushbuf)
890 out_flush();
891#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000892 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200893 {
894 if (ex_normal_busy)
895 did_cursorhold = save_did_cursorhold;
896 else
897 did_cursorhold = FALSE;
898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 if (ca.nchar == ESC)
903 {
904 clearop(oap);
905 if (restart_edit == 0 && goto_im())
906 restart_edit = 'a';
907 goto normal_end;
908 }
909
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000910 if (ca.cmdchar != K_IGNORE)
911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000913 msg_col = 0;
914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When 'keymodel' contains "startsel" some keys start Select/Visual
919 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (!VIsual_active && km_startsel)
921 {
922 if (nv_cmds[idx].cmd_flags & NV_SS)
923 {
924 start_selection();
925 unshift_special(&ca);
926 idx = find_command(ca.cmdchar);
927 }
928 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
929 && (mod_mask & MOD_MASK_SHIFT))
930 {
931 start_selection();
932 mod_mask &= ~MOD_MASK_SHIFT;
933 }
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000936 // Execute the command!
937 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ca.arg = nv_cmds[idx].cmd_arg;
939 (nv_cmds[idx].cmd_func)(&ca);
940
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000941 // If we didn't start or finish an operator, reset oap->regname, unless we
942 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (!finish_op
944 && !oap->op_type
945 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
946 {
947 clearop(oap);
948#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200949 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951 }
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // Get the length of mapped chars again after typing a count, second
954 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000955 if (old_mapped_len > 0)
956 old_mapped_len = typebuf_maplen();
957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000958 // If an operation is pending, handle it. But not for K_IGNORE or
959 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200960 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100961 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000963 // Wait for a moment when a message is displayed that will be overwritten
964 // by the mode message.
965 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
966 normal_cmd_wait_for_msg();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969normal_end:
970
971 msg_nowait = FALSE;
972
Bram Moolenaarcc613032020-06-07 21:31:18 +0200973#ifdef FEAT_EVAL
974 if (finish_op)
975 reset_reg_var();
976#endif
977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100978 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef CURSOR_SHAPE
980 c = finish_op;
981#endif
982 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100983 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100985 // Redraw the cursor with another shape, if we were in Operator-pending
986 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (c || ca.cmdchar == 'r')
988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# ifdef FEAT_MOUSESHAPE
991 update_mouseshape(-1);
992# endif
993 }
994#endif
995
996#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000997 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100998 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 clear_showcmd();
1000#endif
1001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001002 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 vim_free(ca.searchbuf);
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (has_mbyte)
1006 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (curwin->w_p_scb && toplevel)
1009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001010 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 do_check_scrollbind(TRUE);
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar860cae12010-06-05 23:22:07 +02001014 if (curwin->w_p_crb && toplevel)
1015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001016 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017 do_check_cursorbind();
1018 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001019
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001020#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001021 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001022 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001023 restart_edit = 0;
1024#endif
1025
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001026 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1027 // if still inside a mapping that started in Visual mode).
1028 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1031 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && !(ca.retval & CA_COMMAND_BUSY)
1033 && stuff_empty()
1034 && oap->regname == 0)
1035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (restart_VIsual_select == 1)
1037 {
1038 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001039 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 showmode();
1041 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001042 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001044 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 (void)edit(restart_edit, FALSE, 1L);
1046 }
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (restart_VIsual_select == 2)
1049 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001051 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 opcount = ca.opcount;
1053}
1054
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001055#ifdef FEAT_EVAL
1056/*
1057 * Set v:count and v:count1 according to "cap".
1058 * Set v:prevcount only when "set_prevcount" is TRUE.
1059 */
1060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001062{
1063 long count = cap->count0;
1064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001065 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001066 if (cap->opcount != 0)
1067 count = cap->opcount * (count == 0 ? 1 : count);
1068 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001070}
1071#endif
1072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001074 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 * if not.
1076 */
1077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001078check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
1080 static int did_check = FALSE;
1081
1082 if (full_screen)
1083 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001084 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001085 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 did_check = TRUE;
1087 }
1088}
1089
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001090#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1091/*
1092 * Call yank_do_autocmd() for "regname".
1093 */
1094 static void
1095call_yank_do_autocmd(int regname)
1096{
1097 oparg_T oa;
1098 yankreg_T *reg;
1099
1100 clear_oparg(&oa);
1101 oa.regname = regname;
1102 oa.op_type = OP_YANK;
1103 oa.is_VIsual = TRUE;
1104 reg = get_register(regname, TRUE);
1105 yank_do_autocmd(&oa, reg);
1106 free_register(reg);
1107}
1108#endif
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001111 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001112 * This function or the next should ALWAYS be called to end Visual mode, except
1113 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
1115 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001116end_visual_mode()
1117{
1118 end_visual_mode_keep_button();
1119 reset_held_button();
1120}
1121
1122 void
1123end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001126 // If we are using the clipboard, then remember what was selected in case
1127 // we need to paste it somewhere while we still own the selection.
1128 // Only do this when the clipboard is already owned. Don't want to grab
1129 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (clip_star.available && clip_star.owned)
1131 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001132
1133# if defined(FEAT_EVAL)
1134 // Emit a TextYankPost for the automatic copy of the selection into the
1135 // star and/or plus register.
1136 if (has_textyankpost())
1137 {
1138 if (clip_isautosel_star())
1139 call_yank_do_autocmd('*');
1140 if (clip_isautosel_plus())
1141 call_yank_do_autocmd('+');
1142 }
1143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145
1146 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 setmouse();
1148 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001150 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001151 curbuf->b_visual.vi_mode = VIsual_mode;
1152 curbuf->b_visual.vi_start = VIsual;
1153 curbuf->b_visual.vi_end = curwin->w_cursor;
1154 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155#ifdef FEAT_EVAL
1156 curbuf->b_visual_mode_eval = VIsual_mode;
1157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (!virtual_active())
1159 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001160 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001162 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001163 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164}
1165
1166/*
1167 * Reset VIsual_active and VIsual_reselect.
1168 */
1169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001170reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 if (VIsual_active)
1173 {
1174 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001175 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177 VIsual_reselect = FALSE;
1178}
1179
1180/*
1181 * Reset VIsual_active and VIsual_reselect if it's set.
1182 */
1183 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001184reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 if (VIsual_active)
1187 {
1188 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 VIsual_reselect = FALSE;
1191 }
1192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001194 void
1195restore_visual_mode(void)
1196{
1197 if (VIsual_mode_orig != NUL)
1198 {
1199 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1200 VIsual_mode_orig = NUL;
1201 }
1202}
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204/*
1205 * Check for a balloon-eval special item to include when searching for an
1206 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1207 * Returns TRUE if the character at "*ptr" should be included.
1208 * "dir" is FORWARD or BACKWARD, the direction of searching.
1209 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1210 * "bnp" points to a counter for square brackets.
1211 */
1212 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001213find_is_eval_item(
1214 char_u *ptr,
1215 int *colp,
1216 int *bnp,
1217 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1221 ++*bnp;
1222 if (*bnp > 0)
1223 {
1224 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1225 --*bnp;
1226 return TRUE;
1227 }
1228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (*ptr == '.')
1231 return TRUE;
1232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001233 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1235 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1236 {
1237 *colp += dir;
1238 return TRUE;
1239 }
1240 return FALSE;
1241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243/*
1244 * Find the identifier under or to the right of the cursor.
1245 * "find_type" can have one of three values:
1246 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001247 * FIND_STRING: find any non-white text
1248 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001249 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 *
1251 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001254 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * This doesn't match the real Vi but I like it a little better and it
1256 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001257 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 * When FIND_IDENT isn't defined, we backup until a blank.
1259 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001260 * Returns the length of the text, or zero if no text is found.
1261 * If text is found, a pointer to the text is put in "*text". This
1262 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 */
1264 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001265find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001268 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269}
1270
1271/*
1272 * Like find_ident_under_cursor(), but for any window and any position.
1273 * However: Uses 'iskeyword' from the current window!.
1274 */
1275 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001276find_ident_at_pos(
1277 win_T *wp,
1278 linenr_T lnum,
1279 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001280 char_u **text,
1281 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001282 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001285 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 int this_class = 0;
1288 int prev_class;
1289 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001290 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001292 // if i == 0: try to find an identifier
1293 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1295 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1296 {
1297 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001298 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 */
1300 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (has_mbyte)
1302 {
1303 while (ptr[col] != NUL)
1304 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001305 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 this_class = mb_get_class(ptr + col);
1309 if (this_class != 0 && (i == 1 || this_class != 1))
1310 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001311 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
1314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001316 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 )
1319 ++col;
1320
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001321 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001325 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (has_mbyte)
1328 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1331 this_class = mb_get_class((char_u *)"a");
1332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001334 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1337 prev_class = mb_get_class(ptr + prevcol);
1338 if (this_class != prev_class
1339 && (i == 0
1340 || prev_class == 0
1341 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 && (!(find_type & FIND_EVAL)
1343 || prevcol == 0
1344 || !find_is_eval_item(ptr + prevcol, &prevcol,
1345 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 )
1347 break;
1348 col = prevcol;
1349 }
1350
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001351 // If we don't want just any old text, or we've found an
1352 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (this_class > 2)
1354 this_class = 2;
1355 if (!(find_type & FIND_STRING) || this_class == 2)
1356 break;
1357 }
1358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 while (col > 0
1361 && ((i == 0
1362 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001363 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 && (!(find_type & FIND_IDENT)
1365 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 || ((find_type & FIND_EVAL)
1367 && col > 1
1368 && find_is_eval_item(ptr + col - 1, &col,
1369 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ))
1371 --col;
1372
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001373 // If we don't want just any old text, or we've found an
1374 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1376 break;
1377 }
1378 }
1379
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001380 if (ptr[col] == NUL || (i == 0
1381 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001383 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001384 if ((find_type & FIND_NOERROR) == 0)
1385 {
1386 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001389 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 return 0;
1392 }
1393 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001394 *text = ptr;
1395 if (textcol != NULL)
1396 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
1398 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001399 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 bn = 0;
1402 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (has_mbyte)
1405 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001406 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 this_class = mb_get_class(ptr);
1408 while (ptr[col] != NUL
1409 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1410 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 || ((find_type & FIND_EVAL)
1412 && col <= (int)startcol
1413 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001415 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
1417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001419 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 || ((find_type & FIND_EVAL)
1421 && col <= (int)startcol
1422 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 return col;
1427}
1428
1429/*
1430 * Prepare for redo of a normal command.
1431 */
1432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001433prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 prep_redo(cap->oap->regname, cap->count0,
1436 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1437}
1438
1439/*
1440 * Prepare for redo of any command.
1441 * Note that only the last argument can be a multi-byte char.
1442 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001443 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001444prep_redo(
1445 int regname,
1446 long num,
1447 int cmd1,
1448 int cmd2,
1449 int cmd3,
1450 int cmd4,
1451 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001453 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1454}
1455
1456/*
1457 * Prepare for redo of any command with extra count after "cmd2".
1458 */
1459 void
1460prep_redo_num2(
1461 int regname,
1462 long num1,
1463 int cmd1,
1464 int cmd2,
1465 long num2,
1466 int cmd3,
1467 int cmd4,
1468 int cmd5)
1469{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 AppendCharToRedobuff('"');
1474 AppendCharToRedobuff(regname);
1475 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001476 if (num1 != 0)
1477 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (cmd1 != NUL)
1479 AppendCharToRedobuff(cmd1);
1480 if (cmd2 != NUL)
1481 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001482 if (num2 != 0)
1483 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (cmd3 != NUL)
1485 AppendCharToRedobuff(cmd3);
1486 if (cmd4 != NUL)
1487 AppendCharToRedobuff(cmd4);
1488 if (cmd5 != NUL)
1489 AppendCharToRedobuff(cmd5);
1490}
1491
1492/*
1493 * check for operator active and clear it
1494 *
1495 * return TRUE if operator was active
1496 */
1497 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001498checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 if (oap->op_type == OP_NOP)
1501 return FALSE;
1502 clearopbeep(oap);
1503 return TRUE;
1504}
1505
1506/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001507 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001509 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001514 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 return FALSE;
1516 clearopbeep(oap);
1517 return TRUE;
1518}
1519
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001521clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 oap->op_type = OP_NOP;
1524 oap->regname = 0;
1525 oap->motion_force = NUL;
1526 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001527 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528}
1529
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001531clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 clearop(oap);
1534 beep_flush();
1535}
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537/*
1538 * Remove the shift modifier from a special key.
1539 */
1540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001541unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 switch (cap->cmdchar)
1544 {
1545 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1546 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1547 case K_S_UP: cap->cmdchar = K_UP; break;
1548 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1549 case K_S_HOME: cap->cmdchar = K_HOME; break;
1550 case K_S_END: cap->cmdchar = K_END; break;
1551 }
1552 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001555/*
1556 * If the mode is currently displayed clear the command line or update the
1557 * command displayed.
1558 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001560may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001561{
1562 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001563 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001564#ifdef FEAT_CMDL_INFO
1565 else
1566 clear_showcmd();
1567#endif
1568}
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1571/*
1572 * Routines for displaying a partly typed command
1573 */
1574
kylo252ae6f1d82022-02-16 19:24:07 +00001575#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001577static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static int showcmd_is_clear = TRUE;
1579static int showcmd_visual = FALSE;
1580
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001581static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001584clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 if (!p_sc)
1587 return;
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (VIsual_active && !char_avail())
1590 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001591 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 long lines;
1593 colnr_T leftcol, rightcol;
1594 linenr_T top, bot;
1595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001597 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 top = VIsual.lnum;
1600 bot = curwin->w_cursor.lnum;
1601 }
1602 else
1603 {
1604 top = curwin->w_cursor.lnum;
1605 bot = VIsual.lnum;
1606 }
1607# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001609 (void)hasFolding(top, &top, NULL);
1610 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611# endif
1612 lines = bot - top + 1;
1613
1614 if (VIsual_mode == Ctrl_V)
1615 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001616# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001618 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001622 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001626 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001627 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1630 (long)(rightcol - leftcol + 1));
1631 }
1632 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1633 sprintf((char *)showcmd_buf, "%ld", lines);
1634 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001635 {
1636 char_u *s, *e;
1637 int l;
1638 int bytes = 0;
1639 int chars = 0;
1640
1641 if (cursor_bot)
1642 {
1643 s = ml_get_pos(&VIsual);
1644 e = ml_get_cursor();
1645 }
1646 else
1647 {
1648 s = ml_get_cursor();
1649 e = ml_get_pos(&VIsual);
1650 }
1651 while ((*p_sel != 'e') ? s <= e : s < e)
1652 {
1653 l = (*mb_ptr2len)(s);
1654 if (l == 0)
1655 {
1656 ++bytes;
1657 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001658 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001659 }
1660 bytes += l;
1661 ++chars;
1662 s += l;
1663 }
1664 if (bytes == chars)
1665 sprintf((char *)showcmd_buf, "%d", chars);
1666 else
1667 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 showcmd_visual = TRUE;
1671 }
1672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 showcmd_buf[0] = NUL;
1675 showcmd_visual = FALSE;
1676
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001677 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (showcmd_is_clear)
1679 return;
1680 }
1681
1682 display_showcmd();
1683}
1684
1685/*
1686 * Add 'c' to string of shown command chars.
1687 * Return TRUE if output has been written (and setcursor() has been called).
1688 */
1689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001690add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 char_u *p;
1693 int old_len;
1694 int extra_len;
1695 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int i;
1697 static int ignore[] =
1698 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1701 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001702#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001703 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001704 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1706 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001707 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001709 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 0
1711 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar09df3122006-01-23 22:23:09 +00001713 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return FALSE;
1715
1716 if (showcmd_visual)
1717 {
1718 showcmd_buf[0] = NUL;
1719 showcmd_visual = FALSE;
1720 }
1721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (IS_SPECIAL(c))
1724 for (i = 0; ignore[i] != 0; ++i)
1725 if (ignore[i] == c)
1726 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001729 if (*p == ' ')
1730 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 old_len = (int)STRLEN(showcmd_buf);
1732 extra_len = (int)STRLEN(p);
1733 overflow = old_len + extra_len - SHOWCMD_COLS;
1734 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001735 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1736 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 STRCAT(showcmd_buf, p);
1738
1739 if (char_avail())
1740 return FALSE;
1741
1742 display_showcmd();
1743
1744 return TRUE;
1745}
1746
1747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001748add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 if (!add_to_showcmd(c))
1751 setcursor();
1752}
1753
1754/*
1755 * Delete 'len' characters from the end of the shown command.
1756 */
1757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 int old_len;
1761
1762 if (!p_sc)
1763 return;
1764
1765 old_len = (int)STRLEN(showcmd_buf);
1766 if (len > old_len)
1767 len = old_len;
1768 showcmd_buf[old_len - len] = NUL;
1769
1770 if (!char_avail())
1771 display_showcmd();
1772}
1773
1774/*
1775 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1776 * something and there is a partial mapping.
1777 */
1778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 if (p_sc)
1782 STRCPY(old_showcmd_buf, showcmd_buf);
1783}
1784
1785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 if (!p_sc)
1789 return;
1790
1791 STRCPY(showcmd_buf, old_showcmd_buf);
1792
1793 display_showcmd();
1794}
1795
1796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 int len;
1800
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001801 if (p_ch == 0)
1802 return;
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 cursor_off();
1805
1806 len = (int)STRLEN(showcmd_buf);
1807 if (len == 0)
1808 showcmd_is_clear = TRUE;
1809 else
1810 {
1811 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1812 showcmd_is_clear = FALSE;
1813 }
1814
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001815 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1816 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
1824 * When "check" is FALSE, prepare for commands that scroll the window.
1825 * When "check" is TRUE, take care of scroll-binding after the window has
1826 * scrolled. Called from normal_cmd() and edit().
1827 */
1828 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001829do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 static win_T *old_curwin = NULL;
1832 static linenr_T old_topline = 0;
1833#ifdef FEAT_DIFF
1834 static int old_topfill = 0;
1835#endif
1836 static buf_T *old_buf = NULL;
1837 static colnr_T old_leftcol = 0;
1838
1839 if (check && curwin->w_p_scb)
1840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001841 // If a ":syncbind" command was just used, don't scroll, only reset
1842 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (did_syncbind)
1844 did_syncbind = FALSE;
1845 else if (curwin == old_curwin)
1846 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001847 // Synchronize other windows, as necessary according to
1848 // 'scrollbind'. Don't do this after an ":edit" command, except
1849 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if ((curwin->w_buffer == old_buf
1851#ifdef FEAT_DIFF
1852 || curwin->w_p_diff
1853#endif
1854 )
1855 && (curwin->w_topline != old_topline
1856#ifdef FEAT_DIFF
1857 || curwin->w_topfill != old_topfill
1858#endif
1859 || curwin->w_leftcol != old_leftcol))
1860 {
1861 check_scrollbind(curwin->w_topline - old_topline,
1862 (long)(curwin->w_leftcol - old_leftcol));
1863 }
1864 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001865 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001867 // When switching between windows, make sure that the relative
1868 // vertical offset is valid for the new window. The relative
1869 // offset is invalid whenever another 'scrollbind' window has
1870 // scrolled to a point that would force the current window to
1871 // scroll past the beginning or end of its buffer. When the
1872 // resync is performed, some of the other 'scrollbind' windows may
1873 // need to jump so that the current window's relative position is
1874 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1876 }
1877 curwin->w_scbind_pos = curwin->w_topline;
1878 }
1879
1880 old_curwin = curwin;
1881 old_topline = curwin->w_topline;
1882#ifdef FEAT_DIFF
1883 old_topfill = curwin->w_topfill;
1884#endif
1885 old_buf = curwin->w_buffer;
1886 old_leftcol = curwin->w_leftcol;
1887}
1888
1889/*
1890 * Synchronize any windows that have "scrollbind" set, based on the
1891 * number of rows by which the current window has changed
1892 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1893 */
1894 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001895check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 int want_ver;
1898 int want_hor;
1899 win_T *old_curwin = curwin;
1900 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 int old_VIsual_select = VIsual_select;
1902 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 colnr_T tgt_leftcol = curwin->w_leftcol;
1904 long topline;
1905 long y;
1906
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001907 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1909#ifdef FEAT_DIFF
1910 want_ver |= old_curwin->w_p_diff;
1911#endif
1912 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1913
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001914 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001916 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001919 // skip original window and windows with 'noscrollbind'
1920 if (curwin == old_curwin || !curwin->w_p_scb)
1921 continue;
1922
1923 // do the vertical scroll
1924 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001927 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
zeertzjq101d57b2022-07-31 18:34:32 +01001929 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
zeertzjq101d57b2022-07-31 18:34:32 +01001931 else
1932#endif
1933 {
1934 curwin->w_scbind_pos += topline_diff;
1935 topline = curwin->w_scbind_pos;
1936 if (topline > curbuf->b_ml.ml_line_count)
1937 topline = curbuf->b_ml.ml_line_count;
1938 if (topline < 1)
1939 topline = 1;
1940
1941 y = topline - curwin->w_topline;
1942 if (y > 0)
1943 scrollup(y, FALSE);
1944 else
1945 scrolldown(-y, FALSE);
1946 }
1947
1948 redraw_later(VALID);
1949 cursor_correct();
1950 curwin->w_redr_status = TRUE;
1951 }
1952
1953 // do the horizontal scroll
1954 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1955 {
1956 curwin->w_leftcol = tgt_leftcol;
1957 leftcol_changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001961 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 VIsual_select = old_VIsual_select;
1963 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 curwin = old_curwin;
1965 curbuf = old_curbuf;
1966}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968/*
1969 * Command character that's ignored.
1970 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001971 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001974nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977}
1978
1979/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001980 * Command character that doesn't do anything, but unlike nv_ignore() does
1981 * start edit(). Used for "startinsert" executed while starting up.
1982 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001984nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001985{
1986}
1987
1988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 * Command character doesn't exist.
1990 */
1991 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001992nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 clearopbeep(cap->oap);
1995}
1996
1997/*
1998 * <Help> and <F1> commands.
1999 */
2000 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002001nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 if (!checkclearopq(cap->oap))
2004 ex_help(NULL);
2005}
2006
2007/*
2008 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2009 */
2010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002013#ifdef FEAT_JOB_CHANNEL
2014 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2015 clearopbeep(cap->oap);
2016 else
2017#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002018 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002019 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002020 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002021 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2022 op_addsub(cap->oap, cap->count1, cap->arg);
2023 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002024 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002025 else if (VIsual_active)
2026 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002027 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002028 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029}
2030
2031/*
2032 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2033 */
2034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002035nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036{
2037 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002038 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 if (mod_mask & MOD_MASK_CTRL)
2040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002041 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002042 if (cap->arg == BACKWARD)
2043 goto_tabpage(-(int)cap->count1);
2044 else
2045 goto_tabpage((int)cap->count0);
2046 }
2047 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002048 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050}
2051
2052/*
2053 * Implementation of "gd" and "gD" command.
2054 */
2055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002056nv_gd(
2057 oparg_T *oap,
2058 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002059 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 char_u *ptr;
2063
Bram Moolenaard9d30582005-05-18 22:10:28 +00002064 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002065 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002066 == FAIL)
2067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002069 }
2070 else
2071 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002072#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002073 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2074 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002075#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002076 // clear any search statistics
2077 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2078 clear_cmdline = TRUE;
2079 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002080}
2081
2082/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002083 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2084 * otherwise.
2085 */
2086 static int
2087is_ident(char_u *line, int offset)
2088{
2089 int i;
2090 int incomment = FALSE;
2091 int instring = 0;
2092 int prev = 0;
2093
2094 for (i = 0; i < offset && line[i] != NUL; i++)
2095 {
2096 if (instring != 0)
2097 {
2098 if (prev != '\\' && line[i] == instring)
2099 instring = 0;
2100 }
2101 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2102 {
2103 instring = line[i];
2104 }
2105 else
2106 {
2107 if (incomment)
2108 {
2109 if (prev == '*' && line[i] == '/')
2110 incomment = FALSE;
2111 }
2112 else if (prev == '/' && line[i] == '*')
2113 {
2114 incomment = TRUE;
2115 }
2116 else if (prev == '/' && line[i] == '/')
2117 {
2118 return FALSE;
2119 }
2120 }
2121
2122 prev = line[i];
2123 }
2124
2125 return incomment == FALSE && instring == 0;
2126}
2127
2128/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002129 * Search for variable declaration of "ptr[len]".
2130 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2131 * current file ("gD").
2132 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002133 * Return FAIL when not found.
2134 */
2135 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136find_decl(
2137 char_u *ptr,
2138 int len,
2139 int locally,
2140 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002142{
2143 char_u *pat;
2144 pos_T old_pos;
2145 pos_T par_pos;
2146 pos_T found_pos;
2147 int t;
2148 int save_p_ws;
2149 int save_p_scs;
2150 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002151 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002152 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002153 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002154
2155 if ((pat = alloc(len + 7)) == NULL)
2156 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002158 // Put "\V" before the pattern to avoid that the special meaning of "."
2159 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002160 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2161 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 old_pos = curwin->w_cursor;
2163 save_p_ws = p_ws;
2164 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002165 p_ws = FALSE; // don't wrap around end of file now
2166 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002168 // With "gD" go to line 1.
2169 // With "gd" Search back for the start of the current function, then go
2170 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002171 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002173 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002175 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 else
2178 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002179 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2181 --curwin->w_cursor.lnum;
2182 }
2183 curwin->w_cursor.col = 0;
2184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002185 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002186 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187 for (;;)
2188 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002189 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002190 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002191 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002193
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002194 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195 {
2196 pos_T *pos;
2197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 // Check that the block the match is in doesn't end before the
2199 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002200 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2201 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2202 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002203 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 // There can't be a useful match before the end of this block.
2205 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002206 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002207 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002208 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002209 }
2210
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002211 if (t == FAIL)
2212 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002213 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002214 if (found_pos.lnum != 0)
2215 {
2216 curwin->w_cursor = found_pos;
2217 t = OK;
2218 }
2219 break;
2220 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002221 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002222 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002223 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002224 ++curwin->w_cursor.lnum;
2225 curwin->w_cursor.col = 0;
2226 continue;
2227 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002228 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002230 // If the current position is not a valid identifier and a previous
2231 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002232 if (!valid && found_pos.lnum != 0)
2233 {
2234 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002235 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002236 }
2237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002239 if (valid && !locally)
2240 break;
2241 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002242 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002243 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002244 if (found_pos.lnum != 0)
2245 curwin->w_cursor = found_pos;
2246 break;
2247 }
2248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002249 // For finding a local variable and the match is before the "{" or
2250 // inside a comment, continue searching. For K&R style function
2251 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002252 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002253 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002254 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002255 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002256 // Remove SEARCH_START from flags to avoid getting stuck at one
2257 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002258 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002260
2261 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002263 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 curwin->w_cursor = old_pos;
2265 }
2266 else
2267 {
2268 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 reset_search_dir();
2271 }
2272
2273 vim_free(pat);
2274 p_ws = save_p_ws;
2275 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002276
2277 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
2280/*
2281 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2282 * lines rather than lines in the file.
2283 * 'dist' must be positive.
2284 *
2285 * Return OK if able to move cursor, FAIL otherwise.
2286 */
2287 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002288nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 int linelen = linetabsize(ml_get_curline());
2291 int retval = OK;
2292 int atend = FALSE;
2293 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002294 int col_off1; // margin offset for first screen line
2295 int col_off2; // margin offset for wrapped screen line
2296 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002297 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002300 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302 col_off1 = curwin_col_off();
2303 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002304 width1 = curwin->w_width - col_off1;
2305 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002306 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002307 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002310 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002311 // Instead of sticking at the last character of the buffer line we
2312 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (curwin->w_curswant == MAXCOL)
2314 {
2315 atend = TRUE;
2316 validate_virtcol();
2317 if (width1 <= 0)
2318 curwin->w_curswant = 0;
2319 else
2320 {
2321 curwin->w_curswant = width1 - 1;
2322 if (curwin->w_virtcol > curwin->w_curswant)
2323 curwin->w_curswant += ((curwin->w_virtcol
2324 - curwin->w_curswant - 1) / width2 + 1) * width2;
2325 }
2326 }
2327 else
2328 {
2329 if (linelen > width1)
2330 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2331 else
2332 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002333 if (curwin->w_curswant >= (colnr_T)n)
2334 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336
2337 while (dist--)
2338 {
2339 if (dir == BACKWARD)
2340 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002341 if ((long)curwin->w_curswant >= width1
2342#ifdef FEAT_FOLDING
2343 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2344#endif
2345 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002346 // Move back within the line. This can give a negative value
2347 // for w_curswant if width1 < width2 (with cpoptions+=n),
2348 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 curwin->w_curswant -= width2;
2350 else
2351 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002352 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002354 // Move to the start of a closed fold. Don't do that when
2355 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (!(fdo_flags & FDO_ALL))
2357 (void)hasFolding(curwin->w_cursor.lnum,
2358 &curwin->w_cursor.lnum, NULL);
2359#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002360 if (curwin->w_cursor.lnum == 1)
2361 {
2362 retval = FAIL;
2363 break;
2364 }
2365 --curwin->w_cursor.lnum;
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 linelen = linetabsize(ml_get_curline());
2368 if (linelen > width1)
2369 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2370 + 1) * width2;
2371 }
2372 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002373 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375 if (linelen > width1)
2376 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2377 else
2378 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002379 if (curwin->w_curswant + width2 < (colnr_T)n
2380#ifdef FEAT_FOLDING
2381 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2382#endif
2383 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002384 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 curwin->w_curswant += width2;
2386 else
2387 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002390 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2392 &curwin->w_cursor.lnum);
2393#endif
2394 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2395 {
2396 retval = FAIL;
2397 break;
2398 }
2399 curwin->w_cursor.lnum++;
2400 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002401 // Check if the cursor has moved below the number display
2402 // when width1 < width2 (with cpoptions+=n). Subtract width2
2403 // to get a negative value for w_curswant, which will get
2404 // clipped to column 0.
2405 if (curwin->w_curswant >= width1)
2406 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002407 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 }
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002413 if (virtual_active() && atend)
2414 coladvance(MAXCOL);
2415 else
2416 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2419 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002420 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002421 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002422
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002423 // Check for landing on a character that got split at the end of the
2424 // last line. We want to advance a screenline, not end up in the same
2425 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002427 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002428#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002429 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2430 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002431#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002432
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002433 c = (*mb_ptr2char)(ml_get_cursor());
2434 if (dir == FORWARD && virtcol < curwin->w_curswant
2435 && (curwin->w_curswant <= (colnr_T)width1)
2436 && !vim_isprintc(c) && c > 255)
2437 oneright();
2438
Bram Moolenaar773b1582014-08-29 14:20:51 +02002439 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 && (curwin->w_curswant < (colnr_T)width1
2441 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2442 : ((curwin->w_curswant - width1) % width2
2443 > (colnr_T)width2 / 2)))
2444 --curwin->w_cursor.col;
2445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002448 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 return retval;
2451}
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453/*
2454 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2455 * cap->arg must be TRUE for CTRL-E.
2456 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002458nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460 if (!checkclearop(cap->oap))
2461 scroll_redraw(cap->arg, cap->count1);
2462}
2463
2464/*
2465 * Scroll "count" lines up or down, and redraw.
2466 */
2467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002468scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 linenr_T prev_topline = curwin->w_topline;
2471#ifdef FEAT_DIFF
2472 int prev_topfill = curwin->w_topfill;
2473#endif
2474 linenr_T prev_lnum = curwin->w_cursor.lnum;
2475
2476 if (up)
2477 scrollup(count, TRUE);
2478 else
2479 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002480 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002482 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2483 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 cursor_correct();
2485 check_cursor_moved(curwin);
2486 curwin->w_valid |= VALID_TOPLINE;
2487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002488 // If moved back to where we were, at least move the cursor, otherwise
2489 // we get stuck at one position. Don't move the cursor up if the
2490 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 while (curwin->w_topline == prev_topline
2492#ifdef FEAT_DIFF
2493 && curwin->w_topfill == prev_topfill
2494#endif
2495 )
2496 {
2497 if (up)
2498 {
2499 if (curwin->w_cursor.lnum > prev_lnum
2500 || cursor_down(1L, FALSE) == FAIL)
2501 break;
2502 }
2503 else
2504 {
2505 if (curwin->w_cursor.lnum < prev_lnum
2506 || prev_topline == 1L
2507 || cursor_up(1L, FALSE) == FAIL)
2508 break;
2509 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002510 // Mark w_topline as valid, otherwise the screen jumps back at the
2511 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 check_cursor_moved(curwin);
2513 curwin->w_valid |= VALID_TOPLINE;
2514 }
2515 }
2516 if (curwin->w_cursor.lnum != prev_lnum)
2517 coladvance(curwin->w_curswant);
2518 redraw_later(VALID);
2519}
2520
2521/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002522 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2523 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2524 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002525 */
2526 static int
2527nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2528{
2529 int nchar = *nchar_arg;
2530 long n;
2531
2532 // "z123{nchar}": edit the count before obtaining {nchar}
2533 if (checkclearop(cap->oap))
2534 return FALSE;
2535 n = nchar - '0';
2536
2537 for (;;)
2538 {
2539#ifdef USE_ON_FLY_SCROLL
2540 dont_scroll = TRUE; // disallow scrolling here
2541#endif
2542 ++no_mapping;
2543 ++allow_keys; // no mapping for nchar, but allow key codes
2544 nchar = plain_vgetc();
2545 LANGMAP_ADJUST(nchar, TRUE);
2546 --no_mapping;
2547 --allow_keys;
2548#ifdef FEAT_CMDL_INFO
2549 (void)add_to_showcmd(nchar);
2550#endif
2551 if (nchar == K_DEL || nchar == K_KDEL)
2552 n /= 10;
2553 else if (VIM_ISDIGIT(nchar))
2554 n = n * 10 + (nchar - '0');
2555 else if (nchar == CAR)
2556 {
2557#ifdef FEAT_GUI
2558 need_mouse_correct = TRUE;
2559#endif
2560 win_setheight((int)n);
2561 break;
2562 }
2563 else if (nchar == 'l'
2564 || nchar == 'h'
2565 || nchar == K_LEFT
2566 || nchar == K_RIGHT)
2567 {
2568 cap->count1 = n ? n * cap->count1 : cap->count1;
2569 *nchar_arg = nchar;
2570 return TRUE;
2571 }
2572 else
2573 {
2574 clearopbeep(cap->oap);
2575 break;
2576 }
2577 }
2578 cap->oap->op_type = OP_NOP;
2579 return FALSE;
2580}
2581
2582#ifdef FEAT_SPELL
2583/*
2584 * "zug" and "zuw": undo "zg" and "zw"
2585 * "zg": add good word to word list
2586 * "zw": add wrong word to word list
2587 * "zG": add good word to temp word list
2588 * "zW": add wrong word to temp word list
2589 */
2590 static int
2591nv_zg_zw(cmdarg_T *cap, int nchar)
2592{
2593 char_u *ptr = NULL;
2594 int len;
2595 int undo = FALSE;
2596
2597 if (nchar == 'u')
2598 {
2599 ++no_mapping;
2600 ++allow_keys; // no mapping for nchar, but allow key codes
2601 nchar = plain_vgetc();
2602 LANGMAP_ADJUST(nchar, TRUE);
2603 --no_mapping;
2604 --allow_keys;
2605#ifdef FEAT_CMDL_INFO
2606 (void)add_to_showcmd(nchar);
2607#endif
2608 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2609 {
2610 clearopbeep(cap->oap);
2611 return OK;
2612 }
2613 undo = TRUE;
2614 }
2615
2616 if (checkclearop(cap->oap))
2617 return OK;
2618 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2619 return FAIL;
2620 if (ptr == NULL)
2621 {
2622 pos_T pos = curwin->w_cursor;
2623
2624 // Find bad word under the cursor. When 'spell' is
2625 // off this fails and find_ident_under_cursor() is
2626 // used below.
2627 emsg_off++;
2628 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2629 emsg_off--;
2630 if (len != 0 && curwin->w_cursor.col <= pos.col)
2631 ptr = ml_get_pos(&curwin->w_cursor);
2632 curwin->w_cursor = pos;
2633 }
2634
2635 if (ptr == NULL
2636 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2637 return FAIL;
2638 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2639 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2640 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2641
2642 return OK;
2643}
2644#endif
2645
2646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * Commands that start with "z".
2648 */
2649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002650nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 long n;
2653 colnr_T col;
2654 int nchar = cap->nchar;
2655#ifdef FEAT_FOLDING
2656 long old_fdl = curwin->w_p_fdl;
2657 int old_fen = curwin->w_p_fen;
2658#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002659 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002661 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (
2665#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2667 // and "zC" only in Visual mode. "zj" and "zk" are motion
2668 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 cap->nchar != 'f' && cap->nchar != 'F'
2670 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2671 && cap->nchar != 'j' && cap->nchar != 'k'
2672 &&
2673#endif
2674 checkclearop(cap->oap))
2675 return;
2676
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002677 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2678 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2680 && cap->count0
2681 && cap->count0 != curwin->w_cursor.lnum)
2682 {
2683 setpcmark();
2684 if (cap->count0 > curbuf->b_ml.ml_line_count)
2685 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2686 else
2687 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002688 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690
2691 switch (nchar)
2692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 case '+':
2695 if (cap->count0 == 0)
2696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // No count given: put cursor at the line below screen
2698 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2700 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2701 else
2702 curwin->w_cursor.lnum = curwin->w_botline;
2703 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 case NL:
2706 case CAR:
2707 case K_KENTER:
2708 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 case 't': scroll_cursor_top(0, TRUE);
2712 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002713 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 case 'z': scroll_cursor_halfway(TRUE);
2721 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002722 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 break;
2724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // "z^", "z-" and "zb": put cursor at bottom of screen
2726 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2727 // when <count> is at bottom of window, and puts that one at
2728 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (cap->count0 != 0)
2730 {
2731 scroll_cursor_bot(0, TRUE);
2732 curwin->w_cursor.lnum = curwin->w_topline;
2733 }
2734 else if (curwin->w_topline == 1)
2735 curwin->w_cursor.lnum = 1;
2736 else
2737 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 case '-':
2740 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 case 'b': scroll_cursor_bot(0, TRUE);
2744 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002745 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 break;
2747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002750 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 case 'h':
2755 case K_LEFT:
2756 if (!curwin->w_p_wrap)
2757 {
2758 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2759 curwin->w_leftcol = 0;
2760 else
2761 curwin->w_leftcol -= (colnr_T)cap->count1;
2762 leftcol_changed();
2763 }
2764 break;
2765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002767 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 case 'l':
2772 case K_RIGHT:
2773 if (!curwin->w_p_wrap)
2774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 curwin->w_leftcol += (colnr_T)cap->count1;
2777 leftcol_changed();
2778 }
2779 break;
2780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 case 's': if (!curwin->w_p_wrap)
2783 {
2784#ifdef FEAT_FOLDING
2785 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 else
2788#endif
2789 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002790 if ((long)col > siso)
2791 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 else
2793 col = 0;
2794 if (curwin->w_leftcol != col)
2795 {
2796 curwin->w_leftcol = col;
2797 redraw_later(NOT_VALID);
2798 }
2799 }
2800 break;
2801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 case 'e': if (!curwin->w_p_wrap)
2804 {
2805#ifdef FEAT_FOLDING
2806 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002807 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 else
2809#endif
2810 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002811 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002812 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 col = 0;
2814 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002815 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (curwin->w_leftcol != col)
2817 {
2818 curwin->w_leftcol = col;
2819 redraw_later(NOT_VALID);
2820 }
2821 }
2822 break;
2823
Christian Brabandt2fa93842021-05-30 22:17:25 +02002824 // "zp", "zP" in block mode put without addind trailing spaces
2825 case 'P':
2826 case 'p': nv_put(cap);
2827 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002828 // "zy" Yank without trailing spaces
2829 case 'y': nv_operator(cap);
2830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // "zF": create fold command
2833 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case 'F':
2835 case 'f': if (foldManualAllowed(TRUE))
2836 {
2837 cap->nchar = 'f';
2838 nv_operator(cap);
2839 curwin->w_p_fen = TRUE;
2840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2843 {
2844 nv_operator(cap);
2845 finish_op = TRUE;
2846 }
2847 }
2848 else
2849 clearopbeep(cap->oap);
2850 break;
2851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // "zd": delete fold at cursor
2853 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case 'd':
2855 case 'D': if (foldManualAllowed(FALSE))
2856 {
2857 if (VIsual_active)
2858 nv_operator(cap);
2859 else
2860 deleteFold(curwin->w_cursor.lnum,
2861 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2862 }
2863 break;
2864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 case 'E': if (foldmethodIsManual(curwin))
2867 {
2868 clearFolding(curwin);
2869 changed_window_setting();
2870 }
2871 else if (foldmethodIsMarker(curwin))
2872 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2873 TRUE, FALSE);
2874 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002875 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case 'n': curwin->w_p_fen = FALSE;
2880 break;
2881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 case 'N': curwin->w_p_fen = TRUE;
2884 break;
2885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2888 break;
2889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2892 openFold(curwin->w_cursor.lnum, cap->count1);
2893 else
2894 {
2895 closeFold(curwin->w_cursor.lnum, cap->count1);
2896 curwin->w_p_fen = TRUE;
2897 }
2898 break;
2899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2902 openFoldRecurse(curwin->w_cursor.lnum);
2903 else
2904 {
2905 closeFoldRecurse(curwin->w_cursor.lnum);
2906 curwin->w_p_fen = TRUE;
2907 }
2908 break;
2909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case 'o': if (VIsual_active)
2912 nv_operator(cap);
2913 else
2914 openFold(curwin->w_cursor.lnum, cap->count1);
2915 break;
2916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 case 'O': if (VIsual_active)
2919 nv_operator(cap);
2920 else
2921 openFoldRecurse(curwin->w_cursor.lnum);
2922 break;
2923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 case 'c': if (VIsual_active)
2926 nv_operator(cap);
2927 else
2928 closeFold(curwin->w_cursor.lnum, cap->count1);
2929 curwin->w_p_fen = TRUE;
2930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case 'C': if (VIsual_active)
2934 nv_operator(cap);
2935 else
2936 closeFoldRecurse(curwin->w_cursor.lnum);
2937 curwin->w_p_fen = TRUE;
2938 break;
2939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 case 'v': foldOpenCursor();
2942 break;
2943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 curwin->w_foldinvalid = TRUE; // recompute folds
2947 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 foldOpenCursor();
2949 break;
2950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 curwin->w_foldinvalid = TRUE; // recompute folds
2954 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 break;
2956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002959 {
2960 curwin->w_p_fdl -= cap->count1;
2961 if (curwin->w_p_fdl < 0)
2962 curwin->w_p_fdl = 0;
2963 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 curwin->w_p_fen = TRUE;
2966 break;
2967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 curwin->w_p_fen = TRUE;
2972 break;
2973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002975 case 'r': curwin->w_p_fdl += cap->count1;
2976 {
2977 int d = getDeepestNesting();
2978
2979 if (curwin->w_p_fdl >= d)
2980 curwin->w_p_fdl = d;
2981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 break;
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 break;
2988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 case 'j': // "zj" move to next fold downwards
2990 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2992 cap->count1) == FAIL)
2993 clearopbeep(cap->oap);
2994 break;
2995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002998#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 case 'g': // "zg": add good word to word list
3001 case 'w': // "zw": add wrong word to word list
3002 case 'G': // "zG": add good word to temp word list
3003 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003004 if (nv_zg_zw(cap, nchar) == FAIL)
3005 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003006 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003009 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003010 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003012#endif
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 default: clearopbeep(cap->oap);
3015 }
3016
3017#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (old_fen != curwin->w_p_fen)
3020 {
3021# ifdef FEAT_DIFF
3022 win_T *wp;
3023
3024 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003026 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 FOR_ALL_WINDOWS(wp)
3028 {
3029 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3030 {
3031 wp->w_p_fen = curwin->w_p_fen;
3032 changed_window_setting_win(wp);
3033 }
3034 }
3035 }
3036# endif
3037 changed_window_setting();
3038 }
3039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (old_fdl != curwin->w_p_fdl)
3042 newFoldLevel();
3043#endif
3044}
3045
3046#ifdef FEAT_GUI
3047/*
3048 * Vertical scrollbar movement.
3049 */
3050 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 if (cap->oap->op_type != OP_NOP)
3054 clearopbeep(cap->oap);
3055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 gui_do_scroll();
3058}
3059
3060/*
3061 * Horizontal scrollbar movement.
3062 */
3063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003064nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 if (cap->oap->op_type != OP_NOP)
3067 clearopbeep(cap->oap);
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003070 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072#endif
3073
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003074#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003075/*
3076 * Click in GUI tab.
3077 */
3078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003079nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003080{
3081 if (cap->oap->op_type != OP_NOP)
3082 clearopbeep(cap->oap);
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003085 goto_tabpage(current_tab);
3086}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003087
3088/*
3089 * Selected item in tab line menu.
3090 */
3091 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003092nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003093{
3094 if (cap->oap->op_type != OP_NOP)
3095 clearopbeep(cap->oap);
3096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003098 handle_tabmenu();
3099}
3100
3101/*
3102 * Handle selecting an item of the GUI tab line menu.
3103 * Used in Normal and Insert mode.
3104 */
3105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003106handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003107{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003108 switch (current_tabmenu)
3109 {
3110 case TABLINE_MENU_CLOSE:
3111 if (current_tab == 0)
3112 do_cmdline_cmd((char_u *)"tabclose");
3113 else
3114 {
3115 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3116 current_tab);
3117 do_cmdline_cmd(IObuff);
3118 }
3119 break;
3120
3121 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003122 if (current_tab == 0)
3123 do_cmdline_cmd((char_u *)"$tabnew");
3124 else
3125 {
3126 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3127 current_tab - 1);
3128 do_cmdline_cmd(IObuff);
3129 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003130 break;
3131
3132 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003133 if (current_tab == 0)
3134 do_cmdline_cmd((char_u *)"browse $tabnew");
3135 else
3136 {
3137 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3138 current_tab - 1);
3139 do_cmdline_cmd(IObuff);
3140 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003141 break;
3142 }
3143}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003144#endif
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146/*
3147 * "Q" command.
3148 */
3149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003150nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003152 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003154 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003155 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 do_exmode(FALSE);
3157}
3158
3159/*
3160 * Handle a ":" command.
3161 */
3162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003163nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003165 int old_p_im;
3166 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003167 int is_cmdkey = cap->cmdchar == K_COMMAND
3168 || cap->cmdchar == K_SCRIPT_COMMAND;
3169 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaar957cf672020-11-12 14:21:06 +01003171 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 nv_operator(cap);
3173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
3175 if (cap->oap->op_type != OP_NOP)
3176 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 cap->oap->motion_type = MCHAR;
3179 cap->oap->inclusive = FALSE;
3180 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003181 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003183 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 stuffcharReadbuff('.');
3185 if (cap->count0 > 1)
3186 {
3187 stuffReadbuff((char_u *)",.+");
3188 stuffnumReadbuff((long)cap->count0 - 1L);
3189 }
3190 }
3191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (KeyTyped)
3194 compute_cmdrow();
3195
3196 old_p_im = p_im;
3197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003198 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003199 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3200 if (is_cmdkey)
3201 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3202 else
3203 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (p_im != old_p_im)
3207 {
3208 if (p_im)
3209 restart_edit = 'i';
3210 else
3211 restart_edit = 0;
3212 }
3213
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003214 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003216 clearop(cap->oap);
3217 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3219 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003220 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3221 || did_emsg
3222 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003223 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 clearopbeep(cap->oap);
3225 }
3226}
3227
3228/*
3229 * Handle CTRL-G command.
3230 */
3231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003232nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003237 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 showmode();
3239 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003240 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003241 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 fileinfo((int)cap->count0, FALSE, TRUE);
3243}
3244
3245/*
3246 * Handle CTRL-H <Backspace> command.
3247 */
3248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003249nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (VIsual_active && VIsual_select)
3252 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003253 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 v_visop(cap);
3255 }
3256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 nv_left(cap);
3258}
3259
3260/*
3261 * CTRL-L: clear screen and redraw.
3262 */
3263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003264nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 if (!checkclearop(cap->oap))
3267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003269 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003270 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003271# ifdef FEAT_RELTIME
3272 {
3273 win_T *wp;
3274
3275 FOR_ALL_WINDOWS(wp)
3276 wp->w_s->b_syn_slow = FALSE;
3277 }
3278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003281#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3282# ifdef VIMDLL
3283 if (!gui.in_use)
3284# endif
3285 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288}
3289
3290/*
3291 * CTRL-O: In Select mode: switch to Visual mode for one command.
3292 * Otherwise: Go to older pcmark.
3293 */
3294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003295nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (VIsual_active && VIsual_select)
3298 {
3299 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003300 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 cap->count1 = -cap->count1;
3307 nv_pcmark(cap);
3308 }
3309}
3310
3311/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003312 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3313 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
3315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003316nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if (!checkclearopq(cap->oap))
3319 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3320 GETF_SETMARK|GETF_ALT, FALSE);
3321}
3322
3323/*
3324 * "Z" commands.
3325 */
3326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 if (!checkclearopq(cap->oap))
3330 {
3331 switch (cap->nchar)
3332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003333 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 case 'Z': do_cmdline_cmd((char_u *)"x");
3335 break;
3336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003337 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 case 'Q': do_cmdline_cmd((char_u *)"q!");
3339 break;
3340
3341 default: clearopbeep(cap->oap);
3342 }
3343 }
3344}
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346/*
3347 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3348 */
3349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003350do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 oparg_T oa;
3353 cmdarg_T ca;
3354
3355 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003356 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 ca.oap = &oa;
3358 ca.cmdchar = c1;
3359 ca.nchar = c2;
3360 nv_ident(&ca);
3361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003364 * 'K' normal-mode command. Get the command to lookup the keyword under the
3365 * cursor.
3366 */
3367 static int
3368nv_K_getcmd(
3369 cmdarg_T *cap,
3370 char_u *kp,
3371 int kp_help,
3372 int kp_ex,
3373 char_u **ptr_arg,
3374 int n,
3375 char_u *buf,
3376 unsigned buflen)
3377{
3378 char_u *ptr = *ptr_arg;
3379 int isman;
3380 int isman_s;
3381
3382 if (kp_help)
3383 {
3384 // in the help buffer
3385 STRCPY(buf, "he! ");
3386 return n;
3387 }
3388
3389 if (kp_ex)
3390 {
3391 // 'keywordprog' is an ex command
3392 if (cap->count0 != 0)
3393 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3394 else
3395 STRCPY(buf, kp);
3396 STRCAT(buf, " ");
3397 return n;
3398 }
3399
3400 // An external command will probably use an argument starting
3401 // with "-" as an option. To avoid trouble we skip the "-".
3402 while (*ptr == '-' && n > 0)
3403 {
3404 ++ptr;
3405 --n;
3406 }
3407 if (n == 0)
3408 {
3409 // found dashes only
3410 emsg(_(e_no_identifier_under_cursor));
3411 vim_free(buf);
3412 *ptr_arg = ptr;
3413 return 0;
3414 }
3415
3416 // When a count is given, turn it into a range. Is this
3417 // really what we want?
3418 isman = (STRCMP(kp, "man") == 0);
3419 isman_s = (STRCMP(kp, "man -s") == 0);
3420 if (cap->count0 != 0 && !(isman || isman_s))
3421 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3422
3423 STRCAT(buf, "! ");
3424 if (cap->count0 == 0 && isman_s)
3425 STRCAT(buf, "man");
3426 else
3427 STRCAT(buf, kp);
3428 STRCAT(buf, " ");
3429 if (cap->count0 != 0 && (isman || isman_s))
3430 {
3431 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3432 STRCAT(buf, " ");
3433 }
3434
3435 *ptr_arg = ptr;
3436 return n;
3437}
3438
3439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * Handle the commands that use the word under the cursor.
3441 * [g] CTRL-] :ta to current identifier
3442 * [g] 'K' run program for current identifier
3443 * [g] '*' / to current identifier or string
3444 * [g] '#' ? to current identifier or string
3445 * g ']' :tselect for current identifier
3446 */
3447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003448nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *ptr = NULL;
3451 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003452 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003453 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003455 char_u *kp; // value of 'keywordprg'
3456 int kp_help; // 'keywordprg' is ":he"
3457 int kp_ex; // 'keywordprg' starts with ":"
3458 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003461 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 cmdchar = cap->nchar;
3467 g_cmd = TRUE;
3468 }
3469 else
3470 {
3471 cmdchar = cap->cmdchar;
3472 g_cmd = FALSE;
3473 }
3474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 cmdchar = '#';
3477
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003478 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3482 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (checkclearopq(cap->oap))
3484 return;
3485 }
3486
3487 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3488 (cmdchar == '*' || cmdchar == '#')
3489 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3490 {
3491 clearop(cap->oap);
3492 return;
3493 }
3494
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003495 // Allocate buffer to put the command in. Inserting backslashes can
3496 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3497 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3499 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3500 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003501 if (kp_help && *skipwhite(ptr) == NUL)
3502 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003503 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003504 return;
3505 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003506 kp_ex = (*kp == ':');
3507 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3508 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (buf == NULL)
3510 return;
3511 buf[0] = NUL;
3512
3513 switch (cmdchar)
3514 {
3515 case '*':
3516 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003517 // Put cursor at start of word, makes search skip the word
3518 // under the cursor.
3519 // Call setpcmark() first, so "*``" puts the cursor back where
3520 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 setpcmark();
3522 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3523
3524 if (!g_cmd && vim_iswordp(ptr))
3525 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003526 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 break;
3528
3529 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003530 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3531 if (n == 0)
3532 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 break;
3534
3535 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003536 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537#ifdef FEAT_CSCOPE
3538 if (p_cst)
3539 STRCPY(buf, "cstag ");
3540 else
3541#endif
3542 STRCPY(buf, "ts ");
3543 break;
3544
3545 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003546 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (curbuf->b_help)
3548 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003550 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003551 if (g_cmd)
3552 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003553 else if (cap->count0 == 0)
3554 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003555 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003556 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003560 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003563 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003564 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003566 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003567 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003569 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003570 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003573 vim_free(buf);
3574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003576 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003577 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 {
3579 vim_free(buf);
3580 vim_free(p);
3581 return;
3582 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003583 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003584 STRCAT(buf, p);
3585 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003587 else
3588 {
3589 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003590 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003591 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003592 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003593 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003594 {
3595 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003596 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003597 aux_ptr = (char_u *)"";
3598 else
3599 aux_ptr = (char_u *)"\\|\"\n[";
3600 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003601 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003602 aux_ptr = (char_u *)"\\|\"\n*?[";
3603
3604 p = buf + STRLEN(buf);
3605 while (n-- > 0)
3606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003607 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608 if (vim_strchr(aux_ptr, *ptr) != NULL)
3609 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003610 // When current byte is a part of multibyte character, copy all
3611 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003612 if (has_mbyte)
3613 {
3614 int i;
3615 int len = (*mb_ptr2len)(ptr) - 1;
3616
3617 for (i = 0; i < len && n >= 1; ++i, --n)
3618 *p++ = *ptr++;
3619 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003620 *p++ = *ptr++;
3621 }
3622 *p = NUL;
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003625 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (cmdchar == '*' || cmdchar == '#')
3627 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003628 if (!g_cmd && (has_mbyte
3629 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3630 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003632
3633 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003634 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003636
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003637 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003640 {
3641 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003643 g_tag_at_cursor = FALSE;
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 vim_free(buf);
3647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649/*
3650 * Get visually selected text, within one line only.
3651 * Returns FAIL if more than one line selected.
3652 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003654get_visual_text(
3655 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 char_u **pp, // return: start of selected text
3657 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 if (VIsual_mode != 'V')
3660 unadjust_for_sel();
3661 if (VIsual.lnum != curwin->w_cursor.lnum)
3662 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003663 if (cap != NULL)
3664 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return FAIL;
3666 }
3667 if (VIsual_mode == 'V')
3668 {
3669 *pp = ml_get_curline();
3670 *lenp = (int)STRLEN(*pp);
3671 }
3672 else
3673 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003674 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 *pp = ml_get_pos(&curwin->w_cursor);
3677 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3678 }
3679 else
3680 {
3681 *pp = ml_get_pos(&VIsual);
3682 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3683 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003684 if (**pp == NUL)
3685 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003686 if (*lenp > 0)
3687 {
3688 if (has_mbyte)
3689 // Correct the length to include all bytes of the last
3690 // character.
3691 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3692 else if ((*pp)[*lenp - 1] == NUL)
3693 // Do not include a trailing NUL.
3694 *lenp -= 1;
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 reset_VIsual_and_resel();
3698 return OK;
3699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701/*
3702 * CTRL-T: backwards in tag stack
3703 */
3704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003705nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 if (!checkclearopq(cap->oap))
3708 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3709}
3710
3711/*
3712 * Handle scrolling command 'H', 'L' and 'M'.
3713 */
3714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003715nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 int used = 0;
3718 long n;
3719#ifdef FEAT_FOLDING
3720 linenr_T lnum;
3721#endif
3722 int half;
3723
3724 cap->oap->motion_type = MLINE;
3725 setpcmark();
3726
3727 if (cap->cmdchar == 'L')
3728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 curwin->w_cursor.lnum = curwin->w_botline - 1;
3731 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3732 curwin->w_cursor.lnum = 1;
3733 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003734 {
3735#ifdef FEAT_FOLDING
3736 if (hasAnyFolding(curwin))
3737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003739 for (n = cap->count1 - 1; n > 0
3740 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3741 {
3742 (void)hasFolding(curwin->w_cursor.lnum,
3743 &curwin->w_cursor.lnum, NULL);
3744 --curwin->w_cursor.lnum;
3745 }
3746 }
3747 else
3748#endif
3749 curwin->w_cursor.lnum -= cap->count1 - 1;
3750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 else
3753 {
3754 if (cap->cmdchar == 'M')
3755 {
3756#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003757 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 used -= diff_check_fill(curwin, curwin->w_topline)
3759 - curwin->w_topfill;
3760#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003761 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3763 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3764 {
3765#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 // Count half he number of filler lines to be "below this
3767 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3769 + n) / 2 >= half)
3770 {
3771 --n;
3772 break;
3773 }
3774#endif
3775 used += plines(curwin->w_topline + n);
3776 if (used >= half)
3777 break;
3778#ifdef FEAT_FOLDING
3779 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3780 n = lnum - curwin->w_topline;
3781#endif
3782 }
3783 if (n > 0 && used > curwin->w_height)
3784 --n;
3785 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003786 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003787 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003789#ifdef FEAT_FOLDING
3790 if (hasAnyFolding(curwin))
3791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003792 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003793 lnum = curwin->w_topline;
3794 while (n-- > 0 && lnum < curwin->w_botline - 1)
3795 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003796 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003797 ++lnum;
3798 }
3799 n = lnum - curwin->w_topline;
3800 }
3801#endif
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 curwin->w_cursor.lnum = curwin->w_topline + n;
3804 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3805 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3806 }
3807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003808 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003809 if (cap->oap->op_type == OP_NOP)
3810 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 beginline(BL_SOL | BL_FIX);
3812}
3813
3814/*
3815 * Cursor right commands.
3816 */
3817 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003818nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003821 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003823 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003825 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003826 if (mod_mask & MOD_MASK_CTRL)
3827 cap->arg = TRUE;
3828 nv_wordcmd(cap);
3829 return;
3830 }
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 cap->oap->motion_type = MCHAR;
3833 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003834 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003836 // In virtual edit mode, there's no such thing as "past_line", as lines
3837 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003839 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
3841 for (n = cap->count1; n > 0; --n)
3842 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003843 if ((!past_line && oneright() == FAIL)
3844 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003845 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003847 // <Space> wraps to next line if 'whichwrap' has 's'.
3848 // 'l' wraps to next line if 'whichwrap' has 'l'.
3849 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if ( ((cap->cmdchar == ' '
3851 && vim_strchr(p_ww, 's') != NULL)
3852 || (cap->cmdchar == 'l'
3853 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003854 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 && vim_strchr(p_ww, '>') != NULL))
3856 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003858 // When deleting we also count the NL as a character.
3859 // Set cap->oap->inclusive when last char in the line is
3860 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003861 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003863 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 cap->oap->inclusive = TRUE;
3865 else
3866 {
3867 ++curwin->w_cursor.lnum;
3868 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 curwin->w_set_curswant = TRUE;
3871 cap->oap->inclusive = FALSE;
3872 }
3873 continue;
3874 }
3875 if (cap->oap->op_type == OP_NOP)
3876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003877 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (n == cap->count1)
3879 beep_flush();
3880 }
3881 else
3882 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003883 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 cap->oap->inclusive = TRUE;
3885 }
3886 break;
3887 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003888 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
3890 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (virtual_active())
3892 oneright();
3893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003896 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 ++curwin->w_cursor.col;
3899 }
3900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902#ifdef FEAT_FOLDING
3903 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3904 && cap->oap->op_type == OP_NOP)
3905 foldOpenCursor();
3906#endif
3907}
3908
3909/*
3910 * Cursor left commands.
3911 *
3912 * Returns TRUE when operator end should not be adjusted.
3913 */
3914 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003915nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 long n;
3918
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003919 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003921 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003922 if (mod_mask & MOD_MASK_CTRL)
3923 cap->arg = 1;
3924 nv_bck_word(cap);
3925 return;
3926 }
3927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 cap->oap->motion_type = MCHAR;
3929 cap->oap->inclusive = FALSE;
3930 for (n = cap->count1; n > 0; --n)
3931 {
3932 if (oneleft() == FAIL)
3933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003934 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3935 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3936 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if ( (((cap->cmdchar == K_BS
3938 || cap->cmdchar == Ctrl_H)
3939 && vim_strchr(p_ww, 'b') != NULL)
3940 || (cap->cmdchar == 'h'
3941 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003942 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 && vim_strchr(p_ww, '<') != NULL))
3944 && curwin->w_cursor.lnum > 1)
3945 {
3946 --(curwin->w_cursor.lnum);
3947 coladvance((colnr_T)MAXCOL);
3948 curwin->w_set_curswant = TRUE;
3949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003950 // When the NL before the first char has to be deleted we
3951 // put the cursor on the NUL after the previous line.
3952 // This is a very special case, be careful!
3953 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if ( (cap->oap->op_type == OP_DELETE
3955 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003956 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003958 char_u *cp = ml_get_cursor();
3959
3960 if (*cp != NUL)
3961 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003962 if (has_mbyte)
3963 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3964 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003965 ++curwin->w_cursor.col;
3966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 cap->retval |= CA_NO_ADJ_OP_END;
3968 }
3969 continue;
3970 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003971 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3973 beep_flush();
3974 break;
3975 }
3976 }
3977#ifdef FEAT_FOLDING
3978 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3979 && cap->oap->op_type == OP_NOP)
3980 foldOpenCursor();
3981#endif
3982}
3983
3984/*
3985 * Cursor up commands.
3986 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3987 */
3988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003989nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003991 if (mod_mask & MOD_MASK_SHIFT)
3992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003993 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 cap->arg = BACKWARD;
3995 nv_page(cap);
3996 }
3997 else
3998 {
3999 cap->oap->motion_type = MLINE;
4000 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4001 clearopbeep(cap->oap);
4002 else if (cap->arg)
4003 beginline(BL_WHITE | BL_FIX);
4004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005}
4006
4007/*
4008 * Cursor down commands.
4009 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4010 */
4011 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004012nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 if (mod_mask & MOD_MASK_SHIFT)
4015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017 cap->arg = FORWARD;
4018 nv_page(cap);
4019 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004020#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004021 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004022 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4023 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004025 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
4027#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004028 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004029 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 cmdwin_result = CAR;
4031 else
4032#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004033#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004034 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004035 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4036 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4037 {
4038 invoke_prompt_callback();
4039 if (restart_edit == 0)
4040 restart_edit = 'a';
4041 }
4042 else
4043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
4045 cap->oap->motion_type = MLINE;
4046 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4047 clearopbeep(cap->oap);
4048 else if (cap->arg)
4049 beginline(BL_WHITE | BL_FIX);
4050 }
4051 }
4052}
4053
4054#ifdef FEAT_SEARCHPATH
4055/*
4056 * Grab the file name under the cursor and edit it.
4057 */
4058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004059nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004062 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004064 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066 if (curbuf_locked())
4067 {
4068 clearop(cap->oap);
4069 return;
4070 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004071#ifdef FEAT_PROP_POPUP
4072 if (ERROR_IF_TERM_POPUP_WINDOW)
4073 return;
4074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004076 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 if (ptr != NULL)
4079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004080 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004081 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004082 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004084 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004085 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004086 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004087 {
4088 curwin->w_cursor.lnum = lnum;
4089 check_cursor_lnum();
4090 beginline(BL_SOL | BL_FIX);
4091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 vim_free(ptr);
4093 }
4094 else
4095 clearop(cap->oap);
4096}
4097#endif
4098
4099/*
4100 * <End> command: to end of current line or last line.
4101 */
4102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004107 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 nv_dollar(cap);
4112}
4113
4114/*
4115 * Handle the "$" command.
4116 */
4117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004118nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 cap->oap->motion_type = MCHAR;
4121 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004122 // In virtual mode when off the edge of a line and an operator
4123 // is pending (whew!) keep the cursor where it is.
4124 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (!virtual_active() || gchar_cursor() != NUL
4126 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004127 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (cursor_down((long)(cap->count1 - 1),
4129 cap->oap->op_type == OP_NOP) == FAIL)
4130 clearopbeep(cap->oap);
4131#ifdef FEAT_FOLDING
4132 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4133 foldOpenCursor();
4134#endif
4135}
4136
4137/*
4138 * Implementation of '?' and '/' commands.
4139 * If cap->arg is TRUE don't set PC mark.
4140 */
4141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004142nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004145 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 cap->cmdchar = 'g';
4151 cap->nchar = '?';
4152 nv_operator(cap);
4153 return;
4154 }
4155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004156 // When using 'incsearch' the cursor may be moved to set a different search
4157 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004158 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 if (cap->searchbuf == NULL)
4161 {
4162 clearop(oap);
4163 return;
4164 }
4165
Bram Moolenaar46539112015-02-17 15:43:57 +01004166 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004167 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004168 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169}
4170
LemonBoya4399382022-04-09 21:04:08 +01004171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172/*
4173 * Handle "N" and "n" commands.
4174 * cap->arg is SEARCH_REV for "N", 0 for "n".
4175 */
4176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004177nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004179 pos_T old = curwin->w_cursor;
4180 int wrapped = FALSE;
4181 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004182
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004183 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004184 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004185 // Avoid getting stuck on the current cursor position, which can
4186 // happen when an offset is given and the cursor is on the last char
4187 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004188 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004189 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004190 cap->count1 -= 1;
4191 }
LemonBoya4399382022-04-09 21:04:08 +01004192
4193#ifdef FEAT_SEARCH_EXTRA
4194 // Redraw the window to refresh the highlighted matches.
4195 if (i > 0 && p_hls && !no_hlsearch)
4196 redraw_later(SOME_VALID);
4197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198}
4199
4200/*
4201 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4202 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004203 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004205 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004206normal_search(
4207 cmdarg_T *cap,
4208 int dir,
4209 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004210 int opt, // extra flags for do_search()
4211 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004214 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004215#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004216 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219 cap->oap->motion_type = MCHAR;
4220 cap->oap->inclusive = FALSE;
4221 cap->oap->use_reg_one = TRUE;
4222 curwin->w_set_curswant = TRUE;
4223
Bram Moolenaara80faa82020-04-12 19:37:17 +02004224 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004225 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004226 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4227 if (wrapped != NULL)
4228 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (i == 0)
4230 clearop(cap->oap);
4231 else
4232 {
4233 if (i == 2)
4234 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236#ifdef FEAT_FOLDING
4237 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4238 foldOpenCursor();
4239#endif
4240 }
LemonBoya4399382022-04-09 21:04:08 +01004241#ifdef FEAT_SEARCH_EXTRA
4242 // Redraw the window to refresh the highlighted matches.
4243 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
4244 redraw_later(SOME_VALID);
4245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004247 // "/$" will put the cursor after the end of the line, may need to
4248 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004250 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251}
4252
4253/*
4254 * Character search commands.
4255 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4256 * ',' and FALSE for ';'.
4257 * cap->nchar is NUL for ',' and ';' (repeat the search)
4258 */
4259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004260nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 int t_cmd;
4263
4264 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4265 t_cmd = TRUE;
4266 else
4267 t_cmd = FALSE;
4268
4269 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4271 clearopbeep(cap->oap);
4272 else
4273 {
4274 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4277 && (t_cmd || cap->oap->op_type != OP_NOP))
4278 {
4279 colnr_T scol, ecol;
4280
4281 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4282 curwin->w_cursor.coladd = ecol - scol;
4283 }
4284 else
4285 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#ifdef FEAT_FOLDING
4288 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4289 foldOpenCursor();
4290#endif
4291 }
4292}
4293
4294/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004295 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4296 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4297 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4298 * "[m" or "]m" search for prev/next start of (Java) method.
4299 * "[M" or "]M" search for prev/next end of (Java) method.
4300 */
4301 static void
4302nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4303{
4304 pos_T new_pos = {0, 0, 0};
4305 pos_T *pos = NULL; // init for GCC
4306 pos_T prev_pos;
4307 long n;
4308 int findc;
4309 int c;
4310
4311 if (cap->nchar == '*')
4312 cap->nchar = '/';
4313 prev_pos.lnum = 0;
4314 if (cap->nchar == 'm' || cap->nchar == 'M')
4315 {
4316 if (cap->cmdchar == '[')
4317 findc = '{';
4318 else
4319 findc = '}';
4320 n = 9999;
4321 }
4322 else
4323 {
4324 findc = cap->nchar;
4325 n = cap->count1;
4326 }
4327 for ( ; n > 0; --n)
4328 {
4329 if ((pos = findmatchlimit(cap->oap, findc,
4330 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4331 {
4332 if (new_pos.lnum == 0) // nothing found
4333 {
4334 if (cap->nchar != 'm' && cap->nchar != 'M')
4335 clearopbeep(cap->oap);
4336 }
4337 else
4338 pos = &new_pos; // use last one found
4339 break;
4340 }
4341 prev_pos = new_pos;
4342 curwin->w_cursor = *pos;
4343 new_pos = *pos;
4344 }
4345 curwin->w_cursor = *old_pos;
4346
4347 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4348 // brought us to the match for "[m" and "]M" when inside a method.
4349 // Try finding the '{' or '}' we want to be at.
4350 // Also repeat for the given count.
4351 if (cap->nchar == 'm' || cap->nchar == 'M')
4352 {
4353 // norm is TRUE for "]M" and "[m"
4354 int norm = ((findc == '{') == (cap->nchar == 'm'));
4355
4356 n = cap->count1;
4357 // found a match: we were inside a method
4358 if (prev_pos.lnum != 0)
4359 {
4360 pos = &prev_pos;
4361 curwin->w_cursor = prev_pos;
4362 if (norm)
4363 --n;
4364 }
4365 else
4366 pos = NULL;
4367 while (n > 0)
4368 {
4369 for (;;)
4370 {
4371 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4372 {
4373 // if not found anything, that's an error
4374 if (pos == NULL)
4375 clearopbeep(cap->oap);
4376 n = 0;
4377 break;
4378 }
4379 c = gchar_cursor();
4380 if (c == '{' || c == '}')
4381 {
4382 // Must have found end/start of class: use it.
4383 // Or found the place to be at.
4384 if ((c == findc && norm) || (n == 1 && !norm))
4385 {
4386 new_pos = curwin->w_cursor;
4387 pos = &new_pos;
4388 n = 0;
4389 }
4390 // if no match found at all, we started outside of the
4391 // class and we're inside now. Just go on.
4392 else if (new_pos.lnum == 0)
4393 {
4394 new_pos = curwin->w_cursor;
4395 pos = &new_pos;
4396 }
4397 // found start/end of other method: go to match
4398 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004399 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4400 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004401 n = 0;
4402 else
4403 curwin->w_cursor = *pos;
4404 break;
4405 }
4406 }
4407 --n;
4408 }
4409 curwin->w_cursor = *old_pos;
4410 if (pos == NULL && new_pos.lnum != 0)
4411 clearopbeep(cap->oap);
4412 }
4413 if (pos != NULL)
4414 {
4415 setpcmark();
4416 curwin->w_cursor = *pos;
4417 curwin->w_set_curswant = TRUE;
4418#ifdef FEAT_FOLDING
4419 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4420 && cap->oap->op_type == OP_NOP)
4421 foldOpenCursor();
4422#endif
4423 }
4424}
4425
4426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 * "[" and "]" commands.
4428 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4429 */
4430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004431nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004434 pos_T *pos = NULL; // init for GCC
4435 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 int flag;
4437 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 cap->oap->motion_type = MCHAR;
4440 cap->oap->inclusive = FALSE;
4441 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004442 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004445 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (cap->nchar == 'f')
4447 nv_gotofile(cap);
4448 else
4449#endif
4450
4451#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004452 // Find the occurrence(s) of the identifier or define under cursor
4453 // in current and included files or jump to the first occurrence.
4454 //
4455 // search list jump
4456 // fwd bwd fwd bwd fwd bwd
4457 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4458 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004459 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
4461 char_u *ptr;
4462 int len;
4463
4464 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4465 clearop(cap->oap);
4466 else
4467 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004468 // Make a copy, if the line was changed it will be freed.
4469 ptr = vim_strnsave(ptr, len);
4470 if (ptr == NULL)
4471 return;
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 find_pattern_in_path(ptr, 0, len, TRUE,
4474 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4475 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4476 cap->count1,
4477 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4478 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4479 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4480 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004481 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 curwin->w_set_curswant = TRUE;
4483 }
4484 }
4485 else
4486#endif
4487
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004488 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4489 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4490 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4491 // "[m" or "]m" search for prev/next start of (Java) method.
4492 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if ( (cap->cmdchar == '['
4494 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4495 || (cap->cmdchar == ']'
4496 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004497 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004499 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else if (cap->nchar == '[' || cap->nchar == ']')
4501 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004502 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 flag = '{';
4504 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004508 // Imitate strange Vi behaviour: When using "]]" with an operator
4509 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004510 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 (cap->oap->op_type != OP_NOP
4512 && cap->arg == FORWARD && flag == '{')))
4513 clearopbeep(cap->oap);
4514 else
4515 {
4516 if (cap->oap->op_type == OP_NOP)
4517 beginline(BL_WHITE | BL_FIX);
4518#ifdef FEAT_FOLDING
4519 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4520 foldOpenCursor();
4521#endif
4522 }
4523 }
4524
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004525 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 else if (cap->nchar == 'p' || cap->nchar == 'P')
4527 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004528 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004531 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else if (cap->nchar == '\'' || cap->nchar == '`')
4533 {
4534 pos = &curwin->w_cursor;
4535 for (n = cap->count1; n > 0; --n)
4536 {
4537 prev_pos = *pos;
4538 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4539 cap->nchar == '\'');
4540 if (pos == NULL)
4541 break;
4542 }
4543 if (pos == NULL)
4544 pos = &prev_pos;
4545 nv_cursormark(cap, cap->nchar == '\'', pos);
4546 }
4547
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004548 // [ or ] followed by a middle mouse click: put selected text with
4549 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004550 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 (void)do_mouse(cap->oap, cap->nchar,
4553 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4554 cap->count1, PUT_FIXINDENT);
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004558 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 else if (cap->nchar == 'z')
4560 {
4561 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4562 cap->count1) == FAIL)
4563 clearopbeep(cap->oap);
4564 }
4565#endif
4566
4567#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004568 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else if (cap->nchar == 'c')
4570 {
4571 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4572 cap->count1) == FAIL)
4573 clearopbeep(cap->oap);
4574 }
4575#endif
4576
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004577#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004578 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579 else if (cap->nchar == 's' || cap->nchar == 'S')
4580 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004581 setpcmark();
4582 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004583 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4584 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004585 {
4586 clearopbeep(cap->oap);
4587 break;
4588 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004589 else
4590 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004591# ifdef FEAT_FOLDING
4592 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4593 foldOpenCursor();
4594# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 }
4596#endif
4597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004598 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 else
4600 clearopbeep(cap->oap);
4601}
4602
4603/*
4604 * Handle Normal mode "%" command.
4605 */
4606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004607nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004610#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 linenr_T lnum = curwin->w_cursor.lnum;
4612#endif
4613
4614 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 if (cap->count0 > 100)
4618 clearopbeep(cap->oap);
4619 else
4620 {
4621 cap->oap->motion_type = MLINE;
4622 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004623 // Round up, so 'normal 100%' always jumps at the line line.
4624 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4625 // overflow on 32-bits, so use a formula with less accuracy
4626 // to avoid overflows.
4627 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4629 / 100L * cap->count0;
4630 else
4631 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4632 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004633 if (curwin->w_cursor.lnum < 1)
4634 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4636 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4637 beginline(BL_SOL | BL_FIX);
4638 }
4639 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004640 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
4642 cap->oap->motion_type = MCHAR;
4643 cap->oap->use_reg_one = TRUE;
4644 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4645 clearopbeep(cap->oap);
4646 else
4647 {
4648 setpcmark();
4649 curwin->w_cursor = *pos;
4650 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 }
4655#ifdef FEAT_FOLDING
4656 if (cap->oap->op_type == OP_NOP
4657 && lnum != curwin->w_cursor.lnum
4658 && (fdo_flags & FDO_PERCENT)
4659 && KeyTyped)
4660 foldOpenCursor();
4661#endif
4662}
4663
4664/*
4665 * Handle "(" and ")" commands.
4666 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4667 */
4668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004669nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 cap->oap->motion_type = MCHAR;
4672 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004673 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004674 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 curwin->w_set_curswant = TRUE;
4676
4677 if (findsent(cap->arg, cap->count1) == FAIL)
4678 clearopbeep(cap->oap);
4679 else
4680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004681 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004682 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#ifdef FEAT_FOLDING
4685 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4686 foldOpenCursor();
4687#endif
4688 }
4689}
4690
4691/*
4692 * "m" command: Mark a position.
4693 */
4694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004695nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 if (!checkclearop(cap->oap))
4698 {
4699 if (setmark(cap->nchar) == FAIL)
4700 clearopbeep(cap->oap);
4701 }
4702}
4703
4704/*
4705 * "{" and "}" commands.
4706 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4707 */
4708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004709nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 cap->oap->motion_type = MCHAR;
4712 cap->oap->inclusive = FALSE;
4713 cap->oap->use_reg_one = TRUE;
4714 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004715 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 clearopbeep(cap->oap);
4717 else
4718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#ifdef FEAT_FOLDING
4721 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4722 foldOpenCursor();
4723#endif
4724 }
4725}
4726
4727/*
4728 * "u" command: Undo or make lower case.
4729 */
4730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004731nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004733 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 cap->cmdchar = 'g';
4737 cap->nchar = 'u';
4738 nv_operator(cap);
4739 }
4740 else
4741 nv_kundo(cap);
4742}
4743
4744/*
4745 * <Undo> command.
4746 */
4747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 if (!checkclearopq(cap->oap))
4751 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004752#ifdef FEAT_JOB_CHANNEL
4753 if (bt_prompt(curbuf))
4754 {
4755 clearopbeep(cap->oap);
4756 return;
4757 }
4758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 u_undo((int)cap->count1);
4760 curwin->w_set_curswant = TRUE;
4761 }
4762}
4763
4764/*
4765 * Handle the "r" command.
4766 */
4767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004768nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 char_u *ptr;
4771 int had_ctrl_v;
4772 long n;
4773
4774 if (checkclearop(cap->oap))
4775 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004776#ifdef FEAT_JOB_CHANNEL
4777 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4778 {
4779 clearopbeep(cap->oap);
4780 return;
4781 }
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004784 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 if (cap->nchar == Ctrl_V)
4786 {
4787 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004788 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (cap->nchar > DEL)
4791 had_ctrl_v = NUL;
4792 }
4793 else
4794 had_ctrl_v = NUL;
4795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004796 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004797 if (IS_SPECIAL(cap->nchar))
4798 {
4799 clearopbeep(cap->oap);
4800 return;
4801 }
4802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if (VIsual_active)
4805 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004806 if (got_int)
4807 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004808 if (had_ctrl_v)
4809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004810 // Use a special (negative) number to make a difference between a
4811 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004812 if (cap->nchar == CAR)
4813 cap->nchar = REPLACE_CR_NCHAR;
4814 else if (cap->nchar == NL)
4815 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 nv_operator(cap);
4818 return;
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004821 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (virtual_active())
4823 {
4824 if (u_save_cursor() == FAIL)
4825 return;
4826 if (gchar_cursor() == NUL)
4827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004828 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 coladvance_force((colnr_T)(getviscol() + cap->count1));
4830 curwin->w_cursor.col -= cap->count1;
4831 }
4832 else if (gchar_cursor() == TAB)
4833 coladvance_force(getviscol());
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004838 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004839 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 clearopbeep(cap->oap);
4842 return;
4843 }
4844
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004845 // Replacing with a TAB is done by edit() when it is complicated because
4846 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4847 // Other characters are done below to avoid problems with things like
4848 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4850 {
4851 stuffnumReadbuff(cap->count1);
4852 stuffcharReadbuff('R');
4853 stuffcharReadbuff('\t');
4854 stuffcharReadbuff(ESC);
4855 return;
4856 }
4857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (u_save_cursor() == FAIL)
4860 return;
4861
4862 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4863 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004864 // Replace character(s) by a single newline.
4865 // Strange vi behaviour: Only one newline is inserted.
4866 // Delete the characters here.
4867 // Insert the newline with an insert command, takes care of
4868 // autoindent. The insert command depends on being on the last
4869 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 stuffcharReadbuff('\r');
4872 stuffcharReadbuff(ESC);
4873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004874 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 invoke_edit(cap, TRUE, 'r', FALSE);
4876 }
4877 else
4878 {
4879 prep_redo(cap->oap->regname, cap->count1,
4880 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4881
4882 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (has_mbyte)
4884 {
4885 int old_State = State;
4886
4887 if (cap->ncharC1 != 0)
4888 AppendCharToRedobuff(cap->ncharC1);
4889 if (cap->ncharC2 != 0)
4890 AppendCharToRedobuff(cap->ncharC2);
4891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004892 // This is slow, but it handles replacing a single-byte with a
4893 // multi-byte and the other way around. Also handles adding
4894 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 for (n = cap->count1; n > 0; --n)
4896 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004897 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004898 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4899 {
4900 int c = ins_copychar(curwin->w_cursor.lnum
4901 + (cap->nchar == Ctrl_Y ? -1 : 1));
4902 if (c != NUL)
4903 ins_char(c);
4904 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004905 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004906 ++curwin->w_cursor.col;
4907 }
4908 else
4909 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 State = old_State;
4911 if (cap->ncharC1 != 0)
4912 ins_char(cap->ncharC1);
4913 if (cap->ncharC2 != 0)
4914 ins_char(cap->ncharC2);
4915 }
4916 }
4917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004919 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 for (n = cap->count1; n > 0; --n)
4921 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004922 // Get ptr again, because u_save and/or showmatch() will have
4923 // released the line. This may also happen in ins_copychar().
4924 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004925 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4926 {
4927 int c = ins_copychar(curwin->w_cursor.lnum
4928 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004929
4930 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004931 if (c != NUL)
4932 ptr[curwin->w_cursor.col] = c;
4933 }
4934 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004935 {
4936 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004937 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (p_sm && msg_silent == 0)
4940 showmatch(cap->nchar);
4941 ++curwin->w_cursor.col;
4942 }
4943#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004944 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004946 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
Bram Moolenaar009b2592004-10-24 19:18:58 +00004948 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004949 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004951 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953#endif
4954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004955 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 changed_bytes(curwin->w_cursor.lnum,
4957 (colnr_T)(curwin->w_cursor.col - cap->count1));
4958 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 --curwin->w_cursor.col; // cursor on the last replaced char
4960 // if the character on the left of the current cursor is a multi-byte
4961 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (has_mbyte)
4963 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 curbuf->b_op_end = curwin->w_cursor;
4965 curwin->w_set_curswant = TRUE;
4966 set_last_insert(cap->nchar);
4967 }
4968}
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970/*
4971 * 'o': Exchange start and end of Visual area.
4972 * 'O': same, but in block mode exchange left and right corners.
4973 */
4974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004975v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 pos_T old_cursor;
4978 colnr_T left, right;
4979
4980 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4981 {
4982 old_cursor = curwin->w_cursor;
4983 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4984 curwin->w_cursor.lnum = VIsual.lnum;
4985 coladvance(left);
4986 VIsual = curwin->w_cursor;
4987
4988 curwin->w_cursor.lnum = old_cursor.lnum;
4989 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004990 // 'selection "exclusive" and cursor at right-bottom corner: move it
4991 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4993 ++curwin->w_curswant;
4994 coladvance(curwin->w_curswant);
4995 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004997 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 curwin->w_cursor.lnum = VIsual.lnum;
5000 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5001 ++right;
5002 coladvance(right);
5003 VIsual = curwin->w_cursor;
5004
5005 curwin->w_cursor.lnum = old_cursor.lnum;
5006 coladvance(left);
5007 curwin->w_curswant = left;
5008 }
5009 }
5010 else
5011 {
5012 old_cursor = curwin->w_cursor;
5013 curwin->w_cursor = VIsual;
5014 VIsual = old_cursor;
5015 curwin->w_set_curswant = TRUE;
5016 }
5017}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019/*
5020 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5021 */
5022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005023nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005025 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 cap->cmdchar = 'c';
5028 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 VIsual_mode = 'V';
5031 nv_operator(cap);
5032 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005033 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005036 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
5038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (virtual_active())
5040 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5042 }
5043 }
5044}
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046/*
5047 * "gr".
5048 */
5049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005050nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (VIsual_active)
5053 {
5054 cap->cmdchar = 'r';
5055 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005056 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005058 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005061 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else
5063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005064 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005065 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 stuffcharReadbuff(cap->extra_char);
5067 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (virtual_active())
5069 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 invoke_edit(cap, TRUE, 'v', FALSE);
5071 }
5072 }
5073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075/*
5076 * Swap case for "~" command, when it does not work like an operator.
5077 */
5078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005079n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 long n;
5082 pos_T startpos;
5083 int did_change = 0;
5084#ifdef FEAT_NETBEANS_INTG
5085 pos_T pos;
5086 char_u *ptr;
5087 int count;
5088#endif
5089
5090 if (checkclearopq(cap->oap))
5091 return;
5092
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005093 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 clearopbeep(cap->oap);
5096 return;
5097 }
5098
5099 prep_redo_cmd(cap);
5100
5101 if (u_save_cursor() == FAIL)
5102 return;
5103
5104 startpos = curwin->w_cursor;
5105#ifdef FEAT_NETBEANS_INTG
5106 pos = startpos;
5107#endif
5108 for (n = cap->count1; n > 0; --n)
5109 {
5110 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5111 inc_cursor();
5112 if (gchar_cursor() == NUL)
5113 {
5114 if (vim_strchr(p_ww, '~') != NULL
5115 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5116 {
5117#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005118 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 if (did_change)
5121 {
5122 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005123 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005124 netbeans_removed(curbuf, pos.lnum, pos.col,
5125 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005126 // line may have been flushed, get it again
5127 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005129 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 pos.col = 0;
5132 pos.lnum++;
5133 }
5134#endif
5135 ++curwin->w_cursor.lnum;
5136 curwin->w_cursor.col = 0;
5137 if (n > 1)
5138 {
5139 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5140 break;
5141 u_clearline();
5142 }
5143 }
5144 else
5145 break;
5146 }
5147 }
5148#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005149 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 ptr = ml_get(pos.lnum);
5152 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005153 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5154 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156#endif
5157
5158
5159 check_cursor();
5160 curwin->w_set_curswant = TRUE;
5161 if (did_change)
5162 {
5163 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5164 0L);
5165 curbuf->b_op_start = startpos;
5166 curbuf->b_op_end = curwin->w_cursor;
5167 if (curbuf->b_op_end.col > 0)
5168 --curbuf->b_op_end.col;
5169 }
5170}
5171
5172/*
5173 * Move cursor to mark.
5174 */
5175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005176nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
5178 if (check_mark(pos) == FAIL)
5179 clearop(cap->oap);
5180 else
5181 {
5182 if (cap->cmdchar == '\''
5183 || cap->cmdchar == '`'
5184 || cap->cmdchar == '['
5185 || cap->cmdchar == ']')
5186 setpcmark();
5187 curwin->w_cursor = *pos;
5188 if (flag)
5189 beginline(BL_WHITE | BL_FIX);
5190 else
5191 check_cursor();
5192 }
5193 cap->oap->motion_type = flag ? MLINE : MCHAR;
5194 if (cap->cmdchar == '`')
5195 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005196 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 curwin->w_set_curswant = TRUE;
5198}
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200/*
5201 * Handle commands that are operators in Visual mode.
5202 */
5203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
5206 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005208 // Uppercase means linewise, except in block mode, then "D" deletes till
5209 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (isupper(cap->cmdchar))
5211 {
5212 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005213 {
5214 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5218 curwin->w_curswant = MAXCOL;
5219 }
5220 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5221 nv_operator(cap);
5222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224/*
5225 * "s" and "S" commands.
5226 */
5227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005228nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005230#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005231 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005232 if (term_swap_diff() == OK)
5233 return;
5234#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005235#ifdef FEAT_JOB_CHANNEL
5236 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5237 {
5238 clearopbeep(cap->oap);
5239 return;
5240 }
5241#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005242 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005245 {
5246 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 cap->cmdchar = 'c';
5250 nv_operator(cap);
5251 }
5252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 nv_optrans(cap);
5254}
5255
5256/*
5257 * Abbreviated commands.
5258 */
5259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005265 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (VIsual_active)
5267 v_visop(cap);
5268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 nv_optrans(cap);
5270}
5271
5272/*
5273 * Translate a command into another command.
5274 */
5275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005276nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5279 (char_u *)"d$", (char_u *)"c$",
5280 (char_u *)"cl", (char_u *)"cc",
5281 (char_u *)"yy", (char_u *)":s\r"};
5282 static char_u *str = (char_u *)"xXDCsSY&";
5283
5284 if (!checkclearopq(cap->oap))
5285 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005286 // In Vi "2D" doesn't delete the next line. Can't translate it
5287 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005288 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5289 {
5290 cap->oap->start = curwin->w_cursor;
5291 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005292#ifdef FEAT_EVAL
5293 set_op_var(OP_DELETE);
5294#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005295 cap->count1 = 1;
5296 nv_dollar(cap);
5297 finish_op = TRUE;
5298 ResetRedobuff();
5299 AppendCharToRedobuff('D');
5300 }
5301 else
5302 {
5303 if (cap->count0)
5304 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005305 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308 cap->opcount = 0;
5309}
5310
5311/*
5312 * "'" and "`" commands. Also for "g'" and "g`".
5313 * cap->arg is TRUE for "'" and "g'".
5314 */
5315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 pos_T *pos;
5319 int c;
5320#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005321 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005322 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323#endif
5324
5325 if (cap->cmdchar == 'g')
5326 c = cap->extra_char;
5327 else
5328 c = cap->nchar;
5329 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005330 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 if (cap->arg)
5333 {
5334 check_cursor_lnum();
5335 beginline(BL_WHITE | BL_FIX);
5336 }
5337 else
5338 check_cursor();
5339 }
5340 else
5341 nv_cursormark(cap, cap->arg, pos);
5342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 if (!virtual_active())
5345 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005346 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#ifdef FEAT_FOLDING
5348 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005349 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005350 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 && (fdo_flags & FDO_MARK)
5352 && old_KeyTyped)
5353 foldOpenCursor();
5354#endif
5355}
5356
5357/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005358 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
5360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005361nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005364#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005366 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369 if (!checkclearopq(cap->oap))
5370 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005371 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5372 {
5373 if (goto_tabpage_lastused() == FAIL)
5374 clearopbeep(cap->oap);
5375 return;
5376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (cap->cmdchar == 'g')
5378 pos = movechangelist((int)cap->count1);
5379 else
5380 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005381 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 curwin->w_set_curswant = TRUE;
5384 check_cursor();
5385 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005386 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 nv_cursormark(cap, FALSE, pos);
5388 else if (cap->cmdchar == 'g')
5389 {
5390 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005391 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005393 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005395 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 else
5398 clearopbeep(cap->oap);
5399# ifdef FEAT_FOLDING
5400 if (cap->oap->op_type == OP_NOP
5401 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5402 && (fdo_flags & FDO_MARK)
5403 && old_KeyTyped)
5404 foldOpenCursor();
5405# endif
5406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407}
5408
5409/*
5410 * Handle '"' command.
5411 */
5412 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005413nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 if (checkclearop(cap->oap))
5416 return;
5417#ifdef FEAT_EVAL
5418 if (cap->nchar == '=')
5419 cap->nchar = get_expr_register();
5420#endif
5421 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5422 {
5423 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005424 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#ifdef FEAT_EVAL
5426 set_reg_var(cap->oap->regname);
5427#endif
5428 }
5429 else
5430 clearopbeep(cap->oap);
5431}
5432
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433/*
5434 * Handle "v", "V" and "CTRL-V" commands.
5435 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5436 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005437 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 */
5439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005442 if (cap->cmdchar == Ctrl_Q)
5443 cap->cmdchar = Ctrl_V;
5444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005445 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5446 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (cap->oap->op_type != OP_NOP)
5448 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005449 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 return;
5452 }
5453
5454 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 else // toggle char/block mode
5460 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 VIsual_mode = cap->cmdchar;
5462 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005463 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005465 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005467 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
5469 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005470 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005472 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 VIsual = curwin->w_cursor;
5474
5475 VIsual_active = TRUE;
5476 VIsual_reselect = TRUE;
5477 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005478 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005481 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005482 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005483 // For V and ^V, we multiply the number of lines even if there
5484 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5486 {
5487 curwin->w_cursor.lnum +=
5488 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005489 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491 VIsual_mode = resel_VIsual_mode;
5492 if (VIsual_mode == 'v')
5493 {
5494 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005495 {
5496 validate_virtcol();
5497 curwin->w_curswant = curwin->w_virtcol
5498 + resel_VIsual_vcol * cap->count0 - 1;
5499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005501 curwin->w_curswant = resel_VIsual_vcol;
5502 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005504 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 curwin->w_curswant = MAXCOL;
5507 coladvance((colnr_T)MAXCOL);
5508 }
5509 else if (VIsual_mode == Ctrl_V)
5510 {
5511 validate_virtcol();
5512 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005513 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 coladvance(curwin->w_curswant);
5515 }
5516 else
5517 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005518 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
5520 else
5521 {
5522 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005523 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 may_start_select('c');
5525 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005526 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005528 if (cap->count0 > 0 && --cap->count1 > 0)
5529 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005530 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005531 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5532 nv_right(cap);
5533 else if (VIsual_mode == 'V')
5534 nv_down(cap);
5535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
5537 }
5538}
5539
5540/*
5541 * Start selection for Shift-movement keys.
5542 */
5543 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005544start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005546 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 may_start_select('k');
5548 n_start_visual_mode('v');
5549}
5550
5551/*
5552 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005553 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 */
5555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005556may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005558 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5559 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560}
5561
5562/*
5563 * Start Visual mode "c".
5564 * Should set VIsual_select before calling this.
5565 */
5566 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005567n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005569#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005570 int cursor_line_was_concealed = curwin->w_p_cole > 0
5571 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005572#endif
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 VIsual_mode = c;
5575 VIsual_active = TRUE;
5576 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005577
5578 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005579 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005580 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005581 {
5582 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 VIsual = curwin->w_cursor;
5586
5587#ifdef FEAT_FOLDING
5588 foldAdjustVisual();
5589#endif
5590
LemonBoy2bf52dd2022-04-09 18:17:34 +01005591 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005593#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005594 // Check if redraw is needed after changing the state.
5595 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005596#endif
5597
Bram Moolenaar09df3122006-01-23 22:23:09 +00005598 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005601 // Make sure the clipboard gets updated. Needed because start and
5602 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 clip_star.vmode = NUL;
5604#endif
5605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005606 // Only need to redraw this line, unless still need to redraw an old
5607 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (curwin->w_redr_type < INVERTED)
5609 {
5610 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5611 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5612 }
5613}
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616/*
5617 * CTRL-W: Window commands
5618 */
5619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005620nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005622 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005623 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005624 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005625 cap->cmdchar = ':';
5626 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005627 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005628 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005629 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631}
5632
5633/*
5634 * CTRL-Z: Suspend
5635 */
5636 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005637nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005642 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005646 * "gv": Reselect the previous Visual area. If Visual already active,
5647 * exchange previous and current Visual area.
5648 */
5649 static void
5650nv_gv_cmd(cmdarg_T *cap)
5651{
5652 pos_T tpos;
5653 int i;
5654
5655 if (checkclearop(cap->oap))
5656 return;
5657
5658 if (curbuf->b_visual.vi_start.lnum == 0
5659 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5660 || curbuf->b_visual.vi_end.lnum == 0)
5661 {
5662 beep_flush();
5663 return;
5664 }
5665
5666 // set w_cursor to the start of the Visual area, tpos to the end
5667 if (VIsual_active)
5668 {
5669 i = VIsual_mode;
5670 VIsual_mode = curbuf->b_visual.vi_mode;
5671 curbuf->b_visual.vi_mode = i;
5672# ifdef FEAT_EVAL
5673 curbuf->b_visual_mode_eval = i;
5674# endif
5675 i = curwin->w_curswant;
5676 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5677 curbuf->b_visual.vi_curswant = i;
5678
5679 tpos = curbuf->b_visual.vi_end;
5680 curbuf->b_visual.vi_end = curwin->w_cursor;
5681 curwin->w_cursor = curbuf->b_visual.vi_start;
5682 curbuf->b_visual.vi_start = VIsual;
5683 }
5684 else
5685 {
5686 VIsual_mode = curbuf->b_visual.vi_mode;
5687 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5688 tpos = curbuf->b_visual.vi_end;
5689 curwin->w_cursor = curbuf->b_visual.vi_start;
5690 }
5691
5692 VIsual_active = TRUE;
5693 VIsual_reselect = TRUE;
5694
5695 // Set Visual to the start and w_cursor to the end of the Visual
5696 // area. Make sure they are on an existing character.
5697 check_cursor();
5698 VIsual = curwin->w_cursor;
5699 curwin->w_cursor = tpos;
5700 check_cursor();
5701 update_topline();
5702
5703 // When called from normal "g" command: start Select mode when
5704 // 'selectmode' contains "cmd". When called for K_SELECT, always
5705 // start Select mode.
5706 if (cap->arg)
5707 {
5708 VIsual_select = TRUE;
5709 VIsual_select_reg = 0;
5710 }
5711 else
5712 may_start_select('c');
5713 setmouse();
5714#ifdef FEAT_CLIPBOARD
5715 // Make sure the clipboard gets updated. Needed because start and
5716 // end are still the same, and the selection needs to be owned
5717 clip_star.vmode = NUL;
5718#endif
5719 redraw_curbuf_later(INVERTED);
5720 showmode();
5721}
5722
5723/*
5724 * "g0", "g^" : Like "0" and "^" but for screen lines.
5725 * "gm": middle of "g0" and "g$".
5726 */
5727 static void
5728nv_g_home_m_cmd(cmdarg_T *cap)
5729{
5730 int i;
5731 int flag = FALSE;
5732
5733 if (cap->nchar == '^')
5734 flag = TRUE;
5735
5736 cap->oap->motion_type = MCHAR;
5737 cap->oap->inclusive = FALSE;
5738 if (curwin->w_p_wrap && curwin->w_width != 0)
5739 {
5740 int width1 = curwin->w_width - curwin_col_off();
5741 int width2 = width1 + curwin_col_off2();
5742
5743 validate_virtcol();
5744 i = 0;
5745 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5746 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5747 }
5748 else
5749 i = curwin->w_leftcol;
5750 // Go to the middle of the screen line. When 'number' or
5751 // 'relativenumber' is on and lines are wrapping the middle can be more
5752 // to the left.
5753 if (cap->nchar == 'm')
5754 i += (curwin->w_width - curwin_col_off()
5755 + ((curwin->w_p_wrap && i > 0)
5756 ? curwin_col_off2() : 0)) / 2;
5757 coladvance((colnr_T)i);
5758 if (flag)
5759 {
5760 do
5761 i = gchar_cursor();
5762 while (VIM_ISWHITE(i) && oneright() == OK);
5763 curwin->w_valid &= ~VALID_WCOL;
5764 }
5765 curwin->w_set_curswant = TRUE;
5766}
5767
5768/*
5769 * "g_": to the last non-blank character in the line or <count> lines
5770 * downward.
5771 */
5772 static void
5773nv_g_underscore_cmd(cmdarg_T *cap)
5774{
5775 char_u *ptr;
5776
5777 cap->oap->motion_type = MCHAR;
5778 cap->oap->inclusive = TRUE;
5779 curwin->w_curswant = MAXCOL;
5780 if (cursor_down((long)(cap->count1 - 1),
5781 cap->oap->op_type == OP_NOP) == FAIL)
5782 {
5783 clearopbeep(cap->oap);
5784 return;
5785 }
5786
5787 ptr = ml_get_curline();
5788
5789 // In Visual mode we may end up after the line.
5790 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5791 --curwin->w_cursor.col;
5792
5793 // Decrease the cursor column until it's on a non-blank.
5794 while (curwin->w_cursor.col > 0
5795 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5796 --curwin->w_cursor.col;
5797 curwin->w_set_curswant = TRUE;
5798 adjust_for_sel(cap);
5799}
5800
5801/*
5802 * "g$" : Like "$" but for screen lines.
5803 */
5804 static void
5805nv_g_dollar_cmd(cmdarg_T *cap)
5806{
5807 oparg_T *oap = cap->oap;
5808 int i;
5809 int col_off = curwin_col_off();
5810
5811 oap->motion_type = MCHAR;
5812 oap->inclusive = TRUE;
5813 if (curwin->w_p_wrap && curwin->w_width != 0)
5814 {
5815 curwin->w_curswant = MAXCOL; // so we stay at the end
5816 if (cap->count1 == 1)
5817 {
5818 int width1 = curwin->w_width - col_off;
5819 int width2 = width1 + curwin_col_off2();
5820
5821 validate_virtcol();
5822 i = width1 - 1;
5823 if (curwin->w_virtcol >= (colnr_T)width1)
5824 i += ((curwin->w_virtcol - width1) / width2 + 1)
5825 * width2;
5826 coladvance((colnr_T)i);
5827
5828 // Make sure we stick in this column.
5829 validate_virtcol();
5830 curwin->w_curswant = curwin->w_virtcol;
5831 curwin->w_set_curswant = FALSE;
5832 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5833 {
5834 // Check for landing on a character that got split at
5835 // the end of the line. We do not want to advance to
5836 // the next screen line.
5837 if (curwin->w_virtcol > (colnr_T)i)
5838 --curwin->w_cursor.col;
5839 }
5840 }
5841 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5842 clearopbeep(oap);
5843 }
5844 else
5845 {
5846 if (cap->count1 > 1)
5847 // if it fails, let the cursor still move to the last char
5848 (void)cursor_down(cap->count1 - 1, FALSE);
5849
5850 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5851 coladvance((colnr_T)i);
5852
5853 // if the character doesn't fit move one back
5854 if (curwin->w_cursor.col > 0
5855 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5856 {
5857 colnr_T vcol;
5858
5859 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5860 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5861 --curwin->w_cursor.col;
5862 }
5863
5864 // Make sure we stick in this column.
5865 validate_virtcol();
5866 curwin->w_curswant = curwin->w_virtcol;
5867 curwin->w_set_curswant = FALSE;
5868 }
5869}
5870
5871/*
5872 * "gi": start Insert at the last position.
5873 */
5874 static void
5875nv_gi_cmd(cmdarg_T *cap)
5876{
5877 int i;
5878
5879 if (curbuf->b_last_insert.lnum != 0)
5880 {
5881 curwin->w_cursor = curbuf->b_last_insert;
5882 check_cursor_lnum();
5883 i = (int)STRLEN(ml_get_curline());
5884 if (curwin->w_cursor.col > (colnr_T)i)
5885 {
5886 if (virtual_active())
5887 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5888 curwin->w_cursor.col = i;
5889 }
5890 }
5891 cap->cmdchar = 'i';
5892 nv_edit(cap);
5893}
5894
5895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Commands starting with "g".
5897 */
5898 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005899nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904 switch (cap->nchar)
5905 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005906 case Ctrl_A:
5907 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005909 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005910 if (!VIsual_active && cap->nchar == Ctrl_A)
5911 vim_mem_profile_dump();
5912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005914 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005915 if (VIsual_active)
5916 {
5917 cap->arg = TRUE;
5918 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005919 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005920 nv_addsub(cap);
5921 }
5922 else
5923 clearopbeep(oap);
5924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005926 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 case 'R':
5928 cap->arg = TRUE;
5929 nv_Replace(cap);
5930 break;
5931
5932 case 'r':
5933 nv_vreplace(cap);
5934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
5936 case '&':
5937 do_cmdline_cmd((char_u *)"%s//~/&");
5938 break;
5939
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005940 // "gv": Reselect the previous Visual area. If Visual already active,
5941 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005943 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005945
5946 // "gV": Don't reselect the previous Visual area after a Select mode
5947 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 case 'V':
5949 VIsual_reselect = FALSE;
5950 break;
5951
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005952 // "gh": start Select mode.
5953 // "gH": start Select line mode.
5954 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 case K_BS:
5956 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005957 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 case 'h':
5959 case 'H':
5960 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 cap->cmdchar = cap->nchar + ('v' - 'h');
5962 cap->arg = TRUE;
5963 nv_visual(cap);
5964 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005966 // "gn", "gN" visually select next/previous search match
5967 // "gn" selects next match
5968 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005969 case 'N':
5970 case 'n':
5971 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005972 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005975 // "gj" and "gk" two new funny movement keys -- up and down
5976 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 case 'j':
5978 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005979 // with 'nowrap' it works just like the normal "j" command.
5980 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
5982 oap->motion_type = MLINE;
5983 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5984 }
5985 else
5986 i = nv_screengo(oap, FORWARD, cap->count1);
5987 if (i == FAIL)
5988 clearopbeep(oap);
5989 break;
5990
5991 case 'k':
5992 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005993 // with 'nowrap' it works just like the normal "k" command.
5994 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 oap->motion_type = MLINE;
5997 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5998 }
5999 else
6000 i = nv_screengo(oap, BACKWARD, cap->count1);
6001 if (i == FAIL)
6002 clearopbeep(oap);
6003 break;
6004
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006005 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 case 'J':
6007 nv_join(cap);
6008 break;
6009
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006010 // "g0", "g^" : Like "0" and "^" but for screen lines.
6011 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 case '0':
6014 case 'm':
6015 case K_HOME:
6016 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006017 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 break;
6019
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006020 case 'M':
6021 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006022 oap->motion_type = MCHAR;
6023 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006024 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006025 if (cap->count0 > 0 && cap->count0 <= 100)
6026 coladvance((colnr_T)(i * cap->count0 / 100));
6027 else
6028 coladvance((colnr_T)(i / 2));
6029 curwin->w_set_curswant = TRUE;
6030 }
6031 break;
6032
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 // "g_": to the last non-blank character in the line or <count> lines
6034 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006036 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 break;
6038
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006039 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 case '$':
6041 case K_END:
6042 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006043 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 break;
6045
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006046 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 case '*':
6048 case '#':
6049#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006050 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006052 case Ctrl_RSB: // :tag or :tselect for current identifier
6053 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 nv_ident(cap);
6055 break;
6056
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006057 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 case 'e':
6059 case 'E':
6060 oap->motion_type = MCHAR;
6061 curwin->w_set_curswant = TRUE;
6062 oap->inclusive = TRUE;
6063 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6064 clearopbeep(oap);
6065 break;
6066
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006067 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006069 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 break;
6071
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006072 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 break;
6076
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006077 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case 'I':
6079 beginline(0);
6080 if (!checkclearopq(oap))
6081 invoke_edit(cap, FALSE, 'g', FALSE);
6082 break;
6083
6084#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006085 // "gf": goto file, edit file under cursor
6086 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006088 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 nv_gotofile(cap);
6090 break;
6091#endif
6092
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006093 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 case '\'':
6095 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006096 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 case '`':
6098 nv_gomark(cap);
6099 break;
6100
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006101 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006103 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 break;
6105
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006106 // "ga": Display the ascii value of the character under the
6107 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 case 'a':
6109 do_ascii(NULL);
6110 break;
6111
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006112 // "g8": Display the bytes used for the UTF-8 character under the
6113 // cursor. It is displayed in hex.
6114 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006116 if (cap->count0 == 8)
6117 utf_find_illegal();
6118 else
6119 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006122 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006123 case '<':
6124 show_sb_text();
6125 break;
6126
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006127 // "gg": Goto the first line in file. With a count it goes to
6128 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 case 'g':
6130 cap->arg = FALSE;
6131 nv_goto(cap);
6132 break;
6133
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006134 // Two-character operators:
6135 // "gq" Format text
6136 // "gw" Format text and keep cursor position
6137 // "g~" Toggle the case of the text.
6138 // "gu" Change text to lower case.
6139 // "gU" Change text to upper case.
6140 // "g?" rot13 encoding
6141 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 case 'q':
6143 case 'w':
6144 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006145 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case '~':
6147 case 'u':
6148 case 'U':
6149 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006150 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 nv_operator(cap);
6152 break;
6153
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006154 // "gd": Find first occurrence of pattern under the cursor in the
6155 // current function
6156 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 case 'd':
6158 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006159 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 break;
6161
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006162 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 case K_MIDDLEMOUSE:
6164 case K_MIDDLEDRAG:
6165 case K_MIDDLERELEASE:
6166 case K_LEFTMOUSE:
6167 case K_LEFTDRAG:
6168 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006169 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 case K_RIGHTMOUSE:
6171 case K_RIGHTDRAG:
6172 case K_RIGHTRELEASE:
6173 case K_X1MOUSE:
6174 case K_X1DRAG:
6175 case K_X1RELEASE:
6176 case K_X2MOUSE:
6177 case K_X2DRAG:
6178 case K_X2RELEASE:
6179 mod_mask = MOD_MASK_CTRL;
6180 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 case K_IGNORE:
6184 break;
6185
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006186 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 case 'p':
6188 case 'P':
6189 nv_put(cap);
6190 break;
6191
6192#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006193 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 case 'o':
6195 goto_byte(cap->count0);
6196 break;
6197#endif
6198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006199 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006201 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 do_exmode(TRUE);
6203 break;
6204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case ',':
6206 nv_pcmark(cap);
6207 break;
6208
6209 case ';':
6210 cap->count1 = -cap->count1;
6211 nv_pcmark(cap);
6212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006215 if (!checkclearop(oap))
6216 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006218 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006219 if (!checkclearop(oap))
6220 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006221 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222
Bram Moolenaar62a23252020-08-09 14:04:42 +02006223 case TAB:
6224 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6225 clearopbeep(oap);
6226 break;
6227
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006228 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006229 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006230 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006231 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006232 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006233 break;
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 default:
6236 clearopbeep(oap);
6237 break;
6238 }
6239}
6240
6241/*
6242 * Handle "o" and "O" commands.
6243 */
6244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006245n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006247#ifdef FEAT_CONCEAL
6248 linenr_T oldline = curwin->w_cursor.lnum;
6249#endif
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (!checkclearopq(cap->oap))
6252 {
6253#ifdef FEAT_FOLDING
6254 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006255 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 (void)hasFolding(curwin->w_cursor.lnum,
6257 &curwin->w_cursor.lnum, NULL);
6258 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006259 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 (void)hasFolding(curwin->w_cursor.lnum,
6261 NULL, &curwin->w_cursor.lnum);
6262#endif
6263 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6264 (cap->cmdchar == 'O' ? 1 : 0)),
6265 (linenr_T)(curwin->w_cursor.lnum +
6266 (cap->cmdchar == 'o' ? 1 : 0))
6267 ) == OK
6268 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006269 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006270 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006272#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006273 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006274 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006275#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006276#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006277 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006279 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006280#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006281 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006282 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6283 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6285 }
6286 }
6287}
6288
6289/*
6290 * "." command: redo last change.
6291 */
6292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006293nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 if (!checkclearopq(cap->oap))
6296 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006297 // If "restart_edit" is TRUE, the last but one command is repeated
6298 // instead of the last command (inserting text). This is used for
6299 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6301 clearopbeep(cap->oap);
6302 }
6303}
6304
6305/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006306 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
6308 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006309nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006311 if (VIsual_select && VIsual_active)
6312 {
6313 int reg;
6314 // Get register name
6315 ++no_mapping;
6316 ++allow_keys;
6317 reg = plain_vgetc();
6318 LANGMAP_ADJUST(reg, TRUE);
6319 --no_mapping;
6320 --allow_keys;
6321
6322 if (reg == '"')
6323 // the unnamed register is 0
6324 reg = 0;
6325
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006326 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006327 return;
6328 }
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (!checkclearopq(cap->oap))
6331 {
6332 u_redo((int)cap->count1);
6333 curwin->w_set_curswant = TRUE;
6334 }
6335}
6336
6337/*
6338 * Handle "U" command.
6339 */
6340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006341nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006344 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006346 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 cap->cmdchar = 'g';
6348 cap->nchar = 'U';
6349 nv_operator(cap);
6350 }
6351 else if (!checkclearopq(cap->oap))
6352 {
6353 u_undoline();
6354 curwin->w_set_curswant = TRUE;
6355 }
6356}
6357
6358/*
6359 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6360 * single character.
6361 */
6362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006363nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006365 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006366 {
6367#ifdef FEAT_JOB_CHANNEL
6368 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6369 {
6370 clearopbeep(cap->oap);
6371 return;
6372 }
6373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 else
6377 nv_operator(cap);
6378}
6379
6380/*
6381 * Handle an operator command.
6382 * The actual work is done by do_pending_operator().
6383 */
6384 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006385nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 int op_type;
6388
6389 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006390#ifdef FEAT_JOB_CHANNEL
6391 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6392 {
6393 clearopbeep(cap->oap);
6394 return;
6395 }
6396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006398 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 nv_lineop(cap);
6400 else if (!checkclearop(cap->oap))
6401 {
6402 cap->oap->start = curwin->w_cursor;
6403 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006404#ifdef FEAT_EVAL
6405 set_op_var(op_type);
6406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408}
6409
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006410#ifdef FEAT_EVAL
6411/*
6412 * Set v:operator to the characters for "optype".
6413 */
6414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006415set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006416{
6417 char_u opchars[3];
6418
6419 if (optype == OP_NOP)
6420 set_vim_var_string(VV_OP, NULL, 0);
6421 else
6422 {
6423 opchars[0] = get_op_char(optype);
6424 opchars[1] = get_extra_op_char(optype);
6425 opchars[2] = NUL;
6426 set_vim_var_string(VV_OP, opchars, -1);
6427 }
6428}
6429#endif
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431/*
6432 * Handle linewise operator "dd", "yy", etc.
6433 *
6434 * "_" is is a strange motion command that helps make operators more logical.
6435 * It is actually implemented, but not documented in the real Vi. This motion
6436 * command actually refers to "the current line". Commands like "dd" and "yy"
6437 * are really an alternate form of "d_" and "y_". It does accept a count, so
6438 * "d3_" works to delete 3 lines.
6439 */
6440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006441nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
6443 cap->oap->motion_type = MLINE;
6444 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6445 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006446 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006447 && cap->oap->motion_force != 'v'
6448 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 || cap->oap->op_type == OP_LSHIFT
6450 || cap->oap->op_type == OP_RSHIFT)
6451 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006452 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 beginline(BL_WHITE | BL_FIX);
6454}
6455
6456/*
6457 * <Home> command.
6458 */
6459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006460nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006462 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006463 if (mod_mask & MOD_MASK_CTRL)
6464 nv_goto(cap);
6465 else
6466 {
6467 cap->count0 = 1;
6468 nv_pipe(cap);
6469 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6471 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472}
6473
6474/*
6475 * "|" command.
6476 */
6477 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006478nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 cap->oap->motion_type = MCHAR;
6481 cap->oap->inclusive = FALSE;
6482 beginline(0);
6483 if (cap->count0 > 0)
6484 {
6485 coladvance((colnr_T)(cap->count0 - 1));
6486 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6487 }
6488 else
6489 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006490 // keep curswant at the column where we wanted to go, not where
6491 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 curwin->w_set_curswant = FALSE;
6493}
6494
6495/*
6496 * Handle back-word command "b" and "B".
6497 * cap->arg is 1 for "B"
6498 */
6499 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006500nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 cap->oap->motion_type = MCHAR;
6503 cap->oap->inclusive = FALSE;
6504 curwin->w_set_curswant = TRUE;
6505 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6506 clearopbeep(cap->oap);
6507#ifdef FEAT_FOLDING
6508 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6509 foldOpenCursor();
6510#endif
6511}
6512
6513/*
6514 * Handle word motion commands "e", "E", "w" and "W".
6515 * cap->arg is TRUE for "E" and "W".
6516 */
6517 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006518nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 int n;
6521 int word_end;
6522 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006523 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006525 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6527 word_end = TRUE;
6528 else
6529 word_end = FALSE;
6530 cap->oap->inclusive = word_end;
6531
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006532 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (!word_end && cap->oap->op_type == OP_CHANGE)
6534 {
6535 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006536 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006538 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006540 // Reproduce a funny Vi behaviour: "cw" on a blank only
6541 // changes one character, not all blanks until the start of
6542 // the next word. Only do this when the 'w' flag is included
6543 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6545 {
6546 cap->oap->inclusive = TRUE;
6547 cap->oap->motion_type = MCHAR;
6548 return;
6549 }
6550 }
6551 else
6552 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006553 // This is a little strange. To match what the real Vi does,
6554 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6555 // that we are not on a space or a TAB. This seems impolite
6556 // at first, but it's really more what we mean when we say
6557 // 'cw'.
6558 // Another strangeness: When standing on the end of a word
6559 // "ce" will change until the end of the next word, but "cw"
6560 // will change only one character! This is done by setting
6561 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 cap->oap->inclusive = TRUE;
6563 word_end = TRUE;
6564 flag = TRUE;
6565 }
6566 }
6567 }
6568
6569 cap->oap->motion_type = MCHAR;
6570 curwin->w_set_curswant = TRUE;
6571 if (word_end)
6572 n = end_word(cap->count1, cap->arg, flag, FALSE);
6573 else
6574 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006576 // Don't leave the cursor on the NUL past the end of line. Unless we
6577 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006578 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006579 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
6581 if (n == FAIL && cap->oap->op_type == OP_NOP)
6582 clearopbeep(cap->oap);
6583 else
6584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586#ifdef FEAT_FOLDING
6587 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6588 foldOpenCursor();
6589#endif
6590 }
6591}
6592
6593/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006594 * Used after a movement command: If the cursor ends up on the NUL after the
6595 * end of the line, may move it back to the last character and make the motion
6596 * inclusive.
6597 */
6598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006599adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006600{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 // The cursor cannot remain on the NUL when:
6602 // - the column is > 0
6603 // - not in Visual mode or 'selection' is "o"
6604 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006605 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006606 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006607 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006608 {
6609 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006610 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006611 if (has_mbyte)
6612 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006613 oap->inclusive = TRUE;
6614 }
6615}
6616
6617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 * "0" and "^" commands.
6619 * cap->arg is the argument for beginline().
6620 */
6621 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006622nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 cap->oap->motion_type = MCHAR;
6625 cap->oap->inclusive = FALSE;
6626 beginline(cap->arg);
6627#ifdef FEAT_FOLDING
6628 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6629 foldOpenCursor();
6630#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006631 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6632 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633}
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635/*
6636 * In exclusive Visual mode, may include the last character.
6637 */
6638 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006639adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006642 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (has_mbyte)
6645 inc_cursor();
6646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 ++curwin->w_cursor.col;
6648 cap->oap->inclusive = FALSE;
6649 }
6650}
6651
6652/*
6653 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6654 * Should check VIsual_mode before calling this.
6655 * Returns TRUE when backed up to the previous line.
6656 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006657 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006658unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 pos_T *pp;
6661
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006662 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006664 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 pp = &curwin->w_cursor;
6666 else
6667 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (pp->coladd > 0)
6669 --pp->coladd;
6670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (pp->col > 0)
6672 {
6673 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006674 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 }
6676 else if (pp->lnum > 1)
6677 {
6678 --pp->lnum;
6679 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6680 return TRUE;
6681 }
6682 }
6683 return FALSE;
6684}
6685
6686/*
6687 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6688 */
6689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006690nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006695 VIsual_select_reg = 0;
6696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 else if (VIsual_reselect)
6698 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006699 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 cap->arg = TRUE;
6701 nv_g_cmd(cap);
6702 }
6703}
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706/*
6707 * "G", "gg", CTRL-END, CTRL-HOME.
6708 * cap->arg is TRUE for "G".
6709 */
6710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006711nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 linenr_T lnum;
6714
6715 if (cap->arg)
6716 lnum = curbuf->b_ml.ml_line_count;
6717 else
6718 lnum = 1L;
6719 cap->oap->motion_type = MLINE;
6720 setpcmark();
6721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006722 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 if (cap->count0 != 0)
6724 lnum = cap->count0;
6725 if (lnum < 1L)
6726 lnum = 1L;
6727 else if (lnum > curbuf->b_ml.ml_line_count)
6728 lnum = curbuf->b_ml.ml_line_count;
6729 curwin->w_cursor.lnum = lnum;
6730 beginline(BL_SOL | BL_FIX);
6731#ifdef FEAT_FOLDING
6732 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6733 foldOpenCursor();
6734#endif
6735}
6736
6737/*
6738 * CTRL-\ in Normal mode.
6739 */
6740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006741nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6744 {
6745 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006746 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006747 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 restart_edit = 0;
6749#ifdef FEAT_CMDWIN
6750 if (cmdwin_type != 0)
6751 cmdwin_result = Ctrl_C;
6752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (VIsual_active)
6754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 redraw_curbuf_later(INVERTED);
6757 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006758 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 if (cap->nchar == Ctrl_G && p_im)
6760 restart_edit = 'a';
6761 }
6762 else
6763 clearopbeep(cap->oap);
6764}
6765
6766/*
6767 * ESC in Normal mode: beep, but don't flush buffers.
6768 * Don't even beep if we are canceling a command.
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 int no_reason;
6774
6775 no_reason = (cap->oap->op_type == OP_NOP
6776 && cap->opcount == 0
6777 && cap->count0 == 0
6778 && cap->oap->regname == 0
6779 && !p_im);
6780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006781 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 if (restart_edit == 0
6784#ifdef FEAT_CMDWIN
6785 && cmdwin_type == 0
6786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006789 {
6790 if (anyBufIsChanged())
6791 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6792 else
6793 msg(_("Type :qa and press <Enter> to exit Vim"));
6794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006796 if (restart_edit != 0)
6797 redraw_mode = TRUE; // remove "-- (insert) --"
6798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6800 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (!p_im)
6802 restart_edit = 0;
6803#ifdef FEAT_CMDWIN
6804 if (cmdwin_type != 0)
6805 {
6806 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006807 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 return;
6809 }
6810#endif
6811 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006812#ifdef FEAT_CMDWIN
6813 else if (cmdwin_type != 0 && ex_normal_busy)
6814 {
6815 // When :normal runs out of characters while in the command line window
6816 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6817 // loop.
6818 cmdwin_result = K_IGNORE;
6819 return;
6820 }
6821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (VIsual_active)
6824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006825 end_visual_mode(); // stop Visual
6826 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 curwin->w_set_curswant = TRUE;
6828 redraw_curbuf_later(INVERTED);
6829 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006830 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006831 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 clearop(cap->oap);
6833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006834 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6835 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006836 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 restart_edit = 'a';
6838}
6839
6840/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006841 * Move the cursor for the "A" command.
6842 */
6843 void
6844set_cursor_for_append_to_line(void)
6845{
6846 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006847 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006848 {
6849 int save_State = State;
6850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006851 // Pretend Insert mode here to allow the cursor on the
6852 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006853 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006854 coladvance((colnr_T)MAXCOL);
6855 State = save_State;
6856 }
6857 else
6858 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6859}
6860
6861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006863 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 */
6865 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006866nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6870 cap->cmdchar = 'i';
6871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006872 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006874 {
6875#ifdef FEAT_TERMINAL
6876 if (term_in_normal_mode())
6877 {
6878 end_visual_mode();
6879 clearop(cap->oap);
6880 term_enter_job_mode();
6881 return;
6882 }
6883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006887 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006888 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6889 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
6891#ifdef FEAT_TEXTOBJ
6892 nv_object(cap);
6893#else
6894 clearopbeep(cap->oap);
6895#endif
6896 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006897#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006898 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006899 {
6900 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006901 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006902 return;
6903 }
6904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 else if (!curbuf->b_p_ma && !p_im)
6906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006907 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006908 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006910 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006911 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006912 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006914 else if (cap->cmdchar == K_PS && VIsual_active)
6915 {
6916 pos_T old_pos = curwin->w_cursor;
6917 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006918 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006920 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006921 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6922 {
6923 shift_delete_registers();
6924 cap->oap->regname = '1';
6925 }
6926 else
6927 cap->oap->regname = '-';
6928 cap->cmdchar = 'd';
6929 cap->nchar = NUL;
6930 nv_operator(cap);
6931 do_pending_operator(cap, 0, FALSE);
6932 cap->cmdchar = K_PS;
6933
zeertzjq7a732522022-03-14 20:46:41 +00006934 if (*ml_get_cursor() != NUL)
6935 {
6936 if (old_visual_mode == 'V')
6937 {
6938 // In linewise Visual mode insert before the beginning of the
6939 // next line.
6940 // When the last line in the buffer was deleted then create a
6941 // new line, otherwise there is not need to move cursor.
6942 // Detect this by checking if cursor moved above Visual area.
6943 if (curwin->w_cursor.lnum < old_pos.lnum
6944 && curwin->w_cursor.lnum < old_visual.lnum)
6945 {
6946 if (u_save_cursor() == OK)
6947 {
6948 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6949 FALSE);
6950 appended_lines(curwin->w_cursor.lnum++, 1L);
6951 }
6952 }
6953 }
6954 // When the last char in the line was deleted then append.
6955 // Detect this by checking if cursor moved before Visual area.
6956 else if (curwin->w_cursor.col < old_pos.col
6957 && curwin->w_cursor.col < old_visual.col)
6958 inc_cursor();
6959 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006961 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006962 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 else if (!checkclearopq(cap->oap))
6965 {
6966 switch (cap->cmdchar)
6967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006969 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 break;
6971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006973 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6974 beginline(BL_WHITE);
6975 else
6976 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 break;
6978
Bram Moolenaara1891842017-02-04 21:34:31 +01006979 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 // Bracketed paste works like "a"ppend, unless the cursor is in
6981 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006982 if (curwin->w_cursor.col == 0)
6983 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006984 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006986 case 'a': // "a"ppend is like "i"nsert on the next character.
6987 // increment coladd when in virtual space, increment the
6988 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (virtual_active()
6990 && (curwin->w_cursor.coladd > 0
6991 || *ml_get_cursor() == NUL
6992 || *ml_get_cursor() == TAB))
6993 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006994 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 inc_cursor();
6996 break;
6997 }
6998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7000 {
7001 int save_State = State;
7002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 // Pretend Insert mode here to allow the cursor on the
7004 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007005 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 coladvance(getviscol());
7007 State = save_State;
7008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7011 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007012 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007013 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007014 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015}
7016
7017/*
7018 * Invoke edit() and take care of "restart_edit" and the return value.
7019 */
7020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007021invoke_edit(
7022 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007023 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007024 int cmd,
7025 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 int restart_edit_save = 0;
7028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007029 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7030 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7031 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 if (repl || !stuff_empty())
7033 restart_edit_save = restart_edit;
7034 else
7035 restart_edit_save = 0;
7036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007037 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 restart_edit = 0;
7039
7040 if (edit(cmd, startln, cap->count1))
7041 cap->retval |= CA_COMMAND_BUSY;
7042
7043 if (restart_edit == 0)
7044 restart_edit = restart_edit_save;
7045}
7046
7047#ifdef FEAT_TEXTOBJ
7048/*
7049 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7050 */
7051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007052nv_object(
7053 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055 int flag;
7056 int include;
7057 char_u *mps_save;
7058
7059 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 mps_save = curbuf->b_p_mps;
7066 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7067
7068 switch (cap->nchar)
7069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 flag = current_word(cap->oap, cap->count1, include, FALSE);
7072 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 flag = current_word(cap->oap, cap->count1, include, TRUE);
7075 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 case '(':
7078 case ')':
7079 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7080 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 case '{':
7083 case '}':
7084 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7085 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 case ']':
7088 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7089 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 case '>':
7092 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7093 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case 't': // "at" = a tag block (xml and html)
7095 // Do not adjust oap->end in do_pending_operator()
7096 // otherwise there are different results for 'dit'
7097 // (note leading whitespace in last line):
7098 // 1) <b> 2) <b>
7099 // foobar foobar
7100 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007101 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007102 flag = current_tagblock(cap->oap, cap->count1, include);
7103 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 flag = current_par(cap->oap, cap->count1, include, 'p');
7106 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007107 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 flag = current_sent(cap->oap, cap->count1, include);
7109 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 case '"': // "a"" = a double quoted string
7111 case '\'': // "a'" = a single quoted string
7112 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007113 flag = current_quote(cap->oap, cap->count1, include,
7114 cap->nchar);
7115 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007116#if 0 // TODO
7117 case 'S': // "aS" = a section
7118 case 'f': // "af" = a filename
7119 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120#endif
7121 default:
7122 flag = FAIL;
7123 break;
7124 }
7125
7126 curbuf->b_p_mps = mps_save;
7127 if (flag == FAIL)
7128 clearopbeep(cap->oap);
7129 adjust_cursor_col();
7130 curwin->w_set_curswant = TRUE;
7131}
7132#endif
7133
7134/*
7135 * "q" command: Start/stop recording.
7136 * "q:", "q/", "q?": edit command-line in command-line window.
7137 */
7138 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007139nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 if (cap->oap->op_type == OP_FORMAT)
7142 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007143 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 cap->cmdchar = 'g';
7145 cap->nchar = 'q';
7146 nv_operator(cap);
7147 }
7148 else if (!checkclearop(cap->oap))
7149 {
7150#ifdef FEAT_CMDWIN
7151 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7152 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007153 if (cmdwin_type != 0)
7154 {
7155 emsg(_(e_cmdline_window_already_open));
7156 return;
7157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 stuffcharReadbuff(cap->nchar);
7159 stuffcharReadbuff(K_CMDWIN);
7160 }
7161 else
7162#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 // (stop) recording into a named register, unless executing a
7164 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007165 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 clearopbeep(cap->oap);
7167 }
7168}
7169
7170/*
7171 * Handle the "@r" command.
7172 */
7173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007174nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 if (checkclearop(cap->oap))
7177 return;
7178#ifdef FEAT_EVAL
7179 if (cap->nchar == '=')
7180 {
7181 if (get_expr_register() == NUL)
7182 return;
7183 }
7184#endif
7185 while (cap->count1-- && !got_int)
7186 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007187 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
7189 clearopbeep(cap->oap);
7190 break;
7191 }
7192 line_breakcheck();
7193 }
7194}
7195
7196/*
7197 * Handle the CTRL-U and CTRL-D commands.
7198 */
7199 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007200nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7203 || (cap->cmdchar == Ctrl_D
7204 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7205 clearopbeep(cap->oap);
7206 else if (!checkclearop(cap->oap))
7207 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7208}
7209
7210/*
7211 * Handle "J" or "gJ" command.
7212 */
7213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007214nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007216 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007218 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7223 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007226 if (cap->count0 <= 2)
7227 {
7228 clearopbeep(cap->oap);
7229 return;
7230 }
7231 cap->count0 = curbuf->b_ml.ml_line_count
7232 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007234
7235 prep_redo(cap->oap->regname, cap->count0,
7236 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7237 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239}
7240
7241/*
7242 * "P", "gP", "p" and "gp" commands.
7243 */
7244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007245nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007247 nv_put_opt(cap, FALSE);
7248}
7249
7250/*
7251 * "P", "gP", "p" and "gp" commands.
7252 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7253 */
7254 static void
7255nv_put_opt(cmdarg_T *cap, int fix_indent)
7256{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 int regname = 0;
7258 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007259 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007260 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 int dir;
7262 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007263 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 if (cap->oap->op_type != OP_NOP)
7266 {
7267#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007268 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7270 {
7271 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007272 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 else
7275#endif
7276 clearopbeep(cap->oap);
7277 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007278#ifdef FEAT_JOB_CHANNEL
7279 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7280 {
7281 clearopbeep(cap->oap);
7282 }
7283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 else
7285 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007286 if (fix_indent)
7287 {
7288 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7289 ? FORWARD : BACKWARD;
7290 flags |= PUT_FIXINDENT;
7291 }
7292 else
7293 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007294 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7295 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 prep_redo_cmd(cap);
7297 if (cap->cmdchar == 'g')
7298 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007299 else if (cap->cmdchar == 'z')
7300 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (VIsual_active)
7303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007304 // Putting in Visual mode: The put text replaces the selected
7305 // text. First delete the selected text, then put the new text.
7306 // Need to save and restore the registers that the delete
7307 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007308 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007310 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007311#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007313#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007314 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007315 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007316#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 )
7321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 // The delete is going to overwrite the register we want to
7323 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 reg1 = get_register(regname, TRUE);
7325 }
7326
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007327 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 cap->cmdchar = 'd';
7329 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007330 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007331 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 nv_operator(cap);
7333 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007334 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007335 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007337 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 cap->oap->regname = regname;
7339
7340 if (reg1 != NULL)
7341 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 // Delete probably changed the register we want to put, save
7343 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 reg2 = get_register(regname, FALSE);
7345 put_register(regname, reg1);
7346 }
7347
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007348 // When deleted a linewise Visual area, put the register as
7349 // lines to avoid it joined with the next line. When deletion was
7350 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (VIsual_mode == 'V')
7352 flags |= PUT_LINE;
7353 else if (VIsual_mode == 'v')
7354 flags |= PUT_LINE_SPLIT;
7355 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7356 flags |= PUT_LINE_FORWARD;
7357 dir = BACKWARD;
7358 if ((VIsual_mode != 'V'
7359 && curwin->w_cursor.col < curbuf->b_op_start.col)
7360 || (VIsual_mode == 'V'
7361 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007362 // cursor is at the end of the line or end of file, put
7363 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007365 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007366 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007368 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007370 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (reg2 != NULL)
7372 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007374 // What to reselect with "gv"? Selecting the just put text seems to
7375 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 if (was_visual)
7377 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007378 curbuf->b_visual.vi_start = curbuf->b_op_start;
7379 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007380 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007381 if (*p_sel == 'e')
7382 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007383 }
7384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007385 // When all lines were selected and deleted do_put() leaves an empty
7386 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007387 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007388 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007389 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007390 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // If the cursor was in that line, move it to the end of the last
7393 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007394 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7395 {
7396 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7397 coladvance((colnr_T)MAXCOL);
7398 }
7399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 auto_format(FALSE, TRUE);
7401 }
7402}
7403
7404/*
7405 * "o" and "O" commands.
7406 */
7407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007408nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007411 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7413 {
7414 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007415 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 }
7417 else
7418#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007419 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007421#ifdef FEAT_JOB_CHANNEL
7422 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007423 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 n_opencmd(cap);
7427}
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#ifdef FEAT_NETBEANS_INTG
7430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007431nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 netbeans_keycommand(cap->nchar);
7434}
7435#endif
7436
7437#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007439nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007441 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442}
7443#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007444
Bram Moolenaar3918c952005-03-15 22:34:55 +00007445/*
7446 * Trigger CursorHold event.
7447 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7448 * input buffer. "did_cursorhold" is set to avoid retriggering.
7449 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007451nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007452{
7453 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7454 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007455 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007456}