blob: 773c667a8b515e87d0731dc7742efb9c484e69aa [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;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002473 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#ifdef FEAT_DIFF
2475 int prev_topfill = curwin->w_topfill;
2476#endif
2477 linenr_T prev_lnum = curwin->w_cursor.lnum;
2478
2479 if (up)
2480 scrollup(count, TRUE);
2481 else
2482 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002483 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002485 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2486 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 cursor_correct();
2488 check_cursor_moved(curwin);
2489 curwin->w_valid |= VALID_TOPLINE;
2490
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002491 // If moved back to where we were, at least move the cursor, otherwise
2492 // we get stuck at one position. Don't move the cursor up if the
2493 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002495 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496#ifdef FEAT_DIFF
2497 && curwin->w_topfill == prev_topfill
2498#endif
2499 )
2500 {
2501 if (up)
2502 {
2503 if (curwin->w_cursor.lnum > prev_lnum
2504 || cursor_down(1L, FALSE) == FAIL)
2505 break;
2506 }
2507 else
2508 {
2509 if (curwin->w_cursor.lnum < prev_lnum
2510 || prev_topline == 1L
2511 || cursor_up(1L, FALSE) == FAIL)
2512 break;
2513 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002514 // Mark w_topline as valid, otherwise the screen jumps back at the
2515 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 check_cursor_moved(curwin);
2517 curwin->w_valid |= VALID_TOPLINE;
2518 }
2519 }
2520 if (curwin->w_cursor.lnum != prev_lnum)
2521 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002522 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523}
2524
2525/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002526 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2527 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2528 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002529 */
2530 static int
2531nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2532{
2533 int nchar = *nchar_arg;
2534 long n;
2535
2536 // "z123{nchar}": edit the count before obtaining {nchar}
2537 if (checkclearop(cap->oap))
2538 return FALSE;
2539 n = nchar - '0';
2540
2541 for (;;)
2542 {
2543#ifdef USE_ON_FLY_SCROLL
2544 dont_scroll = TRUE; // disallow scrolling here
2545#endif
2546 ++no_mapping;
2547 ++allow_keys; // no mapping for nchar, but allow key codes
2548 nchar = plain_vgetc();
2549 LANGMAP_ADJUST(nchar, TRUE);
2550 --no_mapping;
2551 --allow_keys;
2552#ifdef FEAT_CMDL_INFO
2553 (void)add_to_showcmd(nchar);
2554#endif
2555 if (nchar == K_DEL || nchar == K_KDEL)
2556 n /= 10;
2557 else if (VIM_ISDIGIT(nchar))
2558 n = n * 10 + (nchar - '0');
2559 else if (nchar == CAR)
2560 {
2561#ifdef FEAT_GUI
2562 need_mouse_correct = TRUE;
2563#endif
2564 win_setheight((int)n);
2565 break;
2566 }
2567 else if (nchar == 'l'
2568 || nchar == 'h'
2569 || nchar == K_LEFT
2570 || nchar == K_RIGHT)
2571 {
2572 cap->count1 = n ? n * cap->count1 : cap->count1;
2573 *nchar_arg = nchar;
2574 return TRUE;
2575 }
2576 else
2577 {
2578 clearopbeep(cap->oap);
2579 break;
2580 }
2581 }
2582 cap->oap->op_type = OP_NOP;
2583 return FALSE;
2584}
2585
2586#ifdef FEAT_SPELL
2587/*
2588 * "zug" and "zuw": undo "zg" and "zw"
2589 * "zg": add good word to word list
2590 * "zw": add wrong word to word list
2591 * "zG": add good word to temp word list
2592 * "zW": add wrong word to temp word list
2593 */
2594 static int
2595nv_zg_zw(cmdarg_T *cap, int nchar)
2596{
2597 char_u *ptr = NULL;
2598 int len;
2599 int undo = FALSE;
2600
2601 if (nchar == 'u')
2602 {
2603 ++no_mapping;
2604 ++allow_keys; // no mapping for nchar, but allow key codes
2605 nchar = plain_vgetc();
2606 LANGMAP_ADJUST(nchar, TRUE);
2607 --no_mapping;
2608 --allow_keys;
2609#ifdef FEAT_CMDL_INFO
2610 (void)add_to_showcmd(nchar);
2611#endif
2612 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2613 {
2614 clearopbeep(cap->oap);
2615 return OK;
2616 }
2617 undo = TRUE;
2618 }
2619
2620 if (checkclearop(cap->oap))
2621 return OK;
2622 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2623 return FAIL;
2624 if (ptr == NULL)
2625 {
2626 pos_T pos = curwin->w_cursor;
2627
2628 // Find bad word under the cursor. When 'spell' is
2629 // off this fails and find_ident_under_cursor() is
2630 // used below.
2631 emsg_off++;
2632 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2633 emsg_off--;
2634 if (len != 0 && curwin->w_cursor.col <= pos.col)
2635 ptr = ml_get_pos(&curwin->w_cursor);
2636 curwin->w_cursor = pos;
2637 }
2638
2639 if (ptr == NULL
2640 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2641 return FAIL;
2642 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2643 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2644 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2645
2646 return OK;
2647}
2648#endif
2649
2650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 * Commands that start with "z".
2652 */
2653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002654nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 long n;
2657 colnr_T col;
2658 int nchar = cap->nchar;
2659#ifdef FEAT_FOLDING
2660 long old_fdl = curwin->w_p_fdl;
2661 int old_fen = curwin->w_p_fen;
2662#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002663 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002665 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (
2669#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2671 // and "zC" only in Visual mode. "zj" and "zk" are motion
2672 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 cap->nchar != 'f' && cap->nchar != 'F'
2674 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2675 && cap->nchar != 'j' && cap->nchar != 'k'
2676 &&
2677#endif
2678 checkclearop(cap->oap))
2679 return;
2680
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002681 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2682 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2684 && cap->count0
2685 && cap->count0 != curwin->w_cursor.lnum)
2686 {
2687 setpcmark();
2688 if (cap->count0 > curbuf->b_ml.ml_line_count)
2689 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2690 else
2691 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002692 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694
2695 switch (nchar)
2696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 case '+':
2699 if (cap->count0 == 0)
2700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // No count given: put cursor at the line below screen
2702 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2704 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2705 else
2706 curwin->w_cursor.lnum = curwin->w_botline;
2707 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 case NL:
2710 case CAR:
2711 case K_KENTER:
2712 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002716 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002717 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 break;
2719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002725 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002726 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 break;
2728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // "z^", "z-" and "zb": put cursor at bottom of screen
2730 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2731 // when <count> is at bottom of window, and puts that one at
2732 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (cap->count0 != 0)
2734 {
2735 scroll_cursor_bot(0, TRUE);
2736 curwin->w_cursor.lnum = curwin->w_topline;
2737 }
2738 else if (curwin->w_topline == 1)
2739 curwin->w_cursor.lnum = 1;
2740 else
2741 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 case '-':
2744 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002748 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002749 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 break;
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002754 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002757 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 case 'h':
2759 case K_LEFT:
2760 if (!curwin->w_p_wrap)
2761 {
2762 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2763 curwin->w_leftcol = 0;
2764 else
2765 curwin->w_leftcol -= (colnr_T)cap->count1;
2766 leftcol_changed();
2767 }
2768 break;
2769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002771 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 case 'l':
2776 case K_RIGHT:
2777 if (!curwin->w_p_wrap)
2778 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 curwin->w_leftcol += (colnr_T)cap->count1;
2781 leftcol_changed();
2782 }
2783 break;
2784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 case 's': if (!curwin->w_p_wrap)
2787 {
2788#ifdef FEAT_FOLDING
2789 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792#endif
2793 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002794 if ((long)col > siso)
2795 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else
2797 col = 0;
2798 if (curwin->w_leftcol != col)
2799 {
2800 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002801 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803 }
2804 break;
2805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 case 'e': if (!curwin->w_p_wrap)
2808 {
2809#ifdef FEAT_FOLDING
2810 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002811 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
2813#endif
2814 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002815 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002816 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 col = 0;
2818 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002819 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (curwin->w_leftcol != col)
2821 {
2822 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002823 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 }
2826 break;
2827
Christian Brabandt2fa93842021-05-30 22:17:25 +02002828 // "zp", "zP" in block mode put without addind trailing spaces
2829 case 'P':
2830 case 'p': nv_put(cap);
2831 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002832 // "zy" Yank without trailing spaces
2833 case 'y': nv_operator(cap);
2834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002836 // "zF": create fold command
2837 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case 'F':
2839 case 'f': if (foldManualAllowed(TRUE))
2840 {
2841 cap->nchar = 'f';
2842 nv_operator(cap);
2843 curwin->w_p_fen = TRUE;
2844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2847 {
2848 nv_operator(cap);
2849 finish_op = TRUE;
2850 }
2851 }
2852 else
2853 clearopbeep(cap->oap);
2854 break;
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // "zd": delete fold at cursor
2857 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 case 'd':
2859 case 'D': if (foldManualAllowed(FALSE))
2860 {
2861 if (VIsual_active)
2862 nv_operator(cap);
2863 else
2864 deleteFold(curwin->w_cursor.lnum,
2865 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2866 }
2867 break;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case 'E': if (foldmethodIsManual(curwin))
2871 {
2872 clearFolding(curwin);
2873 changed_window_setting();
2874 }
2875 else if (foldmethodIsMarker(curwin))
2876 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2877 TRUE, FALSE);
2878 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002879 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 break;
2881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 case 'n': curwin->w_p_fen = FALSE;
2884 break;
2885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 case 'N': curwin->w_p_fen = TRUE;
2888 break;
2889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2892 break;
2893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2896 openFold(curwin->w_cursor.lnum, cap->count1);
2897 else
2898 {
2899 closeFold(curwin->w_cursor.lnum, cap->count1);
2900 curwin->w_p_fen = TRUE;
2901 }
2902 break;
2903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2906 openFoldRecurse(curwin->w_cursor.lnum);
2907 else
2908 {
2909 closeFoldRecurse(curwin->w_cursor.lnum);
2910 curwin->w_p_fen = TRUE;
2911 }
2912 break;
2913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 case 'o': if (VIsual_active)
2916 nv_operator(cap);
2917 else
2918 openFold(curwin->w_cursor.lnum, cap->count1);
2919 break;
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case 'O': if (VIsual_active)
2923 nv_operator(cap);
2924 else
2925 openFoldRecurse(curwin->w_cursor.lnum);
2926 break;
2927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 case 'c': if (VIsual_active)
2930 nv_operator(cap);
2931 else
2932 closeFold(curwin->w_cursor.lnum, cap->count1);
2933 curwin->w_p_fen = TRUE;
2934 break;
2935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 case 'C': if (VIsual_active)
2938 nv_operator(cap);
2939 else
2940 closeFoldRecurse(curwin->w_cursor.lnum);
2941 curwin->w_p_fen = TRUE;
2942 break;
2943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 case 'v': foldOpenCursor();
2946 break;
2947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 curwin->w_foldinvalid = TRUE; // recompute folds
2951 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 foldOpenCursor();
2953 break;
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 curwin->w_foldinvalid = TRUE; // recompute folds
2958 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 break;
2960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002963 {
2964 curwin->w_p_fdl -= cap->count1;
2965 if (curwin->w_p_fdl < 0)
2966 curwin->w_p_fdl = 0;
2967 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 curwin->w_p_fen = TRUE;
2970 break;
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 curwin->w_p_fen = TRUE;
2976 break;
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002979 case 'r': curwin->w_p_fdl += cap->count1;
2980 {
2981 int d = getDeepestNesting();
2982
2983 if (curwin->w_p_fdl >= d)
2984 curwin->w_p_fdl = d;
2985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 break;
2987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 break;
2992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 case 'j': // "zj" move to next fold downwards
2994 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2996 cap->count1) == FAIL)
2997 clearopbeep(cap->oap);
2998 break;
2999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003002#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 case 'g': // "zg": add good word to word list
3005 case 'w': // "zw": add wrong word to word list
3006 case 'G': // "zG": add good word to temp word list
3007 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003008 if (nv_zg_zw(cap, nchar) == FAIL)
3009 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003010 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003013 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003014 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003016#endif
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 default: clearopbeep(cap->oap);
3019 }
3020
3021#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003022 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 if (old_fen != curwin->w_p_fen)
3024 {
3025# ifdef FEAT_DIFF
3026 win_T *wp;
3027
3028 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3029 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003030 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 FOR_ALL_WINDOWS(wp)
3032 {
3033 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3034 {
3035 wp->w_p_fen = curwin->w_p_fen;
3036 changed_window_setting_win(wp);
3037 }
3038 }
3039 }
3040# endif
3041 changed_window_setting();
3042 }
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (old_fdl != curwin->w_p_fdl)
3046 newFoldLevel();
3047#endif
3048}
3049
3050#ifdef FEAT_GUI
3051/*
3052 * Vertical scrollbar movement.
3053 */
3054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003055nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 if (cap->oap->op_type != OP_NOP)
3058 clearopbeep(cap->oap);
3059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 gui_do_scroll();
3062}
3063
3064/*
3065 * Horizontal scrollbar movement.
3066 */
3067 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003068nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 if (cap->oap->op_type != OP_NOP)
3071 clearopbeep(cap->oap);
3072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003074 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075}
3076#endif
3077
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003078#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003079/*
3080 * Click in GUI tab.
3081 */
3082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003083nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003084{
3085 if (cap->oap->op_type != OP_NOP)
3086 clearopbeep(cap->oap);
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003089 goto_tabpage(current_tab);
3090}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003091
3092/*
3093 * Selected item in tab line menu.
3094 */
3095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003096nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003097{
3098 if (cap->oap->op_type != OP_NOP)
3099 clearopbeep(cap->oap);
3100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003102 handle_tabmenu();
3103}
3104
3105/*
3106 * Handle selecting an item of the GUI tab line menu.
3107 * Used in Normal and Insert mode.
3108 */
3109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003110handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003111{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003112 switch (current_tabmenu)
3113 {
3114 case TABLINE_MENU_CLOSE:
3115 if (current_tab == 0)
3116 do_cmdline_cmd((char_u *)"tabclose");
3117 else
3118 {
3119 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3120 current_tab);
3121 do_cmdline_cmd(IObuff);
3122 }
3123 break;
3124
3125 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003126 if (current_tab == 0)
3127 do_cmdline_cmd((char_u *)"$tabnew");
3128 else
3129 {
3130 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3131 current_tab - 1);
3132 do_cmdline_cmd(IObuff);
3133 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003134 break;
3135
3136 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003137 if (current_tab == 0)
3138 do_cmdline_cmd((char_u *)"browse $tabnew");
3139 else
3140 {
3141 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3142 current_tab - 1);
3143 do_cmdline_cmd(IObuff);
3144 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003145 break;
3146 }
3147}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003148#endif
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150/*
3151 * "Q" command.
3152 */
3153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003154nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003156 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003158 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003159 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 do_exmode(FALSE);
3161}
3162
3163/*
3164 * Handle a ":" command.
3165 */
3166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003167nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003169 int old_p_im;
3170 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003171 int is_cmdkey = cap->cmdchar == K_COMMAND
3172 || cap->cmdchar == K_SCRIPT_COMMAND;
3173 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
Bram Moolenaar957cf672020-11-12 14:21:06 +01003175 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 nv_operator(cap);
3177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 if (cap->oap->op_type != OP_NOP)
3180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 cap->oap->motion_type = MCHAR;
3183 cap->oap->inclusive = FALSE;
3184 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003185 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003187 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 stuffcharReadbuff('.');
3189 if (cap->count0 > 1)
3190 {
3191 stuffReadbuff((char_u *)",.+");
3192 stuffnumReadbuff((long)cap->count0 - 1L);
3193 }
3194 }
3195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003196 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (KeyTyped)
3198 compute_cmdrow();
3199
3200 old_p_im = p_im;
3201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003202 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003203 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3204 if (is_cmdkey)
3205 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3206 else
3207 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (p_im != old_p_im)
3211 {
3212 if (p_im)
3213 restart_edit = 'i';
3214 else
3215 restart_edit = 0;
3216 }
3217
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003218 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003219 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003220 clearop(cap->oap);
3221 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3223 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003224 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3225 || did_emsg
3226 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 clearopbeep(cap->oap);
3229 }
3230}
3231
3232/*
3233 * Handle CTRL-G command.
3234 */
3235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003241 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 showmode();
3243 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003244 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003245 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 fileinfo((int)cap->count0, FALSE, TRUE);
3247}
3248
3249/*
3250 * Handle CTRL-H <Backspace> command.
3251 */
3252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003253nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (VIsual_active && VIsual_select)
3256 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 v_visop(cap);
3259 }
3260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 nv_left(cap);
3262}
3263
3264/*
3265 * CTRL-L: clear screen and redraw.
3266 */
3267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003268nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if (!checkclearop(cap->oap))
3271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003274 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003275# ifdef FEAT_RELTIME
3276 {
3277 win_T *wp;
3278
3279 FOR_ALL_WINDOWS(wp)
3280 wp->w_s->b_syn_slow = FALSE;
3281 }
3282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003284 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003285#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3286# ifdef VIMDLL
3287 if (!gui.in_use)
3288# endif
3289 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292}
3293
3294/*
3295 * CTRL-O: In Select mode: switch to Visual mode for one command.
3296 * Otherwise: Go to older pcmark.
3297 */
3298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 if (VIsual_active && VIsual_select)
3302 {
3303 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003304 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003306 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 cap->count1 = -cap->count1;
3311 nv_pcmark(cap);
3312 }
3313}
3314
3315/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003316 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3317 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 */
3319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003320nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 if (!checkclearopq(cap->oap))
3323 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3324 GETF_SETMARK|GETF_ALT, FALSE);
3325}
3326
3327/*
3328 * "Z" commands.
3329 */
3330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003331nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 if (!checkclearopq(cap->oap))
3334 {
3335 switch (cap->nchar)
3336 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003337 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 case 'Z': do_cmdline_cmd((char_u *)"x");
3339 break;
3340
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003341 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 case 'Q': do_cmdline_cmd((char_u *)"q!");
3343 break;
3344
3345 default: clearopbeep(cap->oap);
3346 }
3347 }
3348}
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350/*
3351 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3352 */
3353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 oparg_T oa;
3357 cmdarg_T ca;
3358
3359 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003360 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 ca.oap = &oa;
3362 ca.cmdchar = c1;
3363 ca.nchar = c2;
3364 nv_ident(&ca);
3365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003368 * 'K' normal-mode command. Get the command to lookup the keyword under the
3369 * cursor.
3370 */
3371 static int
3372nv_K_getcmd(
3373 cmdarg_T *cap,
3374 char_u *kp,
3375 int kp_help,
3376 int kp_ex,
3377 char_u **ptr_arg,
3378 int n,
3379 char_u *buf,
3380 unsigned buflen)
3381{
3382 char_u *ptr = *ptr_arg;
3383 int isman;
3384 int isman_s;
3385
3386 if (kp_help)
3387 {
3388 // in the help buffer
3389 STRCPY(buf, "he! ");
3390 return n;
3391 }
3392
3393 if (kp_ex)
3394 {
3395 // 'keywordprog' is an ex command
3396 if (cap->count0 != 0)
3397 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3398 else
3399 STRCPY(buf, kp);
3400 STRCAT(buf, " ");
3401 return n;
3402 }
3403
3404 // An external command will probably use an argument starting
3405 // with "-" as an option. To avoid trouble we skip the "-".
3406 while (*ptr == '-' && n > 0)
3407 {
3408 ++ptr;
3409 --n;
3410 }
3411 if (n == 0)
3412 {
3413 // found dashes only
3414 emsg(_(e_no_identifier_under_cursor));
3415 vim_free(buf);
3416 *ptr_arg = ptr;
3417 return 0;
3418 }
3419
3420 // When a count is given, turn it into a range. Is this
3421 // really what we want?
3422 isman = (STRCMP(kp, "man") == 0);
3423 isman_s = (STRCMP(kp, "man -s") == 0);
3424 if (cap->count0 != 0 && !(isman || isman_s))
3425 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3426
3427 STRCAT(buf, "! ");
3428 if (cap->count0 == 0 && isman_s)
3429 STRCAT(buf, "man");
3430 else
3431 STRCAT(buf, kp);
3432 STRCAT(buf, " ");
3433 if (cap->count0 != 0 && (isman || isman_s))
3434 {
3435 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3436 STRCAT(buf, " ");
3437 }
3438
3439 *ptr_arg = ptr;
3440 return n;
3441}
3442
3443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 * Handle the commands that use the word under the cursor.
3445 * [g] CTRL-] :ta to current identifier
3446 * [g] 'K' run program for current identifier
3447 * [g] '*' / to current identifier or string
3448 * [g] '#' ? to current identifier or string
3449 * g ']' :tselect for current identifier
3450 */
3451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003452nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 char_u *ptr = NULL;
3455 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003456 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003457 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 char_u *kp; // value of 'keywordprg'
3460 int kp_help; // 'keywordprg' is ":he"
3461 int kp_ex; // 'keywordprg' starts with ":"
3462 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003465 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 cmdchar = cap->nchar;
3471 g_cmd = TRUE;
3472 }
3473 else
3474 {
3475 cmdchar = cap->cmdchar;
3476 g_cmd = FALSE;
3477 }
3478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 cmdchar = '#';
3481
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003482 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (checkclearopq(cap->oap))
3488 return;
3489 }
3490
3491 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3492 (cmdchar == '*' || cmdchar == '#')
3493 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3494 {
3495 clearop(cap->oap);
3496 return;
3497 }
3498
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003499 // Allocate buffer to put the command in. Inserting backslashes can
3500 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3501 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3503 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3504 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003505 if (kp_help && *skipwhite(ptr) == NUL)
3506 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003507 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003508 return;
3509 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003510 kp_ex = (*kp == ':');
3511 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3512 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (buf == NULL)
3514 return;
3515 buf[0] = NUL;
3516
3517 switch (cmdchar)
3518 {
3519 case '*':
3520 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003521 // Put cursor at start of word, makes search skip the word
3522 // under the cursor.
3523 // Call setpcmark() first, so "*``" puts the cursor back where
3524 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 setpcmark();
3526 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3527
3528 if (!g_cmd && vim_iswordp(ptr))
3529 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003530 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 break;
3532
3533 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003534 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3535 if (n == 0)
3536 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 break;
3538
3539 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003540 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#ifdef FEAT_CSCOPE
3542 if (p_cst)
3543 STRCPY(buf, "cstag ");
3544 else
3545#endif
3546 STRCPY(buf, "ts ");
3547 break;
3548
3549 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003550 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (curbuf->b_help)
3552 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003554 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003555 if (g_cmd)
3556 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003557 else if (cap->count0 == 0)
3558 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003559 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003560 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003564 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003565 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003567 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003568 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003569 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003570 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003571 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003573 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003574 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003577 vim_free(buf);
3578 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003580 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003581 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003582 {
3583 vim_free(buf);
3584 vim_free(p);
3585 return;
3586 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003587 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003588 STRCAT(buf, p);
3589 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003591 else
3592 {
3593 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003594 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003595 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003596 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003597 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003598 {
3599 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003600 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003601 aux_ptr = (char_u *)"";
3602 else
3603 aux_ptr = (char_u *)"\\|\"\n[";
3604 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003605 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003606 aux_ptr = (char_u *)"\\|\"\n*?[";
3607
3608 p = buf + STRLEN(buf);
3609 while (n-- > 0)
3610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003611 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003612 if (vim_strchr(aux_ptr, *ptr) != NULL)
3613 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003614 // When current byte is a part of multibyte character, copy all
3615 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003616 if (has_mbyte)
3617 {
3618 int i;
3619 int len = (*mb_ptr2len)(ptr) - 1;
3620
3621 for (i = 0; i < len && n >= 1; ++i, --n)
3622 *p++ = *ptr++;
3623 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003624 *p++ = *ptr++;
3625 }
3626 *p = NUL;
3627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003629 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (cmdchar == '*' || cmdchar == '#')
3631 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003632 if (!g_cmd && (has_mbyte
3633 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3634 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003636
3637 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003638 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003640
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003641 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003644 {
3645 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003647 g_tag_at_cursor = FALSE;
3648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 vim_free(buf);
3651}
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653/*
3654 * Get visually selected text, within one line only.
3655 * Returns FAIL if more than one line selected.
3656 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003657 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003658get_visual_text(
3659 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003660 char_u **pp, // return: start of selected text
3661 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 if (VIsual_mode != 'V')
3664 unadjust_for_sel();
3665 if (VIsual.lnum != curwin->w_cursor.lnum)
3666 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003667 if (cap != NULL)
3668 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 return FAIL;
3670 }
3671 if (VIsual_mode == 'V')
3672 {
3673 *pp = ml_get_curline();
3674 *lenp = (int)STRLEN(*pp);
3675 }
3676 else
3677 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003678 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
3680 *pp = ml_get_pos(&curwin->w_cursor);
3681 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3682 }
3683 else
3684 {
3685 *pp = ml_get_pos(&VIsual);
3686 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3687 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003688 if (**pp == NUL)
3689 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003690 if (*lenp > 0)
3691 {
3692 if (has_mbyte)
3693 // Correct the length to include all bytes of the last
3694 // character.
3695 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3696 else if ((*pp)[*lenp - 1] == NUL)
3697 // Do not include a trailing NUL.
3698 *lenp -= 1;
3699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 reset_VIsual_and_resel();
3702 return OK;
3703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705/*
3706 * CTRL-T: backwards in tag stack
3707 */
3708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003709nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 if (!checkclearopq(cap->oap))
3712 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3713}
3714
3715/*
3716 * Handle scrolling command 'H', 'L' and 'M'.
3717 */
3718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003719nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
3721 int used = 0;
3722 long n;
3723#ifdef FEAT_FOLDING
3724 linenr_T lnum;
3725#endif
3726 int half;
3727
3728 cap->oap->motion_type = MLINE;
3729 setpcmark();
3730
3731 if (cap->cmdchar == 'L')
3732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003733 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 curwin->w_cursor.lnum = curwin->w_botline - 1;
3735 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3736 curwin->w_cursor.lnum = 1;
3737 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003738 {
3739#ifdef FEAT_FOLDING
3740 if (hasAnyFolding(curwin))
3741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003743 for (n = cap->count1 - 1; n > 0
3744 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3745 {
3746 (void)hasFolding(curwin->w_cursor.lnum,
3747 &curwin->w_cursor.lnum, NULL);
3748 --curwin->w_cursor.lnum;
3749 }
3750 }
3751 else
3752#endif
3753 curwin->w_cursor.lnum -= cap->count1 - 1;
3754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 else
3757 {
3758 if (cap->cmdchar == 'M')
3759 {
3760#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003761 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 used -= diff_check_fill(curwin, curwin->w_topline)
3763 - curwin->w_topfill;
3764#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003765 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3767 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3768 {
3769#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003770 // Count half he number of filler lines to be "below this
3771 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3773 + n) / 2 >= half)
3774 {
3775 --n;
3776 break;
3777 }
3778#endif
3779 used += plines(curwin->w_topline + n);
3780 if (used >= half)
3781 break;
3782#ifdef FEAT_FOLDING
3783 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3784 n = lnum - curwin->w_topline;
3785#endif
3786 }
3787 if (n > 0 && used > curwin->w_height)
3788 --n;
3789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003790 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003793#ifdef FEAT_FOLDING
3794 if (hasAnyFolding(curwin))
3795 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003796 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003797 lnum = curwin->w_topline;
3798 while (n-- > 0 && lnum < curwin->w_botline - 1)
3799 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003800 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003801 ++lnum;
3802 }
3803 n = lnum - curwin->w_topline;
3804 }
3805#endif
3806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 curwin->w_cursor.lnum = curwin->w_topline + n;
3808 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3809 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3810 }
3811
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003812 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003813 if (cap->oap->op_type == OP_NOP)
3814 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 beginline(BL_SOL | BL_FIX);
3816}
3817
3818/*
3819 * Cursor right commands.
3820 */
3821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003822nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003825 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003827 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003829 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003830 if (mod_mask & MOD_MASK_CTRL)
3831 cap->arg = TRUE;
3832 nv_wordcmd(cap);
3833 return;
3834 }
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 cap->oap->motion_type = MCHAR;
3837 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003838 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003840 // In virtual edit mode, there's no such thing as "past_line", as lines
3841 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003843 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 for (n = cap->count1; n > 0; --n)
3846 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003847 if ((!past_line && oneright() == FAIL)
3848 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003849 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003851 // <Space> wraps to next line if 'whichwrap' has 's'.
3852 // 'l' wraps to next line if 'whichwrap' has 'l'.
3853 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if ( ((cap->cmdchar == ' '
3855 && vim_strchr(p_ww, 's') != NULL)
3856 || (cap->cmdchar == 'l'
3857 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003858 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 && vim_strchr(p_ww, '>') != NULL))
3860 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003862 // When deleting we also count the NL as a character.
3863 // Set cap->oap->inclusive when last char in the line is
3864 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003865 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003867 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 cap->oap->inclusive = TRUE;
3869 else
3870 {
3871 ++curwin->w_cursor.lnum;
3872 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 curwin->w_set_curswant = TRUE;
3875 cap->oap->inclusive = FALSE;
3876 }
3877 continue;
3878 }
3879 if (cap->oap->op_type == OP_NOP)
3880 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003881 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (n == cap->count1)
3883 beep_flush();
3884 }
3885 else
3886 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003887 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 cap->oap->inclusive = TRUE;
3889 }
3890 break;
3891 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003892 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
3894 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (virtual_active())
3896 oneright();
3897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003900 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 ++curwin->w_cursor.col;
3903 }
3904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906#ifdef FEAT_FOLDING
3907 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3908 && cap->oap->op_type == OP_NOP)
3909 foldOpenCursor();
3910#endif
3911}
3912
3913/*
3914 * Cursor left commands.
3915 *
3916 * Returns TRUE when operator end should not be adjusted.
3917 */
3918 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003919nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921 long n;
3922
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003923 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003925 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003926 if (mod_mask & MOD_MASK_CTRL)
3927 cap->arg = 1;
3928 nv_bck_word(cap);
3929 return;
3930 }
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 cap->oap->motion_type = MCHAR;
3933 cap->oap->inclusive = FALSE;
3934 for (n = cap->count1; n > 0; --n)
3935 {
3936 if (oneleft() == FAIL)
3937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3939 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3940 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if ( (((cap->cmdchar == K_BS
3942 || cap->cmdchar == Ctrl_H)
3943 && vim_strchr(p_ww, 'b') != NULL)
3944 || (cap->cmdchar == 'h'
3945 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003946 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 && vim_strchr(p_ww, '<') != NULL))
3948 && curwin->w_cursor.lnum > 1)
3949 {
3950 --(curwin->w_cursor.lnum);
3951 coladvance((colnr_T)MAXCOL);
3952 curwin->w_set_curswant = TRUE;
3953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003954 // When the NL before the first char has to be deleted we
3955 // put the cursor on the NUL after the previous line.
3956 // This is a very special case, be careful!
3957 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 if ( (cap->oap->op_type == OP_DELETE
3959 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003960 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003962 char_u *cp = ml_get_cursor();
3963
3964 if (*cp != NUL)
3965 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003966 if (has_mbyte)
3967 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3968 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003969 ++curwin->w_cursor.col;
3970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 cap->retval |= CA_NO_ADJ_OP_END;
3972 }
3973 continue;
3974 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003975 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3977 beep_flush();
3978 break;
3979 }
3980 }
3981#ifdef FEAT_FOLDING
3982 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3983 && cap->oap->op_type == OP_NOP)
3984 foldOpenCursor();
3985#endif
3986}
3987
3988/*
3989 * Cursor up commands.
3990 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3991 */
3992 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003993nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 if (mod_mask & MOD_MASK_SHIFT)
3996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003997 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 cap->arg = BACKWARD;
3999 nv_page(cap);
4000 }
4001 else
4002 {
4003 cap->oap->motion_type = MLINE;
4004 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4005 clearopbeep(cap->oap);
4006 else if (cap->arg)
4007 beginline(BL_WHITE | BL_FIX);
4008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009}
4010
4011/*
4012 * Cursor down commands.
4013 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4014 */
4015 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004016nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 if (mod_mask & MOD_MASK_SHIFT)
4019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004021 cap->arg = FORWARD;
4022 nv_page(cap);
4023 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004024#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004025 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004026 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4027 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
4031#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004033 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 cmdwin_result = CAR;
4035 else
4036#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004037#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004038 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004039 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4040 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4041 {
4042 invoke_prompt_callback();
4043 if (restart_edit == 0)
4044 restart_edit = 'a';
4045 }
4046 else
4047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
4049 cap->oap->motion_type = MLINE;
4050 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4051 clearopbeep(cap->oap);
4052 else if (cap->arg)
4053 beginline(BL_WHITE | BL_FIX);
4054 }
4055 }
4056}
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058/*
4059 * Grab the file name under the cursor and edit it.
4060 */
4061 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063{
4064 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004065 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004067 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 if (curbuf_locked())
4070 {
4071 clearop(cap->oap);
4072 return;
4073 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004074#ifdef FEAT_PROP_POPUP
4075 if (ERROR_IF_TERM_POPUP_WINDOW)
4076 return;
4077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004079 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (ptr != NULL)
4082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004083 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004084 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004085 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004087 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004088 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004089 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004090 {
4091 curwin->w_cursor.lnum = lnum;
4092 check_cursor_lnum();
4093 beginline(BL_SOL | BL_FIX);
4094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 vim_free(ptr);
4096 }
4097 else
4098 clearop(cap->oap);
4099}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101/*
4102 * <End> command: to end of current line or last line.
4103 */
4104 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004105nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004107 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004109 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004111 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 nv_dollar(cap);
4114}
4115
4116/*
4117 * Handle the "$" command.
4118 */
4119 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004120nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121{
4122 cap->oap->motion_type = MCHAR;
4123 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004124 // In virtual mode when off the edge of a line and an operator
4125 // is pending (whew!) keep the cursor where it is.
4126 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (!virtual_active() || gchar_cursor() != NUL
4128 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004129 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (cursor_down((long)(cap->count1 - 1),
4131 cap->oap->op_type == OP_NOP) == FAIL)
4132 clearopbeep(cap->oap);
4133#ifdef FEAT_FOLDING
4134 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4135 foldOpenCursor();
4136#endif
4137}
4138
4139/*
4140 * Implementation of '?' and '/' commands.
4141 * If cap->arg is TRUE don't set PC mark.
4142 */
4143 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004144nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004147 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004151 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 cap->cmdchar = 'g';
4153 cap->nchar = '?';
4154 nv_operator(cap);
4155 return;
4156 }
4157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004158 // When using 'incsearch' the cursor may be moved to set a different search
4159 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004160 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 if (cap->searchbuf == NULL)
4163 {
4164 clearop(oap);
4165 return;
4166 }
4167
Bram Moolenaar46539112015-02-17 15:43:57 +01004168 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004169 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004170 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171}
4172
LemonBoya4399382022-04-09 21:04:08 +01004173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174/*
4175 * Handle "N" and "n" commands.
4176 * cap->arg is SEARCH_REV for "N", 0 for "n".
4177 */
4178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004179nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004181 pos_T old = curwin->w_cursor;
4182 int wrapped = FALSE;
4183 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004184
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004185 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004187 // Avoid getting stuck on the current cursor position, which can
4188 // happen when an offset is given and the cursor is on the last char
4189 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004190 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004191 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004192 cap->count1 -= 1;
4193 }
LemonBoya4399382022-04-09 21:04:08 +01004194
4195#ifdef FEAT_SEARCH_EXTRA
4196 // Redraw the window to refresh the highlighted matches.
4197 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004198 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200}
4201
4202/*
4203 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4204 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004205 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004207 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004208normal_search(
4209 cmdarg_T *cap,
4210 int dir,
4211 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004212 int opt, // extra flags for do_search()
4213 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004216 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004217#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004218 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 cap->oap->motion_type = MCHAR;
4222 cap->oap->inclusive = FALSE;
4223 cap->oap->use_reg_one = TRUE;
4224 curwin->w_set_curswant = TRUE;
4225
Bram Moolenaara80faa82020-04-12 19:37:17 +02004226 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004227 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004228 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4229 if (wrapped != NULL)
4230 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (i == 0)
4232 clearop(cap->oap);
4233 else
4234 {
4235 if (i == 2)
4236 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#ifdef FEAT_FOLDING
4239 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4240 foldOpenCursor();
4241#endif
4242 }
LemonBoya4399382022-04-09 21:04:08 +01004243#ifdef FEAT_SEARCH_EXTRA
4244 // Redraw the window to refresh the highlighted matches.
4245 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004246 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004249 // "/$" will put the cursor after the end of the line, may need to
4250 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004252 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253}
4254
4255/*
4256 * Character search commands.
4257 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4258 * ',' and FALSE for ';'.
4259 * cap->nchar is NUL for ',' and ';' (repeat the search)
4260 */
4261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004262nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 int t_cmd;
4265
4266 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4267 t_cmd = TRUE;
4268 else
4269 t_cmd = FALSE;
4270
4271 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4273 clearopbeep(cap->oap);
4274 else
4275 {
4276 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004277 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4279 && (t_cmd || cap->oap->op_type != OP_NOP))
4280 {
4281 colnr_T scol, ecol;
4282
4283 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4284 curwin->w_cursor.coladd = ecol - scol;
4285 }
4286 else
4287 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#ifdef FEAT_FOLDING
4290 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4291 foldOpenCursor();
4292#endif
4293 }
4294}
4295
4296/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004297 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4298 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4299 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4300 * "[m" or "]m" search for prev/next start of (Java) method.
4301 * "[M" or "]M" search for prev/next end of (Java) method.
4302 */
4303 static void
4304nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4305{
4306 pos_T new_pos = {0, 0, 0};
4307 pos_T *pos = NULL; // init for GCC
4308 pos_T prev_pos;
4309 long n;
4310 int findc;
4311 int c;
4312
4313 if (cap->nchar == '*')
4314 cap->nchar = '/';
4315 prev_pos.lnum = 0;
4316 if (cap->nchar == 'm' || cap->nchar == 'M')
4317 {
4318 if (cap->cmdchar == '[')
4319 findc = '{';
4320 else
4321 findc = '}';
4322 n = 9999;
4323 }
4324 else
4325 {
4326 findc = cap->nchar;
4327 n = cap->count1;
4328 }
4329 for ( ; n > 0; --n)
4330 {
4331 if ((pos = findmatchlimit(cap->oap, findc,
4332 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4333 {
4334 if (new_pos.lnum == 0) // nothing found
4335 {
4336 if (cap->nchar != 'm' && cap->nchar != 'M')
4337 clearopbeep(cap->oap);
4338 }
4339 else
4340 pos = &new_pos; // use last one found
4341 break;
4342 }
4343 prev_pos = new_pos;
4344 curwin->w_cursor = *pos;
4345 new_pos = *pos;
4346 }
4347 curwin->w_cursor = *old_pos;
4348
4349 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4350 // brought us to the match for "[m" and "]M" when inside a method.
4351 // Try finding the '{' or '}' we want to be at.
4352 // Also repeat for the given count.
4353 if (cap->nchar == 'm' || cap->nchar == 'M')
4354 {
4355 // norm is TRUE for "]M" and "[m"
4356 int norm = ((findc == '{') == (cap->nchar == 'm'));
4357
4358 n = cap->count1;
4359 // found a match: we were inside a method
4360 if (prev_pos.lnum != 0)
4361 {
4362 pos = &prev_pos;
4363 curwin->w_cursor = prev_pos;
4364 if (norm)
4365 --n;
4366 }
4367 else
4368 pos = NULL;
4369 while (n > 0)
4370 {
4371 for (;;)
4372 {
4373 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4374 {
4375 // if not found anything, that's an error
4376 if (pos == NULL)
4377 clearopbeep(cap->oap);
4378 n = 0;
4379 break;
4380 }
4381 c = gchar_cursor();
4382 if (c == '{' || c == '}')
4383 {
4384 // Must have found end/start of class: use it.
4385 // Or found the place to be at.
4386 if ((c == findc && norm) || (n == 1 && !norm))
4387 {
4388 new_pos = curwin->w_cursor;
4389 pos = &new_pos;
4390 n = 0;
4391 }
4392 // if no match found at all, we started outside of the
4393 // class and we're inside now. Just go on.
4394 else if (new_pos.lnum == 0)
4395 {
4396 new_pos = curwin->w_cursor;
4397 pos = &new_pos;
4398 }
4399 // found start/end of other method: go to match
4400 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004401 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4402 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004403 n = 0;
4404 else
4405 curwin->w_cursor = *pos;
4406 break;
4407 }
4408 }
4409 --n;
4410 }
4411 curwin->w_cursor = *old_pos;
4412 if (pos == NULL && new_pos.lnum != 0)
4413 clearopbeep(cap->oap);
4414 }
4415 if (pos != NULL)
4416 {
4417 setpcmark();
4418 curwin->w_cursor = *pos;
4419 curwin->w_set_curswant = TRUE;
4420#ifdef FEAT_FOLDING
4421 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4422 && cap->oap->op_type == OP_NOP)
4423 foldOpenCursor();
4424#endif
4425 }
4426}
4427
4428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * "[" and "]" commands.
4430 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4431 */
4432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004433nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004436 pos_T *pos = NULL; // init for GCC
4437 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 int flag;
4439 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 cap->oap->motion_type = MCHAR;
4442 cap->oap->inclusive = FALSE;
4443 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004444 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004446 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (cap->nchar == 'f')
4448 nv_gotofile(cap);
4449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004452 // Find the occurrence(s) of the identifier or define under cursor
4453 // in current and included files or jump to the first occurrence.
4454 //
4455 // search list jump
4456 // fwd bwd fwd bwd fwd bwd
4457 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4458 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004459 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
4461 char_u *ptr;
4462 int len;
4463
4464 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4465 clearop(cap->oap);
4466 else
4467 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004468 // Make a copy, if the line was changed it will be freed.
4469 ptr = vim_strnsave(ptr, len);
4470 if (ptr == NULL)
4471 return;
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 find_pattern_in_path(ptr, 0, len, TRUE,
4474 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4475 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4476 cap->count1,
4477 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4478 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4479 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4480 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004481 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 curwin->w_set_curswant = TRUE;
4483 }
4484 }
4485 else
4486#endif
4487
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004488 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4489 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4490 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4491 // "[m" or "]m" search for prev/next start of (Java) method.
4492 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if ( (cap->cmdchar == '['
4494 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4495 || (cap->cmdchar == ']'
4496 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004497 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004499 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else if (cap->nchar == '[' || cap->nchar == ']')
4501 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004502 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 flag = '{';
4504 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004508 // Imitate strange Vi behaviour: When using "]]" with an operator
4509 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004510 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 (cap->oap->op_type != OP_NOP
4512 && cap->arg == FORWARD && flag == '{')))
4513 clearopbeep(cap->oap);
4514 else
4515 {
4516 if (cap->oap->op_type == OP_NOP)
4517 beginline(BL_WHITE | BL_FIX);
4518#ifdef FEAT_FOLDING
4519 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4520 foldOpenCursor();
4521#endif
4522 }
4523 }
4524
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004525 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 else if (cap->nchar == 'p' || cap->nchar == 'P')
4527 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004528 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004531 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else if (cap->nchar == '\'' || cap->nchar == '`')
4533 {
4534 pos = &curwin->w_cursor;
4535 for (n = cap->count1; n > 0; --n)
4536 {
4537 prev_pos = *pos;
4538 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4539 cap->nchar == '\'');
4540 if (pos == NULL)
4541 break;
4542 }
4543 if (pos == NULL)
4544 pos = &prev_pos;
4545 nv_cursormark(cap, cap->nchar == '\'', pos);
4546 }
4547
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004548 // [ or ] followed by a middle mouse click: put selected text with
4549 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004550 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 (void)do_mouse(cap->oap, cap->nchar,
4553 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4554 cap->count1, PUT_FIXINDENT);
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004558 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 else if (cap->nchar == 'z')
4560 {
4561 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4562 cap->count1) == FAIL)
4563 clearopbeep(cap->oap);
4564 }
4565#endif
4566
4567#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004568 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else if (cap->nchar == 'c')
4570 {
4571 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4572 cap->count1) == FAIL)
4573 clearopbeep(cap->oap);
4574 }
4575#endif
4576
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004577#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004578 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579 else if (cap->nchar == 's' || cap->nchar == 'S')
4580 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004581 setpcmark();
4582 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004583 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4584 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004585 {
4586 clearopbeep(cap->oap);
4587 break;
4588 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004589 else
4590 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004591# ifdef FEAT_FOLDING
4592 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4593 foldOpenCursor();
4594# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 }
4596#endif
4597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004598 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 else
4600 clearopbeep(cap->oap);
4601}
4602
4603/*
4604 * Handle Normal mode "%" command.
4605 */
4606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004607nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004610#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 linenr_T lnum = curwin->w_cursor.lnum;
4612#endif
4613
4614 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 if (cap->count0 > 100)
4618 clearopbeep(cap->oap);
4619 else
4620 {
4621 cap->oap->motion_type = MLINE;
4622 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004623 // Round up, so 'normal 100%' always jumps at the line line.
4624 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4625 // overflow on 32-bits, so use a formula with less accuracy
4626 // to avoid overflows.
4627 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4629 / 100L * cap->count0;
4630 else
4631 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4632 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004633 if (curwin->w_cursor.lnum < 1)
4634 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4636 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4637 beginline(BL_SOL | BL_FIX);
4638 }
4639 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004640 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
4642 cap->oap->motion_type = MCHAR;
4643 cap->oap->use_reg_one = TRUE;
4644 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4645 clearopbeep(cap->oap);
4646 else
4647 {
4648 setpcmark();
4649 curwin->w_cursor = *pos;
4650 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 }
4655#ifdef FEAT_FOLDING
4656 if (cap->oap->op_type == OP_NOP
4657 && lnum != curwin->w_cursor.lnum
4658 && (fdo_flags & FDO_PERCENT)
4659 && KeyTyped)
4660 foldOpenCursor();
4661#endif
4662}
4663
4664/*
4665 * Handle "(" and ")" commands.
4666 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4667 */
4668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004669nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 cap->oap->motion_type = MCHAR;
4672 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004673 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004674 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 curwin->w_set_curswant = TRUE;
4676
4677 if (findsent(cap->arg, cap->count1) == FAIL)
4678 clearopbeep(cap->oap);
4679 else
4680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004681 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004682 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#ifdef FEAT_FOLDING
4685 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4686 foldOpenCursor();
4687#endif
4688 }
4689}
4690
4691/*
4692 * "m" command: Mark a position.
4693 */
4694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004695nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 if (!checkclearop(cap->oap))
4698 {
4699 if (setmark(cap->nchar) == FAIL)
4700 clearopbeep(cap->oap);
4701 }
4702}
4703
4704/*
4705 * "{" and "}" commands.
4706 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4707 */
4708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004709nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 cap->oap->motion_type = MCHAR;
4712 cap->oap->inclusive = FALSE;
4713 cap->oap->use_reg_one = TRUE;
4714 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004715 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 clearopbeep(cap->oap);
4717 else
4718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#ifdef FEAT_FOLDING
4721 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4722 foldOpenCursor();
4723#endif
4724 }
4725}
4726
4727/*
4728 * "u" command: Undo or make lower case.
4729 */
4730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004731nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004733 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 cap->cmdchar = 'g';
4737 cap->nchar = 'u';
4738 nv_operator(cap);
4739 }
4740 else
4741 nv_kundo(cap);
4742}
4743
4744/*
4745 * <Undo> command.
4746 */
4747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 if (!checkclearopq(cap->oap))
4751 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004752#ifdef FEAT_JOB_CHANNEL
4753 if (bt_prompt(curbuf))
4754 {
4755 clearopbeep(cap->oap);
4756 return;
4757 }
4758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 u_undo((int)cap->count1);
4760 curwin->w_set_curswant = TRUE;
4761 }
4762}
4763
4764/*
4765 * Handle the "r" command.
4766 */
4767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004768nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 char_u *ptr;
4771 int had_ctrl_v;
4772 long n;
4773
4774 if (checkclearop(cap->oap))
4775 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004776#ifdef FEAT_JOB_CHANNEL
4777 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4778 {
4779 clearopbeep(cap->oap);
4780 return;
4781 }
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004784 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 if (cap->nchar == Ctrl_V)
4786 {
4787 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004788 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (cap->nchar > DEL)
4791 had_ctrl_v = NUL;
4792 }
4793 else
4794 had_ctrl_v = NUL;
4795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004796 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004797 if (IS_SPECIAL(cap->nchar))
4798 {
4799 clearopbeep(cap->oap);
4800 return;
4801 }
4802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if (VIsual_active)
4805 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004806 if (got_int)
4807 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004808 if (had_ctrl_v)
4809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004810 // Use a special (negative) number to make a difference between a
4811 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004812 if (cap->nchar == CAR)
4813 cap->nchar = REPLACE_CR_NCHAR;
4814 else if (cap->nchar == NL)
4815 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 nv_operator(cap);
4818 return;
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004821 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (virtual_active())
4823 {
4824 if (u_save_cursor() == FAIL)
4825 return;
4826 if (gchar_cursor() == NUL)
4827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004828 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 coladvance_force((colnr_T)(getviscol() + cap->count1));
4830 curwin->w_cursor.col -= cap->count1;
4831 }
4832 else if (gchar_cursor() == TAB)
4833 coladvance_force(getviscol());
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004838 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004839 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 clearopbeep(cap->oap);
4842 return;
4843 }
4844
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004845 // Replacing with a TAB is done by edit() when it is complicated because
4846 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4847 // Other characters are done below to avoid problems with things like
4848 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4850 {
4851 stuffnumReadbuff(cap->count1);
4852 stuffcharReadbuff('R');
4853 stuffcharReadbuff('\t');
4854 stuffcharReadbuff(ESC);
4855 return;
4856 }
4857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (u_save_cursor() == FAIL)
4860 return;
4861
4862 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4863 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004864 // Replace character(s) by a single newline.
4865 // Strange vi behaviour: Only one newline is inserted.
4866 // Delete the characters here.
4867 // Insert the newline with an insert command, takes care of
4868 // autoindent. The insert command depends on being on the last
4869 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 stuffcharReadbuff('\r');
4872 stuffcharReadbuff(ESC);
4873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004874 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 invoke_edit(cap, TRUE, 'r', FALSE);
4876 }
4877 else
4878 {
4879 prep_redo(cap->oap->regname, cap->count1,
4880 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4881
4882 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (has_mbyte)
4884 {
4885 int old_State = State;
4886
4887 if (cap->ncharC1 != 0)
4888 AppendCharToRedobuff(cap->ncharC1);
4889 if (cap->ncharC2 != 0)
4890 AppendCharToRedobuff(cap->ncharC2);
4891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004892 // This is slow, but it handles replacing a single-byte with a
4893 // multi-byte and the other way around. Also handles adding
4894 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 for (n = cap->count1; n > 0; --n)
4896 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004897 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004898 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4899 {
4900 int c = ins_copychar(curwin->w_cursor.lnum
4901 + (cap->nchar == Ctrl_Y ? -1 : 1));
4902 if (c != NUL)
4903 ins_char(c);
4904 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004905 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004906 ++curwin->w_cursor.col;
4907 }
4908 else
4909 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 State = old_State;
4911 if (cap->ncharC1 != 0)
4912 ins_char(cap->ncharC1);
4913 if (cap->ncharC2 != 0)
4914 ins_char(cap->ncharC2);
4915 }
4916 }
4917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004919 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 for (n = cap->count1; n > 0; --n)
4921 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004922 // Get ptr again, because u_save and/or showmatch() will have
4923 // released the line. This may also happen in ins_copychar().
4924 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004925 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4926 {
4927 int c = ins_copychar(curwin->w_cursor.lnum
4928 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004929
4930 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004931 if (c != NUL)
4932 ptr[curwin->w_cursor.col] = c;
4933 }
4934 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004935 {
4936 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004937 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (p_sm && msg_silent == 0)
4940 showmatch(cap->nchar);
4941 ++curwin->w_cursor.col;
4942 }
4943#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004944 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004946 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
Bram Moolenaar009b2592004-10-24 19:18:58 +00004948 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004949 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004951 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953#endif
4954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004955 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 changed_bytes(curwin->w_cursor.lnum,
4957 (colnr_T)(curwin->w_cursor.col - cap->count1));
4958 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 --curwin->w_cursor.col; // cursor on the last replaced char
4960 // if the character on the left of the current cursor is a multi-byte
4961 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (has_mbyte)
4963 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 curbuf->b_op_end = curwin->w_cursor;
4965 curwin->w_set_curswant = TRUE;
4966 set_last_insert(cap->nchar);
4967 }
4968}
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970/*
4971 * 'o': Exchange start and end of Visual area.
4972 * 'O': same, but in block mode exchange left and right corners.
4973 */
4974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004975v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 pos_T old_cursor;
4978 colnr_T left, right;
4979
4980 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4981 {
4982 old_cursor = curwin->w_cursor;
4983 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4984 curwin->w_cursor.lnum = VIsual.lnum;
4985 coladvance(left);
4986 VIsual = curwin->w_cursor;
4987
4988 curwin->w_cursor.lnum = old_cursor.lnum;
4989 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004990 // 'selection "exclusive" and cursor at right-bottom corner: move it
4991 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4993 ++curwin->w_curswant;
4994 coladvance(curwin->w_curswant);
4995 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004997 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 curwin->w_cursor.lnum = VIsual.lnum;
5000 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5001 ++right;
5002 coladvance(right);
5003 VIsual = curwin->w_cursor;
5004
5005 curwin->w_cursor.lnum = old_cursor.lnum;
5006 coladvance(left);
5007 curwin->w_curswant = left;
5008 }
5009 }
5010 else
5011 {
5012 old_cursor = curwin->w_cursor;
5013 curwin->w_cursor = VIsual;
5014 VIsual = old_cursor;
5015 curwin->w_set_curswant = TRUE;
5016 }
5017}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019/*
5020 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5021 */
5022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005023nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005025 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 cap->cmdchar = 'c';
5028 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 VIsual_mode = 'V';
5031 nv_operator(cap);
5032 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005033 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005036 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
5038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (virtual_active())
5040 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5042 }
5043 }
5044}
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046/*
5047 * "gr".
5048 */
5049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005050nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (VIsual_active)
5053 {
5054 cap->cmdchar = 'r';
5055 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005056 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005058 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005061 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else
5063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005064 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005065 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 stuffcharReadbuff(cap->extra_char);
5067 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (virtual_active())
5069 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 invoke_edit(cap, TRUE, 'v', FALSE);
5071 }
5072 }
5073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075/*
5076 * Swap case for "~" command, when it does not work like an operator.
5077 */
5078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005079n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 long n;
5082 pos_T startpos;
5083 int did_change = 0;
5084#ifdef FEAT_NETBEANS_INTG
5085 pos_T pos;
5086 char_u *ptr;
5087 int count;
5088#endif
5089
5090 if (checkclearopq(cap->oap))
5091 return;
5092
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005093 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 clearopbeep(cap->oap);
5096 return;
5097 }
5098
5099 prep_redo_cmd(cap);
5100
5101 if (u_save_cursor() == FAIL)
5102 return;
5103
5104 startpos = curwin->w_cursor;
5105#ifdef FEAT_NETBEANS_INTG
5106 pos = startpos;
5107#endif
5108 for (n = cap->count1; n > 0; --n)
5109 {
5110 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5111 inc_cursor();
5112 if (gchar_cursor() == NUL)
5113 {
5114 if (vim_strchr(p_ww, '~') != NULL
5115 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5116 {
5117#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005118 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 if (did_change)
5121 {
5122 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005123 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005124 netbeans_removed(curbuf, pos.lnum, pos.col,
5125 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005126 // line may have been flushed, get it again
5127 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005129 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 pos.col = 0;
5132 pos.lnum++;
5133 }
5134#endif
5135 ++curwin->w_cursor.lnum;
5136 curwin->w_cursor.col = 0;
5137 if (n > 1)
5138 {
5139 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5140 break;
5141 u_clearline();
5142 }
5143 }
5144 else
5145 break;
5146 }
5147 }
5148#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005149 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 ptr = ml_get(pos.lnum);
5152 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005153 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5154 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156#endif
5157
5158
5159 check_cursor();
5160 curwin->w_set_curswant = TRUE;
5161 if (did_change)
5162 {
5163 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5164 0L);
5165 curbuf->b_op_start = startpos;
5166 curbuf->b_op_end = curwin->w_cursor;
5167 if (curbuf->b_op_end.col > 0)
5168 --curbuf->b_op_end.col;
5169 }
5170}
5171
5172/*
5173 * Move cursor to mark.
5174 */
5175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005176nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
5178 if (check_mark(pos) == FAIL)
5179 clearop(cap->oap);
5180 else
5181 {
5182 if (cap->cmdchar == '\''
5183 || cap->cmdchar == '`'
5184 || cap->cmdchar == '['
5185 || cap->cmdchar == ']')
5186 setpcmark();
5187 curwin->w_cursor = *pos;
5188 if (flag)
5189 beginline(BL_WHITE | BL_FIX);
5190 else
5191 check_cursor();
5192 }
5193 cap->oap->motion_type = flag ? MLINE : MCHAR;
5194 if (cap->cmdchar == '`')
5195 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005196 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 curwin->w_set_curswant = TRUE;
5198}
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200/*
5201 * Handle commands that are operators in Visual mode.
5202 */
5203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
5206 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005208 // Uppercase means linewise, except in block mode, then "D" deletes till
5209 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (isupper(cap->cmdchar))
5211 {
5212 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005213 {
5214 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5218 curwin->w_curswant = MAXCOL;
5219 }
5220 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5221 nv_operator(cap);
5222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224/*
5225 * "s" and "S" commands.
5226 */
5227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005228nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005230#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005231 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005232 if (term_swap_diff() == OK)
5233 return;
5234#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005235#ifdef FEAT_JOB_CHANNEL
5236 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5237 {
5238 clearopbeep(cap->oap);
5239 return;
5240 }
5241#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005242 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005245 {
5246 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 cap->cmdchar = 'c';
5250 nv_operator(cap);
5251 }
5252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 nv_optrans(cap);
5254}
5255
5256/*
5257 * Abbreviated commands.
5258 */
5259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005265 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (VIsual_active)
5267 v_visop(cap);
5268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 nv_optrans(cap);
5270}
5271
5272/*
5273 * Translate a command into another command.
5274 */
5275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005276nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5279 (char_u *)"d$", (char_u *)"c$",
5280 (char_u *)"cl", (char_u *)"cc",
5281 (char_u *)"yy", (char_u *)":s\r"};
5282 static char_u *str = (char_u *)"xXDCsSY&";
5283
5284 if (!checkclearopq(cap->oap))
5285 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005286 // In Vi "2D" doesn't delete the next line. Can't translate it
5287 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005288 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5289 {
5290 cap->oap->start = curwin->w_cursor;
5291 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005292#ifdef FEAT_EVAL
5293 set_op_var(OP_DELETE);
5294#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005295 cap->count1 = 1;
5296 nv_dollar(cap);
5297 finish_op = TRUE;
5298 ResetRedobuff();
5299 AppendCharToRedobuff('D');
5300 }
5301 else
5302 {
5303 if (cap->count0)
5304 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005305 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308 cap->opcount = 0;
5309}
5310
5311/*
5312 * "'" and "`" commands. Also for "g'" and "g`".
5313 * cap->arg is TRUE for "'" and "g'".
5314 */
5315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 pos_T *pos;
5319 int c;
5320#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005321 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005322 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323#endif
5324
5325 if (cap->cmdchar == 'g')
5326 c = cap->extra_char;
5327 else
5328 c = cap->nchar;
5329 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005330 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 if (cap->arg)
5333 {
5334 check_cursor_lnum();
5335 beginline(BL_WHITE | BL_FIX);
5336 }
5337 else
5338 check_cursor();
5339 }
5340 else
5341 nv_cursormark(cap, cap->arg, pos);
5342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 if (!virtual_active())
5345 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005346 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#ifdef FEAT_FOLDING
5348 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005349 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005350 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 && (fdo_flags & FDO_MARK)
5352 && old_KeyTyped)
5353 foldOpenCursor();
5354#endif
5355}
5356
5357/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005358 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
5360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005361nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005364#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005366 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369 if (!checkclearopq(cap->oap))
5370 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005371 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5372 {
5373 if (goto_tabpage_lastused() == FAIL)
5374 clearopbeep(cap->oap);
5375 return;
5376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (cap->cmdchar == 'g')
5378 pos = movechangelist((int)cap->count1);
5379 else
5380 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005381 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 curwin->w_set_curswant = TRUE;
5384 check_cursor();
5385 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005386 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 nv_cursormark(cap, FALSE, pos);
5388 else if (cap->cmdchar == 'g')
5389 {
5390 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005391 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005393 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005395 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 else
5398 clearopbeep(cap->oap);
5399# ifdef FEAT_FOLDING
5400 if (cap->oap->op_type == OP_NOP
5401 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5402 && (fdo_flags & FDO_MARK)
5403 && old_KeyTyped)
5404 foldOpenCursor();
5405# endif
5406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407}
5408
5409/*
5410 * Handle '"' command.
5411 */
5412 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005413nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 if (checkclearop(cap->oap))
5416 return;
5417#ifdef FEAT_EVAL
5418 if (cap->nchar == '=')
5419 cap->nchar = get_expr_register();
5420#endif
5421 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5422 {
5423 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005424 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#ifdef FEAT_EVAL
5426 set_reg_var(cap->oap->regname);
5427#endif
5428 }
5429 else
5430 clearopbeep(cap->oap);
5431}
5432
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433/*
5434 * Handle "v", "V" and "CTRL-V" commands.
5435 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5436 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005437 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 */
5439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005442 if (cap->cmdchar == Ctrl_Q)
5443 cap->cmdchar = Ctrl_V;
5444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005445 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5446 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (cap->oap->op_type != OP_NOP)
5448 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005449 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 return;
5452 }
5453
5454 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 else // toggle char/block mode
5460 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 VIsual_mode = cap->cmdchar;
5462 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005463 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005465 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005467 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
5469 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005470 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005472 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 VIsual = curwin->w_cursor;
5474
5475 VIsual_active = TRUE;
5476 VIsual_reselect = TRUE;
5477 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005478 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005481 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005482 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005483 // For V and ^V, we multiply the number of lines even if there
5484 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5486 {
5487 curwin->w_cursor.lnum +=
5488 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005489 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491 VIsual_mode = resel_VIsual_mode;
5492 if (VIsual_mode == 'v')
5493 {
5494 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005495 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005496 update_curswant_force();
5497 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005500 curwin->w_curswant = resel_VIsual_vcol;
5501 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005503 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 curwin->w_curswant = MAXCOL;
5506 coladvance((colnr_T)MAXCOL);
5507 }
5508 else if (VIsual_mode == Ctrl_V)
5509 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005510 update_curswant_force();
5511 curwin->w_curswant += + 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 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005738 int width1 = curwin->w_width - curwin_col_off();
5739 int width2 = width1 + curwin_col_off2();
5740 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005741
5742 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005743 virtcol = curwin->w_virtcol
5744#ifdef FEAT_PROP_POPUP
5745 - curwin->w_virtcol_first_char
5746#endif
5747 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005748 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005749 if (virtcol >= (colnr_T)width1 && width2 > 0)
5750 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005751 }
5752 else
5753 i = curwin->w_leftcol;
5754 // Go to the middle of the screen line. When 'number' or
5755 // 'relativenumber' is on and lines are wrapping the middle can be more
5756 // to the left.
5757 if (cap->nchar == 'm')
5758 i += (curwin->w_width - curwin_col_off()
5759 + ((curwin->w_p_wrap && i > 0)
5760 ? curwin_col_off2() : 0)) / 2;
5761 coladvance((colnr_T)i);
5762 if (flag)
5763 {
5764 do
5765 i = gchar_cursor();
5766 while (VIM_ISWHITE(i) && oneright() == OK);
5767 curwin->w_valid &= ~VALID_WCOL;
5768 }
5769 curwin->w_set_curswant = TRUE;
5770}
5771
5772/*
5773 * "g_": to the last non-blank character in the line or <count> lines
5774 * downward.
5775 */
5776 static void
5777nv_g_underscore_cmd(cmdarg_T *cap)
5778{
5779 char_u *ptr;
5780
5781 cap->oap->motion_type = MCHAR;
5782 cap->oap->inclusive = TRUE;
5783 curwin->w_curswant = MAXCOL;
5784 if (cursor_down((long)(cap->count1 - 1),
5785 cap->oap->op_type == OP_NOP) == FAIL)
5786 {
5787 clearopbeep(cap->oap);
5788 return;
5789 }
5790
5791 ptr = ml_get_curline();
5792
5793 // In Visual mode we may end up after the line.
5794 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5795 --curwin->w_cursor.col;
5796
5797 // Decrease the cursor column until it's on a non-blank.
5798 while (curwin->w_cursor.col > 0
5799 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5800 --curwin->w_cursor.col;
5801 curwin->w_set_curswant = TRUE;
5802 adjust_for_sel(cap);
5803}
5804
5805/*
5806 * "g$" : Like "$" but for screen lines.
5807 */
5808 static void
5809nv_g_dollar_cmd(cmdarg_T *cap)
5810{
5811 oparg_T *oap = cap->oap;
5812 int i;
5813 int col_off = curwin_col_off();
5814
5815 oap->motion_type = MCHAR;
5816 oap->inclusive = TRUE;
5817 if (curwin->w_p_wrap && curwin->w_width != 0)
5818 {
5819 curwin->w_curswant = MAXCOL; // so we stay at the end
5820 if (cap->count1 == 1)
5821 {
5822 int width1 = curwin->w_width - col_off;
5823 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005824 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005825
5826 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005827 virtcol = curwin->w_virtcol
5828#ifdef FEAT_PROP_POPUP
5829 - curwin->w_virtcol_first_char
5830#endif
5831 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005832 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005833 if (virtcol >= (colnr_T)width1)
5834 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005835 * width2;
5836 coladvance((colnr_T)i);
5837
5838 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005839 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005840 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5841 {
5842 // Check for landing on a character that got split at
5843 // the end of the line. We do not want to advance to
5844 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005845 if (curwin->w_virtcol
5846#ifdef FEAT_PROP_POPUP
5847 - curwin->w_virtcol_first_char
5848#endif
5849 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005850 --curwin->w_cursor.col;
5851 }
5852 }
5853 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5854 clearopbeep(oap);
5855 }
5856 else
5857 {
5858 if (cap->count1 > 1)
5859 // if it fails, let the cursor still move to the last char
5860 (void)cursor_down(cap->count1 - 1, FALSE);
5861
5862 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5863 coladvance((colnr_T)i);
5864
5865 // if the character doesn't fit move one back
5866 if (curwin->w_cursor.col > 0
5867 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5868 {
5869 colnr_T vcol;
5870
5871 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5872 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5873 --curwin->w_cursor.col;
5874 }
5875
5876 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005877 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005878 }
5879}
5880
5881/*
5882 * "gi": start Insert at the last position.
5883 */
5884 static void
5885nv_gi_cmd(cmdarg_T *cap)
5886{
5887 int i;
5888
5889 if (curbuf->b_last_insert.lnum != 0)
5890 {
5891 curwin->w_cursor = curbuf->b_last_insert;
5892 check_cursor_lnum();
5893 i = (int)STRLEN(ml_get_curline());
5894 if (curwin->w_cursor.col > (colnr_T)i)
5895 {
5896 if (virtual_active())
5897 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5898 curwin->w_cursor.col = i;
5899 }
5900 }
5901 cap->cmdchar = 'i';
5902 nv_edit(cap);
5903}
5904
5905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 * Commands starting with "g".
5907 */
5908 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005909nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
5914 switch (cap->nchar)
5915 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005916 case Ctrl_A:
5917 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005919 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005920 if (!VIsual_active && cap->nchar == Ctrl_A)
5921 vim_mem_profile_dump();
5922 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005924 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005925 if (VIsual_active)
5926 {
5927 cap->arg = TRUE;
5928 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005929 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005930 nv_addsub(cap);
5931 }
5932 else
5933 clearopbeep(oap);
5934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005936 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 case 'R':
5938 cap->arg = TRUE;
5939 nv_Replace(cap);
5940 break;
5941
5942 case 'r':
5943 nv_vreplace(cap);
5944 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
5946 case '&':
5947 do_cmdline_cmd((char_u *)"%s//~/&");
5948 break;
5949
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005950 // "gv": Reselect the previous Visual area. If Visual already active,
5951 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005953 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005955
5956 // "gV": Don't reselect the previous Visual area after a Select mode
5957 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 case 'V':
5959 VIsual_reselect = FALSE;
5960 break;
5961
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005962 // "gh": start Select mode.
5963 // "gH": start Select line mode.
5964 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 case K_BS:
5966 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005967 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 case 'h':
5969 case 'H':
5970 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 cap->cmdchar = cap->nchar + ('v' - 'h');
5972 cap->arg = TRUE;
5973 nv_visual(cap);
5974 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005976 // "gn", "gN" visually select next/previous search match
5977 // "gn" selects next match
5978 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005979 case 'N':
5980 case 'n':
5981 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005982 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005985 // "gj" and "gk" two new funny movement keys -- up and down
5986 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 case 'j':
5988 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005989 // with 'nowrap' it works just like the normal "j" command.
5990 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
5992 oap->motion_type = MLINE;
5993 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5994 }
5995 else
5996 i = nv_screengo(oap, FORWARD, cap->count1);
5997 if (i == FAIL)
5998 clearopbeep(oap);
5999 break;
6000
6001 case 'k':
6002 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006003 // with 'nowrap' it works just like the normal "k" command.
6004 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
6006 oap->motion_type = MLINE;
6007 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6008 }
6009 else
6010 i = nv_screengo(oap, BACKWARD, cap->count1);
6011 if (i == FAIL)
6012 clearopbeep(oap);
6013 break;
6014
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006015 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 case 'J':
6017 nv_join(cap);
6018 break;
6019
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006020 // "g0", "g^" : Like "0" and "^" but for screen lines.
6021 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 case '0':
6024 case 'm':
6025 case K_HOME:
6026 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006027 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 break;
6029
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006030 case 'M':
6031 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006032 oap->motion_type = MCHAR;
6033 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006034 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006035 if (cap->count0 > 0 && cap->count0 <= 100)
6036 coladvance((colnr_T)(i * cap->count0 / 100));
6037 else
6038 coladvance((colnr_T)(i / 2));
6039 curwin->w_set_curswant = TRUE;
6040 }
6041 break;
6042
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006043 // "g_": to the last non-blank character in the line or <count> lines
6044 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006046 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 break;
6048
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006049 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 case '$':
6051 case K_END:
6052 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006053 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 break;
6055
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006056 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 case '*':
6058 case '#':
6059#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006060 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006062 case Ctrl_RSB: // :tag or :tselect for current identifier
6063 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 nv_ident(cap);
6065 break;
6066
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006067 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 case 'e':
6069 case 'E':
6070 oap->motion_type = MCHAR;
6071 curwin->w_set_curswant = TRUE;
6072 oap->inclusive = TRUE;
6073 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6074 clearopbeep(oap);
6075 break;
6076
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006077 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006079 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 break;
6081
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006084 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 break;
6086
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006087 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 case 'I':
6089 beginline(0);
6090 if (!checkclearopq(oap))
6091 invoke_edit(cap, FALSE, 'g', FALSE);
6092 break;
6093
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006094 // "gf": goto file, edit file under cursor
6095 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006097 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 nv_gotofile(cap);
6099 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006101 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 case '\'':
6103 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006104 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 case '`':
6106 nv_gomark(cap);
6107 break;
6108
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006109 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006111 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 break;
6113
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006114 // "ga": Display the ascii value of the character under the
6115 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 case 'a':
6117 do_ascii(NULL);
6118 break;
6119
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006120 // "g8": Display the bytes used for the UTF-8 character under the
6121 // cursor. It is displayed in hex.
6122 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006124 if (cap->count0 == 8)
6125 utf_find_illegal();
6126 else
6127 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006130 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006131 case '<':
6132 show_sb_text();
6133 break;
6134
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006135 // "gg": Goto the first line in file. With a count it goes to
6136 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 case 'g':
6138 cap->arg = FALSE;
6139 nv_goto(cap);
6140 break;
6141
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006142 // Two-character operators:
6143 // "gq" Format text
6144 // "gw" Format text and keep cursor position
6145 // "g~" Toggle the case of the text.
6146 // "gu" Change text to lower case.
6147 // "gU" Change text to upper case.
6148 // "g?" rot13 encoding
6149 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 case 'q':
6151 case 'w':
6152 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006153 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 case '~':
6155 case 'u':
6156 case 'U':
6157 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006158 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 nv_operator(cap);
6160 break;
6161
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006162 // "gd": Find first occurrence of pattern under the cursor in the
6163 // current function
6164 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 case 'd':
6166 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006167 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 break;
6169
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006170 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 case K_MIDDLEMOUSE:
6172 case K_MIDDLEDRAG:
6173 case K_MIDDLERELEASE:
6174 case K_LEFTMOUSE:
6175 case K_LEFTDRAG:
6176 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006177 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 case K_RIGHTMOUSE:
6179 case K_RIGHTDRAG:
6180 case K_RIGHTRELEASE:
6181 case K_X1MOUSE:
6182 case K_X1DRAG:
6183 case K_X1RELEASE:
6184 case K_X2MOUSE:
6185 case K_X2DRAG:
6186 case K_X2RELEASE:
6187 mod_mask = MOD_MASK_CTRL;
6188 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6189 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
6191 case K_IGNORE:
6192 break;
6193
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006194 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 case 'p':
6196 case 'P':
6197 nv_put(cap);
6198 break;
6199
6200#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006201 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 case 'o':
6203 goto_byte(cap->count0);
6204 break;
6205#endif
6206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006207 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006209 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 do_exmode(TRUE);
6211 break;
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 case ',':
6214 nv_pcmark(cap);
6215 break;
6216
6217 case ';':
6218 cap->count1 = -cap->count1;
6219 nv_pcmark(cap);
6220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006223 if (!checkclearop(oap))
6224 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006226 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006227 if (!checkclearop(oap))
6228 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006229 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006230
Bram Moolenaar62a23252020-08-09 14:04:42 +02006231 case TAB:
6232 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6233 clearopbeep(oap);
6234 break;
6235
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006236 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006238 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006239 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006240 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006241 break;
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 default:
6244 clearopbeep(oap);
6245 break;
6246 }
6247}
6248
6249/*
6250 * Handle "o" and "O" commands.
6251 */
6252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006253n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006255#ifdef FEAT_CONCEAL
6256 linenr_T oldline = curwin->w_cursor.lnum;
6257#endif
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (!checkclearopq(cap->oap))
6260 {
6261#ifdef FEAT_FOLDING
6262 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 (void)hasFolding(curwin->w_cursor.lnum,
6265 &curwin->w_cursor.lnum, NULL);
6266 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006267 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 (void)hasFolding(curwin->w_cursor.lnum,
6269 NULL, &curwin->w_cursor.lnum);
6270#endif
6271 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6272 (cap->cmdchar == 'O' ? 1 : 0)),
6273 (linenr_T)(curwin->w_cursor.lnum +
6274 (cap->cmdchar == 'o' ? 1 : 0))
6275 ) == OK
6276 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006277 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006278 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006280#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006281 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006282 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006283#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006284#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006285 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006286 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006287 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006288#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006289 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006290 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6291 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6293 }
6294 }
6295}
6296
6297/*
6298 * "." command: redo last change.
6299 */
6300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006301nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 if (!checkclearopq(cap->oap))
6304 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006305 // If "restart_edit" is TRUE, the last but one command is repeated
6306 // instead of the last command (inserting text). This is used for
6307 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6309 clearopbeep(cap->oap);
6310 }
6311}
6312
6313/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006314 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 */
6316 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006317nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006319 if (VIsual_select && VIsual_active)
6320 {
6321 int reg;
6322 // Get register name
6323 ++no_mapping;
6324 ++allow_keys;
6325 reg = plain_vgetc();
6326 LANGMAP_ADJUST(reg, TRUE);
6327 --no_mapping;
6328 --allow_keys;
6329
6330 if (reg == '"')
6331 // the unnamed register is 0
6332 reg = 0;
6333
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006334 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006335 return;
6336 }
6337
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (!checkclearopq(cap->oap))
6339 {
6340 u_redo((int)cap->count1);
6341 curwin->w_set_curswant = TRUE;
6342 }
6343}
6344
6345/*
6346 * Handle "U" command.
6347 */
6348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006349nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006351 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006352 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006354 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 cap->cmdchar = 'g';
6356 cap->nchar = 'U';
6357 nv_operator(cap);
6358 }
6359 else if (!checkclearopq(cap->oap))
6360 {
6361 u_undoline();
6362 curwin->w_set_curswant = TRUE;
6363 }
6364}
6365
6366/*
6367 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6368 * single character.
6369 */
6370 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006373 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006374 {
6375#ifdef FEAT_JOB_CHANNEL
6376 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6377 {
6378 clearopbeep(cap->oap);
6379 return;
6380 }
6381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 else
6385 nv_operator(cap);
6386}
6387
6388/*
6389 * Handle an operator command.
6390 * The actual work is done by do_pending_operator().
6391 */
6392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006393nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 int op_type;
6396
6397 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006398#ifdef FEAT_JOB_CHANNEL
6399 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6400 {
6401 clearopbeep(cap->oap);
6402 return;
6403 }
6404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006406 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 nv_lineop(cap);
6408 else if (!checkclearop(cap->oap))
6409 {
6410 cap->oap->start = curwin->w_cursor;
6411 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006412#ifdef FEAT_EVAL
6413 set_op_var(op_type);
6414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416}
6417
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006418#ifdef FEAT_EVAL
6419/*
6420 * Set v:operator to the characters for "optype".
6421 */
6422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006424{
6425 char_u opchars[3];
6426
6427 if (optype == OP_NOP)
6428 set_vim_var_string(VV_OP, NULL, 0);
6429 else
6430 {
6431 opchars[0] = get_op_char(optype);
6432 opchars[1] = get_extra_op_char(optype);
6433 opchars[2] = NUL;
6434 set_vim_var_string(VV_OP, opchars, -1);
6435 }
6436}
6437#endif
6438
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439/*
6440 * Handle linewise operator "dd", "yy", etc.
6441 *
6442 * "_" is is a strange motion command that helps make operators more logical.
6443 * It is actually implemented, but not documented in the real Vi. This motion
6444 * command actually refers to "the current line". Commands like "dd" and "yy"
6445 * are really an alternate form of "d_" and "y_". It does accept a count, so
6446 * "d3_" works to delete 3 lines.
6447 */
6448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006449nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 cap->oap->motion_type = MLINE;
6452 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6453 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006454 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006455 && cap->oap->motion_force != 'v'
6456 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 || cap->oap->op_type == OP_LSHIFT
6458 || cap->oap->op_type == OP_RSHIFT)
6459 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006460 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 beginline(BL_WHITE | BL_FIX);
6462}
6463
6464/*
6465 * <Home> command.
6466 */
6467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006471 if (mod_mask & MOD_MASK_CTRL)
6472 nv_goto(cap);
6473 else
6474 {
6475 cap->count0 = 1;
6476 nv_pipe(cap);
6477 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006478 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6479 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480}
6481
6482/*
6483 * "|" command.
6484 */
6485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006486nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 cap->oap->motion_type = MCHAR;
6489 cap->oap->inclusive = FALSE;
6490 beginline(0);
6491 if (cap->count0 > 0)
6492 {
6493 coladvance((colnr_T)(cap->count0 - 1));
6494 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6495 }
6496 else
6497 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006498 // keep curswant at the column where we wanted to go, not where
6499 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 curwin->w_set_curswant = FALSE;
6501}
6502
6503/*
6504 * Handle back-word command "b" and "B".
6505 * cap->arg is 1 for "B"
6506 */
6507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006508nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 cap->oap->motion_type = MCHAR;
6511 cap->oap->inclusive = FALSE;
6512 curwin->w_set_curswant = TRUE;
6513 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6514 clearopbeep(cap->oap);
6515#ifdef FEAT_FOLDING
6516 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6517 foldOpenCursor();
6518#endif
6519}
6520
6521/*
6522 * Handle word motion commands "e", "E", "w" and "W".
6523 * cap->arg is TRUE for "E" and "W".
6524 */
6525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006526nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 int n;
6529 int word_end;
6530 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006531 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006533 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6535 word_end = TRUE;
6536 else
6537 word_end = FALSE;
6538 cap->oap->inclusive = word_end;
6539
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006540 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (!word_end && cap->oap->op_type == OP_CHANGE)
6542 {
6543 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006544 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006546 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006548 // Reproduce a funny Vi behaviour: "cw" on a blank only
6549 // changes one character, not all blanks until the start of
6550 // the next word. Only do this when the 'w' flag is included
6551 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6553 {
6554 cap->oap->inclusive = TRUE;
6555 cap->oap->motion_type = MCHAR;
6556 return;
6557 }
6558 }
6559 else
6560 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006561 // This is a little strange. To match what the real Vi does,
6562 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6563 // that we are not on a space or a TAB. This seems impolite
6564 // at first, but it's really more what we mean when we say
6565 // 'cw'.
6566 // Another strangeness: When standing on the end of a word
6567 // "ce" will change until the end of the next word, but "cw"
6568 // will change only one character! This is done by setting
6569 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 cap->oap->inclusive = TRUE;
6571 word_end = TRUE;
6572 flag = TRUE;
6573 }
6574 }
6575 }
6576
6577 cap->oap->motion_type = MCHAR;
6578 curwin->w_set_curswant = TRUE;
6579 if (word_end)
6580 n = end_word(cap->count1, cap->arg, flag, FALSE);
6581 else
6582 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006584 // Don't leave the cursor on the NUL past the end of line. Unless we
6585 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006586 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006587 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589 if (n == FAIL && cap->oap->op_type == OP_NOP)
6590 clearopbeep(cap->oap);
6591 else
6592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594#ifdef FEAT_FOLDING
6595 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6596 foldOpenCursor();
6597#endif
6598 }
6599}
6600
6601/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 * Used after a movement command: If the cursor ends up on the NUL after the
6603 * end of the line, may move it back to the last character and make the motion
6604 * inclusive.
6605 */
6606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006607adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006608{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006609 // The cursor cannot remain on the NUL when:
6610 // - the column is > 0
6611 // - not in Visual mode or 'selection' is "o"
6612 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006613 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006614 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006615 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006616 {
6617 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006618 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006619 if (has_mbyte)
6620 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006621 oap->inclusive = TRUE;
6622 }
6623}
6624
6625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 * "0" and "^" commands.
6627 * cap->arg is the argument for beginline().
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 cap->oap->motion_type = MCHAR;
6633 cap->oap->inclusive = FALSE;
6634 beginline(cap->arg);
6635#ifdef FEAT_FOLDING
6636 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6637 foldOpenCursor();
6638#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006639 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6640 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643/*
6644 * In exclusive Visual mode, may include the last character.
6645 */
6646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006647adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006650 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (has_mbyte)
6653 inc_cursor();
6654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 ++curwin->w_cursor.col;
6656 cap->oap->inclusive = FALSE;
6657 }
6658}
6659
6660/*
6661 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6662 * Should check VIsual_mode before calling this.
6663 * Returns TRUE when backed up to the previous line.
6664 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006666unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 pos_T *pp;
6669
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006670 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006672 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 pp = &curwin->w_cursor;
6674 else
6675 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 if (pp->coladd > 0)
6677 --pp->coladd;
6678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (pp->col > 0)
6680 {
6681 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006682 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684 else if (pp->lnum > 1)
6685 {
6686 --pp->lnum;
6687 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6688 return TRUE;
6689 }
6690 }
6691 return FALSE;
6692}
6693
6694/*
6695 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6696 */
6697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006698nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006703 VIsual_select_reg = 0;
6704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 else if (VIsual_reselect)
6706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006707 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 cap->arg = TRUE;
6709 nv_g_cmd(cap);
6710 }
6711}
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714/*
6715 * "G", "gg", CTRL-END, CTRL-HOME.
6716 * cap->arg is TRUE for "G".
6717 */
6718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006719nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 linenr_T lnum;
6722
6723 if (cap->arg)
6724 lnum = curbuf->b_ml.ml_line_count;
6725 else
6726 lnum = 1L;
6727 cap->oap->motion_type = MLINE;
6728 setpcmark();
6729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006730 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (cap->count0 != 0)
6732 lnum = cap->count0;
6733 if (lnum < 1L)
6734 lnum = 1L;
6735 else if (lnum > curbuf->b_ml.ml_line_count)
6736 lnum = curbuf->b_ml.ml_line_count;
6737 curwin->w_cursor.lnum = lnum;
6738 beginline(BL_SOL | BL_FIX);
6739#ifdef FEAT_FOLDING
6740 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6741 foldOpenCursor();
6742#endif
6743}
6744
6745/*
6746 * CTRL-\ in Normal mode.
6747 */
6748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6752 {
6753 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006754 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 restart_edit = 0;
6757#ifdef FEAT_CMDWIN
6758 if (cmdwin_type != 0)
6759 cmdwin_result = Ctrl_C;
6760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (VIsual_active)
6762 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006763 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006764 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006766 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (cap->nchar == Ctrl_G && p_im)
6768 restart_edit = 'a';
6769 }
6770 else
6771 clearopbeep(cap->oap);
6772}
6773
6774/*
6775 * ESC in Normal mode: beep, but don't flush buffers.
6776 * Don't even beep if we are canceling a command.
6777 */
6778 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006779nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int no_reason;
6782
6783 no_reason = (cap->oap->op_type == OP_NOP
6784 && cap->opcount == 0
6785 && cap->count0 == 0
6786 && cap->oap->regname == 0
6787 && !p_im);
6788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006789 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 if (restart_edit == 0
6792#ifdef FEAT_CMDWIN
6793 && cmdwin_type == 0
6794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006797 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006798 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6799
Bram Moolenaar5939c352022-09-24 12:50:45 +01006800 // The user may accidentally do "vim file | grep word" and then
6801 // CTRL-C doesn't show anything. With a changed buffer give the
6802 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006803 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006804 {
6805 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6806
6807 if (out_redir)
6808 mch_errmsg(ms);
6809 else
6810 msg(ms);
6811 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006812 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006813 {
6814 if (out_redir)
6815 {
6816 got_int = FALSE;
6817 do_cmdline_cmd((char_u *)"qa");
6818 }
6819 else
6820 msg(_("Type :qa and press <Enter> to exit Vim"));
6821 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006824 if (restart_edit != 0)
6825 redraw_mode = TRUE; // remove "-- (insert) --"
6826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006827 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6828 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 if (!p_im)
6830 restart_edit = 0;
6831#ifdef FEAT_CMDWIN
6832 if (cmdwin_type != 0)
6833 {
6834 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006835 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 return;
6837 }
6838#endif
6839 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006840#ifdef FEAT_CMDWIN
Bram Moolenaar8d696372022-08-21 10:40:07 +01006841 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006842 {
6843 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006844 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6845 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006846 cmdwin_result = K_IGNORE;
6847 return;
6848 }
6849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (VIsual_active)
6852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006853 end_visual_mode(); // stop Visual
6854 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006856 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006858 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006859 {
6860#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006861 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006862 popup_hide_message_win();
6863 else
6864#endif
6865 vim_beep(BO_ESC);
6866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 clearop(cap->oap);
6868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006869 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6870 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006871 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 restart_edit = 'a';
6873}
6874
6875/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006876 * Move the cursor for the "A" command.
6877 */
6878 void
6879set_cursor_for_append_to_line(void)
6880{
6881 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006882 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006883 {
6884 int save_State = State;
6885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006886 // Pretend Insert mode here to allow the cursor on the
6887 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006888 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006889 coladvance((colnr_T)MAXCOL);
6890 State = save_State;
6891 }
6892 else
6893 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6894}
6895
6896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006898 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 */
6900 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006901nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006903 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6905 cap->cmdchar = 'i';
6906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006907 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006909 {
6910#ifdef FEAT_TERMINAL
6911 if (term_in_normal_mode())
6912 {
6913 end_visual_mode();
6914 clearop(cap->oap);
6915 term_enter_job_mode();
6916 return;
6917 }
6918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006922 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006923 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6924 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006928#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006929 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006930 {
6931 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006932 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006933 return;
6934 }
6935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 else if (!curbuf->b_p_ma && !p_im)
6937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006938 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006939 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006941 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006942 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006943 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006945 else if (cap->cmdchar == K_PS && VIsual_active)
6946 {
6947 pos_T old_pos = curwin->w_cursor;
6948 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006949 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006951 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006952 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6953 {
6954 shift_delete_registers();
6955 cap->oap->regname = '1';
6956 }
6957 else
6958 cap->oap->regname = '-';
6959 cap->cmdchar = 'd';
6960 cap->nchar = NUL;
6961 nv_operator(cap);
6962 do_pending_operator(cap, 0, FALSE);
6963 cap->cmdchar = K_PS;
6964
zeertzjq7a732522022-03-14 20:46:41 +00006965 if (*ml_get_cursor() != NUL)
6966 {
6967 if (old_visual_mode == 'V')
6968 {
6969 // In linewise Visual mode insert before the beginning of the
6970 // next line.
6971 // When the last line in the buffer was deleted then create a
6972 // new line, otherwise there is not need to move cursor.
6973 // Detect this by checking if cursor moved above Visual area.
6974 if (curwin->w_cursor.lnum < old_pos.lnum
6975 && curwin->w_cursor.lnum < old_visual.lnum)
6976 {
6977 if (u_save_cursor() == OK)
6978 {
6979 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6980 FALSE);
6981 appended_lines(curwin->w_cursor.lnum++, 1L);
6982 }
6983 }
6984 }
6985 // When the last char in the line was deleted then append.
6986 // Detect this by checking if cursor moved before Visual area.
6987 else if (curwin->w_cursor.col < old_pos.col
6988 && curwin->w_cursor.col < old_visual.col)
6989 inc_cursor();
6990 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006992 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006993 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 else if (!checkclearopq(cap->oap))
6996 {
6997 switch (cap->cmdchar)
6998 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006999 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007000 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 break;
7002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007004 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7005 beginline(BL_WHITE);
7006 else
7007 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 break;
7009
Bram Moolenaara1891842017-02-04 21:34:31 +01007010 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 // Bracketed paste works like "a"ppend, unless the cursor is in
7012 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007013 if (curwin->w_cursor.col == 0)
7014 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 case 'a': // "a"ppend is like "i"nsert on the next character.
7018 // increment coladd when in virtual space, increment the
7019 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (virtual_active()
7021 && (curwin->w_cursor.coladd > 0
7022 || *ml_get_cursor() == NUL
7023 || *ml_get_cursor() == TAB))
7024 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007025 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 inc_cursor();
7027 break;
7028 }
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7031 {
7032 int save_State = State;
7033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007034 // Pretend Insert mode here to allow the cursor on the
7035 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007036 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 coladvance(getviscol());
7038 State = save_State;
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
7041 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7042 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007043 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007044 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007045 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046}
7047
7048/*
7049 * Invoke edit() and take care of "restart_edit" and the return value.
7050 */
7051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007052invoke_edit(
7053 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007055 int cmd,
7056 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058 int restart_edit_save = 0;
7059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7061 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7062 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (repl || !stuff_empty())
7064 restart_edit_save = restart_edit;
7065 else
7066 restart_edit_save = 0;
7067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 restart_edit = 0;
7070
7071 if (edit(cmd, startln, cap->count1))
7072 cap->retval |= CA_COMMAND_BUSY;
7073
7074 if (restart_edit == 0)
7075 restart_edit = restart_edit_save;
7076}
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078/*
7079 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7080 */
7081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007082nv_object(
7083 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 int flag;
7086 int include;
7087 char_u *mps_save;
7088
7089 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 mps_save = curbuf->b_p_mps;
7096 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7097
7098 switch (cap->nchar)
7099 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 flag = current_word(cap->oap, cap->count1, include, FALSE);
7102 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 flag = current_word(cap->oap, cap->count1, include, TRUE);
7105 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 case '(':
7108 case ')':
7109 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7110 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007111 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 case '{':
7113 case '}':
7114 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7115 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007116 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 case ']':
7118 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7119 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007120 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 case '>':
7122 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7123 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007124#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 case 't': // "at" = a tag block (xml and html)
7126 // Do not adjust oap->end in do_pending_operator()
7127 // otherwise there are different results for 'dit'
7128 // (note leading whitespace in last line):
7129 // 1) <b> 2) <b>
7130 // foobar foobar
7131 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007132 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007133 flag = current_tagblock(cap->oap, cap->count1, include);
7134 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007135#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 flag = current_par(cap->oap, cap->count1, include, 'p');
7138 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007139 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 flag = current_sent(cap->oap, cap->count1, include);
7141 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142 case '"': // "a"" = a double quoted string
7143 case '\'': // "a'" = a single quoted string
7144 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007145 flag = current_quote(cap->oap, cap->count1, include,
7146 cap->nchar);
7147 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148#if 0 // TODO
7149 case 'S': // "aS" = a section
7150 case 'f': // "af" = a filename
7151 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152#endif
7153 default:
7154 flag = FAIL;
7155 break;
7156 }
7157
7158 curbuf->b_p_mps = mps_save;
7159 if (flag == FAIL)
7160 clearopbeep(cap->oap);
7161 adjust_cursor_col();
7162 curwin->w_set_curswant = TRUE;
7163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
7165/*
7166 * "q" command: Start/stop recording.
7167 * "q:", "q/", "q?": edit command-line in command-line window.
7168 */
7169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007170nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
7172 if (cap->oap->op_type == OP_FORMAT)
7173 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007174 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 cap->cmdchar = 'g';
7176 cap->nchar = 'q';
7177 nv_operator(cap);
7178 }
7179 else if (!checkclearop(cap->oap))
7180 {
7181#ifdef FEAT_CMDWIN
7182 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7183 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007184 if (cmdwin_type != 0)
7185 {
7186 emsg(_(e_cmdline_window_already_open));
7187 return;
7188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 stuffcharReadbuff(cap->nchar);
7190 stuffcharReadbuff(K_CMDWIN);
7191 }
7192 else
7193#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007194 // (stop) recording into a named register, unless executing a
7195 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007196 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 clearopbeep(cap->oap);
7198 }
7199}
7200
7201/*
7202 * Handle the "@r" command.
7203 */
7204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007205nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 if (checkclearop(cap->oap))
7208 return;
7209#ifdef FEAT_EVAL
7210 if (cap->nchar == '=')
7211 {
7212 if (get_expr_register() == NUL)
7213 return;
7214 }
7215#endif
7216 while (cap->count1-- && !got_int)
7217 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007218 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 clearopbeep(cap->oap);
7221 break;
7222 }
7223 line_breakcheck();
7224 }
7225}
7226
7227/*
7228 * Handle the CTRL-U and CTRL-D commands.
7229 */
7230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007231nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7234 || (cap->cmdchar == Ctrl_D
7235 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7236 clearopbeep(cap->oap);
7237 else if (!checkclearop(cap->oap))
7238 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7239}
7240
7241/*
7242 * Handle "J" or "gJ" command.
7243 */
7244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007245nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007247 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007249 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
7251 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007252 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7254 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007256 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007257 if (cap->count0 <= 2)
7258 {
7259 clearopbeep(cap->oap);
7260 return;
7261 }
7262 cap->count0 = curbuf->b_ml.ml_line_count
7263 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007265
7266 prep_redo(cap->oap->regname, cap->count0,
7267 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7268 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270}
7271
7272/*
7273 * "P", "gP", "p" and "gp" commands.
7274 */
7275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007276nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007278 nv_put_opt(cap, FALSE);
7279}
7280
7281/*
7282 * "P", "gP", "p" and "gp" commands.
7283 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7284 */
7285 static void
7286nv_put_opt(cmdarg_T *cap, int fix_indent)
7287{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 int regname = 0;
7289 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007290 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007291 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 int dir;
7293 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007294 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
7296 if (cap->oap->op_type != OP_NOP)
7297 {
7298#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007299 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7301 {
7302 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007303 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
7305 else
7306#endif
7307 clearopbeep(cap->oap);
7308 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007309#ifdef FEAT_JOB_CHANNEL
7310 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7311 {
7312 clearopbeep(cap->oap);
7313 }
7314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 else
7316 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007317 if (fix_indent)
7318 {
7319 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7320 ? FORWARD : BACKWARD;
7321 flags |= PUT_FIXINDENT;
7322 }
7323 else
7324 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007325 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7326 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 prep_redo_cmd(cap);
7328 if (cap->cmdchar == 'g')
7329 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007330 else if (cap->cmdchar == 'z')
7331 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (VIsual_active)
7334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007335 // Putting in Visual mode: The put text replaces the selected
7336 // text. First delete the selected text, then put the new text.
7337 // Need to save and restore the registers that the delete
7338 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007339 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007341 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007342#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007344#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007345 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007346 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007347#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 )
7352 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 // The delete is going to overwrite the register we want to
7354 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 reg1 = get_register(regname, TRUE);
7356 }
7357
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007358 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 cap->cmdchar = 'd';
7360 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007361 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007362 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 nv_operator(cap);
7364 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007365 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007366 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 cap->oap->regname = regname;
7370
7371 if (reg1 != NULL)
7372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // Delete probably changed the register we want to put, save
7374 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 reg2 = get_register(regname, FALSE);
7376 put_register(regname, reg1);
7377 }
7378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007379 // When deleted a linewise Visual area, put the register as
7380 // lines to avoid it joined with the next line. When deletion was
7381 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if (VIsual_mode == 'V')
7383 flags |= PUT_LINE;
7384 else if (VIsual_mode == 'v')
7385 flags |= PUT_LINE_SPLIT;
7386 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7387 flags |= PUT_LINE_FORWARD;
7388 dir = BACKWARD;
7389 if ((VIsual_mode != 'V'
7390 && curwin->w_cursor.col < curbuf->b_op_start.col)
7391 || (VIsual_mode == 'V'
7392 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007393 // cursor is at the end of the line or end of file, put
7394 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007396 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007397 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007399 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007401 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (reg2 != NULL)
7403 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007405 // What to reselect with "gv"? Selecting the just put text seems to
7406 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007407 if (was_visual)
7408 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007409 curbuf->b_visual.vi_start = curbuf->b_op_start;
7410 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007411 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007412 if (*p_sel == 'e')
7413 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007414 }
7415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007416 // When all lines were selected and deleted do_put() leaves an empty
7417 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007418 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007419 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007420 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007421 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007423 // If the cursor was in that line, move it to the end of the last
7424 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007425 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7426 {
7427 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7428 coladvance((colnr_T)MAXCOL);
7429 }
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 auto_format(FALSE, TRUE);
7432 }
7433}
7434
7435/*
7436 * "o" and "O" commands.
7437 */
7438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007439nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007442 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7444 {
7445 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007446 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
7448 else
7449#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007450 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007452#ifdef FEAT_JOB_CHANNEL
7453 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007454 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 n_opencmd(cap);
7458}
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460#ifdef FEAT_NETBEANS_INTG
7461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007462nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
7464 netbeans_keycommand(cap->nchar);
7465}
7466#endif
7467
7468#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007470nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007472 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473}
7474#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007475
Bram Moolenaar3918c952005-03-15 22:34:55 +00007476/*
7477 * Trigger CursorHold event.
7478 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7479 * input buffer. "did_cursorhold" is set to avoid retriggering.
7480 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007482nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007483{
7484 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7485 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007486 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007487}