blob: 8351685d1d4ef7393f999c8c7bbadc140be93880 [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 Moolenaar92b8b2d2016-01-29 22:36:45 +0100112static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void nv_record(cmdarg_T *cap);
114static void nv_at(cmdarg_T *cap);
115static void nv_halfpage(cmdarg_T *cap);
116static void nv_join(cmdarg_T *cap);
117static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200118static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#endif
123#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
ichizok672776d2022-01-31 12:27:18 +0000128// Declare nv_cmds[].
129#define DO_DECLARE_NVCMD
130#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000132// Include the lookuptable generated by create_nvcmdidx.vim.
133#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135/*
136 * Search for a command in the commands table.
137 * Returns -1 for invalid command.
138 */
139 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100140find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141{
142 int i;
143 int idx;
144 int top, bot;
145 int c;
146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100147 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 if (cmdchar >= 0x100)
149 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100151 // We use the absolute value of the character. Special keys have a
152 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 if (cmdchar < 0)
154 cmdchar = -cmdchar;
155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100156 // If the character is in the first part: The character is the index into
157 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 if (cmdchar <= nv_max_linear)
159 return nv_cmd_idx[cmdchar];
160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100161 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 bot = nv_max_linear + 1;
163 top = NV_CMDS_SIZE - 1;
164 idx = -1;
165 while (bot <= top)
166 {
167 i = (top + bot) / 2;
168 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
169 if (c < 0)
170 c = -c;
171 if (cmdchar == c)
172 {
173 idx = nv_cmd_idx[i];
174 break;
175 }
176 if (cmdchar > c)
177 bot = i + 1;
178 else
179 top = i - 1;
180 }
181 return idx;
182}
183
184/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100185 * If currently editing a cmdline or text is locked: beep and give an error
186 * message, return TRUE.
187 */
188 static int
189check_text_locked(oparg_T *oap)
190{
191 if (text_locked())
192 {
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
196 }
197 return FALSE;
198}
199
200/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000201 * Handle the count before a normal command and set cap->count0.
202 */
203 static int
204normal_cmd_get_count(
205 cmdarg_T *cap,
206 int c,
207 int toplevel UNUSED,
208 int set_prevcount UNUSED,
209 int *ctrl_w,
210 int *need_flushbuf UNUSED)
211{
212getcount:
213 if (!(VIsual_active && VIsual_select))
214 {
215 // Handle a count before a command and compute ca.count0.
216 // Note that '0' is a command and not the start of a count, but it's
217 // part of a count after other digits.
218 while ((c >= '1' && c <= '9')
219 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
220 || c == '0')))
221 {
222 if (c == K_DEL || c == K_KDEL)
223 {
224 cap->count0 /= 10;
225#ifdef FEAT_CMDL_INFO
226 del_from_showcmd(4); // delete the digit and ~@%
227#endif
228 }
229 else if (cap->count0 > 99999999L)
230 {
231 cap->count0 = 999999999L;
232 }
233 else
234 {
235 cap->count0 = cap->count0 * 10 + (c - '0');
236 }
237#ifdef FEAT_EVAL
238 // Set v:count here, when called from main() and not a stuffed
239 // command, so that v:count can be used in an expression mapping
240 // right after the count. Do set it for redo.
241 if (toplevel && readbuf1_empty())
242 set_vcount_ca(cap, &set_prevcount);
243#endif
244 if (*ctrl_w)
245 {
246 ++no_mapping;
247 ++allow_keys; // no mapping for nchar, but keys
248 }
249 ++no_zero_mapping; // don't map zero here
250 c = plain_vgetc();
251 LANGMAP_ADJUST(c, TRUE);
252 --no_zero_mapping;
253 if (*ctrl_w)
254 {
255 --no_mapping;
256 --allow_keys;
257 }
258#ifdef FEAT_CMDL_INFO
259 *need_flushbuf |= add_to_showcmd(c);
260#endif
261 }
262
263 // If we got CTRL-W there may be a/another count
264 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
265 {
266 *ctrl_w = TRUE;
267 cap->opcount = cap->count0; // remember first count
268 cap->count0 = 0;
269 ++no_mapping;
270 ++allow_keys; // no mapping for nchar, but keys
271 c = plain_vgetc(); // get next character
272 LANGMAP_ADJUST(c, TRUE);
273 --no_mapping;
274 --allow_keys;
275#ifdef FEAT_CMDL_INFO
276 *need_flushbuf |= add_to_showcmd(c);
277#endif
278 goto getcount; // jump back
279 }
280 }
281
282 if (c == K_CURSORHOLD)
283 {
284 // Save the count values so that ca.opcount and ca.count0 are exactly
285 // the same when coming back here after handling K_CURSORHOLD.
286 cap->oap->prev_opcount = cap->opcount;
287 cap->oap->prev_count0 = cap->count0;
288 }
289 else if (cap->opcount != 0)
290 {
291 // If we're in the middle of an operator (including after entering a
292 // yank buffer with '"') AND we had a count before the operator, then
293 // that count overrides the current value of ca.count0.
294 // What this means effectively, is that commands like "3dw" get turned
295 // into "d3w" which makes things fall into place pretty neatly.
296 // If you give a count before AND after the operator, they are
297 // multiplied.
298 if (cap->count0)
299 {
300 if (cap->opcount >= 999999999L / cap->count0)
301 cap->count0 = 999999999L;
302 else
303 cap->count0 *= cap->opcount;
304 }
305 else
306 cap->count0 = cap->opcount;
307 }
308
309 // Always remember the count. It will be set to zero (on the next call,
310 // above) when there is no pending operator.
311 // When called from main(), save the count for use by the "count" built-in
312 // variable.
313 cap->opcount = cap->count0;
314 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
315
316#ifdef FEAT_EVAL
317 // Only set v:count when called from main() and not a stuffed command.
318 // Do set it for redo.
319 if (toplevel && readbuf1_empty())
320 set_vcount(cap->count0, cap->count1, set_prevcount);
321#endif
322
323 return c;
324}
325
326/*
327 * Returns TRUE if the normal command (cap) needs a second character.
328 */
329 static int
330normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
331{
332 return ((cmd_flags & NV_NCH)
333 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
334 && cap->oap->op_type == OP_NOP)
335 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
336 || (cap->cmdchar == 'q'
337 && cap->oap->op_type == OP_NOP
338 && reg_recording == 0
339 && reg_executing == 0)
340 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
341 && (cap->oap->op_type != OP_NOP || VIsual_active))));
342}
343
344/*
345 * Get one or more additional characters for a normal command.
346 * Return the updated command index (if changed).
347 */
348 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000349normal_cmd_get_more_chars(
350 int idx_arg,
351 cmdarg_T *cap,
352 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000353{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000354 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000355 int c;
356 int *cp;
357 int repl = FALSE; // get character for replace mode
358 int lit = FALSE; // get extra character literally
359 int langmap_active = FALSE; // using :lmap mappings
360 int lang; // getting a text character
361#ifdef HAVE_INPUT_METHOD
362 int save_smd; // saved value of p_smd
363#endif
364
365 ++no_mapping;
366 ++allow_keys; // no mapping for nchar, but allow key codes
367 // Don't generate a CursorHold event here, most commands can't handle
368 // it, e.g., nv_replace(), nv_csearch().
369 did_cursorhold = TRUE;
370 if (cap->cmdchar == 'g')
371 {
372 /*
373 * For 'g' get the next character now, so that we can check for
374 * "gr", "g'" and "g`".
375 */
376 cap->nchar = plain_vgetc();
377 LANGMAP_ADJUST(cap->nchar, TRUE);
378#ifdef FEAT_CMDL_INFO
379 *need_flushbuf |= add_to_showcmd(cap->nchar);
380#endif
381 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
382 || cap->nchar == Ctrl_BSL)
383 {
384 cp = &cap->extra_char; // need to get a third character
385 if (cap->nchar != 'r')
386 lit = TRUE; // get it literally
387 else
388 repl = TRUE; // get it in replace mode
389 }
390 else
391 cp = NULL; // no third character needed
392 }
393 else
394 {
395 if (cap->cmdchar == 'r') // get it in replace mode
396 repl = TRUE;
397 cp = &cap->nchar;
398 }
399 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
400
401 /*
402 * Get a second or third character.
403 */
404 if (cp != NULL)
405 {
406 if (repl)
407 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100408 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000409#ifdef CURSOR_SHAPE
410 ui_cursor_shape(); // show different cursor shape
411#endif
412 }
413 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
414 {
415 // Allow mappings defined with ":lmap".
416 --no_mapping;
417 --allow_keys;
418 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100419 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000420 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100421 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000422 langmap_active = TRUE;
423 }
424#ifdef HAVE_INPUT_METHOD
425 save_smd = p_smd;
426 p_smd = FALSE; // Don't let the IM code show the mode here
427 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
428 im_set_active(TRUE);
429#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100432 MAY_WANT_TO_LOG_THIS;
433
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000434 // Disable bracketed paste and modifyOtherKeys here, we won't
435 // recognize the escape sequences with 'esckeys' off.
436 out_str(T_BD);
437 out_str(T_CTE);
438 }
439
440 *cp = plain_vgetc();
441
Bram Moolenaar24959102022-05-07 20:01:16 +0100442 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100444 MAY_WANT_TO_LOG_THIS;
445
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000446 // Re-enable bracketed paste mode and modifyOtherKeys
447 out_str(T_BE);
448 out_str(T_CTI);
449 }
450
451 if (langmap_active)
452 {
453 // Undo the decrement done above
454 ++no_mapping;
455 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100456 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000457 }
458#ifdef HAVE_INPUT_METHOD
459 if (lang)
460 {
461 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
462 im_save_status(&curbuf->b_p_iminsert);
463 im_set_active(FALSE);
464 }
465 p_smd = save_smd;
466#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100467 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000468#ifdef FEAT_CMDL_INFO
469 *need_flushbuf |= add_to_showcmd(*cp);
470#endif
471
472 if (!lit)
473 {
474#ifdef FEAT_DIGRAPHS
475 // Typing CTRL-K gets a digraph.
476 if (*cp == Ctrl_K
477 && ((nv_cmds[idx].cmd_flags & NV_LANG)
478 || cp == &cap->extra_char)
479 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
480 {
481 c = get_digraph(FALSE);
482 if (c > 0)
483 {
484 *cp = c;
485# ifdef FEAT_CMDL_INFO
486 // Guessing how to update showcmd here...
487 del_from_showcmd(3);
488 *need_flushbuf |= add_to_showcmd(*cp);
489# endif
490 }
491 }
492#endif
493
494 // adjust chars > 127, except after "tTfFr" commands
495 LANGMAP_ADJUST(*cp, !lang);
496#ifdef FEAT_RIGHTLEFT
497 // adjust Hebrew mapped char
498 if (p_hkmap && lang && KeyTyped)
499 *cp = hkmap(*cp);
500#endif
501 }
502
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000503 // When the next character is CTRL-\ a following CTRL-N means the
504 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000505 if (cp == &cap->extra_char
506 && cap->nchar == Ctrl_BSL
507 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
508 {
509 cap->cmdchar = Ctrl_BSL;
510 cap->nchar = cap->extra_char;
511 idx = find_command(cap->cmdchar);
512 }
513 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
514 cap->oap->op_type = get_op_type(*cp, NUL);
515 else if (*cp == Ctrl_BSL)
516 {
517 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
518
519 // There is a busy wait here when typing "f<C-\>" and then
520 // something different from CTRL-N. Can't be avoided.
521 while ((c = vpeekc()) <= 0 && towait > 0L)
522 {
523 do_sleep(towait > 50L ? 50L : towait, FALSE);
524 towait -= 50L;
525 }
526 if (c > 0)
527 {
528 c = plain_vgetc();
529 if (c != Ctrl_N && c != Ctrl_G)
530 vungetc(c);
531 else
532 {
533 cap->cmdchar = Ctrl_BSL;
534 cap->nchar = c;
535 idx = find_command(cap->cmdchar);
536 }
537 }
538 }
539
540 // When getting a text character and the next character is a
541 // multi-byte character, it could be a composing character.
542 // However, don't wait for it to arrive. Also, do enable mapping,
543 // because if it's put back with vungetc() it's too late to apply
544 // mapping.
545 --no_mapping;
546 while (enc_utf8 && lang && (c = vpeekc()) > 0
547 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
548 {
549 c = plain_vgetc();
550 if (!utf_iscomposing(c))
551 {
552 vungetc(c); // it wasn't, put it back
553 break;
554 }
555 else if (cap->ncharC1 == 0)
556 cap->ncharC1 = c;
557 else
558 cap->ncharC2 = c;
559 }
560 ++no_mapping;
561 }
562 --no_mapping;
563 --allow_keys;
564
565 return idx;
566}
567
568/*
569 * Returns TRUE if after processing a normal mode command, need to wait for a
570 * moment when a message is displayed that will be overwritten by the mode
571 * message.
572 */
573 static int
574normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
575{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000576 // In Visual mode and with "^O" in Insert mode, a short message will be
577 // overwritten by the mode message. Wait a bit, until a key is hit.
578 // In Visual mode, it's more important to keep the Visual area updated
579 // than keeping a message (e.g. from a /pat search).
580 // Only do this if the command was typed, not from a mapping.
581 // Don't wait when emsg_silent is non-zero.
582 // Also wait a bit after an error message, e.g. for "^O:".
583 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000584 return ( ((p_smd
585 && msg_silent == 0
586 && (restart_edit != 0
587 || (VIsual_active
588 && old_pos->lnum == curwin->w_cursor.lnum
589 && old_pos->col == curwin->w_cursor.col)
590 )
591 && (clear_cmdline
592 || redraw_cmdline)
593 && (msg_didout || (msg_didany && msg_scroll))
594 && !msg_nowait
595 && KeyTyped)
596 || (restart_edit != 0
597 && !VIsual_active
598 && (msg_scroll
599 || emsg_on_display)))
600 && cap->oap->regname == 0
601 && !(cap->retval & CA_COMMAND_BUSY)
602 && stuff_empty()
603 && typebuf_typed()
604 && emsg_silent == 0
605 && !in_assert_fails
606 && !did_wait_return
607 && cap->oap->op_type == OP_NOP);
608}
609
610/*
611 * After processing a normal mode command, wait for a moment when a message is
612 * displayed that will be overwritten by the mode message.
613 */
614 static void
615normal_cmd_wait_for_msg(void)
616{
617 int save_State = State;
618
619 // Draw the cursor with the right shape here
620 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100621 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000622
623 // If need to redraw, and there is a "keep_msg", redraw before the
624 // delay
625 if (must_redraw && keep_msg != NULL && !emsg_on_display)
626 {
627 char_u *kmsg;
628
629 kmsg = keep_msg;
630 keep_msg = NULL;
631 // Showmode() will clear keep_msg, but we want to use it anyway.
632 // First update w_topline.
633 setcursor();
634 update_screen(0);
635 // now reset it, otherwise it's put in the history again
636 keep_msg = kmsg;
637
638 kmsg = vim_strsave(keep_msg);
639 if (kmsg != NULL)
640 {
641 msg_attr((char *)kmsg, keep_msg_attr);
642 vim_free(kmsg);
643 }
644 }
645 setcursor();
646#ifdef CURSOR_SHAPE
647 ui_cursor_shape(); // may show different cursor shape
648#endif
649 cursor_on();
650 out_flush();
651 if (msg_scroll || emsg_on_display)
652 ui_delay(1003L, TRUE); // wait at least one second
653 ui_delay(3003L, FALSE); // wait up to three seconds
654 State = save_State;
655
656 msg_scroll = FALSE;
657 emsg_on_display = FALSE;
658}
659
660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 * Execute a command in Normal mode.
662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100664normal_cmd(
665 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000672 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100673 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 static int old_mapped_len = 0;
676 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000677 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200678 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaara80faa82020-04-12 19:37:17 +0200680 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Use a count remembered from before entering an operator. After typing
684 // "3d" we return from normal_cmd() and come back here, the "3" is
685 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ca.opcount = opcount;
687
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000688 // If there is an operator pending, then the command we take this time
689 // will terminate it. Finish_op tells us to finish the operation before
690 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#ifdef CURSOR_SHAPE
692 c = finish_op;
693#endif
694 finish_op = (oap->op_type != OP_NOP);
695#ifdef CURSOR_SHAPE
696 if (finish_op != c)
697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100698 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# ifdef FEAT_MOUSESHAPE
700 update_mouseshape(-1);
701# endif
702 }
703#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100704 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100706 // When not finishing an operator and no register name typed, reset the
707 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000711#ifdef FEAT_EVAL
712 set_prevcount = TRUE;
713#endif
714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100716 // Restore counts from before receiving K_CURSORHOLD. This means after
717 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
718 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000719 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
720 {
721 ca.opcount = oap->prev_opcount;
722 ca.count0 = oap->prev_count0;
723 oap->prev_opcount = 0;
724 oap->prev_count0 = 0;
725 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaar24959102022-05-07 20:01:16 +0100729 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100731 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100734#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // Set v:count here, when called from main() and not a stuffed
736 // command, so that v:count can be used in an expression mapping
737 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100738 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100739 set_vcount_ca(&ca, &set_prevcount);
740#endif
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 /*
743 * Get the command character from the user.
744 */
745 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100746 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000748 // If a mapping was started in Visual or Select mode, remember the length
749 // of the mapping. This is used below to not return to Insert mode for as
750 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (restart_edit == 0)
752 old_mapped_len = 0;
753 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000754 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 old_mapped_len = typebuf_maplen();
756
757 if (c == NUL)
758 c = K_ZERO;
759
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000760 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (VIsual_active
762 && VIsual_select
763 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
764 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000765 int len;
766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
768 // 'insertmode' is set) fake a "d"elete command, Insert mode will
769 // restart automatically.
770 // Insert the typed character in the typeahead buffer, so that it can
771 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000772 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
773
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000774 // When recording and gotchars() was called the character will be
775 // recorded again, remove the previous recording.
776 if (KeyTyped)
777 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000778
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000779 if (restart_edit != 0)
780 c = 'd';
781 else
782 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 msg_nowait = TRUE; // don't delay going to insert mode
784 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000787 // If the window was made so small that nothing shows, make it at least one
788 // line and one column when typing a command.
789 if (KeyTyped && !KeyStuffed)
790 win_ensure_size();
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#ifdef FEAT_CMDL_INFO
793 need_flushbuf = add_to_showcmd(c);
794#endif
795
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000796 // Get the command count
797 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
798 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000800 // Find the command character in the table of commands.
801 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (ctrl_w)
803 {
804 ca.nchar = c;
805 ca.cmdchar = Ctrl_W;
806 }
807 else
808 ca.cmdchar = c;
809 idx = find_command(ca.cmdchar);
810 if (idx < 0)
811 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100812 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 clearopbeep(oap);
814 goto normal_end;
815 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000816
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100817 if ((nv_cmds[idx].cmd_flags & NV_NCW)
818 && (check_text_locked(oap) || curbuf_locked()))
819 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000820 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000822 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (VIsual_active)
824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (km_stopsel
827 && (nv_cmds[idx].cmd_flags & NV_STS)
828 && !(mod_mask & MOD_MASK_SHIFT))
829 {
830 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100831 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (km_startsel)
836 {
837 if (nv_cmds[idx].cmd_flags & NV_SS)
838 {
839 unshift_special(&ca);
840 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000841 if (idx < 0)
842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100843 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 clearopbeep(oap);
845 goto normal_end;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
849 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854#ifdef FEAT_RIGHTLEFT
855 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
856 && (nv_cmds[idx].cmd_flags & NV_RL))
857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 // Invert horizontal movements and operations. Only when typed by the
859 // user directly, not when the result of a mapping or "x" translated
860 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 switch (ca.cmdchar)
862 {
863 case 'l': ca.cmdchar = 'h'; break;
864 case K_RIGHT: ca.cmdchar = K_LEFT; break;
865 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
866 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
867 case 'h': ca.cmdchar = 'l'; break;
868 case K_LEFT: ca.cmdchar = K_RIGHT; break;
869 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
870 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
871 case '>': ca.cmdchar = '<'; break;
872 case '<': ca.cmdchar = '>'; break;
873 }
874 idx = find_command(ca.cmdchar);
875 }
876#endif
877
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000878 // Get additional characters if we need them.
879 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
880 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000883 // Flush the showcmd characters onto the screen so we can see them while
884 // the command is being executed. Only do this when the shown command was
885 // actually displayed, otherwise this will slow down a lot when executing
886 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (need_flushbuf)
888 out_flush();
889#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000890 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200891 {
892 if (ex_normal_busy)
893 did_cursorhold = save_did_cursorhold;
894 else
895 did_cursorhold = FALSE;
896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaar24959102022-05-07 20:01:16 +0100898 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900 if (ca.nchar == ESC)
901 {
902 clearop(oap);
903 if (restart_edit == 0 && goto_im())
904 restart_edit = 'a';
905 goto normal_end;
906 }
907
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000908 if (ca.cmdchar != K_IGNORE)
909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000911 msg_col = 0;
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100914 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 // When 'keymodel' contains "startsel" some keys start Select/Visual
917 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (!VIsual_active && km_startsel)
919 {
920 if (nv_cmds[idx].cmd_flags & NV_SS)
921 {
922 start_selection();
923 unshift_special(&ca);
924 idx = find_command(ca.cmdchar);
925 }
926 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
927 && (mod_mask & MOD_MASK_SHIFT))
928 {
929 start_selection();
930 mod_mask &= ~MOD_MASK_SHIFT;
931 }
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000934 // Execute the command!
935 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 ca.arg = nv_cmds[idx].cmd_arg;
937 (nv_cmds[idx].cmd_func)(&ca);
938
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000939 // If we didn't start or finish an operator, reset oap->regname, unless we
940 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (!finish_op
942 && !oap->op_type
943 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
944 {
945 clearop(oap);
946#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200947 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949 }
950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100951 // Get the length of mapped chars again after typing a count, second
952 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000953 if (old_mapped_len > 0)
954 old_mapped_len = typebuf_maplen();
955
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000956 // If an operation is pending, handle it. But not for K_IGNORE or
957 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200958 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100959 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000961 // Wait for a moment when a message is displayed that will be overwritten
962 // by the mode message.
963 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
964 normal_cmd_wait_for_msg();
965
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000966 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967normal_end:
968
969 msg_nowait = FALSE;
970
Bram Moolenaarcc613032020-06-07 21:31:18 +0200971#ifdef FEAT_EVAL
972 if (finish_op)
973 reset_reg_var();
974#endif
975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#ifdef CURSOR_SHAPE
978 c = finish_op;
979#endif
980 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100981 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100983 // Redraw the cursor with another shape, if we were in Operator-pending
984 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (c || ca.cmdchar == 'r')
986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100987 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# ifdef FEAT_MOUSESHAPE
989 update_mouseshape(-1);
990# endif
991 }
992#endif
993
994#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000995 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100996 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 clear_showcmd();
998#endif
999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 vim_free(ca.searchbuf);
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (has_mbyte)
1004 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (curwin->w_p_scb && toplevel)
1007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001008 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 do_check_scrollbind(TRUE);
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar860cae12010-06-05 23:22:07 +02001012 if (curwin->w_p_crb && toplevel)
1013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001015 do_check_cursorbind();
1016 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001018#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001020 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001021 restart_edit = 0;
1022#endif
1023
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001024 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1025 // if still inside a mapping that started in Visual mode).
1026 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1029 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !(ca.retval & CA_COMMAND_BUSY)
1031 && stuff_empty()
1032 && oap->regname == 0)
1033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_VIsual_select == 1)
1035 {
1036 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001037 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 showmode();
1039 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001040 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001042 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)edit(restart_edit, FALSE, 1L);
1044 }
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (restart_VIsual_select == 2)
1047 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 opcount = ca.opcount;
1051}
1052
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053#ifdef FEAT_EVAL
1054/*
1055 * Set v:count and v:count1 according to "cap".
1056 * Set v:prevcount only when "set_prevcount" is TRUE.
1057 */
1058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001060{
1061 long count = cap->count0;
1062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064 if (cap->opcount != 0)
1065 count = cap->opcount * (count == 0 ? 1 : count);
1066 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068}
1069#endif
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001072 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * if not.
1074 */
1075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 static int did_check = FALSE;
1079
1080 if (full_screen)
1081 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001082 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 did_check = TRUE;
1085 }
1086}
1087
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001088#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1089/*
1090 * Call yank_do_autocmd() for "regname".
1091 */
1092 static void
1093call_yank_do_autocmd(int regname)
1094{
1095 oparg_T oa;
1096 yankreg_T *reg;
1097
1098 clear_oparg(&oa);
1099 oa.regname = regname;
1100 oa.op_type = OP_YANK;
1101 oa.is_VIsual = TRUE;
1102 reg = get_register(regname, TRUE);
1103 yank_do_autocmd(&oa, reg);
1104 free_register(reg);
1105}
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001109 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001110 * This function or the next should ALWAYS be called to end Visual mode, except
1111 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114end_visual_mode()
1115{
1116 end_visual_mode_keep_button();
1117 reset_held_button();
1118}
1119
1120 void
1121end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001124 // If we are using the clipboard, then remember what was selected in case
1125 // we need to paste it somewhere while we still own the selection.
1126 // Only do this when the clipboard is already owned. Don't want to grab
1127 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (clip_star.available && clip_star.owned)
1129 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001130
1131# if defined(FEAT_EVAL)
1132 // Emit a TextYankPost for the automatic copy of the selection into the
1133 // star and/or plus register.
1134 if (has_textyankpost())
1135 {
1136 if (clip_isautosel_star())
1137 call_yank_do_autocmd('*');
1138 if (clip_isautosel_plus())
1139 call_yank_do_autocmd('+');
1140 }
1141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 setmouse();
1146 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001149 curbuf->b_visual.vi_mode = VIsual_mode;
1150 curbuf->b_visual.vi_start = VIsual;
1151 curbuf->b_visual.vi_end = curwin->w_cursor;
1152 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153#ifdef FEAT_EVAL
1154 curbuf->b_visual_mode_eval = VIsual_mode;
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!virtual_active())
1157 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001158 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001160 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001161 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162}
1163
1164/*
1165 * Reset VIsual_active and VIsual_reselect.
1166 */
1167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001168reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 if (VIsual_active)
1171 {
1172 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001173 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 VIsual_reselect = FALSE;
1176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect if it's set.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 VIsual_reselect = FALSE;
1189 }
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001192 void
1193restore_visual_mode(void)
1194{
1195 if (VIsual_mode_orig != NUL)
1196 {
1197 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1198 VIsual_mode_orig = NUL;
1199 }
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Check for a balloon-eval special item to include when searching for an
1204 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1205 * Returns TRUE if the character at "*ptr" should be included.
1206 * "dir" is FORWARD or BACKWARD, the direction of searching.
1207 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1208 * "bnp" points to a counter for square brackets.
1209 */
1210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001211find_is_eval_item(
1212 char_u *ptr,
1213 int *colp,
1214 int *bnp,
1215 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1219 ++*bnp;
1220 if (*bnp > 0)
1221 {
1222 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1223 --*bnp;
1224 return TRUE;
1225 }
1226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (*ptr == '.')
1229 return TRUE;
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1233 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1234 {
1235 *colp += dir;
1236 return TRUE;
1237 }
1238 return FALSE;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241/*
1242 * Find the identifier under or to the right of the cursor.
1243 * "find_type" can have one of three values:
1244 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * FIND_STRING: find any non-white text
1246 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001247 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *
1249 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001250 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * This doesn't match the real Vi but I like it a little better and it
1254 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001255 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * When FIND_IDENT isn't defined, we backup until a blank.
1257 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001258 * Returns the length of the text, or zero if no text is found.
1259 * If text is found, a pointer to the text is put in "*text". This
1260 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
1262 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001263find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267}
1268
1269/*
1270 * Like find_ident_under_cursor(), but for any window and any position.
1271 * However: Uses 'iskeyword' from the current window!.
1272 */
1273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274find_ident_at_pos(
1275 win_T *wp,
1276 linenr_T lnum,
1277 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001278 char_u **text,
1279 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001280 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001283 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int this_class = 0;
1286 int prev_class;
1287 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001288 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001290 // if i == 0: try to find an identifier
1291 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1293 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1294 {
1295 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (has_mbyte)
1300 {
1301 while (ptr[col] != NUL)
1302 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001303 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 this_class = mb_get_class(ptr + col);
1307 if (this_class != 0 && (i == 1 || this_class != 1))
1308 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 )
1317 ++col;
1318
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001319 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (has_mbyte)
1326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1329 this_class = mb_get_class((char_u *)"a");
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001332 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1335 prev_class = mb_get_class(ptr + prevcol);
1336 if (this_class != prev_class
1337 && (i == 0
1338 || prev_class == 0
1339 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && (!(find_type & FIND_EVAL)
1341 || prevcol == 0
1342 || !find_is_eval_item(ptr + prevcol, &prevcol,
1343 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 )
1345 break;
1346 col = prevcol;
1347 }
1348
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001349 // If we don't want just any old text, or we've found an
1350 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (this_class > 2)
1352 this_class = 2;
1353 if (!(find_type & FIND_STRING) || this_class == 2)
1354 break;
1355 }
1356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 while (col > 0
1359 && ((i == 0
1360 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001361 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 && (!(find_type & FIND_IDENT)
1363 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || ((find_type & FIND_EVAL)
1365 && col > 1
1366 && find_is_eval_item(ptr + col - 1, &col,
1367 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ))
1369 --col;
1370
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001371 // If we don't want just any old text, or we've found an
1372 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1374 break;
1375 }
1376 }
1377
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001378 if (ptr[col] == NUL || (i == 0
1379 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001381 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001382 if ((find_type & FIND_NOERROR) == 0)
1383 {
1384 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001385 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return 0;
1390 }
1391 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001392 *text = ptr;
1393 if (textcol != NULL)
1394 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 bn = 0;
1400 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (has_mbyte)
1403 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001404 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 this_class = mb_get_class(ptr);
1406 while (ptr[col] != NUL
1407 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1408 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || ((find_type & FIND_EVAL)
1410 && col <= (int)startcol
1411 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001413 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001417 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 || ((find_type & FIND_EVAL)
1419 && col <= (int)startcol
1420 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 return col;
1425}
1426
1427/*
1428 * Prepare for redo of a normal command.
1429 */
1430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001431prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 prep_redo(cap->oap->regname, cap->count0,
1434 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1435}
1436
1437/*
1438 * Prepare for redo of any command.
1439 * Note that only the last argument can be a multi-byte char.
1440 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001442prep_redo(
1443 int regname,
1444 long num,
1445 int cmd1,
1446 int cmd2,
1447 int cmd3,
1448 int cmd4,
1449 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001451 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1452}
1453
1454/*
1455 * Prepare for redo of any command with extra count after "cmd2".
1456 */
1457 void
1458prep_redo_num2(
1459 int regname,
1460 long num1,
1461 int cmd1,
1462 int cmd2,
1463 long num2,
1464 int cmd3,
1465 int cmd4,
1466 int cmd5)
1467{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001469
1470#ifdef FEAT_EVAL
1471 // Put info about a mapping in the redo buffer, so that "." will use the
1472 // same script context.
1473 may_add_last_used_map_to_redobuff();
1474#endif
1475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
1478 AppendCharToRedobuff('"');
1479 AppendCharToRedobuff(regname);
1480 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001481 if (num1 != 0)
1482 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (cmd1 != NUL)
1484 AppendCharToRedobuff(cmd1);
1485 if (cmd2 != NUL)
1486 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001487 if (num2 != 0)
1488 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (cmd3 != NUL)
1490 AppendCharToRedobuff(cmd3);
1491 if (cmd4 != NUL)
1492 AppendCharToRedobuff(cmd4);
1493 if (cmd5 != NUL)
1494 AppendCharToRedobuff(cmd5);
1495}
1496
1497/*
1498 * check for operator active and clear it
1499 *
1500 * return TRUE if operator was active
1501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 if (oap->op_type == OP_NOP)
1506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
1511/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001512 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001514 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001519 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001526clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 oap->op_type = OP_NOP;
1529 oap->regname = 0;
1530 oap->motion_force = NUL;
1531 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001532 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 clearop(oap);
1539 beep_flush();
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
1543 * Remove the shift modifier from a special key.
1544 */
1545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 switch (cap->cmdchar)
1549 {
1550 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1551 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1552 case K_S_UP: cap->cmdchar = K_UP; break;
1553 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1554 case K_S_HOME: cap->cmdchar = K_HOME; break;
1555 case K_S_END: cap->cmdchar = K_END; break;
1556 }
1557 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001560/*
1561 * If the mode is currently displayed clear the command line or update the
1562 * command displayed.
1563 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566{
1567 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001569#ifdef FEAT_CMDL_INFO
1570 else
1571 clear_showcmd();
1572#endif
1573}
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1576/*
1577 * Routines for displaying a partly typed command
1578 */
1579
kylo252ae6f1d82022-02-16 19:24:07 +00001580#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001582static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583static int showcmd_is_clear = TRUE;
1584static int showcmd_visual = FALSE;
1585
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001586static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001589clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
1591 if (!p_sc)
1592 return;
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (VIsual_active && !char_avail())
1595 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001596 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 long lines;
1598 colnr_T leftcol, rightcol;
1599 linenr_T top, bot;
1600
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001601 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001602 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 top = VIsual.lnum;
1605 bot = curwin->w_cursor.lnum;
1606 }
1607 else
1608 {
1609 top = curwin->w_cursor.lnum;
1610 bot = VIsual.lnum;
1611 }
1612# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001613 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001614 (void)hasFolding(top, &top, NULL);
1615 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616# endif
1617 lines = bot - top + 1;
1618
1619 if (VIsual_mode == Ctrl_V)
1620 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001621# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001622 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001623 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001625 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001626 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001627 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001631 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001632 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1635 (long)(rightcol - leftcol + 1));
1636 }
1637 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1638 sprintf((char *)showcmd_buf, "%ld", lines);
1639 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001640 {
1641 char_u *s, *e;
1642 int l;
1643 int bytes = 0;
1644 int chars = 0;
1645
1646 if (cursor_bot)
1647 {
1648 s = ml_get_pos(&VIsual);
1649 e = ml_get_cursor();
1650 }
1651 else
1652 {
1653 s = ml_get_cursor();
1654 e = ml_get_pos(&VIsual);
1655 }
1656 while ((*p_sel != 'e') ? s <= e : s < e)
1657 {
1658 l = (*mb_ptr2len)(s);
1659 if (l == 0)
1660 {
1661 ++bytes;
1662 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001663 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001664 }
1665 bytes += l;
1666 ++chars;
1667 s += l;
1668 }
1669 if (bytes == chars)
1670 sprintf((char *)showcmd_buf, "%d", chars);
1671 else
1672 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1673 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001674 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 showcmd_visual = TRUE;
1676 }
1677 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 showcmd_buf[0] = NUL;
1680 showcmd_visual = FALSE;
1681
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001682 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (showcmd_is_clear)
1684 return;
1685 }
1686
1687 display_showcmd();
1688}
1689
1690/*
1691 * Add 'c' to string of shown command chars.
1692 * Return TRUE if output has been written (and setcursor() has been called).
1693 */
1694 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001695add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 char_u *p;
1698 int old_len;
1699 int extra_len;
1700 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 int i;
1702 static int ignore[] =
1703 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001704#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1706 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001707#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001708 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001709 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1711 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001712 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001714 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 0
1716 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar09df3122006-01-23 22:23:09 +00001718 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return FALSE;
1720
1721 if (showcmd_visual)
1722 {
1723 showcmd_buf[0] = NUL;
1724 showcmd_visual = FALSE;
1725 }
1726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001727 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (IS_SPECIAL(c))
1729 for (i = 0; ignore[i] != 0; ++i)
1730 if (ignore[i] == c)
1731 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001734 if (*p == ' ')
1735 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 old_len = (int)STRLEN(showcmd_buf);
1737 extra_len = (int)STRLEN(p);
1738 overflow = old_len + extra_len - SHOWCMD_COLS;
1739 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001740 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1741 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 STRCAT(showcmd_buf, p);
1743
1744 if (char_avail())
1745 return FALSE;
1746
1747 display_showcmd();
1748
1749 return TRUE;
1750}
1751
1752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 if (!add_to_showcmd(c))
1756 setcursor();
1757}
1758
1759/*
1760 * Delete 'len' characters from the end of the shown command.
1761 */
1762 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001763del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 int old_len;
1766
1767 if (!p_sc)
1768 return;
1769
1770 old_len = (int)STRLEN(showcmd_buf);
1771 if (len > old_len)
1772 len = old_len;
1773 showcmd_buf[old_len - len] = NUL;
1774
1775 if (!char_avail())
1776 display_showcmd();
1777}
1778
1779/*
1780 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1781 * something and there is a partial mapping.
1782 */
1783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 if (p_sc)
1787 STRCPY(old_showcmd_buf, showcmd_buf);
1788}
1789
1790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001791pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 if (!p_sc)
1794 return;
1795
1796 STRCPY(showcmd_buf, old_showcmd_buf);
1797
1798 display_showcmd();
1799}
1800
1801 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001802display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 int len;
1805
1806 cursor_off();
1807
1808 len = (int)STRLEN(showcmd_buf);
1809 if (len == 0)
1810 showcmd_is_clear = TRUE;
1811 else
1812 {
1813 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1814 showcmd_is_clear = FALSE;
1815 }
1816
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001817 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1818 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001821 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822}
1823#endif
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825/*
1826 * When "check" is FALSE, prepare for commands that scroll the window.
1827 * When "check" is TRUE, take care of scroll-binding after the window has
1828 * scrolled. Called from normal_cmd() and edit().
1829 */
1830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001831do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
1833 static win_T *old_curwin = NULL;
1834 static linenr_T old_topline = 0;
1835#ifdef FEAT_DIFF
1836 static int old_topfill = 0;
1837#endif
1838 static buf_T *old_buf = NULL;
1839 static colnr_T old_leftcol = 0;
1840
1841 if (check && curwin->w_p_scb)
1842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001843 // If a ":syncbind" command was just used, don't scroll, only reset
1844 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (did_syncbind)
1846 did_syncbind = FALSE;
1847 else if (curwin == old_curwin)
1848 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001849 // Synchronize other windows, as necessary according to
1850 // 'scrollbind'. Don't do this after an ":edit" command, except
1851 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if ((curwin->w_buffer == old_buf
1853#ifdef FEAT_DIFF
1854 || curwin->w_p_diff
1855#endif
1856 )
1857 && (curwin->w_topline != old_topline
1858#ifdef FEAT_DIFF
1859 || curwin->w_topfill != old_topfill
1860#endif
1861 || curwin->w_leftcol != old_leftcol))
1862 {
1863 check_scrollbind(curwin->w_topline - old_topline,
1864 (long)(curwin->w_leftcol - old_leftcol));
1865 }
1866 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001869 // When switching between windows, make sure that the relative
1870 // vertical offset is valid for the new window. The relative
1871 // offset is invalid whenever another 'scrollbind' window has
1872 // scrolled to a point that would force the current window to
1873 // scroll past the beginning or end of its buffer. When the
1874 // resync is performed, some of the other 'scrollbind' windows may
1875 // need to jump so that the current window's relative position is
1876 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1878 }
1879 curwin->w_scbind_pos = curwin->w_topline;
1880 }
1881
1882 old_curwin = curwin;
1883 old_topline = curwin->w_topline;
1884#ifdef FEAT_DIFF
1885 old_topfill = curwin->w_topfill;
1886#endif
1887 old_buf = curwin->w_buffer;
1888 old_leftcol = curwin->w_leftcol;
1889}
1890
1891/*
1892 * Synchronize any windows that have "scrollbind" set, based on the
1893 * number of rows by which the current window has changed
1894 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1895 */
1896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001897check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int want_ver;
1900 int want_hor;
1901 win_T *old_curwin = curwin;
1902 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int old_VIsual_select = VIsual_select;
1904 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 colnr_T tgt_leftcol = curwin->w_leftcol;
1906 long topline;
1907 long y;
1908
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001909 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1911#ifdef FEAT_DIFF
1912 want_ver |= old_curwin->w_p_diff;
1913#endif
1914 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1915
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001916 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001918 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001921 // skip original window and windows with 'noscrollbind'
1922 if (curwin == old_curwin || !curwin->w_p_scb)
1923 continue;
1924
1925 // do the vertical scroll
1926 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001929 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
zeertzjq101d57b2022-07-31 18:34:32 +01001931 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
zeertzjq101d57b2022-07-31 18:34:32 +01001933 else
1934#endif
1935 {
1936 curwin->w_scbind_pos += topline_diff;
1937 topline = curwin->w_scbind_pos;
1938 if (topline > curbuf->b_ml.ml_line_count)
1939 topline = curbuf->b_ml.ml_line_count;
1940 if (topline < 1)
1941 topline = 1;
1942
1943 y = topline - curwin->w_topline;
1944 if (y > 0)
1945 scrollup(y, FALSE);
1946 else
1947 scrolldown(-y, FALSE);
1948 }
1949
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001950 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001951 cursor_correct();
1952 curwin->w_redr_status = TRUE;
1953 }
1954
1955 // do the horizontal scroll
1956 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1957 {
1958 curwin->w_leftcol = tgt_leftcol;
1959 leftcol_changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 }
1962
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001963 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 VIsual_select = old_VIsual_select;
1965 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 curwin = old_curwin;
1967 curbuf = old_curbuf;
1968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970/*
1971 * Command character that's ignored.
1972 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001973 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001978 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979}
1980
1981/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001982 * Command character that doesn't do anything, but unlike nv_ignore() does
1983 * start edit(). Used for "startinsert" executed while starting up.
1984 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001986nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001987{
1988}
1989
1990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 * Command character doesn't exist.
1992 */
1993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 clearopbeep(cap->oap);
1997}
1998
1999/*
2000 * <Help> and <F1> commands.
2001 */
2002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002003nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 if (!checkclearopq(cap->oap))
2006 ex_help(NULL);
2007}
2008
2009/*
2010 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2011 */
2012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002013nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002015#ifdef FEAT_JOB_CHANNEL
2016 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2017 clearopbeep(cap->oap);
2018 else
2019#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002020 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002021 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002022 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002023 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2024 op_addsub(cap->oap, cap->count1, cap->arg);
2025 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002026 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002027 else if (VIsual_active)
2028 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002029 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002030 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031}
2032
2033/*
2034 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2035 */
2036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002040 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 if (mod_mask & MOD_MASK_CTRL)
2042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 if (cap->arg == BACKWARD)
2045 goto_tabpage(-(int)cap->count1);
2046 else
2047 goto_tabpage((int)cap->count0);
2048 }
2049 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002050 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
2054/*
2055 * Implementation of "gd" and "gD" command.
2056 */
2057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002058nv_gd(
2059 oparg_T *oap,
2060 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 char_u *ptr;
2065
Bram Moolenaard9d30582005-05-18 22:10:28 +00002066 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002067 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002068 == FAIL)
2069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002071 }
2072 else
2073 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002074#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002075 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2076 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002077#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002078 // clear any search statistics
2079 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2080 clear_cmdline = TRUE;
2081 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002082}
2083
2084/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002085 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2086 * otherwise.
2087 */
2088 static int
2089is_ident(char_u *line, int offset)
2090{
2091 int i;
2092 int incomment = FALSE;
2093 int instring = 0;
2094 int prev = 0;
2095
2096 for (i = 0; i < offset && line[i] != NUL; i++)
2097 {
2098 if (instring != 0)
2099 {
2100 if (prev != '\\' && line[i] == instring)
2101 instring = 0;
2102 }
2103 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2104 {
2105 instring = line[i];
2106 }
2107 else
2108 {
2109 if (incomment)
2110 {
2111 if (prev == '*' && line[i] == '/')
2112 incomment = FALSE;
2113 }
2114 else if (prev == '/' && line[i] == '*')
2115 {
2116 incomment = TRUE;
2117 }
2118 else if (prev == '/' && line[i] == '/')
2119 {
2120 return FALSE;
2121 }
2122 }
2123
2124 prev = line[i];
2125 }
2126
2127 return incomment == FALSE && instring == 0;
2128}
2129
2130/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002131 * Search for variable declaration of "ptr[len]".
2132 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2133 * current file ("gD").
2134 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002135 * Return FAIL when not found.
2136 */
2137 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002138find_decl(
2139 char_u *ptr,
2140 int len,
2141 int locally,
2142 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002143 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002144{
2145 char_u *pat;
2146 pos_T old_pos;
2147 pos_T par_pos;
2148 pos_T found_pos;
2149 int t;
2150 int save_p_ws;
2151 int save_p_scs;
2152 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002153 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002154 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002155 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002156
2157 if ((pat = alloc(len + 7)) == NULL)
2158 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002160 // Put "\V" before the pattern to avoid that the special meaning of "."
2161 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002162 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2163 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 old_pos = curwin->w_cursor;
2165 save_p_ws = p_ws;
2166 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002167 p_ws = FALSE; // don't wrap around end of file now
2168 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002170 // With "gD" go to line 1.
2171 // With "gd" Search back for the start of the current function, then go
2172 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002173 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002175 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002177 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179 else
2180 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002181 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2183 --curwin->w_cursor.lnum;
2184 }
2185 curwin->w_cursor.col = 0;
2186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002188 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002189 for (;;)
2190 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002191 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002192 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002193 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002196 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002197 {
2198 pos_T *pos;
2199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // Check that the block the match is in doesn't end before the
2201 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002202 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2203 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2204 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002205 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 // There can't be a useful match before the end of this block.
2207 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002208 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002209 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002210 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002211 }
2212
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002213 if (t == FAIL)
2214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002215 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002216 if (found_pos.lnum != 0)
2217 {
2218 curwin->w_cursor = found_pos;
2219 t = OK;
2220 }
2221 break;
2222 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002223 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002226 ++curwin->w_cursor.lnum;
2227 curwin->w_cursor.col = 0;
2228 continue;
2229 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002230 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // If the current position is not a valid identifier and a previous
2233 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 if (!valid && found_pos.lnum != 0)
2235 {
2236 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002238 }
2239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002240 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002241 if (valid && !locally)
2242 break;
2243 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002246 if (found_pos.lnum != 0)
2247 curwin->w_cursor = found_pos;
2248 break;
2249 }
2250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // For finding a local variable and the match is before the "{" or
2252 // inside a comment, continue searching. For K&R style function
2253 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002254 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002255 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002256 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002257 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 // Remove SEARCH_START from flags to avoid getting stuck at one
2259 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002260 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002262
2263 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002265 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curwin->w_cursor = old_pos;
2267 }
2268 else
2269 {
2270 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 reset_search_dir();
2273 }
2274
2275 vim_free(pat);
2276 p_ws = save_p_ws;
2277 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002278
2279 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280}
2281
2282/*
2283 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2284 * lines rather than lines in the file.
2285 * 'dist' must be positive.
2286 *
2287 * Return OK if able to move cursor, FAIL otherwise.
2288 */
2289 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002290nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 int linelen = linetabsize(ml_get_curline());
2293 int retval = OK;
2294 int atend = FALSE;
2295 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002296 int col_off1; // margin offset for first screen line
2297 int col_off2; // margin offset for wrapped screen line
2298 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002299 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002302 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 col_off1 = curwin_col_off();
2305 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002306 width1 = curwin->w_width - col_off1;
2307 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002308 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002309 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002312 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002313 // Instead of sticking at the last character of the buffer line we
2314 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (curwin->w_curswant == MAXCOL)
2316 {
2317 atend = TRUE;
2318 validate_virtcol();
2319 if (width1 <= 0)
2320 curwin->w_curswant = 0;
2321 else
2322 {
2323 curwin->w_curswant = width1 - 1;
2324 if (curwin->w_virtcol > curwin->w_curswant)
2325 curwin->w_curswant += ((curwin->w_virtcol
2326 - curwin->w_curswant - 1) / width2 + 1) * width2;
2327 }
2328 }
2329 else
2330 {
2331 if (linelen > width1)
2332 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2333 else
2334 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002335 if (curwin->w_curswant >= (colnr_T)n)
2336 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338
2339 while (dist--)
2340 {
2341 if (dir == BACKWARD)
2342 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002343 if ((long)curwin->w_curswant >= width1
2344#ifdef FEAT_FOLDING
2345 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2346#endif
2347 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002348 // Move back within the line. This can give a negative value
2349 // for w_curswant if width1 < width2 (with cpoptions+=n),
2350 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 curwin->w_curswant -= width2;
2352 else
2353 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002354 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002356 // Move to the start of a closed fold. Don't do that when
2357 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (!(fdo_flags & FDO_ALL))
2359 (void)hasFolding(curwin->w_cursor.lnum,
2360 &curwin->w_cursor.lnum, NULL);
2361#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002362 if (curwin->w_cursor.lnum == 1)
2363 {
2364 retval = FAIL;
2365 break;
2366 }
2367 --curwin->w_cursor.lnum;
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 linelen = linetabsize(ml_get_curline());
2370 if (linelen > width1)
2371 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2372 + 1) * width2;
2373 }
2374 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 if (linelen > width1)
2378 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2379 else
2380 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002381 if (curwin->w_curswant + width2 < (colnr_T)n
2382#ifdef FEAT_FOLDING
2383 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2384#endif
2385 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 curwin->w_curswant += width2;
2388 else
2389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002390 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002392 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2394 &curwin->w_cursor.lnum);
2395#endif
2396 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2397 {
2398 retval = FAIL;
2399 break;
2400 }
2401 curwin->w_cursor.lnum++;
2402 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002403 // Check if the cursor has moved below the number display
2404 // when width1 < width2 (with cpoptions+=n). Subtract width2
2405 // to get a negative value for w_curswant, which will get
2406 // clipped to column 0.
2407 if (curwin->w_curswant >= width1)
2408 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002409 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411 }
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002415 if (virtual_active() && atend)
2416 coladvance(MAXCOL);
2417 else
2418 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2421 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002422 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002423 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002424
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002425 // Check for landing on a character that got split at the end of the
2426 // last line. We want to advance a screenline, not end up in the same
2427 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002429 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002430#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002431 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2432 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002433#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002434
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002435 c = (*mb_ptr2char)(ml_get_cursor());
2436 if (dir == FORWARD && virtcol < curwin->w_curswant
2437 && (curwin->w_curswant <= (colnr_T)width1)
2438 && !vim_isprintc(c) && c > 255)
2439 oneright();
2440
Bram Moolenaar773b1582014-08-29 14:20:51 +02002441 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 && (curwin->w_curswant < (colnr_T)width1
2443 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2444 : ((curwin->w_curswant - width1) % width2
2445 > (colnr_T)width2 / 2)))
2446 --curwin->w_cursor.col;
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002450 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 return retval;
2453}
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455/*
2456 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2457 * cap->arg must be TRUE for CTRL-E.
2458 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002459 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 if (!checkclearop(cap->oap))
2463 scroll_redraw(cap->arg, cap->count1);
2464}
2465
2466/*
2467 * Scroll "count" lines up or down, and redraw.
2468 */
2469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002470scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 linenr_T prev_topline = curwin->w_topline;
2473#ifdef FEAT_DIFF
2474 int prev_topfill = curwin->w_topfill;
2475#endif
2476 linenr_T prev_lnum = curwin->w_cursor.lnum;
2477
2478 if (up)
2479 scrollup(count, TRUE);
2480 else
2481 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002482 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002484 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2485 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 cursor_correct();
2487 check_cursor_moved(curwin);
2488 curwin->w_valid |= VALID_TOPLINE;
2489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002490 // If moved back to where we were, at least move the cursor, otherwise
2491 // we get stuck at one position. Don't move the cursor up if the
2492 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 while (curwin->w_topline == prev_topline
2494#ifdef FEAT_DIFF
2495 && curwin->w_topfill == prev_topfill
2496#endif
2497 )
2498 {
2499 if (up)
2500 {
2501 if (curwin->w_cursor.lnum > prev_lnum
2502 || cursor_down(1L, FALSE) == FAIL)
2503 break;
2504 }
2505 else
2506 {
2507 if (curwin->w_cursor.lnum < prev_lnum
2508 || prev_topline == 1L
2509 || cursor_up(1L, FALSE) == FAIL)
2510 break;
2511 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // Mark w_topline as valid, otherwise the screen jumps back at the
2513 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 check_cursor_moved(curwin);
2515 curwin->w_valid |= VALID_TOPLINE;
2516 }
2517 }
2518 if (curwin->w_cursor.lnum != prev_lnum)
2519 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002520 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521}
2522
2523/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002524 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2525 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2526 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002527 */
2528 static int
2529nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2530{
2531 int nchar = *nchar_arg;
2532 long n;
2533
2534 // "z123{nchar}": edit the count before obtaining {nchar}
2535 if (checkclearop(cap->oap))
2536 return FALSE;
2537 n = nchar - '0';
2538
2539 for (;;)
2540 {
2541#ifdef USE_ON_FLY_SCROLL
2542 dont_scroll = TRUE; // disallow scrolling here
2543#endif
2544 ++no_mapping;
2545 ++allow_keys; // no mapping for nchar, but allow key codes
2546 nchar = plain_vgetc();
2547 LANGMAP_ADJUST(nchar, TRUE);
2548 --no_mapping;
2549 --allow_keys;
2550#ifdef FEAT_CMDL_INFO
2551 (void)add_to_showcmd(nchar);
2552#endif
2553 if (nchar == K_DEL || nchar == K_KDEL)
2554 n /= 10;
2555 else if (VIM_ISDIGIT(nchar))
2556 n = n * 10 + (nchar - '0');
2557 else if (nchar == CAR)
2558 {
2559#ifdef FEAT_GUI
2560 need_mouse_correct = TRUE;
2561#endif
2562 win_setheight((int)n);
2563 break;
2564 }
2565 else if (nchar == 'l'
2566 || nchar == 'h'
2567 || nchar == K_LEFT
2568 || nchar == K_RIGHT)
2569 {
2570 cap->count1 = n ? n * cap->count1 : cap->count1;
2571 *nchar_arg = nchar;
2572 return TRUE;
2573 }
2574 else
2575 {
2576 clearopbeep(cap->oap);
2577 break;
2578 }
2579 }
2580 cap->oap->op_type = OP_NOP;
2581 return FALSE;
2582}
2583
2584#ifdef FEAT_SPELL
2585/*
2586 * "zug" and "zuw": undo "zg" and "zw"
2587 * "zg": add good word to word list
2588 * "zw": add wrong word to word list
2589 * "zG": add good word to temp word list
2590 * "zW": add wrong word to temp word list
2591 */
2592 static int
2593nv_zg_zw(cmdarg_T *cap, int nchar)
2594{
2595 char_u *ptr = NULL;
2596 int len;
2597 int undo = FALSE;
2598
2599 if (nchar == 'u')
2600 {
2601 ++no_mapping;
2602 ++allow_keys; // no mapping for nchar, but allow key codes
2603 nchar = plain_vgetc();
2604 LANGMAP_ADJUST(nchar, TRUE);
2605 --no_mapping;
2606 --allow_keys;
2607#ifdef FEAT_CMDL_INFO
2608 (void)add_to_showcmd(nchar);
2609#endif
2610 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2611 {
2612 clearopbeep(cap->oap);
2613 return OK;
2614 }
2615 undo = TRUE;
2616 }
2617
2618 if (checkclearop(cap->oap))
2619 return OK;
2620 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2621 return FAIL;
2622 if (ptr == NULL)
2623 {
2624 pos_T pos = curwin->w_cursor;
2625
2626 // Find bad word under the cursor. When 'spell' is
2627 // off this fails and find_ident_under_cursor() is
2628 // used below.
2629 emsg_off++;
2630 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2631 emsg_off--;
2632 if (len != 0 && curwin->w_cursor.col <= pos.col)
2633 ptr = ml_get_pos(&curwin->w_cursor);
2634 curwin->w_cursor = pos;
2635 }
2636
2637 if (ptr == NULL
2638 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2639 return FAIL;
2640 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2641 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2642 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2643
2644 return OK;
2645}
2646#endif
2647
2648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * Commands that start with "z".
2650 */
2651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002652nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
2654 long n;
2655 colnr_T col;
2656 int nchar = cap->nchar;
2657#ifdef FEAT_FOLDING
2658 long old_fdl = curwin->w_p_fdl;
2659 int old_fen = curwin->w_p_fen;
2660#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002661 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002663 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (
2667#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002668 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2669 // and "zC" only in Visual mode. "zj" and "zk" are motion
2670 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 cap->nchar != 'f' && cap->nchar != 'F'
2672 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2673 && cap->nchar != 'j' && cap->nchar != 'k'
2674 &&
2675#endif
2676 checkclearop(cap->oap))
2677 return;
2678
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002679 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2680 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2682 && cap->count0
2683 && cap->count0 != curwin->w_cursor.lnum)
2684 {
2685 setpcmark();
2686 if (cap->count0 > curbuf->b_ml.ml_line_count)
2687 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2688 else
2689 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002690 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 switch (nchar)
2694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 case '+':
2697 if (cap->count0 == 0)
2698 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // No count given: put cursor at the line below screen
2700 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2702 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2703 else
2704 curwin->w_cursor.lnum = curwin->w_botline;
2705 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 case NL:
2708 case CAR:
2709 case K_KENTER:
2710 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002714 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002715 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 break;
2717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002723 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002724 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 break;
2726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 // "z^", "z-" and "zb": put cursor at bottom of screen
2728 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2729 // when <count> is at bottom of window, and puts that one at
2730 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (cap->count0 != 0)
2732 {
2733 scroll_cursor_bot(0, TRUE);
2734 curwin->w_cursor.lnum = curwin->w_topline;
2735 }
2736 else if (curwin->w_topline == 1)
2737 curwin->w_cursor.lnum = 1;
2738 else
2739 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 case '-':
2742 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002746 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002747 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 break;
2749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002752 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 case 'h':
2757 case K_LEFT:
2758 if (!curwin->w_p_wrap)
2759 {
2760 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2761 curwin->w_leftcol = 0;
2762 else
2763 curwin->w_leftcol -= (colnr_T)cap->count1;
2764 leftcol_changed();
2765 }
2766 break;
2767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002769 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 case 'l':
2774 case K_RIGHT:
2775 if (!curwin->w_p_wrap)
2776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 curwin->w_leftcol += (colnr_T)cap->count1;
2779 leftcol_changed();
2780 }
2781 break;
2782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 case 's': if (!curwin->w_p_wrap)
2785 {
2786#ifdef FEAT_FOLDING
2787 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002788 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 else
2790#endif
2791 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002792 if ((long)col > siso)
2793 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else
2795 col = 0;
2796 if (curwin->w_leftcol != col)
2797 {
2798 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002799 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
2802 break;
2803
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 case 'e': if (!curwin->w_p_wrap)
2806 {
2807#ifdef FEAT_FOLDING
2808 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
2811#endif
2812 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002813 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002814 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 col = 0;
2816 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002817 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (curwin->w_leftcol != col)
2819 {
2820 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002821 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823 }
2824 break;
2825
Christian Brabandt2fa93842021-05-30 22:17:25 +02002826 // "zp", "zP" in block mode put without addind trailing spaces
2827 case 'P':
2828 case 'p': nv_put(cap);
2829 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002830 // "zy" Yank without trailing spaces
2831 case 'y': nv_operator(cap);
2832 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002834 // "zF": create fold command
2835 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 case 'F':
2837 case 'f': if (foldManualAllowed(TRUE))
2838 {
2839 cap->nchar = 'f';
2840 nv_operator(cap);
2841 curwin->w_p_fen = TRUE;
2842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2845 {
2846 nv_operator(cap);
2847 finish_op = TRUE;
2848 }
2849 }
2850 else
2851 clearopbeep(cap->oap);
2852 break;
2853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // "zd": delete fold at cursor
2855 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case 'd':
2857 case 'D': if (foldManualAllowed(FALSE))
2858 {
2859 if (VIsual_active)
2860 nv_operator(cap);
2861 else
2862 deleteFold(curwin->w_cursor.lnum,
2863 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2864 }
2865 break;
2866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 case 'E': if (foldmethodIsManual(curwin))
2869 {
2870 clearFolding(curwin);
2871 changed_window_setting();
2872 }
2873 else if (foldmethodIsMarker(curwin))
2874 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2875 TRUE, FALSE);
2876 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002877 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 break;
2879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002880 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 case 'n': curwin->w_p_fen = FALSE;
2882 break;
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 case 'N': curwin->w_p_fen = TRUE;
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2890 break;
2891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2894 openFold(curwin->w_cursor.lnum, cap->count1);
2895 else
2896 {
2897 closeFold(curwin->w_cursor.lnum, cap->count1);
2898 curwin->w_p_fen = TRUE;
2899 }
2900 break;
2901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2904 openFoldRecurse(curwin->w_cursor.lnum);
2905 else
2906 {
2907 closeFoldRecurse(curwin->w_cursor.lnum);
2908 curwin->w_p_fen = TRUE;
2909 }
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'o': if (VIsual_active)
2914 nv_operator(cap);
2915 else
2916 openFold(curwin->w_cursor.lnum, cap->count1);
2917 break;
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 case 'O': if (VIsual_active)
2921 nv_operator(cap);
2922 else
2923 openFoldRecurse(curwin->w_cursor.lnum);
2924 break;
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 case 'c': if (VIsual_active)
2928 nv_operator(cap);
2929 else
2930 closeFold(curwin->w_cursor.lnum, cap->count1);
2931 curwin->w_p_fen = TRUE;
2932 break;
2933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 case 'C': if (VIsual_active)
2936 nv_operator(cap);
2937 else
2938 closeFoldRecurse(curwin->w_cursor.lnum);
2939 curwin->w_p_fen = TRUE;
2940 break;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 case 'v': foldOpenCursor();
2944 break;
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 curwin->w_foldinvalid = TRUE; // recompute folds
2949 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 foldOpenCursor();
2951 break;
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 curwin->w_foldinvalid = TRUE; // recompute folds
2956 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 break;
2958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002961 {
2962 curwin->w_p_fdl -= cap->count1;
2963 if (curwin->w_p_fdl < 0)
2964 curwin->w_p_fdl = 0;
2965 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 curwin->w_p_fen = TRUE;
2968 break;
2969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 curwin->w_p_fen = TRUE;
2974 break;
2975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002977 case 'r': curwin->w_p_fdl += cap->count1;
2978 {
2979 int d = getDeepestNesting();
2980
2981 if (curwin->w_p_fdl >= d)
2982 curwin->w_p_fdl = d;
2983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 break;
2985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 break;
2990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 case 'j': // "zj" move to next fold downwards
2992 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2994 cap->count1) == FAIL)
2995 clearopbeep(cap->oap);
2996 break;
2997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003000#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003002 case 'g': // "zg": add good word to word list
3003 case 'w': // "zw": add wrong word to word list
3004 case 'G': // "zG": add good word to temp word list
3005 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003006 if (nv_zg_zw(cap, nchar) == FAIL)
3007 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003008 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003011 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003012 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003014#endif
3015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 default: clearopbeep(cap->oap);
3017 }
3018
3019#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (old_fen != curwin->w_p_fen)
3022 {
3023# ifdef FEAT_DIFF
3024 win_T *wp;
3025
3026 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 FOR_ALL_WINDOWS(wp)
3030 {
3031 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3032 {
3033 wp->w_p_fen = curwin->w_p_fen;
3034 changed_window_setting_win(wp);
3035 }
3036 }
3037 }
3038# endif
3039 changed_window_setting();
3040 }
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (old_fdl != curwin->w_p_fdl)
3044 newFoldLevel();
3045#endif
3046}
3047
3048#ifdef FEAT_GUI
3049/*
3050 * Vertical scrollbar movement.
3051 */
3052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003053nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 if (cap->oap->op_type != OP_NOP)
3056 clearopbeep(cap->oap);
3057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 gui_do_scroll();
3060}
3061
3062/*
3063 * Horizontal scrollbar movement.
3064 */
3065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003066nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 if (cap->oap->op_type != OP_NOP)
3069 clearopbeep(cap->oap);
3070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003072 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073}
3074#endif
3075
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003076#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003077/*
3078 * Click in GUI tab.
3079 */
3080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003081nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003082{
3083 if (cap->oap->op_type != OP_NOP)
3084 clearopbeep(cap->oap);
3085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003086 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003087 goto_tabpage(current_tab);
3088}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003089
3090/*
3091 * Selected item in tab line menu.
3092 */
3093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003094nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003095{
3096 if (cap->oap->op_type != OP_NOP)
3097 clearopbeep(cap->oap);
3098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003100 handle_tabmenu();
3101}
3102
3103/*
3104 * Handle selecting an item of the GUI tab line menu.
3105 * Used in Normal and Insert mode.
3106 */
3107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003108handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003109{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003110 switch (current_tabmenu)
3111 {
3112 case TABLINE_MENU_CLOSE:
3113 if (current_tab == 0)
3114 do_cmdline_cmd((char_u *)"tabclose");
3115 else
3116 {
3117 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3118 current_tab);
3119 do_cmdline_cmd(IObuff);
3120 }
3121 break;
3122
3123 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003124 if (current_tab == 0)
3125 do_cmdline_cmd((char_u *)"$tabnew");
3126 else
3127 {
3128 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3129 current_tab - 1);
3130 do_cmdline_cmd(IObuff);
3131 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003132 break;
3133
3134 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003135 if (current_tab == 0)
3136 do_cmdline_cmd((char_u *)"browse $tabnew");
3137 else
3138 {
3139 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3140 current_tab - 1);
3141 do_cmdline_cmd(IObuff);
3142 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003143 break;
3144 }
3145}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003146#endif
3147
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148/*
3149 * "Q" command.
3150 */
3151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003152nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003154 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003156 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003157 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 do_exmode(FALSE);
3159}
3160
3161/*
3162 * Handle a ":" command.
3163 */
3164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003165nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003167 int old_p_im;
3168 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003169 int is_cmdkey = cap->cmdchar == K_COMMAND
3170 || cap->cmdchar == K_SCRIPT_COMMAND;
3171 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaar957cf672020-11-12 14:21:06 +01003173 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 nv_operator(cap);
3175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
3177 if (cap->oap->op_type != OP_NOP)
3178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003179 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 cap->oap->motion_type = MCHAR;
3181 cap->oap->inclusive = FALSE;
3182 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003183 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 stuffcharReadbuff('.');
3187 if (cap->count0 > 1)
3188 {
3189 stuffReadbuff((char_u *)",.+");
3190 stuffnumReadbuff((long)cap->count0 - 1L);
3191 }
3192 }
3193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (KeyTyped)
3196 compute_cmdrow();
3197
3198 old_p_im = p_im;
3199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003201 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3202 if (is_cmdkey)
3203 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3204 else
3205 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003207 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (p_im != old_p_im)
3209 {
3210 if (p_im)
3211 restart_edit = 'i';
3212 else
3213 restart_edit = 0;
3214 }
3215
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003216 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003217 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003218 clearop(cap->oap);
3219 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3221 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003222 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3223 || did_emsg
3224 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 clearopbeep(cap->oap);
3227 }
3228}
3229
3230/*
3231 * Handle CTRL-G command.
3232 */
3233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003234nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003236 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003239 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 showmode();
3241 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003242 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003243 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 fileinfo((int)cap->count0, FALSE, TRUE);
3245}
3246
3247/*
3248 * Handle CTRL-H <Backspace> command.
3249 */
3250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003251nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (VIsual_active && VIsual_select)
3254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003255 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 v_visop(cap);
3257 }
3258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 nv_left(cap);
3260}
3261
3262/*
3263 * CTRL-L: clear screen and redraw.
3264 */
3265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003266nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 if (!checkclearop(cap->oap))
3269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003271 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003272 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003273# ifdef FEAT_RELTIME
3274 {
3275 win_T *wp;
3276
3277 FOR_ALL_WINDOWS(wp)
3278 wp->w_s->b_syn_slow = FALSE;
3279 }
3280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003282 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003283#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3284# ifdef VIMDLL
3285 if (!gui.in_use)
3286# endif
3287 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290}
3291
3292/*
3293 * CTRL-O: In Select mode: switch to Visual mode for one command.
3294 * Otherwise: Go to older pcmark.
3295 */
3296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003297nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (VIsual_active && VIsual_select)
3300 {
3301 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003302 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
3308 cap->count1 = -cap->count1;
3309 nv_pcmark(cap);
3310 }
3311}
3312
3313/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003314 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3315 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
3317 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003318nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 if (!checkclearopq(cap->oap))
3321 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3322 GETF_SETMARK|GETF_ALT, FALSE);
3323}
3324
3325/*
3326 * "Z" commands.
3327 */
3328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003329nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 if (!checkclearopq(cap->oap))
3332 {
3333 switch (cap->nchar)
3334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003335 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 case 'Z': do_cmdline_cmd((char_u *)"x");
3337 break;
3338
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 case 'Q': do_cmdline_cmd((char_u *)"q!");
3341 break;
3342
3343 default: clearopbeep(cap->oap);
3344 }
3345 }
3346}
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348/*
3349 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3350 */
3351 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003352do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 oparg_T oa;
3355 cmdarg_T ca;
3356
3357 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003358 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 ca.oap = &oa;
3360 ca.cmdchar = c1;
3361 ca.nchar = c2;
3362 nv_ident(&ca);
3363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003366 * 'K' normal-mode command. Get the command to lookup the keyword under the
3367 * cursor.
3368 */
3369 static int
3370nv_K_getcmd(
3371 cmdarg_T *cap,
3372 char_u *kp,
3373 int kp_help,
3374 int kp_ex,
3375 char_u **ptr_arg,
3376 int n,
3377 char_u *buf,
3378 unsigned buflen)
3379{
3380 char_u *ptr = *ptr_arg;
3381 int isman;
3382 int isman_s;
3383
3384 if (kp_help)
3385 {
3386 // in the help buffer
3387 STRCPY(buf, "he! ");
3388 return n;
3389 }
3390
3391 if (kp_ex)
3392 {
3393 // 'keywordprog' is an ex command
3394 if (cap->count0 != 0)
3395 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3396 else
3397 STRCPY(buf, kp);
3398 STRCAT(buf, " ");
3399 return n;
3400 }
3401
3402 // An external command will probably use an argument starting
3403 // with "-" as an option. To avoid trouble we skip the "-".
3404 while (*ptr == '-' && n > 0)
3405 {
3406 ++ptr;
3407 --n;
3408 }
3409 if (n == 0)
3410 {
3411 // found dashes only
3412 emsg(_(e_no_identifier_under_cursor));
3413 vim_free(buf);
3414 *ptr_arg = ptr;
3415 return 0;
3416 }
3417
3418 // When a count is given, turn it into a range. Is this
3419 // really what we want?
3420 isman = (STRCMP(kp, "man") == 0);
3421 isman_s = (STRCMP(kp, "man -s") == 0);
3422 if (cap->count0 != 0 && !(isman || isman_s))
3423 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3424
3425 STRCAT(buf, "! ");
3426 if (cap->count0 == 0 && isman_s)
3427 STRCAT(buf, "man");
3428 else
3429 STRCAT(buf, kp);
3430 STRCAT(buf, " ");
3431 if (cap->count0 != 0 && (isman || isman_s))
3432 {
3433 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3434 STRCAT(buf, " ");
3435 }
3436
3437 *ptr_arg = ptr;
3438 return n;
3439}
3440
3441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 * Handle the commands that use the word under the cursor.
3443 * [g] CTRL-] :ta to current identifier
3444 * [g] 'K' run program for current identifier
3445 * [g] '*' / to current identifier or string
3446 * [g] '#' ? to current identifier or string
3447 * g ']' :tselect for current identifier
3448 */
3449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003450nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
3452 char_u *ptr = NULL;
3453 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003454 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003455 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 char_u *kp; // value of 'keywordprg'
3458 int kp_help; // 'keywordprg' is ":he"
3459 int kp_ex; // 'keywordprg' starts with ":"
3460 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003463 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003466 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 cmdchar = cap->nchar;
3469 g_cmd = TRUE;
3470 }
3471 else
3472 {
3473 cmdchar = cap->cmdchar;
3474 g_cmd = FALSE;
3475 }
3476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 cmdchar = '#';
3479
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003480 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3484 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (checkclearopq(cap->oap))
3486 return;
3487 }
3488
3489 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3490 (cmdchar == '*' || cmdchar == '#')
3491 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3492 {
3493 clearop(cap->oap);
3494 return;
3495 }
3496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497 // Allocate buffer to put the command in. Inserting backslashes can
3498 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3499 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3501 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3502 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003503 if (kp_help && *skipwhite(ptr) == NUL)
3504 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003505 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003506 return;
3507 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003508 kp_ex = (*kp == ':');
3509 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3510 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (buf == NULL)
3512 return;
3513 buf[0] = NUL;
3514
3515 switch (cmdchar)
3516 {
3517 case '*':
3518 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003519 // Put cursor at start of word, makes search skip the word
3520 // under the cursor.
3521 // Call setpcmark() first, so "*``" puts the cursor back where
3522 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 setpcmark();
3524 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3525
3526 if (!g_cmd && vim_iswordp(ptr))
3527 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003528 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 break;
3530
3531 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003532 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3533 if (n == 0)
3534 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 break;
3536
3537 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#ifdef FEAT_CSCOPE
3540 if (p_cst)
3541 STRCPY(buf, "cstag ");
3542 else
3543#endif
3544 STRCPY(buf, "ts ");
3545 break;
3546
3547 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003548 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (curbuf->b_help)
3550 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003552 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003553 if (g_cmd)
3554 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003555 else if (cap->count0 == 0)
3556 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003557 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003558 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003562 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003563 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003565 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003566 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003568 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003569 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003570 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003571 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003572 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003573 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 vim_free(buf);
3576 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003578 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003579 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003580 {
3581 vim_free(buf);
3582 vim_free(p);
3583 return;
3584 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003585 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003586 STRCAT(buf, p);
3587 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 else
3590 {
3591 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003592 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003593 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003594 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003595 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003596 {
3597 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003599 aux_ptr = (char_u *)"";
3600 else
3601 aux_ptr = (char_u *)"\\|\"\n[";
3602 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003603 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003604 aux_ptr = (char_u *)"\\|\"\n*?[";
3605
3606 p = buf + STRLEN(buf);
3607 while (n-- > 0)
3608 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003609 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003610 if (vim_strchr(aux_ptr, *ptr) != NULL)
3611 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003612 // When current byte is a part of multibyte character, copy all
3613 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003614 if (has_mbyte)
3615 {
3616 int i;
3617 int len = (*mb_ptr2len)(ptr) - 1;
3618
3619 for (i = 0; i < len && n >= 1; ++i, --n)
3620 *p++ = *ptr++;
3621 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003622 *p++ = *ptr++;
3623 }
3624 *p = NUL;
3625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003627 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (cmdchar == '*' || cmdchar == '#')
3629 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003630 if (!g_cmd && (has_mbyte
3631 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3632 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003634
3635 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003636 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003638
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003639 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003642 {
3643 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003645 g_tag_at_cursor = FALSE;
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 vim_free(buf);
3649}
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651/*
3652 * Get visually selected text, within one line only.
3653 * Returns FAIL if more than one line selected.
3654 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003655 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003656get_visual_text(
3657 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003658 char_u **pp, // return: start of selected text
3659 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660{
3661 if (VIsual_mode != 'V')
3662 unadjust_for_sel();
3663 if (VIsual.lnum != curwin->w_cursor.lnum)
3664 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003665 if (cap != NULL)
3666 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return FAIL;
3668 }
3669 if (VIsual_mode == 'V')
3670 {
3671 *pp = ml_get_curline();
3672 *lenp = (int)STRLEN(*pp);
3673 }
3674 else
3675 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003676 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
3678 *pp = ml_get_pos(&curwin->w_cursor);
3679 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3680 }
3681 else
3682 {
3683 *pp = ml_get_pos(&VIsual);
3684 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3685 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003686 if (**pp == NUL)
3687 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003688 if (*lenp > 0)
3689 {
3690 if (has_mbyte)
3691 // Correct the length to include all bytes of the last
3692 // character.
3693 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3694 else if ((*pp)[*lenp - 1] == NUL)
3695 // Do not include a trailing NUL.
3696 *lenp -= 1;
3697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 reset_VIsual_and_resel();
3700 return OK;
3701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703/*
3704 * CTRL-T: backwards in tag stack
3705 */
3706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003707nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 if (!checkclearopq(cap->oap))
3710 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3711}
3712
3713/*
3714 * Handle scrolling command 'H', 'L' and 'M'.
3715 */
3716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003717nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
3719 int used = 0;
3720 long n;
3721#ifdef FEAT_FOLDING
3722 linenr_T lnum;
3723#endif
3724 int half;
3725
3726 cap->oap->motion_type = MLINE;
3727 setpcmark();
3728
3729 if (cap->cmdchar == 'L')
3730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003731 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 curwin->w_cursor.lnum = curwin->w_botline - 1;
3733 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3734 curwin->w_cursor.lnum = 1;
3735 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003736 {
3737#ifdef FEAT_FOLDING
3738 if (hasAnyFolding(curwin))
3739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003740 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003741 for (n = cap->count1 - 1; n > 0
3742 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3743 {
3744 (void)hasFolding(curwin->w_cursor.lnum,
3745 &curwin->w_cursor.lnum, NULL);
3746 --curwin->w_cursor.lnum;
3747 }
3748 }
3749 else
3750#endif
3751 curwin->w_cursor.lnum -= cap->count1 - 1;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 else
3755 {
3756 if (cap->cmdchar == 'M')
3757 {
3758#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 used -= diff_check_fill(curwin, curwin->w_topline)
3761 - curwin->w_topfill;
3762#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003763 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3765 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3766 {
3767#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003768 // Count half he number of filler lines to be "below this
3769 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3771 + n) / 2 >= half)
3772 {
3773 --n;
3774 break;
3775 }
3776#endif
3777 used += plines(curwin->w_topline + n);
3778 if (used >= half)
3779 break;
3780#ifdef FEAT_FOLDING
3781 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3782 n = lnum - curwin->w_topline;
3783#endif
3784 }
3785 if (n > 0 && used > curwin->w_height)
3786 --n;
3787 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003788 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003791#ifdef FEAT_FOLDING
3792 if (hasAnyFolding(curwin))
3793 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003794 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003795 lnum = curwin->w_topline;
3796 while (n-- > 0 && lnum < curwin->w_botline - 1)
3797 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003798 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003799 ++lnum;
3800 }
3801 n = lnum - curwin->w_topline;
3802 }
3803#endif
3804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 curwin->w_cursor.lnum = curwin->w_topline + n;
3806 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3807 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3808 }
3809
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003810 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003811 if (cap->oap->op_type == OP_NOP)
3812 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 beginline(BL_SOL | BL_FIX);
3814}
3815
3816/*
3817 * Cursor right commands.
3818 */
3819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003820nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003823 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003825 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003827 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003828 if (mod_mask & MOD_MASK_CTRL)
3829 cap->arg = TRUE;
3830 nv_wordcmd(cap);
3831 return;
3832 }
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 cap->oap->motion_type = MCHAR;
3835 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003836 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003838 // In virtual edit mode, there's no such thing as "past_line", as lines
3839 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003841 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 for (n = cap->count1; n > 0; --n)
3844 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003845 if ((!past_line && oneright() == FAIL)
3846 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003847 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003849 // <Space> wraps to next line if 'whichwrap' has 's'.
3850 // 'l' wraps to next line if 'whichwrap' has 'l'.
3851 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if ( ((cap->cmdchar == ' '
3853 && vim_strchr(p_ww, 's') != NULL)
3854 || (cap->cmdchar == 'l'
3855 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003856 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 && vim_strchr(p_ww, '>') != NULL))
3858 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003860 // When deleting we also count the NL as a character.
3861 // Set cap->oap->inclusive when last char in the line is
3862 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003863 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003865 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 cap->oap->inclusive = TRUE;
3867 else
3868 {
3869 ++curwin->w_cursor.lnum;
3870 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 curwin->w_set_curswant = TRUE;
3873 cap->oap->inclusive = FALSE;
3874 }
3875 continue;
3876 }
3877 if (cap->oap->op_type == OP_NOP)
3878 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003879 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (n == cap->count1)
3881 beep_flush();
3882 }
3883 else
3884 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003885 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 cap->oap->inclusive = TRUE;
3887 }
3888 break;
3889 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003890 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
3892 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (virtual_active())
3894 oneright();
3895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003898 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 ++curwin->w_cursor.col;
3901 }
3902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904#ifdef FEAT_FOLDING
3905 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3906 && cap->oap->op_type == OP_NOP)
3907 foldOpenCursor();
3908#endif
3909}
3910
3911/*
3912 * Cursor left commands.
3913 *
3914 * Returns TRUE when operator end should not be adjusted.
3915 */
3916 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003917nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 long n;
3920
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003921 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003923 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003924 if (mod_mask & MOD_MASK_CTRL)
3925 cap->arg = 1;
3926 nv_bck_word(cap);
3927 return;
3928 }
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 cap->oap->motion_type = MCHAR;
3931 cap->oap->inclusive = FALSE;
3932 for (n = cap->count1; n > 0; --n)
3933 {
3934 if (oneleft() == FAIL)
3935 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003936 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3937 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3938 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if ( (((cap->cmdchar == K_BS
3940 || cap->cmdchar == Ctrl_H)
3941 && vim_strchr(p_ww, 'b') != NULL)
3942 || (cap->cmdchar == 'h'
3943 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003944 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 && vim_strchr(p_ww, '<') != NULL))
3946 && curwin->w_cursor.lnum > 1)
3947 {
3948 --(curwin->w_cursor.lnum);
3949 coladvance((colnr_T)MAXCOL);
3950 curwin->w_set_curswant = TRUE;
3951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003952 // When the NL before the first char has to be deleted we
3953 // put the cursor on the NUL after the previous line.
3954 // This is a very special case, be careful!
3955 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if ( (cap->oap->op_type == OP_DELETE
3957 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003958 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003960 char_u *cp = ml_get_cursor();
3961
3962 if (*cp != NUL)
3963 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003964 if (has_mbyte)
3965 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3966 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003967 ++curwin->w_cursor.col;
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 cap->retval |= CA_NO_ADJ_OP_END;
3970 }
3971 continue;
3972 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003973 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3975 beep_flush();
3976 break;
3977 }
3978 }
3979#ifdef FEAT_FOLDING
3980 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3981 && cap->oap->op_type == OP_NOP)
3982 foldOpenCursor();
3983#endif
3984}
3985
3986/*
3987 * Cursor up commands.
3988 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3989 */
3990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003991nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993 if (mod_mask & MOD_MASK_SHIFT)
3994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003995 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 cap->arg = BACKWARD;
3997 nv_page(cap);
3998 }
3999 else
4000 {
4001 cap->oap->motion_type = MLINE;
4002 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4003 clearopbeep(cap->oap);
4004 else if (cap->arg)
4005 beginline(BL_WHITE | BL_FIX);
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007}
4008
4009/*
4010 * Cursor down commands.
4011 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4012 */
4013 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004014nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 if (mod_mask & MOD_MASK_SHIFT)
4017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004019 cap->arg = FORWARD;
4020 nv_page(cap);
4021 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004022#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004023 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004024 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4025 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004027 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
4029#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004030 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004031 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 cmdwin_result = CAR;
4033 else
4034#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004035#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004036 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004037 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4038 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4039 {
4040 invoke_prompt_callback();
4041 if (restart_edit == 0)
4042 restart_edit = 'a';
4043 }
4044 else
4045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 cap->oap->motion_type = MLINE;
4048 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4049 clearopbeep(cap->oap);
4050 else if (cap->arg)
4051 beginline(BL_WHITE | BL_FIX);
4052 }
4053 }
4054}
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056/*
4057 * Grab the file name under the cursor and edit it.
4058 */
4059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004060nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004063 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004065 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 if (curbuf_locked())
4068 {
4069 clearop(cap->oap);
4070 return;
4071 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004072#ifdef FEAT_PROP_POPUP
4073 if (ERROR_IF_TERM_POPUP_WINDOW)
4074 return;
4075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004077 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 if (ptr != NULL)
4080 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004082 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004083 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004085 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004086 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004087 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004088 {
4089 curwin->w_cursor.lnum = lnum;
4090 check_cursor_lnum();
4091 beginline(BL_SOL | BL_FIX);
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 vim_free(ptr);
4094 }
4095 else
4096 clearop(cap->oap);
4097}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
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)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004196 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004197#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)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004244 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004245#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
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004444 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (cap->nchar == 'f')
4446 nv_gotofile(cap);
4447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004450 // Find the occurrence(s) of the identifier or define under cursor
4451 // in current and included files or jump to the first occurrence.
4452 //
4453 // search list jump
4454 // fwd bwd fwd bwd fwd bwd
4455 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4456 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004457 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 char_u *ptr;
4460 int len;
4461
4462 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4463 clearop(cap->oap);
4464 else
4465 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004466 // Make a copy, if the line was changed it will be freed.
4467 ptr = vim_strnsave(ptr, len);
4468 if (ptr == NULL)
4469 return;
4470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 find_pattern_in_path(ptr, 0, len, TRUE,
4472 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4473 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4474 cap->count1,
4475 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4476 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4477 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4478 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004479 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 curwin->w_set_curswant = TRUE;
4481 }
4482 }
4483 else
4484#endif
4485
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004486 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4487 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4488 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4489 // "[m" or "]m" search for prev/next start of (Java) method.
4490 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if ( (cap->cmdchar == '['
4492 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4493 || (cap->cmdchar == ']'
4494 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004495 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004497 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else if (cap->nchar == '[' || cap->nchar == ']')
4499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004500 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 flag = '{';
4502 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004503 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004506 // Imitate strange Vi behaviour: When using "]]" with an operator
4507 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004508 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 (cap->oap->op_type != OP_NOP
4510 && cap->arg == FORWARD && flag == '{')))
4511 clearopbeep(cap->oap);
4512 else
4513 {
4514 if (cap->oap->op_type == OP_NOP)
4515 beginline(BL_WHITE | BL_FIX);
4516#ifdef FEAT_FOLDING
4517 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4518 foldOpenCursor();
4519#endif
4520 }
4521 }
4522
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004523 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else if (cap->nchar == 'p' || cap->nchar == 'P')
4525 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004526 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004529 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else if (cap->nchar == '\'' || cap->nchar == '`')
4531 {
4532 pos = &curwin->w_cursor;
4533 for (n = cap->count1; n > 0; --n)
4534 {
4535 prev_pos = *pos;
4536 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4537 cap->nchar == '\'');
4538 if (pos == NULL)
4539 break;
4540 }
4541 if (pos == NULL)
4542 pos = &prev_pos;
4543 nv_cursormark(cap, cap->nchar == '\'', pos);
4544 }
4545
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004546 // [ or ] followed by a middle mouse click: put selected text with
4547 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004548 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
4550 (void)do_mouse(cap->oap, cap->nchar,
4551 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4552 cap->count1, PUT_FIXINDENT);
4553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004556 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else if (cap->nchar == 'z')
4558 {
4559 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4560 cap->count1) == FAIL)
4561 clearopbeep(cap->oap);
4562 }
4563#endif
4564
4565#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004566 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else if (cap->nchar == 'c')
4568 {
4569 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4570 cap->count1) == FAIL)
4571 clearopbeep(cap->oap);
4572 }
4573#endif
4574
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004575#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004576 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 else if (cap->nchar == 's' || cap->nchar == 'S')
4578 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004579 setpcmark();
4580 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004581 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4582 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004583 {
4584 clearopbeep(cap->oap);
4585 break;
4586 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004587 else
4588 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004589# ifdef FEAT_FOLDING
4590 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4591 foldOpenCursor();
4592# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593 }
4594#endif
4595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004596 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 else
4598 clearopbeep(cap->oap);
4599}
4600
4601/*
4602 * Handle Normal mode "%" command.
4603 */
4604 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004605nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004608#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 linenr_T lnum = curwin->w_cursor.lnum;
4610#endif
4611
4612 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004613 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 if (cap->count0 > 100)
4616 clearopbeep(cap->oap);
4617 else
4618 {
4619 cap->oap->motion_type = MLINE;
4620 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004621 // Round up, so 'normal 100%' always jumps at the line line.
4622 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4623 // overflow on 32-bits, so use a formula with less accuracy
4624 // to avoid overflows.
4625 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4627 / 100L * cap->count0;
4628 else
4629 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4630 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004631 if (curwin->w_cursor.lnum < 1)
4632 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4634 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4635 beginline(BL_SOL | BL_FIX);
4636 }
4637 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 cap->oap->motion_type = MCHAR;
4641 cap->oap->use_reg_one = TRUE;
4642 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4643 clearopbeep(cap->oap);
4644 else
4645 {
4646 setpcmark();
4647 curwin->w_cursor = *pos;
4648 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 }
4653#ifdef FEAT_FOLDING
4654 if (cap->oap->op_type == OP_NOP
4655 && lnum != curwin->w_cursor.lnum
4656 && (fdo_flags & FDO_PERCENT)
4657 && KeyTyped)
4658 foldOpenCursor();
4659#endif
4660}
4661
4662/*
4663 * Handle "(" and ")" commands.
4664 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4665 */
4666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004667nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
4669 cap->oap->motion_type = MCHAR;
4670 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004671 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004672 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 curwin->w_set_curswant = TRUE;
4674
4675 if (findsent(cap->arg, cap->count1) == FAIL)
4676 clearopbeep(cap->oap);
4677 else
4678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004680 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#ifdef FEAT_FOLDING
4683 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4684 foldOpenCursor();
4685#endif
4686 }
4687}
4688
4689/*
4690 * "m" command: Mark a position.
4691 */
4692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004693nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 if (!checkclearop(cap->oap))
4696 {
4697 if (setmark(cap->nchar) == FAIL)
4698 clearopbeep(cap->oap);
4699 }
4700}
4701
4702/*
4703 * "{" and "}" commands.
4704 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4705 */
4706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004707nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 cap->oap->motion_type = MCHAR;
4710 cap->oap->inclusive = FALSE;
4711 cap->oap->use_reg_one = TRUE;
4712 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004713 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 clearopbeep(cap->oap);
4715 else
4716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718#ifdef FEAT_FOLDING
4719 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4720 foldOpenCursor();
4721#endif
4722 }
4723}
4724
4725/*
4726 * "u" command: Undo or make lower case.
4727 */
4728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004729nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004731 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004733 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 cap->cmdchar = 'g';
4735 cap->nchar = 'u';
4736 nv_operator(cap);
4737 }
4738 else
4739 nv_kundo(cap);
4740}
4741
4742/*
4743 * <Undo> command.
4744 */
4745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 if (!checkclearopq(cap->oap))
4749 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004750#ifdef FEAT_JOB_CHANNEL
4751 if (bt_prompt(curbuf))
4752 {
4753 clearopbeep(cap->oap);
4754 return;
4755 }
4756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 u_undo((int)cap->count1);
4758 curwin->w_set_curswant = TRUE;
4759 }
4760}
4761
4762/*
4763 * Handle the "r" command.
4764 */
4765 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004766nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
4768 char_u *ptr;
4769 int had_ctrl_v;
4770 long n;
4771
4772 if (checkclearop(cap->oap))
4773 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004774#ifdef FEAT_JOB_CHANNEL
4775 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4776 {
4777 clearopbeep(cap->oap);
4778 return;
4779 }
4780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (cap->nchar == Ctrl_V)
4784 {
4785 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004786 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (cap->nchar > DEL)
4789 had_ctrl_v = NUL;
4790 }
4791 else
4792 had_ctrl_v = NUL;
4793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004795 if (IS_SPECIAL(cap->nchar))
4796 {
4797 clearopbeep(cap->oap);
4798 return;
4799 }
4800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (VIsual_active)
4803 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004804 if (got_int)
4805 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004806 if (had_ctrl_v)
4807 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004808 // Use a special (negative) number to make a difference between a
4809 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004810 if (cap->nchar == CAR)
4811 cap->nchar = REPLACE_CR_NCHAR;
4812 else if (cap->nchar == NL)
4813 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 nv_operator(cap);
4816 return;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004819 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (virtual_active())
4821 {
4822 if (u_save_cursor() == FAIL)
4823 return;
4824 if (gchar_cursor() == NUL)
4825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004826 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 coladvance_force((colnr_T)(getviscol() + cap->count1));
4828 curwin->w_cursor.col -= cap->count1;
4829 }
4830 else if (gchar_cursor() == TAB)
4831 coladvance_force(getviscol());
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004834 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004836 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004837 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 clearopbeep(cap->oap);
4840 return;
4841 }
4842
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004843 // Replacing with a TAB is done by edit() when it is complicated because
4844 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4845 // Other characters are done below to avoid problems with things like
4846 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4848 {
4849 stuffnumReadbuff(cap->count1);
4850 stuffcharReadbuff('R');
4851 stuffcharReadbuff('\t');
4852 stuffcharReadbuff(ESC);
4853 return;
4854 }
4855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004856 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (u_save_cursor() == FAIL)
4858 return;
4859
4860 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4861 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004862 // Replace character(s) by a single newline.
4863 // Strange vi behaviour: Only one newline is inserted.
4864 // Delete the characters here.
4865 // Insert the newline with an insert command, takes care of
4866 // autoindent. The insert command depends on being on the last
4867 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 stuffcharReadbuff('\r');
4870 stuffcharReadbuff(ESC);
4871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 invoke_edit(cap, TRUE, 'r', FALSE);
4874 }
4875 else
4876 {
4877 prep_redo(cap->oap->regname, cap->count1,
4878 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4879
4880 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (has_mbyte)
4882 {
4883 int old_State = State;
4884
4885 if (cap->ncharC1 != 0)
4886 AppendCharToRedobuff(cap->ncharC1);
4887 if (cap->ncharC2 != 0)
4888 AppendCharToRedobuff(cap->ncharC2);
4889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004890 // This is slow, but it handles replacing a single-byte with a
4891 // multi-byte and the other way around. Also handles adding
4892 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 for (n = cap->count1; n > 0; --n)
4894 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004895 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004896 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4897 {
4898 int c = ins_copychar(curwin->w_cursor.lnum
4899 + (cap->nchar == Ctrl_Y ? -1 : 1));
4900 if (c != NUL)
4901 ins_char(c);
4902 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004903 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004904 ++curwin->w_cursor.col;
4905 }
4906 else
4907 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 State = old_State;
4909 if (cap->ncharC1 != 0)
4910 ins_char(cap->ncharC1);
4911 if (cap->ncharC2 != 0)
4912 ins_char(cap->ncharC2);
4913 }
4914 }
4915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004917 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 for (n = cap->count1; n > 0; --n)
4919 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004920 // Get ptr again, because u_save and/or showmatch() will have
4921 // released the line. This may also happen in ins_copychar().
4922 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004923 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4924 {
4925 int c = ins_copychar(curwin->w_cursor.lnum
4926 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004927
4928 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004929 if (c != NUL)
4930 ptr[curwin->w_cursor.col] = c;
4931 }
4932 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004933 {
4934 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004935 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (p_sm && msg_silent == 0)
4938 showmatch(cap->nchar);
4939 ++curwin->w_cursor.col;
4940 }
4941#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004942 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004944 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar009b2592004-10-24 19:18:58 +00004946 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004947 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004949 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951#endif
4952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 changed_bytes(curwin->w_cursor.lnum,
4955 (colnr_T)(curwin->w_cursor.col - cap->count1));
4956 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 --curwin->w_cursor.col; // cursor on the last replaced char
4958 // if the character on the left of the current cursor is a multi-byte
4959 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (has_mbyte)
4961 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 curbuf->b_op_end = curwin->w_cursor;
4963 curwin->w_set_curswant = TRUE;
4964 set_last_insert(cap->nchar);
4965 }
4966}
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968/*
4969 * 'o': Exchange start and end of Visual area.
4970 * 'O': same, but in block mode exchange left and right corners.
4971 */
4972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 pos_T old_cursor;
4976 colnr_T left, right;
4977
4978 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4979 {
4980 old_cursor = curwin->w_cursor;
4981 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4982 curwin->w_cursor.lnum = VIsual.lnum;
4983 coladvance(left);
4984 VIsual = curwin->w_cursor;
4985
4986 curwin->w_cursor.lnum = old_cursor.lnum;
4987 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004988 // 'selection "exclusive" and cursor at right-bottom corner: move it
4989 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4991 ++curwin->w_curswant;
4992 coladvance(curwin->w_curswant);
4993 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004995 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 curwin->w_cursor.lnum = VIsual.lnum;
4998 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4999 ++right;
5000 coladvance(right);
5001 VIsual = curwin->w_cursor;
5002
5003 curwin->w_cursor.lnum = old_cursor.lnum;
5004 coladvance(left);
5005 curwin->w_curswant = left;
5006 }
5007 }
5008 else
5009 {
5010 old_cursor = curwin->w_cursor;
5011 curwin->w_cursor = VIsual;
5012 VIsual = old_cursor;
5013 curwin->w_set_curswant = TRUE;
5014 }
5015}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017/*
5018 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5019 */
5020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005021nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005023 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 cap->cmdchar = 'c';
5026 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005027 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 VIsual_mode = 'V';
5029 nv_operator(cap);
5030 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005031 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
5033 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005034 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 else
5036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (virtual_active())
5038 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5040 }
5041 }
5042}
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044/*
5045 * "gr".
5046 */
5047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005048nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (VIsual_active)
5051 {
5052 cap->cmdchar = 'r';
5053 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005054 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005056 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
5058 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005059 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else
5061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005062 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005063 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 stuffcharReadbuff(cap->extra_char);
5065 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (virtual_active())
5067 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 invoke_edit(cap, TRUE, 'v', FALSE);
5069 }
5070 }
5071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073/*
5074 * Swap case for "~" command, when it does not work like an operator.
5075 */
5076 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005077n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 long n;
5080 pos_T startpos;
5081 int did_change = 0;
5082#ifdef FEAT_NETBEANS_INTG
5083 pos_T pos;
5084 char_u *ptr;
5085 int count;
5086#endif
5087
5088 if (checkclearopq(cap->oap))
5089 return;
5090
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005091 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 clearopbeep(cap->oap);
5094 return;
5095 }
5096
5097 prep_redo_cmd(cap);
5098
5099 if (u_save_cursor() == FAIL)
5100 return;
5101
5102 startpos = curwin->w_cursor;
5103#ifdef FEAT_NETBEANS_INTG
5104 pos = startpos;
5105#endif
5106 for (n = cap->count1; n > 0; --n)
5107 {
5108 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5109 inc_cursor();
5110 if (gchar_cursor() == NUL)
5111 {
5112 if (vim_strchr(p_ww, '~') != NULL
5113 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5114 {
5115#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005116 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 if (did_change)
5119 {
5120 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005121 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005122 netbeans_removed(curbuf, pos.lnum, pos.col,
5123 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005124 // line may have been flushed, get it again
5125 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005127 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 pos.col = 0;
5130 pos.lnum++;
5131 }
5132#endif
5133 ++curwin->w_cursor.lnum;
5134 curwin->w_cursor.col = 0;
5135 if (n > 1)
5136 {
5137 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5138 break;
5139 u_clearline();
5140 }
5141 }
5142 else
5143 break;
5144 }
5145 }
5146#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005147 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 ptr = ml_get(pos.lnum);
5150 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005151 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5152 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154#endif
5155
5156
5157 check_cursor();
5158 curwin->w_set_curswant = TRUE;
5159 if (did_change)
5160 {
5161 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5162 0L);
5163 curbuf->b_op_start = startpos;
5164 curbuf->b_op_end = curwin->w_cursor;
5165 if (curbuf->b_op_end.col > 0)
5166 --curbuf->b_op_end.col;
5167 }
5168}
5169
5170/*
5171 * Move cursor to mark.
5172 */
5173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005174nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 if (check_mark(pos) == FAIL)
5177 clearop(cap->oap);
5178 else
5179 {
5180 if (cap->cmdchar == '\''
5181 || cap->cmdchar == '`'
5182 || cap->cmdchar == '['
5183 || cap->cmdchar == ']')
5184 setpcmark();
5185 curwin->w_cursor = *pos;
5186 if (flag)
5187 beginline(BL_WHITE | BL_FIX);
5188 else
5189 check_cursor();
5190 }
5191 cap->oap->motion_type = flag ? MLINE : MCHAR;
5192 if (cap->cmdchar == '`')
5193 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005194 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 curwin->w_set_curswant = TRUE;
5196}
5197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198/*
5199 * Handle commands that are operators in Visual mode.
5200 */
5201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005206 // Uppercase means linewise, except in block mode, then "D" deletes till
5207 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (isupper(cap->cmdchar))
5209 {
5210 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005211 {
5212 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5216 curwin->w_curswant = MAXCOL;
5217 }
5218 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5219 nv_operator(cap);
5220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222/*
5223 * "s" and "S" commands.
5224 */
5225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005228#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005229 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005230 if (term_swap_diff() == OK)
5231 return;
5232#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005233#ifdef FEAT_JOB_CHANNEL
5234 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5235 {
5236 clearopbeep(cap->oap);
5237 return;
5238 }
5239#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005243 {
5244 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 cap->cmdchar = 'c';
5248 nv_operator(cap);
5249 }
5250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 nv_optrans(cap);
5252}
5253
5254/*
5255 * Abbreviated commands.
5256 */
5257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005258nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005261 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (VIsual_active)
5265 v_visop(cap);
5266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 nv_optrans(cap);
5268}
5269
5270/*
5271 * Translate a command into another command.
5272 */
5273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005274nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5277 (char_u *)"d$", (char_u *)"c$",
5278 (char_u *)"cl", (char_u *)"cc",
5279 (char_u *)"yy", (char_u *)":s\r"};
5280 static char_u *str = (char_u *)"xXDCsSY&";
5281
5282 if (!checkclearopq(cap->oap))
5283 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005284 // In Vi "2D" doesn't delete the next line. Can't translate it
5285 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005286 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5287 {
5288 cap->oap->start = curwin->w_cursor;
5289 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005290#ifdef FEAT_EVAL
5291 set_op_var(OP_DELETE);
5292#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005293 cap->count1 = 1;
5294 nv_dollar(cap);
5295 finish_op = TRUE;
5296 ResetRedobuff();
5297 AppendCharToRedobuff('D');
5298 }
5299 else
5300 {
5301 if (cap->count0)
5302 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005303 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 cap->opcount = 0;
5307}
5308
5309/*
5310 * "'" and "`" commands. Also for "g'" and "g`".
5311 * cap->arg is TRUE for "'" and "g'".
5312 */
5313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 pos_T *pos;
5317 int c;
5318#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005319 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322
5323 if (cap->cmdchar == 'g')
5324 c = cap->extra_char;
5325 else
5326 c = cap->nchar;
5327 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
5330 if (cap->arg)
5331 {
5332 check_cursor_lnum();
5333 beginline(BL_WHITE | BL_FIX);
5334 }
5335 else
5336 check_cursor();
5337 }
5338 else
5339 nv_cursormark(cap, cap->arg, pos);
5340
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (!virtual_active())
5343 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005344 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345#ifdef FEAT_FOLDING
5346 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005347 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005348 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 && (fdo_flags & FDO_MARK)
5350 && old_KeyTyped)
5351 foldOpenCursor();
5352#endif
5353}
5354
5355/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005356 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 */
5358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005359nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005362#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005364 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367 if (!checkclearopq(cap->oap))
5368 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005369 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5370 {
5371 if (goto_tabpage_lastused() == FAIL)
5372 clearopbeep(cap->oap);
5373 return;
5374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (cap->cmdchar == 'g')
5376 pos = movechangelist((int)cap->count1);
5377 else
5378 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005379 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 curwin->w_set_curswant = TRUE;
5382 check_cursor();
5383 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005384 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 nv_cursormark(cap, FALSE, pos);
5386 else if (cap->cmdchar == 'g')
5387 {
5388 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005389 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005391 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005393 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395 else
5396 clearopbeep(cap->oap);
5397# ifdef FEAT_FOLDING
5398 if (cap->oap->op_type == OP_NOP
5399 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5400 && (fdo_flags & FDO_MARK)
5401 && old_KeyTyped)
5402 foldOpenCursor();
5403# endif
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405}
5406
5407/*
5408 * Handle '"' command.
5409 */
5410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005411nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 if (checkclearop(cap->oap))
5414 return;
5415#ifdef FEAT_EVAL
5416 if (cap->nchar == '=')
5417 cap->nchar = get_expr_register();
5418#endif
5419 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5420 {
5421 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005422 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#ifdef FEAT_EVAL
5424 set_reg_var(cap->oap->regname);
5425#endif
5426 }
5427 else
5428 clearopbeep(cap->oap);
5429}
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431/*
5432 * Handle "v", "V" and "CTRL-V" commands.
5433 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5434 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005435 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 */
5437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005440 if (cap->cmdchar == Ctrl_Q)
5441 cap->cmdchar = Ctrl_V;
5442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005443 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5444 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if (cap->oap->op_type != OP_NOP)
5446 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005447 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005448 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 return;
5450 }
5451
5452 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 else // toggle char/block mode
5458 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 VIsual_mode = cap->cmdchar;
5460 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005461 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005463 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005465 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005468 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 VIsual = curwin->w_cursor;
5472
5473 VIsual_active = TRUE;
5474 VIsual_reselect = TRUE;
5475 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005476 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005479 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005480 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005481 // For V and ^V, we multiply the number of lines even if there
5482 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5484 {
5485 curwin->w_cursor.lnum +=
5486 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005487 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489 VIsual_mode = resel_VIsual_mode;
5490 if (VIsual_mode == 'v')
5491 {
5492 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005493 {
5494 validate_virtcol();
5495 curwin->w_curswant = curwin->w_virtcol
5496 + resel_VIsual_vcol * cap->count0 - 1;
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005499 curwin->w_curswant = resel_VIsual_vcol;
5500 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005502 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 curwin->w_curswant = MAXCOL;
5505 coladvance((colnr_T)MAXCOL);
5506 }
5507 else if (VIsual_mode == Ctrl_V)
5508 {
5509 validate_virtcol();
5510 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005511 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 coladvance(curwin->w_curswant);
5513 }
5514 else
5515 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005516 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518 else
5519 {
5520 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 may_start_select('c');
5523 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005524 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005525 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005526 if (cap->count0 > 0 && --cap->count1 > 0)
5527 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005528 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005529 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5530 nv_right(cap);
5531 else if (VIsual_mode == 'V')
5532 nv_down(cap);
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 }
5536}
5537
5538/*
5539 * Start selection for Shift-movement keys.
5540 */
5541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005542start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005544 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 may_start_select('k');
5546 n_start_visual_mode('v');
5547}
5548
5549/*
5550 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005551 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 */
5553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005554may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005556 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5557 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
5560/*
5561 * Start Visual mode "c".
5562 * Should set VIsual_select before calling this.
5563 */
5564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005565n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005567#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005568 int cursor_line_was_concealed = curwin->w_p_cole > 0
5569 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005570#endif
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 VIsual_mode = c;
5573 VIsual_active = TRUE;
5574 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005575
5576 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005577 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005578 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005579 {
5580 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 VIsual = curwin->w_cursor;
5584
5585#ifdef FEAT_FOLDING
5586 foldAdjustVisual();
5587#endif
5588
LemonBoy2bf52dd2022-04-09 18:17:34 +01005589 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005591#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005592 // Check if redraw is needed after changing the state.
5593 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005594#endif
5595
Bram Moolenaar09df3122006-01-23 22:23:09 +00005596 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 // Make sure the clipboard gets updated. Needed because start and
5600 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 clip_star.vmode = NUL;
5602#endif
5603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 // Only need to redraw this line, unless still need to redraw an old
5605 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005606 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5609 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5610 }
5611}
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614/*
5615 * CTRL-W: Window commands
5616 */
5617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005618nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005620 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005623 cap->cmdchar = ':';
5624 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005625 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005626 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005627 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629}
5630
5631/*
5632 * CTRL-Z: Suspend
5633 */
5634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005635nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005640 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005644 * "gv": Reselect the previous Visual area. If Visual already active,
5645 * exchange previous and current Visual area.
5646 */
5647 static void
5648nv_gv_cmd(cmdarg_T *cap)
5649{
5650 pos_T tpos;
5651 int i;
5652
5653 if (checkclearop(cap->oap))
5654 return;
5655
5656 if (curbuf->b_visual.vi_start.lnum == 0
5657 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5658 || curbuf->b_visual.vi_end.lnum == 0)
5659 {
5660 beep_flush();
5661 return;
5662 }
5663
5664 // set w_cursor to the start of the Visual area, tpos to the end
5665 if (VIsual_active)
5666 {
5667 i = VIsual_mode;
5668 VIsual_mode = curbuf->b_visual.vi_mode;
5669 curbuf->b_visual.vi_mode = i;
5670# ifdef FEAT_EVAL
5671 curbuf->b_visual_mode_eval = i;
5672# endif
5673 i = curwin->w_curswant;
5674 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5675 curbuf->b_visual.vi_curswant = i;
5676
5677 tpos = curbuf->b_visual.vi_end;
5678 curbuf->b_visual.vi_end = curwin->w_cursor;
5679 curwin->w_cursor = curbuf->b_visual.vi_start;
5680 curbuf->b_visual.vi_start = VIsual;
5681 }
5682 else
5683 {
5684 VIsual_mode = curbuf->b_visual.vi_mode;
5685 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5686 tpos = curbuf->b_visual.vi_end;
5687 curwin->w_cursor = curbuf->b_visual.vi_start;
5688 }
5689
5690 VIsual_active = TRUE;
5691 VIsual_reselect = TRUE;
5692
5693 // Set Visual to the start and w_cursor to the end of the Visual
5694 // area. Make sure they are on an existing character.
5695 check_cursor();
5696 VIsual = curwin->w_cursor;
5697 curwin->w_cursor = tpos;
5698 check_cursor();
5699 update_topline();
5700
5701 // When called from normal "g" command: start Select mode when
5702 // 'selectmode' contains "cmd". When called for K_SELECT, always
5703 // start Select mode.
5704 if (cap->arg)
5705 {
5706 VIsual_select = TRUE;
5707 VIsual_select_reg = 0;
5708 }
5709 else
5710 may_start_select('c');
5711 setmouse();
5712#ifdef FEAT_CLIPBOARD
5713 // Make sure the clipboard gets updated. Needed because start and
5714 // end are still the same, and the selection needs to be owned
5715 clip_star.vmode = NUL;
5716#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005717 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005718 showmode();
5719}
5720
5721/*
5722 * "g0", "g^" : Like "0" and "^" but for screen lines.
5723 * "gm": middle of "g0" and "g$".
5724 */
5725 static void
5726nv_g_home_m_cmd(cmdarg_T *cap)
5727{
5728 int i;
5729 int flag = FALSE;
5730
5731 if (cap->nchar == '^')
5732 flag = TRUE;
5733
5734 cap->oap->motion_type = MCHAR;
5735 cap->oap->inclusive = FALSE;
5736 if (curwin->w_p_wrap && curwin->w_width != 0)
5737 {
5738 int width1 = curwin->w_width - curwin_col_off();
5739 int width2 = width1 + curwin_col_off2();
5740
5741 validate_virtcol();
5742 i = 0;
5743 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5744 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5745 }
5746 else
5747 i = curwin->w_leftcol;
5748 // Go to the middle of the screen line. When 'number' or
5749 // 'relativenumber' is on and lines are wrapping the middle can be more
5750 // to the left.
5751 if (cap->nchar == 'm')
5752 i += (curwin->w_width - curwin_col_off()
5753 + ((curwin->w_p_wrap && i > 0)
5754 ? curwin_col_off2() : 0)) / 2;
5755 coladvance((colnr_T)i);
5756 if (flag)
5757 {
5758 do
5759 i = gchar_cursor();
5760 while (VIM_ISWHITE(i) && oneright() == OK);
5761 curwin->w_valid &= ~VALID_WCOL;
5762 }
5763 curwin->w_set_curswant = TRUE;
5764}
5765
5766/*
5767 * "g_": to the last non-blank character in the line or <count> lines
5768 * downward.
5769 */
5770 static void
5771nv_g_underscore_cmd(cmdarg_T *cap)
5772{
5773 char_u *ptr;
5774
5775 cap->oap->motion_type = MCHAR;
5776 cap->oap->inclusive = TRUE;
5777 curwin->w_curswant = MAXCOL;
5778 if (cursor_down((long)(cap->count1 - 1),
5779 cap->oap->op_type == OP_NOP) == FAIL)
5780 {
5781 clearopbeep(cap->oap);
5782 return;
5783 }
5784
5785 ptr = ml_get_curline();
5786
5787 // In Visual mode we may end up after the line.
5788 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5789 --curwin->w_cursor.col;
5790
5791 // Decrease the cursor column until it's on a non-blank.
5792 while (curwin->w_cursor.col > 0
5793 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5794 --curwin->w_cursor.col;
5795 curwin->w_set_curswant = TRUE;
5796 adjust_for_sel(cap);
5797}
5798
5799/*
5800 * "g$" : Like "$" but for screen lines.
5801 */
5802 static void
5803nv_g_dollar_cmd(cmdarg_T *cap)
5804{
5805 oparg_T *oap = cap->oap;
5806 int i;
5807 int col_off = curwin_col_off();
5808
5809 oap->motion_type = MCHAR;
5810 oap->inclusive = TRUE;
5811 if (curwin->w_p_wrap && curwin->w_width != 0)
5812 {
5813 curwin->w_curswant = MAXCOL; // so we stay at the end
5814 if (cap->count1 == 1)
5815 {
5816 int width1 = curwin->w_width - col_off;
5817 int width2 = width1 + curwin_col_off2();
5818
5819 validate_virtcol();
5820 i = width1 - 1;
5821 if (curwin->w_virtcol >= (colnr_T)width1)
5822 i += ((curwin->w_virtcol - width1) / width2 + 1)
5823 * width2;
5824 coladvance((colnr_T)i);
5825
5826 // Make sure we stick in this column.
5827 validate_virtcol();
5828 curwin->w_curswant = curwin->w_virtcol;
5829 curwin->w_set_curswant = FALSE;
5830 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5831 {
5832 // Check for landing on a character that got split at
5833 // the end of the line. We do not want to advance to
5834 // the next screen line.
5835 if (curwin->w_virtcol > (colnr_T)i)
5836 --curwin->w_cursor.col;
5837 }
5838 }
5839 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5840 clearopbeep(oap);
5841 }
5842 else
5843 {
5844 if (cap->count1 > 1)
5845 // if it fails, let the cursor still move to the last char
5846 (void)cursor_down(cap->count1 - 1, FALSE);
5847
5848 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5849 coladvance((colnr_T)i);
5850
5851 // if the character doesn't fit move one back
5852 if (curwin->w_cursor.col > 0
5853 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5854 {
5855 colnr_T vcol;
5856
5857 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5858 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5859 --curwin->w_cursor.col;
5860 }
5861
5862 // Make sure we stick in this column.
5863 validate_virtcol();
5864 curwin->w_curswant = curwin->w_virtcol;
5865 curwin->w_set_curswant = FALSE;
5866 }
5867}
5868
5869/*
5870 * "gi": start Insert at the last position.
5871 */
5872 static void
5873nv_gi_cmd(cmdarg_T *cap)
5874{
5875 int i;
5876
5877 if (curbuf->b_last_insert.lnum != 0)
5878 {
5879 curwin->w_cursor = curbuf->b_last_insert;
5880 check_cursor_lnum();
5881 i = (int)STRLEN(ml_get_curline());
5882 if (curwin->w_cursor.col > (colnr_T)i)
5883 {
5884 if (virtual_active())
5885 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5886 curwin->w_cursor.col = i;
5887 }
5888 }
5889 cap->cmdchar = 'i';
5890 nv_edit(cap);
5891}
5892
5893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 * Commands starting with "g".
5895 */
5896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005897nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 switch (cap->nchar)
5903 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005904 case Ctrl_A:
5905 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005907 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005908 if (!VIsual_active && cap->nchar == Ctrl_A)
5909 vim_mem_profile_dump();
5910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005912 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005913 if (VIsual_active)
5914 {
5915 cap->arg = TRUE;
5916 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005917 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005918 nv_addsub(cap);
5919 }
5920 else
5921 clearopbeep(oap);
5922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005924 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 case 'R':
5926 cap->arg = TRUE;
5927 nv_Replace(cap);
5928 break;
5929
5930 case 'r':
5931 nv_vreplace(cap);
5932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
5934 case '&':
5935 do_cmdline_cmd((char_u *)"%s//~/&");
5936 break;
5937
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005938 // "gv": Reselect the previous Visual area. If Visual already active,
5939 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005941 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005943
5944 // "gV": Don't reselect the previous Visual area after a Select mode
5945 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case 'V':
5947 VIsual_reselect = FALSE;
5948 break;
5949
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005950 // "gh": start Select mode.
5951 // "gH": start Select line mode.
5952 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 case K_BS:
5954 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005955 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 case 'h':
5957 case 'H':
5958 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 cap->cmdchar = cap->nchar + ('v' - 'h');
5960 cap->arg = TRUE;
5961 nv_visual(cap);
5962 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005964 // "gn", "gN" visually select next/previous search match
5965 // "gn" selects next match
5966 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005967 case 'N':
5968 case 'n':
5969 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005970 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005971 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005973 // "gj" and "gk" two new funny movement keys -- up and down
5974 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 case 'j':
5976 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005977 // with 'nowrap' it works just like the normal "j" command.
5978 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 oap->motion_type = MLINE;
5981 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5982 }
5983 else
5984 i = nv_screengo(oap, FORWARD, cap->count1);
5985 if (i == FAIL)
5986 clearopbeep(oap);
5987 break;
5988
5989 case 'k':
5990 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005991 // with 'nowrap' it works just like the normal "k" command.
5992 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 oap->motion_type = MLINE;
5995 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5996 }
5997 else
5998 i = nv_screengo(oap, BACKWARD, cap->count1);
5999 if (i == FAIL)
6000 clearopbeep(oap);
6001 break;
6002
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006003 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 case 'J':
6005 nv_join(cap);
6006 break;
6007
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 // "g0", "g^" : Like "0" and "^" but for screen lines.
6009 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 case '0':
6012 case 'm':
6013 case K_HOME:
6014 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006015 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 break;
6017
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006018 case 'M':
6019 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006020 oap->motion_type = MCHAR;
6021 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006022 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006023 if (cap->count0 > 0 && cap->count0 <= 100)
6024 coladvance((colnr_T)(i * cap->count0 / 100));
6025 else
6026 coladvance((colnr_T)(i / 2));
6027 curwin->w_set_curswant = TRUE;
6028 }
6029 break;
6030
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006031 // "g_": to the last non-blank character in the line or <count> lines
6032 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 break;
6036
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 case '$':
6039 case K_END:
6040 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006041 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 break;
6043
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006044 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 case '*':
6046 case '#':
6047#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006048 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006050 case Ctrl_RSB: // :tag or :tselect for current identifier
6051 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 nv_ident(cap);
6053 break;
6054
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006055 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 case 'e':
6057 case 'E':
6058 oap->motion_type = MCHAR;
6059 curwin->w_set_curswant = TRUE;
6060 oap->inclusive = TRUE;
6061 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6062 clearopbeep(oap);
6063 break;
6064
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006065 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006067 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 break;
6069
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006070 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006072 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 break;
6074
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 case 'I':
6077 beginline(0);
6078 if (!checkclearopq(oap))
6079 invoke_edit(cap, FALSE, 'g', FALSE);
6080 break;
6081
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "gf": goto file, edit file under cursor
6083 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006085 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 nv_gotofile(cap);
6087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006089 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 case '\'':
6091 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006092 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 case '`':
6094 nv_gomark(cap);
6095 break;
6096
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006097 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006099 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 break;
6101
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006102 // "ga": Display the ascii value of the character under the
6103 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 case 'a':
6105 do_ascii(NULL);
6106 break;
6107
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006108 // "g8": Display the bytes used for the UTF-8 character under the
6109 // cursor. It is displayed in hex.
6110 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006112 if (cap->count0 == 8)
6113 utf_find_illegal();
6114 else
6115 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006118 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006119 case '<':
6120 show_sb_text();
6121 break;
6122
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006123 // "gg": Goto the first line in file. With a count it goes to
6124 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 case 'g':
6126 cap->arg = FALSE;
6127 nv_goto(cap);
6128 break;
6129
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006130 // Two-character operators:
6131 // "gq" Format text
6132 // "gw" Format text and keep cursor position
6133 // "g~" Toggle the case of the text.
6134 // "gu" Change text to lower case.
6135 // "gU" Change text to upper case.
6136 // "g?" rot13 encoding
6137 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 case 'q':
6139 case 'w':
6140 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006141 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 case '~':
6143 case 'u':
6144 case 'U':
6145 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006146 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 nv_operator(cap);
6148 break;
6149
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006150 // "gd": Find first occurrence of pattern under the cursor in the
6151 // current function
6152 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 case 'd':
6154 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006155 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 break;
6157
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006158 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 case K_MIDDLEMOUSE:
6160 case K_MIDDLEDRAG:
6161 case K_MIDDLERELEASE:
6162 case K_LEFTMOUSE:
6163 case K_LEFTDRAG:
6164 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006165 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 case K_RIGHTMOUSE:
6167 case K_RIGHTDRAG:
6168 case K_RIGHTRELEASE:
6169 case K_X1MOUSE:
6170 case K_X1DRAG:
6171 case K_X1RELEASE:
6172 case K_X2MOUSE:
6173 case K_X2DRAG:
6174 case K_X2RELEASE:
6175 mod_mask = MOD_MASK_CTRL;
6176 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 case K_IGNORE:
6180 break;
6181
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006182 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 case 'p':
6184 case 'P':
6185 nv_put(cap);
6186 break;
6187
6188#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006189 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 case 'o':
6191 goto_byte(cap->count0);
6192 break;
6193#endif
6194
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006195 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006197 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 do_exmode(TRUE);
6199 break;
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 case ',':
6202 nv_pcmark(cap);
6203 break;
6204
6205 case ';':
6206 cap->count1 = -cap->count1;
6207 nv_pcmark(cap);
6208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006211 if (!checkclearop(oap))
6212 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006214 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006215 if (!checkclearop(oap))
6216 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006217 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218
Bram Moolenaar62a23252020-08-09 14:04:42 +02006219 case TAB:
6220 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6221 clearopbeep(oap);
6222 break;
6223
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006224 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006225 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006226 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006227 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006228 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006229 break;
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 default:
6232 clearopbeep(oap);
6233 break;
6234 }
6235}
6236
6237/*
6238 * Handle "o" and "O" commands.
6239 */
6240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006241n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006243#ifdef FEAT_CONCEAL
6244 linenr_T oldline = curwin->w_cursor.lnum;
6245#endif
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (!checkclearopq(cap->oap))
6248 {
6249#ifdef FEAT_FOLDING
6250 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006251 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 (void)hasFolding(curwin->w_cursor.lnum,
6253 &curwin->w_cursor.lnum, NULL);
6254 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006255 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 (void)hasFolding(curwin->w_cursor.lnum,
6257 NULL, &curwin->w_cursor.lnum);
6258#endif
6259 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6260 (cap->cmdchar == 'O' ? 1 : 0)),
6261 (linenr_T)(curwin->w_cursor.lnum +
6262 (cap->cmdchar == 'o' ? 1 : 0))
6263 ) == OK
6264 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006265 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006266 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006268#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006269 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006270 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006271#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006272#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006273 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006274 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006275 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006276#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006277 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006278 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6279 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6281 }
6282 }
6283}
6284
6285/*
6286 * "." command: redo last change.
6287 */
6288 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006289nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 if (!checkclearopq(cap->oap))
6292 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006293 // If "restart_edit" is TRUE, the last but one command is repeated
6294 // instead of the last command (inserting text). This is used for
6295 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6297 clearopbeep(cap->oap);
6298 }
6299}
6300
6301/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006302 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 */
6304 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006305nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006307 if (VIsual_select && VIsual_active)
6308 {
6309 int reg;
6310 // Get register name
6311 ++no_mapping;
6312 ++allow_keys;
6313 reg = plain_vgetc();
6314 LANGMAP_ADJUST(reg, TRUE);
6315 --no_mapping;
6316 --allow_keys;
6317
6318 if (reg == '"')
6319 // the unnamed register is 0
6320 reg = 0;
6321
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006322 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006323 return;
6324 }
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (!checkclearopq(cap->oap))
6327 {
6328 u_redo((int)cap->count1);
6329 curwin->w_set_curswant = TRUE;
6330 }
6331}
6332
6333/*
6334 * Handle "U" command.
6335 */
6336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006337nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006339 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006340 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006342 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 cap->cmdchar = 'g';
6344 cap->nchar = 'U';
6345 nv_operator(cap);
6346 }
6347 else if (!checkclearopq(cap->oap))
6348 {
6349 u_undoline();
6350 curwin->w_set_curswant = TRUE;
6351 }
6352}
6353
6354/*
6355 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6356 * single character.
6357 */
6358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006359nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006361 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006362 {
6363#ifdef FEAT_JOB_CHANNEL
6364 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6365 {
6366 clearopbeep(cap->oap);
6367 return;
6368 }
6369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 else
6373 nv_operator(cap);
6374}
6375
6376/*
6377 * Handle an operator command.
6378 * The actual work is done by do_pending_operator().
6379 */
6380 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006381nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 int op_type;
6384
6385 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006386#ifdef FEAT_JOB_CHANNEL
6387 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6388 {
6389 clearopbeep(cap->oap);
6390 return;
6391 }
6392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006394 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 nv_lineop(cap);
6396 else if (!checkclearop(cap->oap))
6397 {
6398 cap->oap->start = curwin->w_cursor;
6399 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006400#ifdef FEAT_EVAL
6401 set_op_var(op_type);
6402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
6404}
6405
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006406#ifdef FEAT_EVAL
6407/*
6408 * Set v:operator to the characters for "optype".
6409 */
6410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006411set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006412{
6413 char_u opchars[3];
6414
6415 if (optype == OP_NOP)
6416 set_vim_var_string(VV_OP, NULL, 0);
6417 else
6418 {
6419 opchars[0] = get_op_char(optype);
6420 opchars[1] = get_extra_op_char(optype);
6421 opchars[2] = NUL;
6422 set_vim_var_string(VV_OP, opchars, -1);
6423 }
6424}
6425#endif
6426
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427/*
6428 * Handle linewise operator "dd", "yy", etc.
6429 *
6430 * "_" is is a strange motion command that helps make operators more logical.
6431 * It is actually implemented, but not documented in the real Vi. This motion
6432 * command actually refers to "the current line". Commands like "dd" and "yy"
6433 * are really an alternate form of "d_" and "y_". It does accept a count, so
6434 * "d3_" works to delete 3 lines.
6435 */
6436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006437nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438{
6439 cap->oap->motion_type = MLINE;
6440 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6441 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006442 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006443 && cap->oap->motion_force != 'v'
6444 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 || cap->oap->op_type == OP_LSHIFT
6446 || cap->oap->op_type == OP_RSHIFT)
6447 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006448 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 beginline(BL_WHITE | BL_FIX);
6450}
6451
6452/*
6453 * <Home> command.
6454 */
6455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006456nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006458 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006459 if (mod_mask & MOD_MASK_CTRL)
6460 nv_goto(cap);
6461 else
6462 {
6463 cap->count0 = 1;
6464 nv_pipe(cap);
6465 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006466 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6467 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468}
6469
6470/*
6471 * "|" command.
6472 */
6473 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006474nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 cap->oap->motion_type = MCHAR;
6477 cap->oap->inclusive = FALSE;
6478 beginline(0);
6479 if (cap->count0 > 0)
6480 {
6481 coladvance((colnr_T)(cap->count0 - 1));
6482 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6483 }
6484 else
6485 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006486 // keep curswant at the column where we wanted to go, not where
6487 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 curwin->w_set_curswant = FALSE;
6489}
6490
6491/*
6492 * Handle back-word command "b" and "B".
6493 * cap->arg is 1 for "B"
6494 */
6495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006496nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
6498 cap->oap->motion_type = MCHAR;
6499 cap->oap->inclusive = FALSE;
6500 curwin->w_set_curswant = TRUE;
6501 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6502 clearopbeep(cap->oap);
6503#ifdef FEAT_FOLDING
6504 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6505 foldOpenCursor();
6506#endif
6507}
6508
6509/*
6510 * Handle word motion commands "e", "E", "w" and "W".
6511 * cap->arg is TRUE for "E" and "W".
6512 */
6513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006514nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 int n;
6517 int word_end;
6518 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006519 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006521 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6523 word_end = TRUE;
6524 else
6525 word_end = FALSE;
6526 cap->oap->inclusive = word_end;
6527
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006528 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (!word_end && cap->oap->op_type == OP_CHANGE)
6530 {
6531 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006532 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006534 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006536 // Reproduce a funny Vi behaviour: "cw" on a blank only
6537 // changes one character, not all blanks until the start of
6538 // the next word. Only do this when the 'w' flag is included
6539 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6541 {
6542 cap->oap->inclusive = TRUE;
6543 cap->oap->motion_type = MCHAR;
6544 return;
6545 }
6546 }
6547 else
6548 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006549 // This is a little strange. To match what the real Vi does,
6550 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6551 // that we are not on a space or a TAB. This seems impolite
6552 // at first, but it's really more what we mean when we say
6553 // 'cw'.
6554 // Another strangeness: When standing on the end of a word
6555 // "ce" will change until the end of the next word, but "cw"
6556 // will change only one character! This is done by setting
6557 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 cap->oap->inclusive = TRUE;
6559 word_end = TRUE;
6560 flag = TRUE;
6561 }
6562 }
6563 }
6564
6565 cap->oap->motion_type = MCHAR;
6566 curwin->w_set_curswant = TRUE;
6567 if (word_end)
6568 n = end_word(cap->count1, cap->arg, flag, FALSE);
6569 else
6570 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006572 // Don't leave the cursor on the NUL past the end of line. Unless we
6573 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006574 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006575 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 if (n == FAIL && cap->oap->op_type == OP_NOP)
6578 clearopbeep(cap->oap);
6579 else
6580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582#ifdef FEAT_FOLDING
6583 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6584 foldOpenCursor();
6585#endif
6586 }
6587}
6588
6589/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006590 * Used after a movement command: If the cursor ends up on the NUL after the
6591 * end of the line, may move it back to the last character and make the motion
6592 * inclusive.
6593 */
6594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006595adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006596{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006597 // The cursor cannot remain on the NUL when:
6598 // - the column is > 0
6599 // - not in Visual mode or 'selection' is "o"
6600 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006601 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006603 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006604 {
6605 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006606 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006607 if (has_mbyte)
6608 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006609 oap->inclusive = TRUE;
6610 }
6611}
6612
6613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 * "0" and "^" commands.
6615 * cap->arg is the argument for beginline().
6616 */
6617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006618nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 cap->oap->motion_type = MCHAR;
6621 cap->oap->inclusive = FALSE;
6622 beginline(cap->arg);
6623#ifdef FEAT_FOLDING
6624 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6625 foldOpenCursor();
6626#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006627 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6628 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631/*
6632 * In exclusive Visual mode, may include the last character.
6633 */
6634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006635adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
6637 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006638 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 if (has_mbyte)
6641 inc_cursor();
6642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 ++curwin->w_cursor.col;
6644 cap->oap->inclusive = FALSE;
6645 }
6646}
6647
6648/*
6649 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6650 * Should check VIsual_mode before calling this.
6651 * Returns TRUE when backed up to the previous line.
6652 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006654unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 pos_T *pp;
6657
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006658 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006660 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 pp = &curwin->w_cursor;
6662 else
6663 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (pp->coladd > 0)
6665 --pp->coladd;
6666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if (pp->col > 0)
6668 {
6669 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006670 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
6672 else if (pp->lnum > 1)
6673 {
6674 --pp->lnum;
6675 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6676 return TRUE;
6677 }
6678 }
6679 return FALSE;
6680}
6681
6682/*
6683 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6684 */
6685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006686nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006691 VIsual_select_reg = 0;
6692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 else if (VIsual_reselect)
6694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006695 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 cap->arg = TRUE;
6697 nv_g_cmd(cap);
6698 }
6699}
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702/*
6703 * "G", "gg", CTRL-END, CTRL-HOME.
6704 * cap->arg is TRUE for "G".
6705 */
6706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006707nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 linenr_T lnum;
6710
6711 if (cap->arg)
6712 lnum = curbuf->b_ml.ml_line_count;
6713 else
6714 lnum = 1L;
6715 cap->oap->motion_type = MLINE;
6716 setpcmark();
6717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006718 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (cap->count0 != 0)
6720 lnum = cap->count0;
6721 if (lnum < 1L)
6722 lnum = 1L;
6723 else if (lnum > curbuf->b_ml.ml_line_count)
6724 lnum = curbuf->b_ml.ml_line_count;
6725 curwin->w_cursor.lnum = lnum;
6726 beginline(BL_SOL | BL_FIX);
6727#ifdef FEAT_FOLDING
6728 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6729 foldOpenCursor();
6730#endif
6731}
6732
6733/*
6734 * CTRL-\ in Normal mode.
6735 */
6736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006737nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6740 {
6741 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006742 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006743 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 restart_edit = 0;
6745#ifdef FEAT_CMDWIN
6746 if (cmdwin_type != 0)
6747 cmdwin_result = Ctrl_C;
6748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (VIsual_active)
6750 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006752 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (cap->nchar == Ctrl_G && p_im)
6756 restart_edit = 'a';
6757 }
6758 else
6759 clearopbeep(cap->oap);
6760}
6761
6762/*
6763 * ESC in Normal mode: beep, but don't flush buffers.
6764 * Don't even beep if we are canceling a command.
6765 */
6766 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006767nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 int no_reason;
6770
6771 no_reason = (cap->oap->op_type == OP_NOP
6772 && cap->opcount == 0
6773 && cap->count0 == 0
6774 && cap->oap->regname == 0
6775 && !p_im);
6776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006777 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
6779 if (restart_edit == 0
6780#ifdef FEAT_CMDWIN
6781 && cmdwin_type == 0
6782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006785 {
6786 if (anyBufIsChanged())
6787 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6788 else
6789 msg(_("Type :qa and press <Enter> to exit Vim"));
6790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006792 if (restart_edit != 0)
6793 redraw_mode = TRUE; // remove "-- (insert) --"
6794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006795 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6796 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (!p_im)
6798 restart_edit = 0;
6799#ifdef FEAT_CMDWIN
6800 if (cmdwin_type != 0)
6801 {
6802 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006803 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 return;
6805 }
6806#endif
6807 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006808#ifdef FEAT_CMDWIN
Bram Moolenaar8d696372022-08-21 10:40:07 +01006809 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006810 {
6811 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006812 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6813 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006814 cmdwin_result = K_IGNORE;
6815 return;
6816 }
6817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (VIsual_active)
6820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 end_visual_mode(); // stop Visual
6822 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006824 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006826 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006827 {
6828#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006829 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006830 popup_hide_message_win();
6831 else
6832#endif
6833 vim_beep(BO_ESC);
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 clearop(cap->oap);
6836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6838 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006839 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 restart_edit = 'a';
6841}
6842
6843/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006844 * Move the cursor for the "A" command.
6845 */
6846 void
6847set_cursor_for_append_to_line(void)
6848{
6849 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006850 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006851 {
6852 int save_State = State;
6853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 // Pretend Insert mode here to allow the cursor on the
6855 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006856 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006857 coladvance((colnr_T)MAXCOL);
6858 State = save_State;
6859 }
6860 else
6861 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6862}
6863
6864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006866 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 */
6868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6873 cap->cmdchar = 'i';
6874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006875 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006877 {
6878#ifdef FEAT_TERMINAL
6879 if (term_in_normal_mode())
6880 {
6881 end_visual_mode();
6882 clearop(cap->oap);
6883 term_enter_job_mode();
6884 return;
6885 }
6886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006891 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6892 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006896#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006897 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006898 {
6899 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006900 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006901 return;
6902 }
6903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 else if (!curbuf->b_p_ma && !p_im)
6905 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006906 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006907 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006909 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006911 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006913 else if (cap->cmdchar == K_PS && VIsual_active)
6914 {
6915 pos_T old_pos = curwin->w_cursor;
6916 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006917 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006920 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6921 {
6922 shift_delete_registers();
6923 cap->oap->regname = '1';
6924 }
6925 else
6926 cap->oap->regname = '-';
6927 cap->cmdchar = 'd';
6928 cap->nchar = NUL;
6929 nv_operator(cap);
6930 do_pending_operator(cap, 0, FALSE);
6931 cap->cmdchar = K_PS;
6932
zeertzjq7a732522022-03-14 20:46:41 +00006933 if (*ml_get_cursor() != NUL)
6934 {
6935 if (old_visual_mode == 'V')
6936 {
6937 // In linewise Visual mode insert before the beginning of the
6938 // next line.
6939 // When the last line in the buffer was deleted then create a
6940 // new line, otherwise there is not need to move cursor.
6941 // Detect this by checking if cursor moved above Visual area.
6942 if (curwin->w_cursor.lnum < old_pos.lnum
6943 && curwin->w_cursor.lnum < old_visual.lnum)
6944 {
6945 if (u_save_cursor() == OK)
6946 {
6947 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6948 FALSE);
6949 appended_lines(curwin->w_cursor.lnum++, 1L);
6950 }
6951 }
6952 }
6953 // When the last char in the line was deleted then append.
6954 // Detect this by checking if cursor moved before Visual area.
6955 else if (curwin->w_cursor.col < old_pos.col
6956 && curwin->w_cursor.col < old_visual.col)
6957 inc_cursor();
6958 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006961 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 else if (!checkclearopq(cap->oap))
6964 {
6965 switch (cap->cmdchar)
6966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006968 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 break;
6970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006971 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006972 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6973 beginline(BL_WHITE);
6974 else
6975 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 break;
6977
Bram Moolenaara1891842017-02-04 21:34:31 +01006978 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 // Bracketed paste works like "a"ppend, unless the cursor is in
6980 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006981 if (curwin->w_cursor.col == 0)
6982 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006983 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 case 'a': // "a"ppend is like "i"nsert on the next character.
6986 // increment coladd when in virtual space, increment the
6987 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (virtual_active()
6989 && (curwin->w_cursor.coladd > 0
6990 || *ml_get_cursor() == NUL
6991 || *ml_get_cursor() == TAB))
6992 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006993 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 inc_cursor();
6995 break;
6996 }
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6999 {
7000 int save_State = State;
7001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007002 // Pretend Insert mode here to allow the cursor on the
7003 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007004 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 coladvance(getviscol());
7006 State = save_State;
7007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7010 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007011 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007012 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007013 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
7016/*
7017 * Invoke edit() and take care of "restart_edit" and the return value.
7018 */
7019 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007020invoke_edit(
7021 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007022 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007023 int cmd,
7024 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 int restart_edit_save = 0;
7027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7029 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7030 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (repl || !stuff_empty())
7032 restart_edit_save = restart_edit;
7033 else
7034 restart_edit_save = 0;
7035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007036 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 restart_edit = 0;
7038
7039 if (edit(cmd, startln, cap->count1))
7040 cap->retval |= CA_COMMAND_BUSY;
7041
7042 if (restart_edit == 0)
7043 restart_edit = restart_edit_save;
7044}
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046/*
7047 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7048 */
7049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007050nv_object(
7051 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 int flag;
7054 int include;
7055 char_u *mps_save;
7056
7057 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 mps_save = curbuf->b_p_mps;
7064 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7065
7066 switch (cap->nchar)
7067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 flag = current_word(cap->oap, cap->count1, include, FALSE);
7070 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007071 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 flag = current_word(cap->oap, cap->count1, include, TRUE);
7073 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 case '(':
7076 case ')':
7077 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7078 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 case '{':
7081 case '}':
7082 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7083 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 case ']':
7086 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7087 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 case '>':
7090 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7091 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007092#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 case 't': // "at" = a tag block (xml and html)
7094 // Do not adjust oap->end in do_pending_operator()
7095 // otherwise there are different results for 'dit'
7096 // (note leading whitespace in last line):
7097 // 1) <b> 2) <b>
7098 // foobar foobar
7099 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007100 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007101 flag = current_tagblock(cap->oap, cap->count1, include);
7102 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007103#endif
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
7133/*
7134 * "q" command: Start/stop recording.
7135 * "q:", "q/", "q?": edit command-line in command-line window.
7136 */
7137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007138nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 if (cap->oap->op_type == OP_FORMAT)
7141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 cap->cmdchar = 'g';
7144 cap->nchar = 'q';
7145 nv_operator(cap);
7146 }
7147 else if (!checkclearop(cap->oap))
7148 {
7149#ifdef FEAT_CMDWIN
7150 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7151 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007152 if (cmdwin_type != 0)
7153 {
7154 emsg(_(e_cmdline_window_already_open));
7155 return;
7156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 stuffcharReadbuff(cap->nchar);
7158 stuffcharReadbuff(K_CMDWIN);
7159 }
7160 else
7161#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007162 // (stop) recording into a named register, unless executing a
7163 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007164 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 clearopbeep(cap->oap);
7166 }
7167}
7168
7169/*
7170 * Handle the "@r" command.
7171 */
7172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007173nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 if (checkclearop(cap->oap))
7176 return;
7177#ifdef FEAT_EVAL
7178 if (cap->nchar == '=')
7179 {
7180 if (get_expr_register() == NUL)
7181 return;
7182 }
7183#endif
7184 while (cap->count1-- && !got_int)
7185 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007186 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 clearopbeep(cap->oap);
7189 break;
7190 }
7191 line_breakcheck();
7192 }
7193}
7194
7195/*
7196 * Handle the CTRL-U and CTRL-D commands.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7202 || (cap->cmdchar == Ctrl_D
7203 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7204 clearopbeep(cap->oap);
7205 else if (!checkclearop(cap->oap))
7206 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7207}
7208
7209/*
7210 * Handle "J" or "gJ" command.
7211 */
7212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007213nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007217 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
7219 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7222 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007225 if (cap->count0 <= 2)
7226 {
7227 clearopbeep(cap->oap);
7228 return;
7229 }
7230 cap->count0 = curbuf->b_ml.ml_line_count
7231 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007233
7234 prep_redo(cap->oap->regname, cap->count0,
7235 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7236 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238}
7239
7240/*
7241 * "P", "gP", "p" and "gp" commands.
7242 */
7243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007244nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007246 nv_put_opt(cap, FALSE);
7247}
7248
7249/*
7250 * "P", "gP", "p" and "gp" commands.
7251 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7252 */
7253 static void
7254nv_put_opt(cmdarg_T *cap, int fix_indent)
7255{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 int regname = 0;
7257 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007258 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007259 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 int dir;
7261 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007262 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 if (cap->oap->op_type != OP_NOP)
7265 {
7266#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007267 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7269 {
7270 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007271 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273 else
7274#endif
7275 clearopbeep(cap->oap);
7276 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007277#ifdef FEAT_JOB_CHANNEL
7278 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7279 {
7280 clearopbeep(cap->oap);
7281 }
7282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else
7284 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007285 if (fix_indent)
7286 {
7287 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7288 ? FORWARD : BACKWARD;
7289 flags |= PUT_FIXINDENT;
7290 }
7291 else
7292 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007293 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7294 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 prep_redo_cmd(cap);
7296 if (cap->cmdchar == 'g')
7297 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007298 else if (cap->cmdchar == 'z')
7299 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (VIsual_active)
7302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007303 // Putting in Visual mode: The put text replaces the selected
7304 // text. First delete the selected text, then put the new text.
7305 // Need to save and restore the registers that the delete
7306 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007307 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007309 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007312#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007313 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007314 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007315#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 )
7320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007321 // The delete is going to overwrite the register we want to
7322 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 reg1 = get_register(regname, TRUE);
7324 }
7325
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007326 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 cap->cmdchar = 'd';
7328 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007329 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007330 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 nv_operator(cap);
7332 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007333 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007334 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 cap->oap->regname = regname;
7338
7339 if (reg1 != NULL)
7340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007341 // Delete probably changed the register we want to put, save
7342 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 reg2 = get_register(regname, FALSE);
7344 put_register(regname, reg1);
7345 }
7346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007347 // When deleted a linewise Visual area, put the register as
7348 // lines to avoid it joined with the next line. When deletion was
7349 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (VIsual_mode == 'V')
7351 flags |= PUT_LINE;
7352 else if (VIsual_mode == 'v')
7353 flags |= PUT_LINE_SPLIT;
7354 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7355 flags |= PUT_LINE_FORWARD;
7356 dir = BACKWARD;
7357 if ((VIsual_mode != 'V'
7358 && curwin->w_cursor.col < curbuf->b_op_start.col)
7359 || (VIsual_mode == 'V'
7360 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // cursor is at the end of the line or end of file, put
7362 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007365 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007367 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007369 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (reg2 != NULL)
7371 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // What to reselect with "gv"? Selecting the just put text seems to
7374 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007375 if (was_visual)
7376 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007377 curbuf->b_visual.vi_start = curbuf->b_op_start;
7378 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007379 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007380 if (*p_sel == 'e')
7381 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007382 }
7383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007384 // When all lines were selected and deleted do_put() leaves an empty
7385 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007386 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007387 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007388 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007389 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007390
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007391 // If the cursor was in that line, move it to the end of the last
7392 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007393 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7394 {
7395 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7396 coladvance((colnr_T)MAXCOL);
7397 }
7398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 auto_format(FALSE, TRUE);
7400 }
7401}
7402
7403/*
7404 * "o" and "O" commands.
7405 */
7406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007407nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007410 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7412 {
7413 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007414 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416 else
7417#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007418 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007420#ifdef FEAT_JOB_CHANNEL
7421 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007422 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 n_opencmd(cap);
7426}
7427
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#ifdef FEAT_NETBEANS_INTG
7429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007430nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 netbeans_keycommand(cap->nchar);
7433}
7434#endif
7435
7436#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007438nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007440 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441}
7442#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007443
Bram Moolenaar3918c952005-03-15 22:34:55 +00007444/*
7445 * Trigger CursorHold event.
7446 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7447 * input buffer. "did_cursorhold" is set to avoid retriggering.
7448 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007450nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007451{
7452 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7453 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007454 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007455}