blob: fb2bced04ac84bd3dd1d7a64d825945e4223ef02 [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 Moolenaar6e0ce172019-12-05 20:12:41 +01001469 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
1471 AppendCharToRedobuff('"');
1472 AppendCharToRedobuff(regname);
1473 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001474 if (num1 != 0)
1475 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (cmd1 != NUL)
1477 AppendCharToRedobuff(cmd1);
1478 if (cmd2 != NUL)
1479 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001480 if (num2 != 0)
1481 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (cmd3 != NUL)
1483 AppendCharToRedobuff(cmd3);
1484 if (cmd4 != NUL)
1485 AppendCharToRedobuff(cmd4);
1486 if (cmd5 != NUL)
1487 AppendCharToRedobuff(cmd5);
1488}
1489
1490/*
1491 * check for operator active and clear it
1492 *
1493 * return TRUE if operator was active
1494 */
1495 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001496checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 if (oap->op_type == OP_NOP)
1499 return FALSE;
1500 clearopbeep(oap);
1501 return TRUE;
1502}
1503
1504/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001505 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001507 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
1509 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001510checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001512 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return FALSE;
1514 clearopbeep(oap);
1515 return TRUE;
1516}
1517
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001519clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 oap->op_type = OP_NOP;
1522 oap->regname = 0;
1523 oap->motion_force = NUL;
1524 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001525 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526}
1527
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001529clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 clearop(oap);
1532 beep_flush();
1533}
1534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535/*
1536 * Remove the shift modifier from a special key.
1537 */
1538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001539unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 switch (cap->cmdchar)
1542 {
1543 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1544 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1545 case K_S_UP: cap->cmdchar = K_UP; break;
1546 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1547 case K_S_HOME: cap->cmdchar = K_HOME; break;
1548 case K_S_END: cap->cmdchar = K_END; break;
1549 }
1550 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001553/*
1554 * If the mode is currently displayed clear the command line or update the
1555 * command displayed.
1556 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001557 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001558may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001559{
1560 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001561 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001562#ifdef FEAT_CMDL_INFO
1563 else
1564 clear_showcmd();
1565#endif
1566}
1567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1569/*
1570 * Routines for displaying a partly typed command
1571 */
1572
kylo252ae6f1d82022-02-16 19:24:07 +00001573#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001575static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576static int showcmd_is_clear = TRUE;
1577static int showcmd_visual = FALSE;
1578
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001579static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001582clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 if (!p_sc)
1585 return;
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (VIsual_active && !char_avail())
1588 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001589 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long lines;
1591 colnr_T leftcol, rightcol;
1592 linenr_T top, bot;
1593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001594 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001595 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 top = VIsual.lnum;
1598 bot = curwin->w_cursor.lnum;
1599 }
1600 else
1601 {
1602 top = curwin->w_cursor.lnum;
1603 bot = VIsual.lnum;
1604 }
1605# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001606 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001607 (void)hasFolding(top, &top, NULL);
1608 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609# endif
1610 lines = bot - top + 1;
1611
1612 if (VIsual_mode == Ctrl_V)
1613 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001614# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001615 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001616 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001618 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001620 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001623# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001624 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001625 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1628 (long)(rightcol - leftcol + 1));
1629 }
1630 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1631 sprintf((char *)showcmd_buf, "%ld", lines);
1632 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001633 {
1634 char_u *s, *e;
1635 int l;
1636 int bytes = 0;
1637 int chars = 0;
1638
1639 if (cursor_bot)
1640 {
1641 s = ml_get_pos(&VIsual);
1642 e = ml_get_cursor();
1643 }
1644 else
1645 {
1646 s = ml_get_cursor();
1647 e = ml_get_pos(&VIsual);
1648 }
1649 while ((*p_sel != 'e') ? s <= e : s < e)
1650 {
1651 l = (*mb_ptr2len)(s);
1652 if (l == 0)
1653 {
1654 ++bytes;
1655 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001656 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001657 }
1658 bytes += l;
1659 ++chars;
1660 s += l;
1661 }
1662 if (bytes == chars)
1663 sprintf((char *)showcmd_buf, "%d", chars);
1664 else
1665 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1666 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001667 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 showcmd_visual = TRUE;
1669 }
1670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
1672 showcmd_buf[0] = NUL;
1673 showcmd_visual = FALSE;
1674
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001675 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (showcmd_is_clear)
1677 return;
1678 }
1679
1680 display_showcmd();
1681}
1682
1683/*
1684 * Add 'c' to string of shown command chars.
1685 * Return TRUE if output has been written (and setcursor() has been called).
1686 */
1687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 char_u *p;
1691 int old_len;
1692 int extra_len;
1693 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 int i;
1695 static int ignore[] =
1696 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001697#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1699 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001700#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001701 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001702 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1704 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001705 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001707 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 0
1709 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar09df3122006-01-23 22:23:09 +00001711 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 return FALSE;
1713
1714 if (showcmd_visual)
1715 {
1716 showcmd_buf[0] = NUL;
1717 showcmd_visual = FALSE;
1718 }
1719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001720 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (IS_SPECIAL(c))
1722 for (i = 0; ignore[i] != 0; ++i)
1723 if (ignore[i] == c)
1724 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001727 if (*p == ' ')
1728 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 old_len = (int)STRLEN(showcmd_buf);
1730 extra_len = (int)STRLEN(p);
1731 overflow = old_len + extra_len - SHOWCMD_COLS;
1732 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001733 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1734 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 STRCAT(showcmd_buf, p);
1736
1737 if (char_avail())
1738 return FALSE;
1739
1740 display_showcmd();
1741
1742 return TRUE;
1743}
1744
1745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001746add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 if (!add_to_showcmd(c))
1749 setcursor();
1750}
1751
1752/*
1753 * Delete 'len' characters from the end of the shown command.
1754 */
1755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 int old_len;
1759
1760 if (!p_sc)
1761 return;
1762
1763 old_len = (int)STRLEN(showcmd_buf);
1764 if (len > old_len)
1765 len = old_len;
1766 showcmd_buf[old_len - len] = NUL;
1767
1768 if (!char_avail())
1769 display_showcmd();
1770}
1771
1772/*
1773 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1774 * something and there is a partial mapping.
1775 */
1776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001777push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 if (p_sc)
1780 STRCPY(old_showcmd_buf, showcmd_buf);
1781}
1782
1783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 if (!p_sc)
1787 return;
1788
1789 STRCPY(showcmd_buf, old_showcmd_buf);
1790
1791 display_showcmd();
1792}
1793
1794 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001795display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
1797 int len;
1798
1799 cursor_off();
1800
1801 len = (int)STRLEN(showcmd_buf);
1802 if (len == 0)
1803 showcmd_is_clear = TRUE;
1804 else
1805 {
1806 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1807 showcmd_is_clear = FALSE;
1808 }
1809
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001810 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1811 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001814 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
1819 * When "check" is FALSE, prepare for commands that scroll the window.
1820 * When "check" is TRUE, take care of scroll-binding after the window has
1821 * scrolled. Called from normal_cmd() and edit().
1822 */
1823 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001824do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 static win_T *old_curwin = NULL;
1827 static linenr_T old_topline = 0;
1828#ifdef FEAT_DIFF
1829 static int old_topfill = 0;
1830#endif
1831 static buf_T *old_buf = NULL;
1832 static colnr_T old_leftcol = 0;
1833
1834 if (check && curwin->w_p_scb)
1835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001836 // If a ":syncbind" command was just used, don't scroll, only reset
1837 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (did_syncbind)
1839 did_syncbind = FALSE;
1840 else if (curwin == old_curwin)
1841 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001842 // Synchronize other windows, as necessary according to
1843 // 'scrollbind'. Don't do this after an ":edit" command, except
1844 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if ((curwin->w_buffer == old_buf
1846#ifdef FEAT_DIFF
1847 || curwin->w_p_diff
1848#endif
1849 )
1850 && (curwin->w_topline != old_topline
1851#ifdef FEAT_DIFF
1852 || curwin->w_topfill != old_topfill
1853#endif
1854 || curwin->w_leftcol != old_leftcol))
1855 {
1856 check_scrollbind(curwin->w_topline - old_topline,
1857 (long)(curwin->w_leftcol - old_leftcol));
1858 }
1859 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001860 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001862 // When switching between windows, make sure that the relative
1863 // vertical offset is valid for the new window. The relative
1864 // offset is invalid whenever another 'scrollbind' window has
1865 // scrolled to a point that would force the current window to
1866 // scroll past the beginning or end of its buffer. When the
1867 // resync is performed, some of the other 'scrollbind' windows may
1868 // need to jump so that the current window's relative position is
1869 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1871 }
1872 curwin->w_scbind_pos = curwin->w_topline;
1873 }
1874
1875 old_curwin = curwin;
1876 old_topline = curwin->w_topline;
1877#ifdef FEAT_DIFF
1878 old_topfill = curwin->w_topfill;
1879#endif
1880 old_buf = curwin->w_buffer;
1881 old_leftcol = curwin->w_leftcol;
1882}
1883
1884/*
1885 * Synchronize any windows that have "scrollbind" set, based on the
1886 * number of rows by which the current window has changed
1887 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1888 */
1889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001890check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int want_ver;
1893 int want_hor;
1894 win_T *old_curwin = curwin;
1895 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int old_VIsual_select = VIsual_select;
1897 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 colnr_T tgt_leftcol = curwin->w_leftcol;
1899 long topline;
1900 long y;
1901
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001902 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1904#ifdef FEAT_DIFF
1905 want_ver |= old_curwin->w_p_diff;
1906#endif
1907 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1908
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001909 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001911 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001914 // skip original window and windows with 'noscrollbind'
1915 if (curwin == old_curwin || !curwin->w_p_scb)
1916 continue;
1917
1918 // do the vertical scroll
1919 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001922 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
zeertzjq101d57b2022-07-31 18:34:32 +01001924 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
zeertzjq101d57b2022-07-31 18:34:32 +01001926 else
1927#endif
1928 {
1929 curwin->w_scbind_pos += topline_diff;
1930 topline = curwin->w_scbind_pos;
1931 if (topline > curbuf->b_ml.ml_line_count)
1932 topline = curbuf->b_ml.ml_line_count;
1933 if (topline < 1)
1934 topline = 1;
1935
1936 y = topline - curwin->w_topline;
1937 if (y > 0)
1938 scrollup(y, FALSE);
1939 else
1940 scrolldown(-y, FALSE);
1941 }
1942
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001943 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001944 cursor_correct();
1945 curwin->w_redr_status = TRUE;
1946 }
1947
1948 // do the horizontal scroll
1949 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1950 {
1951 curwin->w_leftcol = tgt_leftcol;
1952 leftcol_changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 }
1955
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001956 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 VIsual_select = old_VIsual_select;
1958 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 curwin = old_curwin;
1960 curbuf = old_curbuf;
1961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963/*
1964 * Command character that's ignored.
1965 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001966 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001969nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001971 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972}
1973
1974/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001975 * Command character that doesn't do anything, but unlike nv_ignore() does
1976 * start edit(). Used for "startinsert" executed while starting up.
1977 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001979nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001980{
1981}
1982
1983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 * Command character doesn't exist.
1985 */
1986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001987nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 clearopbeep(cap->oap);
1990}
1991
1992/*
1993 * <Help> and <F1> commands.
1994 */
1995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001996nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 if (!checkclearopq(cap->oap))
1999 ex_help(NULL);
2000}
2001
2002/*
2003 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2004 */
2005 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002006nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002008#ifdef FEAT_JOB_CHANNEL
2009 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2010 clearopbeep(cap->oap);
2011 else
2012#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002013 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002014 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002015 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002016 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2017 op_addsub(cap->oap, cap->count1, cap->arg);
2018 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002019 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002020 else if (VIsual_active)
2021 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002022 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002023 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
2026/*
2027 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2028 */
2029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002033 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002034 if (mod_mask & MOD_MASK_CTRL)
2035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 if (cap->arg == BACKWARD)
2038 goto_tabpage(-(int)cap->count1);
2039 else
2040 goto_tabpage((int)cap->count0);
2041 }
2042 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002043 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045}
2046
2047/*
2048 * Implementation of "gd" and "gD" command.
2049 */
2050 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002051nv_gd(
2052 oparg_T *oap,
2053 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002054 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 char_u *ptr;
2058
Bram Moolenaard9d30582005-05-18 22:10:28 +00002059 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002060 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002061 == FAIL)
2062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002064 }
2065 else
2066 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002067#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002068 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2069 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002070#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002071 // clear any search statistics
2072 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2073 clear_cmdline = TRUE;
2074 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002075}
2076
2077/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002078 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2079 * otherwise.
2080 */
2081 static int
2082is_ident(char_u *line, int offset)
2083{
2084 int i;
2085 int incomment = FALSE;
2086 int instring = 0;
2087 int prev = 0;
2088
2089 for (i = 0; i < offset && line[i] != NUL; i++)
2090 {
2091 if (instring != 0)
2092 {
2093 if (prev != '\\' && line[i] == instring)
2094 instring = 0;
2095 }
2096 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2097 {
2098 instring = line[i];
2099 }
2100 else
2101 {
2102 if (incomment)
2103 {
2104 if (prev == '*' && line[i] == '/')
2105 incomment = FALSE;
2106 }
2107 else if (prev == '/' && line[i] == '*')
2108 {
2109 incomment = TRUE;
2110 }
2111 else if (prev == '/' && line[i] == '/')
2112 {
2113 return FALSE;
2114 }
2115 }
2116
2117 prev = line[i];
2118 }
2119
2120 return incomment == FALSE && instring == 0;
2121}
2122
2123/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002124 * Search for variable declaration of "ptr[len]".
2125 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2126 * current file ("gD").
2127 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002128 * Return FAIL when not found.
2129 */
2130 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131find_decl(
2132 char_u *ptr,
2133 int len,
2134 int locally,
2135 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002136 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002137{
2138 char_u *pat;
2139 pos_T old_pos;
2140 pos_T par_pos;
2141 pos_T found_pos;
2142 int t;
2143 int save_p_ws;
2144 int save_p_scs;
2145 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002146 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002147 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002148 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002149
2150 if ((pat = alloc(len + 7)) == NULL)
2151 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002153 // Put "\V" before the pattern to avoid that the special meaning of "."
2154 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002155 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2156 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 old_pos = curwin->w_cursor;
2158 save_p_ws = p_ws;
2159 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002160 p_ws = FALSE; // don't wrap around end of file now
2161 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002163 // With "gD" go to line 1.
2164 // With "gd" Search back for the start of the current function, then go
2165 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002166 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002168 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002170 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 else
2173 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002174 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2176 --curwin->w_cursor.lnum;
2177 }
2178 curwin->w_cursor.col = 0;
2179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002181 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002182 for (;;)
2183 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002184 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002185 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002186 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002188
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002189 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002190 {
2191 pos_T *pos;
2192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // Check that the block the match is in doesn't end before the
2194 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2196 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2197 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002198 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 // There can't be a useful match before the end of this block.
2200 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002201 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002202 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002203 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002204 }
2205
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002206 if (t == FAIL)
2207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002208 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002209 if (found_pos.lnum != 0)
2210 {
2211 curwin->w_cursor = found_pos;
2212 t = OK;
2213 }
2214 break;
2215 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002216 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002219 ++curwin->w_cursor.lnum;
2220 curwin->w_cursor.col = 0;
2221 continue;
2222 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002223 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 // If the current position is not a valid identifier and a previous
2226 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 if (!valid && found_pos.lnum != 0)
2228 {
2229 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 }
2232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002233 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 if (valid && !locally)
2235 break;
2236 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 if (found_pos.lnum != 0)
2240 curwin->w_cursor = found_pos;
2241 break;
2242 }
2243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // For finding a local variable and the match is before the "{" or
2245 // inside a comment, continue searching. For K&R style function
2246 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002247 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002248 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002249 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002250 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // Remove SEARCH_START from flags to avoid getting stuck at one
2252 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002253 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002255
2256 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002258 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 curwin->w_cursor = old_pos;
2260 }
2261 else
2262 {
2263 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002264 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 reset_search_dir();
2266 }
2267
2268 vim_free(pat);
2269 p_ws = save_p_ws;
2270 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002271
2272 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273}
2274
2275/*
2276 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2277 * lines rather than lines in the file.
2278 * 'dist' must be positive.
2279 *
2280 * Return OK if able to move cursor, FAIL otherwise.
2281 */
2282 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 int linelen = linetabsize(ml_get_curline());
2286 int retval = OK;
2287 int atend = FALSE;
2288 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 int col_off1; // margin offset for first screen line
2290 int col_off2; // margin offset for wrapped screen line
2291 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002292 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002295 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 col_off1 = curwin_col_off();
2298 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002299 width1 = curwin->w_width - col_off1;
2300 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002301 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002305 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002306 // Instead of sticking at the last character of the buffer line we
2307 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (curwin->w_curswant == MAXCOL)
2309 {
2310 atend = TRUE;
2311 validate_virtcol();
2312 if (width1 <= 0)
2313 curwin->w_curswant = 0;
2314 else
2315 {
2316 curwin->w_curswant = width1 - 1;
2317 if (curwin->w_virtcol > curwin->w_curswant)
2318 curwin->w_curswant += ((curwin->w_virtcol
2319 - curwin->w_curswant - 1) / width2 + 1) * width2;
2320 }
2321 }
2322 else
2323 {
2324 if (linelen > width1)
2325 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2326 else
2327 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002328 if (curwin->w_curswant >= (colnr_T)n)
2329 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
2332 while (dist--)
2333 {
2334 if (dir == BACKWARD)
2335 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002336 if ((long)curwin->w_curswant >= width1
2337#ifdef FEAT_FOLDING
2338 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2339#endif
2340 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002341 // Move back within the line. This can give a negative value
2342 // for w_curswant if width1 < width2 (with cpoptions+=n),
2343 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 curwin->w_curswant -= width2;
2345 else
2346 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // Move to the start of a closed fold. Don't do that when
2350 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (!(fdo_flags & FDO_ALL))
2352 (void)hasFolding(curwin->w_cursor.lnum,
2353 &curwin->w_cursor.lnum, NULL);
2354#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002355 if (curwin->w_cursor.lnum == 1)
2356 {
2357 retval = FAIL;
2358 break;
2359 }
2360 --curwin->w_cursor.lnum;
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 linelen = linetabsize(ml_get_curline());
2363 if (linelen > width1)
2364 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2365 + 1) * width2;
2366 }
2367 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
2370 if (linelen > width1)
2371 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2372 else
2373 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002374 if (curwin->w_curswant + width2 < (colnr_T)n
2375#ifdef FEAT_FOLDING
2376 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2377#endif
2378 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002379 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 curwin->w_curswant += width2;
2381 else
2382 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2387 &curwin->w_cursor.lnum);
2388#endif
2389 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2390 {
2391 retval = FAIL;
2392 break;
2393 }
2394 curwin->w_cursor.lnum++;
2395 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002396 // Check if the cursor has moved below the number display
2397 // when width1 < width2 (with cpoptions+=n). Subtract width2
2398 // to get a negative value for w_curswant, which will get
2399 // clipped to column 0.
2400 if (curwin->w_curswant >= width1)
2401 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002402 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 }
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002408 if (virtual_active() && atend)
2409 coladvance(MAXCOL);
2410 else
2411 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2414 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002415 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002416 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002417
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002418 // Check for landing on a character that got split at the end of the
2419 // last line. We want to advance a screenline, not end up in the same
2420 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002422 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002423#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002424 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2425 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002426#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002427
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002428 c = (*mb_ptr2char)(ml_get_cursor());
2429 if (dir == FORWARD && virtcol < curwin->w_curswant
2430 && (curwin->w_curswant <= (colnr_T)width1)
2431 && !vim_isprintc(c) && c > 255)
2432 oneright();
2433
Bram Moolenaar773b1582014-08-29 14:20:51 +02002434 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 && (curwin->w_curswant < (colnr_T)width1
2436 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2437 : ((curwin->w_curswant - width1) % width2
2438 > (colnr_T)width2 / 2)))
2439 --curwin->w_cursor.col;
2440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002443 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 return retval;
2446}
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448/*
2449 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2450 * cap->arg must be TRUE for CTRL-E.
2451 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002453nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 if (!checkclearop(cap->oap))
2456 scroll_redraw(cap->arg, cap->count1);
2457}
2458
2459/*
2460 * Scroll "count" lines up or down, and redraw.
2461 */
2462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 linenr_T prev_topline = curwin->w_topline;
2466#ifdef FEAT_DIFF
2467 int prev_topfill = curwin->w_topfill;
2468#endif
2469 linenr_T prev_lnum = curwin->w_cursor.lnum;
2470
2471 if (up)
2472 scrollup(count, TRUE);
2473 else
2474 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002475 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002477 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2478 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 cursor_correct();
2480 check_cursor_moved(curwin);
2481 curwin->w_valid |= VALID_TOPLINE;
2482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002483 // If moved back to where we were, at least move the cursor, otherwise
2484 // we get stuck at one position. Don't move the cursor up if the
2485 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 while (curwin->w_topline == prev_topline
2487#ifdef FEAT_DIFF
2488 && curwin->w_topfill == prev_topfill
2489#endif
2490 )
2491 {
2492 if (up)
2493 {
2494 if (curwin->w_cursor.lnum > prev_lnum
2495 || cursor_down(1L, FALSE) == FAIL)
2496 break;
2497 }
2498 else
2499 {
2500 if (curwin->w_cursor.lnum < prev_lnum
2501 || prev_topline == 1L
2502 || cursor_up(1L, FALSE) == FAIL)
2503 break;
2504 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002505 // Mark w_topline as valid, otherwise the screen jumps back at the
2506 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 check_cursor_moved(curwin);
2508 curwin->w_valid |= VALID_TOPLINE;
2509 }
2510 }
2511 if (curwin->w_cursor.lnum != prev_lnum)
2512 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002513 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514}
2515
2516/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002517 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2518 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2519 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002520 */
2521 static int
2522nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2523{
2524 int nchar = *nchar_arg;
2525 long n;
2526
2527 // "z123{nchar}": edit the count before obtaining {nchar}
2528 if (checkclearop(cap->oap))
2529 return FALSE;
2530 n = nchar - '0';
2531
2532 for (;;)
2533 {
2534#ifdef USE_ON_FLY_SCROLL
2535 dont_scroll = TRUE; // disallow scrolling here
2536#endif
2537 ++no_mapping;
2538 ++allow_keys; // no mapping for nchar, but allow key codes
2539 nchar = plain_vgetc();
2540 LANGMAP_ADJUST(nchar, TRUE);
2541 --no_mapping;
2542 --allow_keys;
2543#ifdef FEAT_CMDL_INFO
2544 (void)add_to_showcmd(nchar);
2545#endif
2546 if (nchar == K_DEL || nchar == K_KDEL)
2547 n /= 10;
2548 else if (VIM_ISDIGIT(nchar))
2549 n = n * 10 + (nchar - '0');
2550 else if (nchar == CAR)
2551 {
2552#ifdef FEAT_GUI
2553 need_mouse_correct = TRUE;
2554#endif
2555 win_setheight((int)n);
2556 break;
2557 }
2558 else if (nchar == 'l'
2559 || nchar == 'h'
2560 || nchar == K_LEFT
2561 || nchar == K_RIGHT)
2562 {
2563 cap->count1 = n ? n * cap->count1 : cap->count1;
2564 *nchar_arg = nchar;
2565 return TRUE;
2566 }
2567 else
2568 {
2569 clearopbeep(cap->oap);
2570 break;
2571 }
2572 }
2573 cap->oap->op_type = OP_NOP;
2574 return FALSE;
2575}
2576
2577#ifdef FEAT_SPELL
2578/*
2579 * "zug" and "zuw": undo "zg" and "zw"
2580 * "zg": add good word to word list
2581 * "zw": add wrong word to word list
2582 * "zG": add good word to temp word list
2583 * "zW": add wrong word to temp word list
2584 */
2585 static int
2586nv_zg_zw(cmdarg_T *cap, int nchar)
2587{
2588 char_u *ptr = NULL;
2589 int len;
2590 int undo = FALSE;
2591
2592 if (nchar == 'u')
2593 {
2594 ++no_mapping;
2595 ++allow_keys; // no mapping for nchar, but allow key codes
2596 nchar = plain_vgetc();
2597 LANGMAP_ADJUST(nchar, TRUE);
2598 --no_mapping;
2599 --allow_keys;
2600#ifdef FEAT_CMDL_INFO
2601 (void)add_to_showcmd(nchar);
2602#endif
2603 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2604 {
2605 clearopbeep(cap->oap);
2606 return OK;
2607 }
2608 undo = TRUE;
2609 }
2610
2611 if (checkclearop(cap->oap))
2612 return OK;
2613 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2614 return FAIL;
2615 if (ptr == NULL)
2616 {
2617 pos_T pos = curwin->w_cursor;
2618
2619 // Find bad word under the cursor. When 'spell' is
2620 // off this fails and find_ident_under_cursor() is
2621 // used below.
2622 emsg_off++;
2623 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2624 emsg_off--;
2625 if (len != 0 && curwin->w_cursor.col <= pos.col)
2626 ptr = ml_get_pos(&curwin->w_cursor);
2627 curwin->w_cursor = pos;
2628 }
2629
2630 if (ptr == NULL
2631 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2632 return FAIL;
2633 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2634 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2635 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2636
2637 return OK;
2638}
2639#endif
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Commands that start with "z".
2643 */
2644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002645nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 long n;
2648 colnr_T col;
2649 int nchar = cap->nchar;
2650#ifdef FEAT_FOLDING
2651 long old_fdl = curwin->w_p_fdl;
2652 int old_fen = curwin->w_p_fen;
2653#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002654 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002656 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (
2660#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002661 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2662 // and "zC" only in Visual mode. "zj" and "zk" are motion
2663 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 cap->nchar != 'f' && cap->nchar != 'F'
2665 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2666 && cap->nchar != 'j' && cap->nchar != 'k'
2667 &&
2668#endif
2669 checkclearop(cap->oap))
2670 return;
2671
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002672 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2673 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2675 && cap->count0
2676 && cap->count0 != curwin->w_cursor.lnum)
2677 {
2678 setpcmark();
2679 if (cap->count0 > curbuf->b_ml.ml_line_count)
2680 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2681 else
2682 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002683 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685
2686 switch (nchar)
2687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002688 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 case '+':
2690 if (cap->count0 == 0)
2691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // No count given: put cursor at the line below screen
2693 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2696 else
2697 curwin->w_cursor.lnum = curwin->w_botline;
2698 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 case NL:
2701 case CAR:
2702 case K_KENTER:
2703 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002707 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002708 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 break;
2710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 case 'z': scroll_cursor_halfway(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^", "z-" and "zb": put cursor at bottom of screen
2721 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2722 // when <count> is at bottom of window, and puts that one at
2723 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (cap->count0 != 0)
2725 {
2726 scroll_cursor_bot(0, TRUE);
2727 curwin->w_cursor.lnum = curwin->w_topline;
2728 }
2729 else if (curwin->w_topline == 1)
2730 curwin->w_cursor.lnum = 1;
2731 else
2732 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 case '-':
2735 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002739 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002740 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
2742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 case 'h':
2750 case K_LEFT:
2751 if (!curwin->w_p_wrap)
2752 {
2753 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2754 curwin->w_leftcol = 0;
2755 else
2756 curwin->w_leftcol -= (colnr_T)cap->count1;
2757 leftcol_changed();
2758 }
2759 break;
2760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 case 'l':
2767 case K_RIGHT:
2768 if (!curwin->w_p_wrap)
2769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 curwin->w_leftcol += (colnr_T)cap->count1;
2772 leftcol_changed();
2773 }
2774 break;
2775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002776 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 case 's': if (!curwin->w_p_wrap)
2778 {
2779#ifdef FEAT_FOLDING
2780 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else
2783#endif
2784 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002785 if ((long)col > siso)
2786 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 else
2788 col = 0;
2789 if (curwin->w_leftcol != col)
2790 {
2791 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002792 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 }
2795 break;
2796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 case 'e': if (!curwin->w_p_wrap)
2799 {
2800#ifdef FEAT_FOLDING
2801 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else
2804#endif
2805 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002806 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002807 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 col = 0;
2809 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002810 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (curwin->w_leftcol != col)
2812 {
2813 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002814 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816 }
2817 break;
2818
Christian Brabandt2fa93842021-05-30 22:17:25 +02002819 // "zp", "zP" in block mode put without addind trailing spaces
2820 case 'P':
2821 case 'p': nv_put(cap);
2822 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002823 // "zy" Yank without trailing spaces
2824 case 'y': nv_operator(cap);
2825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // "zF": create fold command
2828 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case 'F':
2830 case 'f': if (foldManualAllowed(TRUE))
2831 {
2832 cap->nchar = 'f';
2833 nv_operator(cap);
2834 curwin->w_p_fen = TRUE;
2835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002836 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2838 {
2839 nv_operator(cap);
2840 finish_op = TRUE;
2841 }
2842 }
2843 else
2844 clearopbeep(cap->oap);
2845 break;
2846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // "zd": delete fold at cursor
2848 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 case 'd':
2850 case 'D': if (foldManualAllowed(FALSE))
2851 {
2852 if (VIsual_active)
2853 nv_operator(cap);
2854 else
2855 deleteFold(curwin->w_cursor.lnum,
2856 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2857 }
2858 break;
2859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 case 'E': if (foldmethodIsManual(curwin))
2862 {
2863 clearFolding(curwin);
2864 changed_window_setting();
2865 }
2866 else if (foldmethodIsMarker(curwin))
2867 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2868 TRUE, FALSE);
2869 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002870 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 break;
2872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case 'n': curwin->w_p_fen = FALSE;
2875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'N': curwin->w_p_fen = TRUE;
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2883 break;
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2887 openFold(curwin->w_cursor.lnum, cap->count1);
2888 else
2889 {
2890 closeFold(curwin->w_cursor.lnum, cap->count1);
2891 curwin->w_p_fen = TRUE;
2892 }
2893 break;
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2897 openFoldRecurse(curwin->w_cursor.lnum);
2898 else
2899 {
2900 closeFoldRecurse(curwin->w_cursor.lnum);
2901 curwin->w_p_fen = TRUE;
2902 }
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'o': if (VIsual_active)
2907 nv_operator(cap);
2908 else
2909 openFold(curwin->w_cursor.lnum, cap->count1);
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'O': if (VIsual_active)
2914 nv_operator(cap);
2915 else
2916 openFoldRecurse(curwin->w_cursor.lnum);
2917 break;
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 case 'c': if (VIsual_active)
2921 nv_operator(cap);
2922 else
2923 closeFold(curwin->w_cursor.lnum, cap->count1);
2924 curwin->w_p_fen = TRUE;
2925 break;
2926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 case 'C': if (VIsual_active)
2929 nv_operator(cap);
2930 else
2931 closeFoldRecurse(curwin->w_cursor.lnum);
2932 curwin->w_p_fen = TRUE;
2933 break;
2934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 case 'v': foldOpenCursor();
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 curwin->w_foldinvalid = TRUE; // recompute folds
2942 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 foldOpenCursor();
2944 break;
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 curwin->w_foldinvalid = TRUE; // recompute folds
2949 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 break;
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002954 {
2955 curwin->w_p_fdl -= cap->count1;
2956 if (curwin->w_p_fdl < 0)
2957 curwin->w_p_fdl = 0;
2958 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 curwin->w_p_fen = TRUE;
2961 break;
2962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 curwin->w_p_fen = TRUE;
2967 break;
2968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002970 case 'r': curwin->w_p_fdl += cap->count1;
2971 {
2972 int d = getDeepestNesting();
2973
2974 if (curwin->w_p_fdl >= d)
2975 curwin->w_p_fdl = d;
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 break;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002981 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 break;
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 case 'j': // "zj" move to next fold downwards
2985 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2987 cap->count1) == FAIL)
2988 clearopbeep(cap->oap);
2989 break;
2990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002993#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 case 'g': // "zg": add good word to word list
2996 case 'w': // "zw": add wrong word to word list
2997 case 'G': // "zG": add good word to temp word list
2998 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002999 if (nv_zg_zw(cap, nchar) == FAIL)
3000 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003001 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003004 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003005 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003007#endif
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 default: clearopbeep(cap->oap);
3010 }
3011
3012#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 if (old_fen != curwin->w_p_fen)
3015 {
3016# ifdef FEAT_DIFF
3017 win_T *wp;
3018
3019 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 FOR_ALL_WINDOWS(wp)
3023 {
3024 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3025 {
3026 wp->w_p_fen = curwin->w_p_fen;
3027 changed_window_setting_win(wp);
3028 }
3029 }
3030 }
3031# endif
3032 changed_window_setting();
3033 }
3034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003035 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 if (old_fdl != curwin->w_p_fdl)
3037 newFoldLevel();
3038#endif
3039}
3040
3041#ifdef FEAT_GUI
3042/*
3043 * Vertical scrollbar movement.
3044 */
3045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003046nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 if (cap->oap->op_type != OP_NOP)
3049 clearopbeep(cap->oap);
3050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003051 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 gui_do_scroll();
3053}
3054
3055/*
3056 * Horizontal scrollbar movement.
3057 */
3058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003059nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 if (cap->oap->op_type != OP_NOP)
3062 clearopbeep(cap->oap);
3063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003064 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003065 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066}
3067#endif
3068
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003069#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003070/*
3071 * Click in GUI tab.
3072 */
3073 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003074nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003075{
3076 if (cap->oap->op_type != OP_NOP)
3077 clearopbeep(cap->oap);
3078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003080 goto_tabpage(current_tab);
3081}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003082
3083/*
3084 * Selected item in tab line menu.
3085 */
3086 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003087nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003088{
3089 if (cap->oap->op_type != OP_NOP)
3090 clearopbeep(cap->oap);
3091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003092 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003093 handle_tabmenu();
3094}
3095
3096/*
3097 * Handle selecting an item of the GUI tab line menu.
3098 * Used in Normal and Insert mode.
3099 */
3100 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003101handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003102{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003103 switch (current_tabmenu)
3104 {
3105 case TABLINE_MENU_CLOSE:
3106 if (current_tab == 0)
3107 do_cmdline_cmd((char_u *)"tabclose");
3108 else
3109 {
3110 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3111 current_tab);
3112 do_cmdline_cmd(IObuff);
3113 }
3114 break;
3115
3116 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003117 if (current_tab == 0)
3118 do_cmdline_cmd((char_u *)"$tabnew");
3119 else
3120 {
3121 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3122 current_tab - 1);
3123 do_cmdline_cmd(IObuff);
3124 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003125 break;
3126
3127 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003128 if (current_tab == 0)
3129 do_cmdline_cmd((char_u *)"browse $tabnew");
3130 else
3131 {
3132 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3133 current_tab - 1);
3134 do_cmdline_cmd(IObuff);
3135 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003136 break;
3137 }
3138}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003139#endif
3140
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141/*
3142 * "Q" command.
3143 */
3144 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003145nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003147 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003149 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003150 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 do_exmode(FALSE);
3152}
3153
3154/*
3155 * Handle a ":" command.
3156 */
3157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003158nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003160 int old_p_im;
3161 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003162 int is_cmdkey = cap->cmdchar == K_COMMAND
3163 || cap->cmdchar == K_SCRIPT_COMMAND;
3164 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar957cf672020-11-12 14:21:06 +01003166 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 nv_operator(cap);
3168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
3170 if (cap->oap->op_type != OP_NOP)
3171 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 cap->oap->motion_type = MCHAR;
3174 cap->oap->inclusive = FALSE;
3175 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003176 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 stuffcharReadbuff('.');
3180 if (cap->count0 > 1)
3181 {
3182 stuffReadbuff((char_u *)",.+");
3183 stuffnumReadbuff((long)cap->count0 - 1L);
3184 }
3185 }
3186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003187 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (KeyTyped)
3189 compute_cmdrow();
3190
3191 old_p_im = p_im;
3192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003194 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3195 if (is_cmdkey)
3196 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3197 else
3198 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (p_im != old_p_im)
3202 {
3203 if (p_im)
3204 restart_edit = 'i';
3205 else
3206 restart_edit = 0;
3207 }
3208
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003209 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003211 clearop(cap->oap);
3212 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3214 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003215 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3216 || did_emsg
3217 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 clearopbeep(cap->oap);
3220 }
3221}
3222
3223/*
3224 * Handle CTRL-G command.
3225 */
3226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003227nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003232 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 showmode();
3234 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003235 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003236 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 fileinfo((int)cap->count0, FALSE, TRUE);
3238}
3239
3240/*
3241 * Handle CTRL-H <Backspace> command.
3242 */
3243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003244nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (VIsual_active && VIsual_select)
3247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003248 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 v_visop(cap);
3250 }
3251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 nv_left(cap);
3253}
3254
3255/*
3256 * CTRL-L: clear screen and redraw.
3257 */
3258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003259nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 if (!checkclearop(cap->oap))
3262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003264 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003265 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003266# ifdef FEAT_RELTIME
3267 {
3268 win_T *wp;
3269
3270 FOR_ALL_WINDOWS(wp)
3271 wp->w_s->b_syn_slow = FALSE;
3272 }
3273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003275 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003276#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3277# ifdef VIMDLL
3278 if (!gui.in_use)
3279# endif
3280 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283}
3284
3285/*
3286 * CTRL-O: In Select mode: switch to Visual mode for one command.
3287 * Otherwise: Go to older pcmark.
3288 */
3289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003290nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (VIsual_active && VIsual_select)
3293 {
3294 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003295 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003297 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
3301 cap->count1 = -cap->count1;
3302 nv_pcmark(cap);
3303 }
3304}
3305
3306/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003307 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3308 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003311nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 if (!checkclearopq(cap->oap))
3314 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3315 GETF_SETMARK|GETF_ALT, FALSE);
3316}
3317
3318/*
3319 * "Z" commands.
3320 */
3321 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003322nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 if (!checkclearopq(cap->oap))
3325 {
3326 switch (cap->nchar)
3327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003328 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 case 'Z': do_cmdline_cmd((char_u *)"x");
3330 break;
3331
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 case 'Q': do_cmdline_cmd((char_u *)"q!");
3334 break;
3335
3336 default: clearopbeep(cap->oap);
3337 }
3338 }
3339}
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341/*
3342 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3343 */
3344 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003345do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 oparg_T oa;
3348 cmdarg_T ca;
3349
3350 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003351 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 ca.oap = &oa;
3353 ca.cmdchar = c1;
3354 ca.nchar = c2;
3355 nv_ident(&ca);
3356}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003359 * 'K' normal-mode command. Get the command to lookup the keyword under the
3360 * cursor.
3361 */
3362 static int
3363nv_K_getcmd(
3364 cmdarg_T *cap,
3365 char_u *kp,
3366 int kp_help,
3367 int kp_ex,
3368 char_u **ptr_arg,
3369 int n,
3370 char_u *buf,
3371 unsigned buflen)
3372{
3373 char_u *ptr = *ptr_arg;
3374 int isman;
3375 int isman_s;
3376
3377 if (kp_help)
3378 {
3379 // in the help buffer
3380 STRCPY(buf, "he! ");
3381 return n;
3382 }
3383
3384 if (kp_ex)
3385 {
3386 // 'keywordprog' is an ex command
3387 if (cap->count0 != 0)
3388 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3389 else
3390 STRCPY(buf, kp);
3391 STRCAT(buf, " ");
3392 return n;
3393 }
3394
3395 // An external command will probably use an argument starting
3396 // with "-" as an option. To avoid trouble we skip the "-".
3397 while (*ptr == '-' && n > 0)
3398 {
3399 ++ptr;
3400 --n;
3401 }
3402 if (n == 0)
3403 {
3404 // found dashes only
3405 emsg(_(e_no_identifier_under_cursor));
3406 vim_free(buf);
3407 *ptr_arg = ptr;
3408 return 0;
3409 }
3410
3411 // When a count is given, turn it into a range. Is this
3412 // really what we want?
3413 isman = (STRCMP(kp, "man") == 0);
3414 isman_s = (STRCMP(kp, "man -s") == 0);
3415 if (cap->count0 != 0 && !(isman || isman_s))
3416 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3417
3418 STRCAT(buf, "! ");
3419 if (cap->count0 == 0 && isman_s)
3420 STRCAT(buf, "man");
3421 else
3422 STRCAT(buf, kp);
3423 STRCAT(buf, " ");
3424 if (cap->count0 != 0 && (isman || isman_s))
3425 {
3426 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3427 STRCAT(buf, " ");
3428 }
3429
3430 *ptr_arg = ptr;
3431 return n;
3432}
3433
3434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * Handle the commands that use the word under the cursor.
3436 * [g] CTRL-] :ta to current identifier
3437 * [g] 'K' run program for current identifier
3438 * [g] '*' / to current identifier or string
3439 * [g] '#' ? to current identifier or string
3440 * g ']' :tselect for current identifier
3441 */
3442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003443nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 char_u *ptr = NULL;
3446 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003447 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003448 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 char_u *kp; // value of 'keywordprg'
3451 int kp_help; // 'keywordprg' is ":he"
3452 int kp_ex; // 'keywordprg' starts with ":"
3453 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003455 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003456 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 cmdchar = cap->nchar;
3462 g_cmd = TRUE;
3463 }
3464 else
3465 {
3466 cmdchar = cap->cmdchar;
3467 g_cmd = FALSE;
3468 }
3469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003470 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 cmdchar = '#';
3472
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003473 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3477 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (checkclearopq(cap->oap))
3479 return;
3480 }
3481
3482 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3483 (cmdchar == '*' || cmdchar == '#')
3484 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3485 {
3486 clearop(cap->oap);
3487 return;
3488 }
3489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // Allocate buffer to put the command in. Inserting backslashes can
3491 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3492 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3494 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3495 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003496 if (kp_help && *skipwhite(ptr) == NUL)
3497 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003498 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003499 return;
3500 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003501 kp_ex = (*kp == ':');
3502 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3503 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (buf == NULL)
3505 return;
3506 buf[0] = NUL;
3507
3508 switch (cmdchar)
3509 {
3510 case '*':
3511 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003512 // Put cursor at start of word, makes search skip the word
3513 // under the cursor.
3514 // Call setpcmark() first, so "*``" puts the cursor back where
3515 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 setpcmark();
3517 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3518
3519 if (!g_cmd && vim_iswordp(ptr))
3520 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003521 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 break;
3523
3524 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003525 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3526 if (n == 0)
3527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 break;
3529
3530 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003531 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#ifdef FEAT_CSCOPE
3533 if (p_cst)
3534 STRCPY(buf, "cstag ");
3535 else
3536#endif
3537 STRCPY(buf, "ts ");
3538 break;
3539
3540 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003541 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (curbuf->b_help)
3543 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003545 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003546 if (g_cmd)
3547 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003548 else if (cap->count0 == 0)
3549 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003550 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003551 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003555 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003556 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003559 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003560 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003561 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003562 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003563 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003564 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003565 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003566 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003568 vim_free(buf);
3569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003571 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003572 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003573 {
3574 vim_free(buf);
3575 vim_free(p);
3576 return;
3577 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003578 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003579 STRCAT(buf, p);
3580 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003582 else
3583 {
3584 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003585 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003586 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003587 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003588 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003589 {
3590 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003591 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003592 aux_ptr = (char_u *)"";
3593 else
3594 aux_ptr = (char_u *)"\\|\"\n[";
3595 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003596 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003597 aux_ptr = (char_u *)"\\|\"\n*?[";
3598
3599 p = buf + STRLEN(buf);
3600 while (n-- > 0)
3601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003602 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003603 if (vim_strchr(aux_ptr, *ptr) != NULL)
3604 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003605 // When current byte is a part of multibyte character, copy all
3606 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 if (has_mbyte)
3608 {
3609 int i;
3610 int len = (*mb_ptr2len)(ptr) - 1;
3611
3612 for (i = 0; i < len && n >= 1; ++i, --n)
3613 *p++ = *ptr++;
3614 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003615 *p++ = *ptr++;
3616 }
3617 *p = NUL;
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003620 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (cmdchar == '*' || cmdchar == '#')
3622 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003623 if (!g_cmd && (has_mbyte
3624 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3625 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003627
3628 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003629 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003631
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003632 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003635 {
3636 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003638 g_tag_at_cursor = FALSE;
3639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 vim_free(buf);
3642}
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644/*
3645 * Get visually selected text, within one line only.
3646 * Returns FAIL if more than one line selected.
3647 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003649get_visual_text(
3650 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003651 char_u **pp, // return: start of selected text
3652 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 if (VIsual_mode != 'V')
3655 unadjust_for_sel();
3656 if (VIsual.lnum != curwin->w_cursor.lnum)
3657 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003658 if (cap != NULL)
3659 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return FAIL;
3661 }
3662 if (VIsual_mode == 'V')
3663 {
3664 *pp = ml_get_curline();
3665 *lenp = (int)STRLEN(*pp);
3666 }
3667 else
3668 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003669 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
3671 *pp = ml_get_pos(&curwin->w_cursor);
3672 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3673 }
3674 else
3675 {
3676 *pp = ml_get_pos(&VIsual);
3677 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3678 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003679 if (**pp == NUL)
3680 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003681 if (*lenp > 0)
3682 {
3683 if (has_mbyte)
3684 // Correct the length to include all bytes of the last
3685 // character.
3686 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3687 else if ((*pp)[*lenp - 1] == NUL)
3688 // Do not include a trailing NUL.
3689 *lenp -= 1;
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 reset_VIsual_and_resel();
3693 return OK;
3694}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
3696/*
3697 * CTRL-T: backwards in tag stack
3698 */
3699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003700nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 if (!checkclearopq(cap->oap))
3703 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3704}
3705
3706/*
3707 * Handle scrolling command 'H', 'L' and 'M'.
3708 */
3709 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 int used = 0;
3713 long n;
3714#ifdef FEAT_FOLDING
3715 linenr_T lnum;
3716#endif
3717 int half;
3718
3719 cap->oap->motion_type = MLINE;
3720 setpcmark();
3721
3722 if (cap->cmdchar == 'L')
3723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003724 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 curwin->w_cursor.lnum = curwin->w_botline - 1;
3726 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3727 curwin->w_cursor.lnum = 1;
3728 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003729 {
3730#ifdef FEAT_FOLDING
3731 if (hasAnyFolding(curwin))
3732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003733 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003734 for (n = cap->count1 - 1; n > 0
3735 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3736 {
3737 (void)hasFolding(curwin->w_cursor.lnum,
3738 &curwin->w_cursor.lnum, NULL);
3739 --curwin->w_cursor.lnum;
3740 }
3741 }
3742 else
3743#endif
3744 curwin->w_cursor.lnum -= cap->count1 - 1;
3745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 else
3748 {
3749 if (cap->cmdchar == 'M')
3750 {
3751#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003752 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 used -= diff_check_fill(curwin, curwin->w_topline)
3754 - curwin->w_topfill;
3755#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003756 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3758 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3759 {
3760#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003761 // Count half he number of filler lines to be "below this
3762 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3764 + n) / 2 >= half)
3765 {
3766 --n;
3767 break;
3768 }
3769#endif
3770 used += plines(curwin->w_topline + n);
3771 if (used >= half)
3772 break;
3773#ifdef FEAT_FOLDING
3774 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3775 n = lnum - curwin->w_topline;
3776#endif
3777 }
3778 if (n > 0 && used > curwin->w_height)
3779 --n;
3780 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003781 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003784#ifdef FEAT_FOLDING
3785 if (hasAnyFolding(curwin))
3786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003787 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003788 lnum = curwin->w_topline;
3789 while (n-- > 0 && lnum < curwin->w_botline - 1)
3790 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003791 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003792 ++lnum;
3793 }
3794 n = lnum - curwin->w_topline;
3795 }
3796#endif
3797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 curwin->w_cursor.lnum = curwin->w_topline + n;
3799 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3800 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3801 }
3802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003803 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003804 if (cap->oap->op_type == OP_NOP)
3805 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 beginline(BL_SOL | BL_FIX);
3807}
3808
3809/*
3810 * Cursor right commands.
3811 */
3812 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003813nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003816 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003818 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3819 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003820 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003821 if (mod_mask & MOD_MASK_CTRL)
3822 cap->arg = TRUE;
3823 nv_wordcmd(cap);
3824 return;
3825 }
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 cap->oap->motion_type = MCHAR;
3828 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003829 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003831 // In virtual edit mode, there's no such thing as "past_line", as lines
3832 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003834 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 for (n = cap->count1; n > 0; --n)
3837 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003838 if ((!past_line && oneright() == FAIL)
3839 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003840 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003842 // <Space> wraps to next line if 'whichwrap' has 's'.
3843 // 'l' wraps to next line if 'whichwrap' has 'l'.
3844 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if ( ((cap->cmdchar == ' '
3846 && vim_strchr(p_ww, 's') != NULL)
3847 || (cap->cmdchar == 'l'
3848 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003849 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 && vim_strchr(p_ww, '>') != NULL))
3851 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003853 // When deleting we also count the NL as a character.
3854 // Set cap->oap->inclusive when last char in the line is
3855 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003856 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003858 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 cap->oap->inclusive = TRUE;
3860 else
3861 {
3862 ++curwin->w_cursor.lnum;
3863 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 curwin->w_set_curswant = TRUE;
3866 cap->oap->inclusive = FALSE;
3867 }
3868 continue;
3869 }
3870 if (cap->oap->op_type == OP_NOP)
3871 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003872 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 if (n == cap->count1)
3874 beep_flush();
3875 }
3876 else
3877 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003878 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 cap->oap->inclusive = TRUE;
3880 }
3881 break;
3882 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003883 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
3885 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (virtual_active())
3887 oneright();
3888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003891 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ++curwin->w_cursor.col;
3894 }
3895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897#ifdef FEAT_FOLDING
3898 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3899 && cap->oap->op_type == OP_NOP)
3900 foldOpenCursor();
3901#endif
3902}
3903
3904/*
3905 * Cursor left commands.
3906 *
3907 * Returns TRUE when operator end should not be adjusted.
3908 */
3909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003910nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 long n;
3913
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003914 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3915 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003916 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003917 if (mod_mask & MOD_MASK_CTRL)
3918 cap->arg = 1;
3919 nv_bck_word(cap);
3920 return;
3921 }
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 cap->oap->motion_type = MCHAR;
3924 cap->oap->inclusive = FALSE;
3925 for (n = cap->count1; n > 0; --n)
3926 {
3927 if (oneleft() == FAIL)
3928 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003929 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3930 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3931 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if ( (((cap->cmdchar == K_BS
3933 || cap->cmdchar == Ctrl_H)
3934 && vim_strchr(p_ww, 'b') != NULL)
3935 || (cap->cmdchar == 'h'
3936 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003937 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 && vim_strchr(p_ww, '<') != NULL))
3939 && curwin->w_cursor.lnum > 1)
3940 {
3941 --(curwin->w_cursor.lnum);
3942 coladvance((colnr_T)MAXCOL);
3943 curwin->w_set_curswant = TRUE;
3944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003945 // When the NL before the first char has to be deleted we
3946 // put the cursor on the NUL after the previous line.
3947 // This is a very special case, be careful!
3948 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 if ( (cap->oap->op_type == OP_DELETE
3950 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003951 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003953 char_u *cp = ml_get_cursor();
3954
3955 if (*cp != NUL)
3956 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003957 if (has_mbyte)
3958 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3959 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003960 ++curwin->w_cursor.col;
3961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 cap->retval |= CA_NO_ADJ_OP_END;
3963 }
3964 continue;
3965 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3968 beep_flush();
3969 break;
3970 }
3971 }
3972#ifdef FEAT_FOLDING
3973 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3974 && cap->oap->op_type == OP_NOP)
3975 foldOpenCursor();
3976#endif
3977}
3978
3979/*
3980 * Cursor up commands.
3981 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3982 */
3983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003984nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003986 if (mod_mask & MOD_MASK_SHIFT)
3987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003988 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003989 cap->arg = BACKWARD;
3990 nv_page(cap);
3991 }
3992 else
3993 {
3994 cap->oap->motion_type = MLINE;
3995 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3996 clearopbeep(cap->oap);
3997 else if (cap->arg)
3998 beginline(BL_WHITE | BL_FIX);
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000}
4001
4002/*
4003 * Cursor down commands.
4004 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4005 */
4006 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004007nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 if (mod_mask & MOD_MASK_SHIFT)
4010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004011 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004012 cap->arg = FORWARD;
4013 nv_page(cap);
4014 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004015#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004017 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4018 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
4022#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004023 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004024 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 cmdwin_result = CAR;
4026 else
4027#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004028#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004029 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004030 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4031 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4032 {
4033 invoke_prompt_callback();
4034 if (restart_edit == 0)
4035 restart_edit = 'a';
4036 }
4037 else
4038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
4040 cap->oap->motion_type = MLINE;
4041 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4042 clearopbeep(cap->oap);
4043 else if (cap->arg)
4044 beginline(BL_WHITE | BL_FIX);
4045 }
4046 }
4047}
4048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049/*
4050 * Grab the file name under the cursor and edit it.
4051 */
4052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004053nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
4055 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004056 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004058 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060 if (curbuf_locked())
4061 {
4062 clearop(cap->oap);
4063 return;
4064 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004065#ifdef FEAT_PROP_POPUP
4066 if (ERROR_IF_TERM_POPUP_WINDOW)
4067 return;
4068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004070 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 if (ptr != NULL)
4073 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004074 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004075 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004076 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004078 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004079 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004080 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004081 {
4082 curwin->w_cursor.lnum = lnum;
4083 check_cursor_lnum();
4084 beginline(BL_SOL | BL_FIX);
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 vim_free(ptr);
4087 }
4088 else
4089 clearop(cap->oap);
4090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
4092/*
4093 * <End> command: to end of current line or last line.
4094 */
4095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004096nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004098 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004100 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 nv_dollar(cap);
4105}
4106
4107/*
4108 * Handle the "$" command.
4109 */
4110 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004111nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 cap->oap->motion_type = MCHAR;
4114 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004115 // In virtual mode when off the edge of a line and an operator
4116 // is pending (whew!) keep the cursor where it is.
4117 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (!virtual_active() || gchar_cursor() != NUL
4119 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004120 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (cursor_down((long)(cap->count1 - 1),
4122 cap->oap->op_type == OP_NOP) == FAIL)
4123 clearopbeep(cap->oap);
4124#ifdef FEAT_FOLDING
4125 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4126 foldOpenCursor();
4127#endif
4128}
4129
4130/*
4131 * Implementation of '?' and '/' commands.
4132 * If cap->arg is TRUE don't set PC mark.
4133 */
4134 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004135nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
4137 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004138 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004142 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 cap->cmdchar = 'g';
4144 cap->nchar = '?';
4145 nv_operator(cap);
4146 return;
4147 }
4148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 // When using 'incsearch' the cursor may be moved to set a different search
4150 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004151 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 if (cap->searchbuf == NULL)
4154 {
4155 clearop(oap);
4156 return;
4157 }
4158
Bram Moolenaar46539112015-02-17 15:43:57 +01004159 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004160 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004161 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
LemonBoya4399382022-04-09 21:04:08 +01004164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165/*
4166 * Handle "N" and "n" commands.
4167 * cap->arg is SEARCH_REV for "N", 0 for "n".
4168 */
4169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004170nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004172 pos_T old = curwin->w_cursor;
4173 int wrapped = FALSE;
4174 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004175
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004176 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004177 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004178 // Avoid getting stuck on the current cursor position, which can
4179 // happen when an offset is given and the cursor is on the last char
4180 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004181 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004182 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004183 cap->count1 -= 1;
4184 }
LemonBoya4399382022-04-09 21:04:08 +01004185
4186#ifdef FEAT_SEARCH_EXTRA
4187 // Redraw the window to refresh the highlighted matches.
4188 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004189 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191}
4192
4193/*
4194 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4195 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004196 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004198 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004199normal_search(
4200 cmdarg_T *cap,
4201 int dir,
4202 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004203 int opt, // extra flags for do_search()
4204 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
4206 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004207 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004208#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004209 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 cap->oap->motion_type = MCHAR;
4213 cap->oap->inclusive = FALSE;
4214 cap->oap->use_reg_one = TRUE;
4215 curwin->w_set_curswant = TRUE;
4216
Bram Moolenaara80faa82020-04-12 19:37:17 +02004217 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004218 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004219 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4220 if (wrapped != NULL)
4221 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (i == 0)
4223 clearop(cap->oap);
4224 else
4225 {
4226 if (i == 2)
4227 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#ifdef FEAT_FOLDING
4230 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4231 foldOpenCursor();
4232#endif
4233 }
LemonBoya4399382022-04-09 21:04:08 +01004234#ifdef FEAT_SEARCH_EXTRA
4235 // Redraw the window to refresh the highlighted matches.
4236 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004237 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004240 // "/$" will put the cursor after the end of the line, may need to
4241 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004243 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244}
4245
4246/*
4247 * Character search commands.
4248 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4249 * ',' and FALSE for ';'.
4250 * cap->nchar is NUL for ',' and ';' (repeat the search)
4251 */
4252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004253nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 int t_cmd;
4256
4257 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4258 t_cmd = TRUE;
4259 else
4260 t_cmd = FALSE;
4261
4262 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4264 clearopbeep(cap->oap);
4265 else
4266 {
4267 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004268 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4270 && (t_cmd || cap->oap->op_type != OP_NOP))
4271 {
4272 colnr_T scol, ecol;
4273
4274 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4275 curwin->w_cursor.coladd = ecol - scol;
4276 }
4277 else
4278 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280#ifdef FEAT_FOLDING
4281 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4282 foldOpenCursor();
4283#endif
4284 }
4285}
4286
4287/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004288 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4289 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4290 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4291 * "[m" or "]m" search for prev/next start of (Java) method.
4292 * "[M" or "]M" search for prev/next end of (Java) method.
4293 */
4294 static void
4295nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4296{
4297 pos_T new_pos = {0, 0, 0};
4298 pos_T *pos = NULL; // init for GCC
4299 pos_T prev_pos;
4300 long n;
4301 int findc;
4302 int c;
4303
4304 if (cap->nchar == '*')
4305 cap->nchar = '/';
4306 prev_pos.lnum = 0;
4307 if (cap->nchar == 'm' || cap->nchar == 'M')
4308 {
4309 if (cap->cmdchar == '[')
4310 findc = '{';
4311 else
4312 findc = '}';
4313 n = 9999;
4314 }
4315 else
4316 {
4317 findc = cap->nchar;
4318 n = cap->count1;
4319 }
4320 for ( ; n > 0; --n)
4321 {
4322 if ((pos = findmatchlimit(cap->oap, findc,
4323 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4324 {
4325 if (new_pos.lnum == 0) // nothing found
4326 {
4327 if (cap->nchar != 'm' && cap->nchar != 'M')
4328 clearopbeep(cap->oap);
4329 }
4330 else
4331 pos = &new_pos; // use last one found
4332 break;
4333 }
4334 prev_pos = new_pos;
4335 curwin->w_cursor = *pos;
4336 new_pos = *pos;
4337 }
4338 curwin->w_cursor = *old_pos;
4339
4340 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4341 // brought us to the match for "[m" and "]M" when inside a method.
4342 // Try finding the '{' or '}' we want to be at.
4343 // Also repeat for the given count.
4344 if (cap->nchar == 'm' || cap->nchar == 'M')
4345 {
4346 // norm is TRUE for "]M" and "[m"
4347 int norm = ((findc == '{') == (cap->nchar == 'm'));
4348
4349 n = cap->count1;
4350 // found a match: we were inside a method
4351 if (prev_pos.lnum != 0)
4352 {
4353 pos = &prev_pos;
4354 curwin->w_cursor = prev_pos;
4355 if (norm)
4356 --n;
4357 }
4358 else
4359 pos = NULL;
4360 while (n > 0)
4361 {
4362 for (;;)
4363 {
4364 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4365 {
4366 // if not found anything, that's an error
4367 if (pos == NULL)
4368 clearopbeep(cap->oap);
4369 n = 0;
4370 break;
4371 }
4372 c = gchar_cursor();
4373 if (c == '{' || c == '}')
4374 {
4375 // Must have found end/start of class: use it.
4376 // Or found the place to be at.
4377 if ((c == findc && norm) || (n == 1 && !norm))
4378 {
4379 new_pos = curwin->w_cursor;
4380 pos = &new_pos;
4381 n = 0;
4382 }
4383 // if no match found at all, we started outside of the
4384 // class and we're inside now. Just go on.
4385 else if (new_pos.lnum == 0)
4386 {
4387 new_pos = curwin->w_cursor;
4388 pos = &new_pos;
4389 }
4390 // found start/end of other method: go to match
4391 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004392 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4393 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004394 n = 0;
4395 else
4396 curwin->w_cursor = *pos;
4397 break;
4398 }
4399 }
4400 --n;
4401 }
4402 curwin->w_cursor = *old_pos;
4403 if (pos == NULL && new_pos.lnum != 0)
4404 clearopbeep(cap->oap);
4405 }
4406 if (pos != NULL)
4407 {
4408 setpcmark();
4409 curwin->w_cursor = *pos;
4410 curwin->w_set_curswant = TRUE;
4411#ifdef FEAT_FOLDING
4412 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4413 && cap->oap->op_type == OP_NOP)
4414 foldOpenCursor();
4415#endif
4416 }
4417}
4418
4419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 * "[" and "]" commands.
4421 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4422 */
4423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004424nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004427 pos_T *pos = NULL; // init for GCC
4428 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 int flag;
4430 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 cap->oap->motion_type = MCHAR;
4433 cap->oap->inclusive = FALSE;
4434 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004435 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004437 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (cap->nchar == 'f')
4439 nv_gotofile(cap);
4440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004443 // Find the occurrence(s) of the identifier or define under cursor
4444 // in current and included files or jump to the first occurrence.
4445 //
4446 // search list jump
4447 // fwd bwd fwd bwd fwd bwd
4448 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4449 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004450 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 char_u *ptr;
4453 int len;
4454
4455 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4456 clearop(cap->oap);
4457 else
4458 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004459 // Make a copy, if the line was changed it will be freed.
4460 ptr = vim_strnsave(ptr, len);
4461 if (ptr == NULL)
4462 return;
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 find_pattern_in_path(ptr, 0, len, TRUE,
4465 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4466 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4467 cap->count1,
4468 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4469 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4470 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4471 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004472 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 curwin->w_set_curswant = TRUE;
4474 }
4475 }
4476 else
4477#endif
4478
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004479 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4480 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4481 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4482 // "[m" or "]m" search for prev/next start of (Java) method.
4483 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if ( (cap->cmdchar == '['
4485 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4486 || (cap->cmdchar == ']'
4487 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004488 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004490 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 else if (cap->nchar == '[' || cap->nchar == ']')
4492 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004493 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 flag = '{';
4495 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004496 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004499 // Imitate strange Vi behaviour: When using "]]" with an operator
4500 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004501 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 (cap->oap->op_type != OP_NOP
4503 && cap->arg == FORWARD && flag == '{')))
4504 clearopbeep(cap->oap);
4505 else
4506 {
4507 if (cap->oap->op_type == OP_NOP)
4508 beginline(BL_WHITE | BL_FIX);
4509#ifdef FEAT_FOLDING
4510 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4511 foldOpenCursor();
4512#endif
4513 }
4514 }
4515
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004516 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 else if (cap->nchar == 'p' || cap->nchar == 'P')
4518 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004519 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else if (cap->nchar == '\'' || cap->nchar == '`')
4524 {
4525 pos = &curwin->w_cursor;
4526 for (n = cap->count1; n > 0; --n)
4527 {
4528 prev_pos = *pos;
4529 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4530 cap->nchar == '\'');
4531 if (pos == NULL)
4532 break;
4533 }
4534 if (pos == NULL)
4535 pos = &prev_pos;
4536 nv_cursormark(cap, cap->nchar == '\'', pos);
4537 }
4538
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004539 // [ or ] followed by a middle mouse click: put selected text with
4540 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004541 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
4543 (void)do_mouse(cap->oap, cap->nchar,
4544 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4545 cap->count1, PUT_FIXINDENT);
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004549 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else if (cap->nchar == 'z')
4551 {
4552 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4553 cap->count1) == FAIL)
4554 clearopbeep(cap->oap);
4555 }
4556#endif
4557
4558#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004559 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else if (cap->nchar == 'c')
4561 {
4562 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4563 cap->count1) == FAIL)
4564 clearopbeep(cap->oap);
4565 }
4566#endif
4567
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004568#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004569 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 else if (cap->nchar == 's' || cap->nchar == 'S')
4571 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004572 setpcmark();
4573 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004574 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4575 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004576 {
4577 clearopbeep(cap->oap);
4578 break;
4579 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004580 else
4581 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004582# ifdef FEAT_FOLDING
4583 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4584 foldOpenCursor();
4585# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004586 }
4587#endif
4588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004589 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 else
4591 clearopbeep(cap->oap);
4592}
4593
4594/*
4595 * Handle Normal mode "%" command.
4596 */
4597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004598nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599{
4600 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004601#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 linenr_T lnum = curwin->w_cursor.lnum;
4603#endif
4604
4605 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004606 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 if (cap->count0 > 100)
4609 clearopbeep(cap->oap);
4610 else
4611 {
4612 cap->oap->motion_type = MLINE;
4613 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004614 // Round up, so 'normal 100%' always jumps at the line line.
4615 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4616 // overflow on 32-bits, so use a formula with less accuracy
4617 // to avoid overflows.
4618 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4620 / 100L * cap->count0;
4621 else
4622 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4623 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004624 if (curwin->w_cursor.lnum < 1)
4625 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4627 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4628 beginline(BL_SOL | BL_FIX);
4629 }
4630 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004631 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
4633 cap->oap->motion_type = MCHAR;
4634 cap->oap->use_reg_one = TRUE;
4635 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4636 clearopbeep(cap->oap);
4637 else
4638 {
4639 setpcmark();
4640 curwin->w_cursor = *pos;
4641 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 }
4646#ifdef FEAT_FOLDING
4647 if (cap->oap->op_type == OP_NOP
4648 && lnum != curwin->w_cursor.lnum
4649 && (fdo_flags & FDO_PERCENT)
4650 && KeyTyped)
4651 foldOpenCursor();
4652#endif
4653}
4654
4655/*
4656 * Handle "(" and ")" commands.
4657 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4658 */
4659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004660nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 cap->oap->motion_type = MCHAR;
4663 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004664 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004665 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 curwin->w_set_curswant = TRUE;
4667
4668 if (findsent(cap->arg, cap->count1) == FAIL)
4669 clearopbeep(cap->oap);
4670 else
4671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004673 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#ifdef FEAT_FOLDING
4676 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4677 foldOpenCursor();
4678#endif
4679 }
4680}
4681
4682/*
4683 * "m" command: Mark a position.
4684 */
4685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004686nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 if (!checkclearop(cap->oap))
4689 {
4690 if (setmark(cap->nchar) == FAIL)
4691 clearopbeep(cap->oap);
4692 }
4693}
4694
4695/*
4696 * "{" and "}" commands.
4697 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4698 */
4699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 cap->oap->motion_type = MCHAR;
4703 cap->oap->inclusive = FALSE;
4704 cap->oap->use_reg_one = TRUE;
4705 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004706 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 clearopbeep(cap->oap);
4708 else
4709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef FEAT_FOLDING
4712 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4713 foldOpenCursor();
4714#endif
4715 }
4716}
4717
4718/*
4719 * "u" command: Undo or make lower case.
4720 */
4721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004722nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004724 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004726 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 cap->cmdchar = 'g';
4728 cap->nchar = 'u';
4729 nv_operator(cap);
4730 }
4731 else
4732 nv_kundo(cap);
4733}
4734
4735/*
4736 * <Undo> command.
4737 */
4738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004739nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 if (!checkclearopq(cap->oap))
4742 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004743#ifdef FEAT_JOB_CHANNEL
4744 if (bt_prompt(curbuf))
4745 {
4746 clearopbeep(cap->oap);
4747 return;
4748 }
4749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 u_undo((int)cap->count1);
4751 curwin->w_set_curswant = TRUE;
4752 }
4753}
4754
4755/*
4756 * Handle the "r" command.
4757 */
4758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004759nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 char_u *ptr;
4762 int had_ctrl_v;
4763 long n;
4764
4765 if (checkclearop(cap->oap))
4766 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004767#ifdef FEAT_JOB_CHANNEL
4768 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4769 {
4770 clearopbeep(cap->oap);
4771 return;
4772 }
4773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (cap->nchar == Ctrl_V)
4777 {
4778 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004779 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (cap->nchar > DEL)
4782 had_ctrl_v = NUL;
4783 }
4784 else
4785 had_ctrl_v = NUL;
4786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004788 if (IS_SPECIAL(cap->nchar))
4789 {
4790 clearopbeep(cap->oap);
4791 return;
4792 }
4793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (VIsual_active)
4796 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004797 if (got_int)
4798 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004799 if (had_ctrl_v)
4800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Use a special (negative) number to make a difference between a
4802 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004803 if (cap->nchar == CAR)
4804 cap->nchar = REPLACE_CR_NCHAR;
4805 else if (cap->nchar == NL)
4806 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 nv_operator(cap);
4809 return;
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004812 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (virtual_active())
4814 {
4815 if (u_save_cursor() == FAIL)
4816 return;
4817 if (gchar_cursor() == NUL)
4818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004819 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 coladvance_force((colnr_T)(getviscol() + cap->count1));
4821 curwin->w_cursor.col -= cap->count1;
4822 }
4823 else if (gchar_cursor() == TAB)
4824 coladvance_force(getviscol());
4825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004829 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004830 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832 clearopbeep(cap->oap);
4833 return;
4834 }
4835
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004836 // Replacing with a TAB is done by edit() when it is complicated because
4837 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4838 // Other characters are done below to avoid problems with things like
4839 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4841 {
4842 stuffnumReadbuff(cap->count1);
4843 stuffcharReadbuff('R');
4844 stuffcharReadbuff('\t');
4845 stuffcharReadbuff(ESC);
4846 return;
4847 }
4848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004849 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (u_save_cursor() == FAIL)
4851 return;
4852
4853 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4854 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004855 // Replace character(s) by a single newline.
4856 // Strange vi behaviour: Only one newline is inserted.
4857 // Delete the characters here.
4858 // Insert the newline with an insert command, takes care of
4859 // autoindent. The insert command depends on being on the last
4860 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 stuffcharReadbuff('\r');
4863 stuffcharReadbuff(ESC);
4864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004865 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 invoke_edit(cap, TRUE, 'r', FALSE);
4867 }
4868 else
4869 {
4870 prep_redo(cap->oap->regname, cap->count1,
4871 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4872
4873 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (has_mbyte)
4875 {
4876 int old_State = State;
4877
4878 if (cap->ncharC1 != 0)
4879 AppendCharToRedobuff(cap->ncharC1);
4880 if (cap->ncharC2 != 0)
4881 AppendCharToRedobuff(cap->ncharC2);
4882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004883 // This is slow, but it handles replacing a single-byte with a
4884 // multi-byte and the other way around. Also handles adding
4885 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 for (n = cap->count1; n > 0; --n)
4887 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004888 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004889 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4890 {
4891 int c = ins_copychar(curwin->w_cursor.lnum
4892 + (cap->nchar == Ctrl_Y ? -1 : 1));
4893 if (c != NUL)
4894 ins_char(c);
4895 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004896 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004897 ++curwin->w_cursor.col;
4898 }
4899 else
4900 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 State = old_State;
4902 if (cap->ncharC1 != 0)
4903 ins_char(cap->ncharC1);
4904 if (cap->ncharC2 != 0)
4905 ins_char(cap->ncharC2);
4906 }
4907 }
4908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004910 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 for (n = cap->count1; n > 0; --n)
4912 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004913 // Get ptr again, because u_save and/or showmatch() will have
4914 // released the line. This may also happen in ins_copychar().
4915 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004916 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4917 {
4918 int c = ins_copychar(curwin->w_cursor.lnum
4919 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004920
4921 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004922 if (c != NUL)
4923 ptr[curwin->w_cursor.col] = c;
4924 }
4925 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004926 {
4927 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004928 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (p_sm && msg_silent == 0)
4931 showmatch(cap->nchar);
4932 ++curwin->w_cursor.col;
4933 }
4934#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004935 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004937 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
Bram Moolenaar009b2592004-10-24 19:18:58 +00004939 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004940 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004942 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944#endif
4945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 changed_bytes(curwin->w_cursor.lnum,
4948 (colnr_T)(curwin->w_cursor.col - cap->count1));
4949 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004950 --curwin->w_cursor.col; // cursor on the last replaced char
4951 // if the character on the left of the current cursor is a multi-byte
4952 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (has_mbyte)
4954 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 curbuf->b_op_end = curwin->w_cursor;
4956 curwin->w_set_curswant = TRUE;
4957 set_last_insert(cap->nchar);
4958 }
4959}
4960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961/*
4962 * 'o': Exchange start and end of Visual area.
4963 * 'O': same, but in block mode exchange left and right corners.
4964 */
4965 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004966v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 pos_T old_cursor;
4969 colnr_T left, right;
4970
4971 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4972 {
4973 old_cursor = curwin->w_cursor;
4974 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4975 curwin->w_cursor.lnum = VIsual.lnum;
4976 coladvance(left);
4977 VIsual = curwin->w_cursor;
4978
4979 curwin->w_cursor.lnum = old_cursor.lnum;
4980 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004981 // 'selection "exclusive" and cursor at right-bottom corner: move it
4982 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4984 ++curwin->w_curswant;
4985 coladvance(curwin->w_curswant);
4986 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004988 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 curwin->w_cursor.lnum = VIsual.lnum;
4991 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4992 ++right;
4993 coladvance(right);
4994 VIsual = curwin->w_cursor;
4995
4996 curwin->w_cursor.lnum = old_cursor.lnum;
4997 coladvance(left);
4998 curwin->w_curswant = left;
4999 }
5000 }
5001 else
5002 {
5003 old_cursor = curwin->w_cursor;
5004 curwin->w_cursor = VIsual;
5005 VIsual = old_cursor;
5006 curwin->w_set_curswant = TRUE;
5007 }
5008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
5010/*
5011 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5012 */
5013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005014nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005016 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 cap->cmdchar = 'c';
5019 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005020 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 VIsual_mode = 'V';
5022 nv_operator(cap);
5023 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005024 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
5026 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005027 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 else
5029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (virtual_active())
5031 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5033 }
5034 }
5035}
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037/*
5038 * "gr".
5039 */
5040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005041nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (VIsual_active)
5044 {
5045 cap->cmdchar = 'r';
5046 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005049 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
5051 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005052 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 else
5054 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005055 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005056 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 stuffcharReadbuff(cap->extra_char);
5058 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (virtual_active())
5060 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 invoke_edit(cap, TRUE, 'v', FALSE);
5062 }
5063 }
5064}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066/*
5067 * Swap case for "~" command, when it does not work like an operator.
5068 */
5069 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005070n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
5072 long n;
5073 pos_T startpos;
5074 int did_change = 0;
5075#ifdef FEAT_NETBEANS_INTG
5076 pos_T pos;
5077 char_u *ptr;
5078 int count;
5079#endif
5080
5081 if (checkclearopq(cap->oap))
5082 return;
5083
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005084 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 clearopbeep(cap->oap);
5087 return;
5088 }
5089
5090 prep_redo_cmd(cap);
5091
5092 if (u_save_cursor() == FAIL)
5093 return;
5094
5095 startpos = curwin->w_cursor;
5096#ifdef FEAT_NETBEANS_INTG
5097 pos = startpos;
5098#endif
5099 for (n = cap->count1; n > 0; --n)
5100 {
5101 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5102 inc_cursor();
5103 if (gchar_cursor() == NUL)
5104 {
5105 if (vim_strchr(p_ww, '~') != NULL
5106 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5107 {
5108#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005109 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 if (did_change)
5112 {
5113 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005114 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005115 netbeans_removed(curbuf, pos.lnum, pos.col,
5116 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005117 // line may have been flushed, get it again
5118 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005120 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122 pos.col = 0;
5123 pos.lnum++;
5124 }
5125#endif
5126 ++curwin->w_cursor.lnum;
5127 curwin->w_cursor.col = 0;
5128 if (n > 1)
5129 {
5130 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5131 break;
5132 u_clearline();
5133 }
5134 }
5135 else
5136 break;
5137 }
5138 }
5139#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005140 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 ptr = ml_get(pos.lnum);
5143 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005144 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5145 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147#endif
5148
5149
5150 check_cursor();
5151 curwin->w_set_curswant = TRUE;
5152 if (did_change)
5153 {
5154 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5155 0L);
5156 curbuf->b_op_start = startpos;
5157 curbuf->b_op_end = curwin->w_cursor;
5158 if (curbuf->b_op_end.col > 0)
5159 --curbuf->b_op_end.col;
5160 }
5161}
5162
5163/*
5164 * Move cursor to mark.
5165 */
5166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005167nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 if (check_mark(pos) == FAIL)
5170 clearop(cap->oap);
5171 else
5172 {
5173 if (cap->cmdchar == '\''
5174 || cap->cmdchar == '`'
5175 || cap->cmdchar == '['
5176 || cap->cmdchar == ']')
5177 setpcmark();
5178 curwin->w_cursor = *pos;
5179 if (flag)
5180 beginline(BL_WHITE | BL_FIX);
5181 else
5182 check_cursor();
5183 }
5184 cap->oap->motion_type = flag ? MLINE : MCHAR;
5185 if (cap->cmdchar == '`')
5186 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005187 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 curwin->w_set_curswant = TRUE;
5189}
5190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191/*
5192 * Handle commands that are operators in Visual mode.
5193 */
5194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005195v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005199 // Uppercase means linewise, except in block mode, then "D" deletes till
5200 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (isupper(cap->cmdchar))
5202 {
5203 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005204 {
5205 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5209 curwin->w_curswant = MAXCOL;
5210 }
5211 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5212 nv_operator(cap);
5213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215/*
5216 * "s" and "S" commands.
5217 */
5218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005219nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005221#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005222 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005223 if (term_swap_diff() == OK)
5224 return;
5225#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005226#ifdef FEAT_JOB_CHANNEL
5227 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5228 {
5229 clearopbeep(cap->oap);
5230 return;
5231 }
5232#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005233 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
5235 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005236 {
5237 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 cap->cmdchar = 'c';
5241 nv_operator(cap);
5242 }
5243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 nv_optrans(cap);
5245}
5246
5247/*
5248 * Abbreviated commands.
5249 */
5250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005254 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005256 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 if (VIsual_active)
5258 v_visop(cap);
5259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 nv_optrans(cap);
5261}
5262
5263/*
5264 * Translate a command into another command.
5265 */
5266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005267nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5270 (char_u *)"d$", (char_u *)"c$",
5271 (char_u *)"cl", (char_u *)"cc",
5272 (char_u *)"yy", (char_u *)":s\r"};
5273 static char_u *str = (char_u *)"xXDCsSY&";
5274
5275 if (!checkclearopq(cap->oap))
5276 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005277 // In Vi "2D" doesn't delete the next line. Can't translate it
5278 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005279 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5280 {
5281 cap->oap->start = curwin->w_cursor;
5282 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005283#ifdef FEAT_EVAL
5284 set_op_var(OP_DELETE);
5285#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005286 cap->count1 = 1;
5287 nv_dollar(cap);
5288 finish_op = TRUE;
5289 ResetRedobuff();
5290 AppendCharToRedobuff('D');
5291 }
5292 else
5293 {
5294 if (cap->count0)
5295 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005296 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299 cap->opcount = 0;
5300}
5301
5302/*
5303 * "'" and "`" commands. Also for "g'" and "g`".
5304 * cap->arg is TRUE for "'" and "g'".
5305 */
5306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 pos_T *pos;
5310 int c;
5311#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005312 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#endif
5315
5316 if (cap->cmdchar == 'g')
5317 c = cap->extra_char;
5318 else
5319 c = cap->nchar;
5320 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 if (cap->arg)
5324 {
5325 check_cursor_lnum();
5326 beginline(BL_WHITE | BL_FIX);
5327 }
5328 else
5329 check_cursor();
5330 }
5331 else
5332 nv_cursormark(cap, cap->arg, pos);
5333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005334 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (!virtual_active())
5336 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005337 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#ifdef FEAT_FOLDING
5339 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005340 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005341 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 && (fdo_flags & FDO_MARK)
5343 && old_KeyTyped)
5344 foldOpenCursor();
5345#endif
5346}
5347
5348/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005349 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
5351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005355#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005357 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 if (!checkclearopq(cap->oap))
5361 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005362 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5363 {
5364 if (goto_tabpage_lastused() == FAIL)
5365 clearopbeep(cap->oap);
5366 return;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (cap->cmdchar == 'g')
5369 pos = movechangelist((int)cap->count1);
5370 else
5371 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005372 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 curwin->w_set_curswant = TRUE;
5375 check_cursor();
5376 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005377 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 nv_cursormark(cap, FALSE, pos);
5379 else if (cap->cmdchar == 'g')
5380 {
5381 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005382 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005384 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005386 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 else
5389 clearopbeep(cap->oap);
5390# ifdef FEAT_FOLDING
5391 if (cap->oap->op_type == OP_NOP
5392 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5393 && (fdo_flags & FDO_MARK)
5394 && old_KeyTyped)
5395 foldOpenCursor();
5396# endif
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398}
5399
5400/*
5401 * Handle '"' command.
5402 */
5403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005404nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 if (checkclearop(cap->oap))
5407 return;
5408#ifdef FEAT_EVAL
5409 if (cap->nchar == '=')
5410 cap->nchar = get_expr_register();
5411#endif
5412 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5413 {
5414 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005415 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#ifdef FEAT_EVAL
5417 set_reg_var(cap->oap->regname);
5418#endif
5419 }
5420 else
5421 clearopbeep(cap->oap);
5422}
5423
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424/*
5425 * Handle "v", "V" and "CTRL-V" commands.
5426 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5427 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005428 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 */
5430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005431nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005433 if (cap->cmdchar == Ctrl_Q)
5434 cap->cmdchar = Ctrl_V;
5435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5437 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (cap->oap->op_type != OP_NOP)
5439 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005440 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return;
5443 }
5444
5445 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005448 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 else // toggle char/block mode
5451 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 VIsual_mode = cap->cmdchar;
5453 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005454 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005456 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005458 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
5460 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005461 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005463 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 VIsual = curwin->w_cursor;
5465
5466 VIsual_active = TRUE;
5467 VIsual_reselect = TRUE;
5468 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005472 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005473 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005474 // For V and ^V, we multiply the number of lines even if there
5475 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5477 {
5478 curwin->w_cursor.lnum +=
5479 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005480 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 VIsual_mode = resel_VIsual_mode;
5483 if (VIsual_mode == 'v')
5484 {
5485 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005486 {
5487 validate_virtcol();
5488 curwin->w_curswant = curwin->w_virtcol
5489 + resel_VIsual_vcol * cap->count0 - 1;
5490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005492 curwin->w_curswant = resel_VIsual_vcol;
5493 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005495 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 curwin->w_curswant = MAXCOL;
5498 coladvance((colnr_T)MAXCOL);
5499 }
5500 else if (VIsual_mode == Ctrl_V)
5501 {
5502 validate_virtcol();
5503 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005504 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 coladvance(curwin->w_curswant);
5506 }
5507 else
5508 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005509 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 else
5512 {
5513 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005514 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 may_start_select('c');
5516 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005517 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005518 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005519 if (cap->count0 > 0 && --cap->count1 > 0)
5520 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005522 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5523 nv_right(cap);
5524 else if (VIsual_mode == 'V')
5525 nv_down(cap);
5526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 }
5528 }
5529}
5530
5531/*
5532 * Start selection for Shift-movement keys.
5533 */
5534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005535start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005537 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 may_start_select('k');
5539 n_start_visual_mode('v');
5540}
5541
5542/*
5543 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005544 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
5546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005547may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005549 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5550 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
5552
5553/*
5554 * Start Visual mode "c".
5555 * Should set VIsual_select before calling this.
5556 */
5557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005560#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005561 int cursor_line_was_concealed = curwin->w_p_cole > 0
5562 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005563#endif
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 VIsual_mode = c;
5566 VIsual_active = TRUE;
5567 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005568
5569 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005570 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005571 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005572 {
5573 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 VIsual = curwin->w_cursor;
5577
5578#ifdef FEAT_FOLDING
5579 foldAdjustVisual();
5580#endif
5581
LemonBoy2bf52dd2022-04-09 18:17:34 +01005582 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005584#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005585 // Check if redraw is needed after changing the state.
5586 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005587#endif
5588
Bram Moolenaar09df3122006-01-23 22:23:09 +00005589 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005590 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 // Make sure the clipboard gets updated. Needed because start and
5593 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 clip_star.vmode = NUL;
5595#endif
5596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 // Only need to redraw this line, unless still need to redraw an old
5598 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005599 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
5601 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5602 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5603 }
5604}
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
5607/*
5608 * CTRL-W: Window commands
5609 */
5610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005611nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005613 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005616 cap->cmdchar = ':';
5617 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005618 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005619 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005620 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623
5624/*
5625 * CTRL-Z: Suspend
5626 */
5627 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005628nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005633 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635
5636/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005637 * "gv": Reselect the previous Visual area. If Visual already active,
5638 * exchange previous and current Visual area.
5639 */
5640 static void
5641nv_gv_cmd(cmdarg_T *cap)
5642{
5643 pos_T tpos;
5644 int i;
5645
5646 if (checkclearop(cap->oap))
5647 return;
5648
5649 if (curbuf->b_visual.vi_start.lnum == 0
5650 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5651 || curbuf->b_visual.vi_end.lnum == 0)
5652 {
5653 beep_flush();
5654 return;
5655 }
5656
5657 // set w_cursor to the start of the Visual area, tpos to the end
5658 if (VIsual_active)
5659 {
5660 i = VIsual_mode;
5661 VIsual_mode = curbuf->b_visual.vi_mode;
5662 curbuf->b_visual.vi_mode = i;
5663# ifdef FEAT_EVAL
5664 curbuf->b_visual_mode_eval = i;
5665# endif
5666 i = curwin->w_curswant;
5667 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5668 curbuf->b_visual.vi_curswant = i;
5669
5670 tpos = curbuf->b_visual.vi_end;
5671 curbuf->b_visual.vi_end = curwin->w_cursor;
5672 curwin->w_cursor = curbuf->b_visual.vi_start;
5673 curbuf->b_visual.vi_start = VIsual;
5674 }
5675 else
5676 {
5677 VIsual_mode = curbuf->b_visual.vi_mode;
5678 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5679 tpos = curbuf->b_visual.vi_end;
5680 curwin->w_cursor = curbuf->b_visual.vi_start;
5681 }
5682
5683 VIsual_active = TRUE;
5684 VIsual_reselect = TRUE;
5685
5686 // Set Visual to the start and w_cursor to the end of the Visual
5687 // area. Make sure they are on an existing character.
5688 check_cursor();
5689 VIsual = curwin->w_cursor;
5690 curwin->w_cursor = tpos;
5691 check_cursor();
5692 update_topline();
5693
5694 // When called from normal "g" command: start Select mode when
5695 // 'selectmode' contains "cmd". When called for K_SELECT, always
5696 // start Select mode.
5697 if (cap->arg)
5698 {
5699 VIsual_select = TRUE;
5700 VIsual_select_reg = 0;
5701 }
5702 else
5703 may_start_select('c');
5704 setmouse();
5705#ifdef FEAT_CLIPBOARD
5706 // Make sure the clipboard gets updated. Needed because start and
5707 // end are still the same, and the selection needs to be owned
5708 clip_star.vmode = NUL;
5709#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005710 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005711 showmode();
5712}
5713
5714/*
5715 * "g0", "g^" : Like "0" and "^" but for screen lines.
5716 * "gm": middle of "g0" and "g$".
5717 */
5718 static void
5719nv_g_home_m_cmd(cmdarg_T *cap)
5720{
5721 int i;
5722 int flag = FALSE;
5723
5724 if (cap->nchar == '^')
5725 flag = TRUE;
5726
5727 cap->oap->motion_type = MCHAR;
5728 cap->oap->inclusive = FALSE;
5729 if (curwin->w_p_wrap && curwin->w_width != 0)
5730 {
5731 int width1 = curwin->w_width - curwin_col_off();
5732 int width2 = width1 + curwin_col_off2();
5733
5734 validate_virtcol();
5735 i = 0;
5736 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5737 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5738 }
5739 else
5740 i = curwin->w_leftcol;
5741 // Go to the middle of the screen line. When 'number' or
5742 // 'relativenumber' is on and lines are wrapping the middle can be more
5743 // to the left.
5744 if (cap->nchar == 'm')
5745 i += (curwin->w_width - curwin_col_off()
5746 + ((curwin->w_p_wrap && i > 0)
5747 ? curwin_col_off2() : 0)) / 2;
5748 coladvance((colnr_T)i);
5749 if (flag)
5750 {
5751 do
5752 i = gchar_cursor();
5753 while (VIM_ISWHITE(i) && oneright() == OK);
5754 curwin->w_valid &= ~VALID_WCOL;
5755 }
5756 curwin->w_set_curswant = TRUE;
5757}
5758
5759/*
5760 * "g_": to the last non-blank character in the line or <count> lines
5761 * downward.
5762 */
5763 static void
5764nv_g_underscore_cmd(cmdarg_T *cap)
5765{
5766 char_u *ptr;
5767
5768 cap->oap->motion_type = MCHAR;
5769 cap->oap->inclusive = TRUE;
5770 curwin->w_curswant = MAXCOL;
5771 if (cursor_down((long)(cap->count1 - 1),
5772 cap->oap->op_type == OP_NOP) == FAIL)
5773 {
5774 clearopbeep(cap->oap);
5775 return;
5776 }
5777
5778 ptr = ml_get_curline();
5779
5780 // In Visual mode we may end up after the line.
5781 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5782 --curwin->w_cursor.col;
5783
5784 // Decrease the cursor column until it's on a non-blank.
5785 while (curwin->w_cursor.col > 0
5786 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5787 --curwin->w_cursor.col;
5788 curwin->w_set_curswant = TRUE;
5789 adjust_for_sel(cap);
5790}
5791
5792/*
5793 * "g$" : Like "$" but for screen lines.
5794 */
5795 static void
5796nv_g_dollar_cmd(cmdarg_T *cap)
5797{
5798 oparg_T *oap = cap->oap;
5799 int i;
5800 int col_off = curwin_col_off();
5801
5802 oap->motion_type = MCHAR;
5803 oap->inclusive = TRUE;
5804 if (curwin->w_p_wrap && curwin->w_width != 0)
5805 {
5806 curwin->w_curswant = MAXCOL; // so we stay at the end
5807 if (cap->count1 == 1)
5808 {
5809 int width1 = curwin->w_width - col_off;
5810 int width2 = width1 + curwin_col_off2();
5811
5812 validate_virtcol();
5813 i = width1 - 1;
5814 if (curwin->w_virtcol >= (colnr_T)width1)
5815 i += ((curwin->w_virtcol - width1) / width2 + 1)
5816 * width2;
5817 coladvance((colnr_T)i);
5818
5819 // Make sure we stick in this column.
5820 validate_virtcol();
5821 curwin->w_curswant = curwin->w_virtcol;
5822 curwin->w_set_curswant = FALSE;
5823 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5824 {
5825 // Check for landing on a character that got split at
5826 // the end of the line. We do not want to advance to
5827 // the next screen line.
5828 if (curwin->w_virtcol > (colnr_T)i)
5829 --curwin->w_cursor.col;
5830 }
5831 }
5832 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5833 clearopbeep(oap);
5834 }
5835 else
5836 {
5837 if (cap->count1 > 1)
5838 // if it fails, let the cursor still move to the last char
5839 (void)cursor_down(cap->count1 - 1, FALSE);
5840
5841 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5842 coladvance((colnr_T)i);
5843
5844 // if the character doesn't fit move one back
5845 if (curwin->w_cursor.col > 0
5846 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5847 {
5848 colnr_T vcol;
5849
5850 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5851 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5852 --curwin->w_cursor.col;
5853 }
5854
5855 // Make sure we stick in this column.
5856 validate_virtcol();
5857 curwin->w_curswant = curwin->w_virtcol;
5858 curwin->w_set_curswant = FALSE;
5859 }
5860}
5861
5862/*
5863 * "gi": start Insert at the last position.
5864 */
5865 static void
5866nv_gi_cmd(cmdarg_T *cap)
5867{
5868 int i;
5869
5870 if (curbuf->b_last_insert.lnum != 0)
5871 {
5872 curwin->w_cursor = curbuf->b_last_insert;
5873 check_cursor_lnum();
5874 i = (int)STRLEN(ml_get_curline());
5875 if (curwin->w_cursor.col > (colnr_T)i)
5876 {
5877 if (virtual_active())
5878 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5879 curwin->w_cursor.col = i;
5880 }
5881 }
5882 cap->cmdchar = 'i';
5883 nv_edit(cap);
5884}
5885
5886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 * Commands starting with "g".
5888 */
5889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005890nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 switch (cap->nchar)
5896 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005897 case Ctrl_A:
5898 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005900 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005901 if (!VIsual_active && cap->nchar == Ctrl_A)
5902 vim_mem_profile_dump();
5903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005905 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005906 if (VIsual_active)
5907 {
5908 cap->arg = TRUE;
5909 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005910 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005911 nv_addsub(cap);
5912 }
5913 else
5914 clearopbeep(oap);
5915 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005917 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 case 'R':
5919 cap->arg = TRUE;
5920 nv_Replace(cap);
5921 break;
5922
5923 case 'r':
5924 nv_vreplace(cap);
5925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 case '&':
5928 do_cmdline_cmd((char_u *)"%s//~/&");
5929 break;
5930
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005931 // "gv": Reselect the previous Visual area. If Visual already active,
5932 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005934 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005936
5937 // "gV": Don't reselect the previous Visual area after a Select mode
5938 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case 'V':
5940 VIsual_reselect = FALSE;
5941 break;
5942
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005943 // "gh": start Select mode.
5944 // "gH": start Select line mode.
5945 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case K_BS:
5947 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005948 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case 'h':
5950 case 'H':
5951 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 cap->cmdchar = cap->nchar + ('v' - 'h');
5953 cap->arg = TRUE;
5954 nv_visual(cap);
5955 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005957 // "gn", "gN" visually select next/previous search match
5958 // "gn" selects next match
5959 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005960 case 'N':
5961 case 'n':
5962 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005963 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005964 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005966 // "gj" and "gk" two new funny movement keys -- up and down
5967 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 case 'j':
5969 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005970 // with 'nowrap' it works just like the normal "j" command.
5971 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 oap->motion_type = MLINE;
5974 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5975 }
5976 else
5977 i = nv_screengo(oap, FORWARD, cap->count1);
5978 if (i == FAIL)
5979 clearopbeep(oap);
5980 break;
5981
5982 case 'k':
5983 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005984 // with 'nowrap' it works just like the normal "k" command.
5985 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 oap->motion_type = MLINE;
5988 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5989 }
5990 else
5991 i = nv_screengo(oap, BACKWARD, cap->count1);
5992 if (i == FAIL)
5993 clearopbeep(oap);
5994 break;
5995
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005996 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 case 'J':
5998 nv_join(cap);
5999 break;
6000
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006001 // "g0", "g^" : Like "0" and "^" but for screen lines.
6002 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 case '0':
6005 case 'm':
6006 case K_HOME:
6007 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 break;
6010
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006011 case 'M':
6012 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006013 oap->motion_type = MCHAR;
6014 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006015 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006016 if (cap->count0 > 0 && cap->count0 <= 100)
6017 coladvance((colnr_T)(i * cap->count0 / 100));
6018 else
6019 coladvance((colnr_T)(i / 2));
6020 curwin->w_set_curswant = TRUE;
6021 }
6022 break;
6023
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006024 // "g_": to the last non-blank character in the line or <count> lines
6025 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006027 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 case '$':
6032 case K_END:
6033 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 break;
6036
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 case '*':
6039 case '#':
6040#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006041 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006043 case Ctrl_RSB: // :tag or :tselect for current identifier
6044 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 nv_ident(cap);
6046 break;
6047
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 case 'e':
6050 case 'E':
6051 oap->motion_type = MCHAR;
6052 curwin->w_set_curswant = TRUE;
6053 oap->inclusive = TRUE;
6054 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6055 clearopbeep(oap);
6056 break;
6057
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006060 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 break;
6062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006065 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 break;
6067
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006068 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 case 'I':
6070 beginline(0);
6071 if (!checkclearopq(oap))
6072 invoke_edit(cap, FALSE, 'g', FALSE);
6073 break;
6074
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 // "gf": goto file, edit file under cursor
6076 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006078 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 nv_gotofile(cap);
6080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 case '\'':
6084 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006085 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 case '`':
6087 nv_gomark(cap);
6088 break;
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006092 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 break;
6094
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006095 // "ga": Display the ascii value of the character under the
6096 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 case 'a':
6098 do_ascii(NULL);
6099 break;
6100
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006101 // "g8": Display the bytes used for the UTF-8 character under the
6102 // cursor. It is displayed in hex.
6103 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006105 if (cap->count0 == 8)
6106 utf_find_illegal();
6107 else
6108 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006111 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006112 case '<':
6113 show_sb_text();
6114 break;
6115
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006116 // "gg": Goto the first line in file. With a count it goes to
6117 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 case 'g':
6119 cap->arg = FALSE;
6120 nv_goto(cap);
6121 break;
6122
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006123 // Two-character operators:
6124 // "gq" Format text
6125 // "gw" Format text and keep cursor position
6126 // "g~" Toggle the case of the text.
6127 // "gu" Change text to lower case.
6128 // "gU" Change text to upper case.
6129 // "g?" rot13 encoding
6130 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 case 'q':
6132 case 'w':
6133 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 case '~':
6136 case 'u':
6137 case 'U':
6138 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006139 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 nv_operator(cap);
6141 break;
6142
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006143 // "gd": Find first occurrence of pattern under the cursor in the
6144 // current function
6145 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case 'd':
6147 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006148 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 break;
6150
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006151 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 case K_MIDDLEMOUSE:
6153 case K_MIDDLEDRAG:
6154 case K_MIDDLERELEASE:
6155 case K_LEFTMOUSE:
6156 case K_LEFTDRAG:
6157 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006158 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 case K_RIGHTMOUSE:
6160 case K_RIGHTDRAG:
6161 case K_RIGHTRELEASE:
6162 case K_X1MOUSE:
6163 case K_X1DRAG:
6164 case K_X1RELEASE:
6165 case K_X2MOUSE:
6166 case K_X2DRAG:
6167 case K_X2RELEASE:
6168 mod_mask = MOD_MASK_CTRL;
6169 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
6172 case K_IGNORE:
6173 break;
6174
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006175 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 case 'p':
6177 case 'P':
6178 nv_put(cap);
6179 break;
6180
6181#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006182 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 case 'o':
6184 goto_byte(cap->count0);
6185 break;
6186#endif
6187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006190 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 do_exmode(TRUE);
6192 break;
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 case ',':
6195 nv_pcmark(cap);
6196 break;
6197
6198 case ';':
6199 cap->count1 = -cap->count1;
6200 nv_pcmark(cap);
6201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006204 if (!checkclearop(oap))
6205 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006207 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006208 if (!checkclearop(oap))
6209 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006210 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211
Bram Moolenaar62a23252020-08-09 14:04:42 +02006212 case TAB:
6213 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6214 clearopbeep(oap);
6215 break;
6216
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006217 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006219 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006220 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006221 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006222 break;
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 default:
6225 clearopbeep(oap);
6226 break;
6227 }
6228}
6229
6230/*
6231 * Handle "o" and "O" commands.
6232 */
6233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006236#ifdef FEAT_CONCEAL
6237 linenr_T oldline = curwin->w_cursor.lnum;
6238#endif
6239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (!checkclearopq(cap->oap))
6241 {
6242#ifdef FEAT_FOLDING
6243 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 (void)hasFolding(curwin->w_cursor.lnum,
6246 &curwin->w_cursor.lnum, NULL);
6247 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006248 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 (void)hasFolding(curwin->w_cursor.lnum,
6250 NULL, &curwin->w_cursor.lnum);
6251#endif
6252 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6253 (cap->cmdchar == 'O' ? 1 : 0)),
6254 (linenr_T)(curwin->w_cursor.lnum +
6255 (cap->cmdchar == 'o' ? 1 : 0))
6256 ) == OK
6257 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006258 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006259 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006261#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006262 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006263 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006264#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006265#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006266 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006267 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006268 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006269#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006271 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6272 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6274 }
6275 }
6276}
6277
6278/*
6279 * "." command: redo last change.
6280 */
6281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006282nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 if (!checkclearopq(cap->oap))
6285 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006286 // If "restart_edit" is TRUE, the last but one command is repeated
6287 // instead of the last command (inserting text). This is used for
6288 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6290 clearopbeep(cap->oap);
6291 }
6292}
6293
6294/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006295 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 */
6297 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006298nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006300 if (VIsual_select && VIsual_active)
6301 {
6302 int reg;
6303 // Get register name
6304 ++no_mapping;
6305 ++allow_keys;
6306 reg = plain_vgetc();
6307 LANGMAP_ADJUST(reg, TRUE);
6308 --no_mapping;
6309 --allow_keys;
6310
6311 if (reg == '"')
6312 // the unnamed register is 0
6313 reg = 0;
6314
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006315 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006316 return;
6317 }
6318
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (!checkclearopq(cap->oap))
6320 {
6321 u_redo((int)cap->count1);
6322 curwin->w_set_curswant = TRUE;
6323 }
6324}
6325
6326/*
6327 * Handle "U" command.
6328 */
6329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006330nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006332 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006333 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006335 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 cap->cmdchar = 'g';
6337 cap->nchar = 'U';
6338 nv_operator(cap);
6339 }
6340 else if (!checkclearopq(cap->oap))
6341 {
6342 u_undoline();
6343 curwin->w_set_curswant = TRUE;
6344 }
6345}
6346
6347/*
6348 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6349 * single character.
6350 */
6351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006352nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006354 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006355 {
6356#ifdef FEAT_JOB_CHANNEL
6357 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6358 {
6359 clearopbeep(cap->oap);
6360 return;
6361 }
6362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 else
6366 nv_operator(cap);
6367}
6368
6369/*
6370 * Handle an operator command.
6371 * The actual work is done by do_pending_operator().
6372 */
6373 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006374nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376 int op_type;
6377
6378 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006379#ifdef FEAT_JOB_CHANNEL
6380 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6381 {
6382 clearopbeep(cap->oap);
6383 return;
6384 }
6385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006387 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 nv_lineop(cap);
6389 else if (!checkclearop(cap->oap))
6390 {
6391 cap->oap->start = curwin->w_cursor;
6392 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006393#ifdef FEAT_EVAL
6394 set_op_var(op_type);
6395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
6397}
6398
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006399#ifdef FEAT_EVAL
6400/*
6401 * Set v:operator to the characters for "optype".
6402 */
6403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006404set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006405{
6406 char_u opchars[3];
6407
6408 if (optype == OP_NOP)
6409 set_vim_var_string(VV_OP, NULL, 0);
6410 else
6411 {
6412 opchars[0] = get_op_char(optype);
6413 opchars[1] = get_extra_op_char(optype);
6414 opchars[2] = NUL;
6415 set_vim_var_string(VV_OP, opchars, -1);
6416 }
6417}
6418#endif
6419
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420/*
6421 * Handle linewise operator "dd", "yy", etc.
6422 *
6423 * "_" is is a strange motion command that helps make operators more logical.
6424 * It is actually implemented, but not documented in the real Vi. This motion
6425 * command actually refers to "the current line". Commands like "dd" and "yy"
6426 * are really an alternate form of "d_" and "y_". It does accept a count, so
6427 * "d3_" works to delete 3 lines.
6428 */
6429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006430nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 cap->oap->motion_type = MLINE;
6433 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6434 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006435 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006436 && cap->oap->motion_force != 'v'
6437 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 || cap->oap->op_type == OP_LSHIFT
6439 || cap->oap->op_type == OP_RSHIFT)
6440 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006441 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 beginline(BL_WHITE | BL_FIX);
6443}
6444
6445/*
6446 * <Home> command.
6447 */
6448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006449nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006451 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006452 if (mod_mask & MOD_MASK_CTRL)
6453 nv_goto(cap);
6454 else
6455 {
6456 cap->count0 = 1;
6457 nv_pipe(cap);
6458 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6460 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461}
6462
6463/*
6464 * "|" command.
6465 */
6466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006467nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 cap->oap->motion_type = MCHAR;
6470 cap->oap->inclusive = FALSE;
6471 beginline(0);
6472 if (cap->count0 > 0)
6473 {
6474 coladvance((colnr_T)(cap->count0 - 1));
6475 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6476 }
6477 else
6478 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006479 // keep curswant at the column where we wanted to go, not where
6480 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 curwin->w_set_curswant = FALSE;
6482}
6483
6484/*
6485 * Handle back-word command "b" and "B".
6486 * cap->arg is 1 for "B"
6487 */
6488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006489nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 cap->oap->motion_type = MCHAR;
6492 cap->oap->inclusive = FALSE;
6493 curwin->w_set_curswant = TRUE;
6494 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6495 clearopbeep(cap->oap);
6496#ifdef FEAT_FOLDING
6497 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6498 foldOpenCursor();
6499#endif
6500}
6501
6502/*
6503 * Handle word motion commands "e", "E", "w" and "W".
6504 * cap->arg is TRUE for "E" and "W".
6505 */
6506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006507nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 int n;
6510 int word_end;
6511 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006512 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006514 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6516 word_end = TRUE;
6517 else
6518 word_end = FALSE;
6519 cap->oap->inclusive = word_end;
6520
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006521 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if (!word_end && cap->oap->op_type == OP_CHANGE)
6523 {
6524 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006525 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006527 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006529 // Reproduce a funny Vi behaviour: "cw" on a blank only
6530 // changes one character, not all blanks until the start of
6531 // the next word. Only do this when the 'w' flag is included
6532 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6534 {
6535 cap->oap->inclusive = TRUE;
6536 cap->oap->motion_type = MCHAR;
6537 return;
6538 }
6539 }
6540 else
6541 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006542 // This is a little strange. To match what the real Vi does,
6543 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6544 // that we are not on a space or a TAB. This seems impolite
6545 // at first, but it's really more what we mean when we say
6546 // 'cw'.
6547 // Another strangeness: When standing on the end of a word
6548 // "ce" will change until the end of the next word, but "cw"
6549 // will change only one character! This is done by setting
6550 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 cap->oap->inclusive = TRUE;
6552 word_end = TRUE;
6553 flag = TRUE;
6554 }
6555 }
6556 }
6557
6558 cap->oap->motion_type = MCHAR;
6559 curwin->w_set_curswant = TRUE;
6560 if (word_end)
6561 n = end_word(cap->count1, cap->arg, flag, FALSE);
6562 else
6563 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006565 // Don't leave the cursor on the NUL past the end of line. Unless we
6566 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006567 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006568 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 if (n == FAIL && cap->oap->op_type == OP_NOP)
6571 clearopbeep(cap->oap);
6572 else
6573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575#ifdef FEAT_FOLDING
6576 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6577 foldOpenCursor();
6578#endif
6579 }
6580}
6581
6582/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006583 * Used after a movement command: If the cursor ends up on the NUL after the
6584 * end of the line, may move it back to the last character and make the motion
6585 * inclusive.
6586 */
6587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006588adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006589{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006590 // The cursor cannot remain on the NUL when:
6591 // - the column is > 0
6592 // - not in Visual mode or 'selection' is "o"
6593 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006594 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006595 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006596 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 {
6598 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006599 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006600 if (has_mbyte)
6601 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 oap->inclusive = TRUE;
6603 }
6604}
6605
6606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 * "0" and "^" commands.
6608 * cap->arg is the argument for beginline().
6609 */
6610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
6613 cap->oap->motion_type = MCHAR;
6614 cap->oap->inclusive = FALSE;
6615 beginline(cap->arg);
6616#ifdef FEAT_FOLDING
6617 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6618 foldOpenCursor();
6619#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6621 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622}
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624/*
6625 * In exclusive Visual mode, may include the last character.
6626 */
6627 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006631 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 if (has_mbyte)
6634 inc_cursor();
6635 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 ++curwin->w_cursor.col;
6637 cap->oap->inclusive = FALSE;
6638 }
6639}
6640
6641/*
6642 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6643 * Should check VIsual_mode before calling this.
6644 * Returns TRUE when backed up to the previous line.
6645 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006646 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006647unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 pos_T *pp;
6650
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006651 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006653 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 pp = &curwin->w_cursor;
6655 else
6656 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (pp->coladd > 0)
6658 --pp->coladd;
6659 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if (pp->col > 0)
6661 {
6662 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006663 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 }
6665 else if (pp->lnum > 1)
6666 {
6667 --pp->lnum;
6668 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6669 return TRUE;
6670 }
6671 }
6672 return FALSE;
6673}
6674
6675/*
6676 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6677 */
6678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006679nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006684 VIsual_select_reg = 0;
6685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 else if (VIsual_reselect)
6687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006688 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 cap->arg = TRUE;
6690 nv_g_cmd(cap);
6691 }
6692}
6693
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
6695/*
6696 * "G", "gg", CTRL-END, CTRL-HOME.
6697 * cap->arg is TRUE for "G".
6698 */
6699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006700nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 linenr_T lnum;
6703
6704 if (cap->arg)
6705 lnum = curbuf->b_ml.ml_line_count;
6706 else
6707 lnum = 1L;
6708 cap->oap->motion_type = MLINE;
6709 setpcmark();
6710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006711 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 if (cap->count0 != 0)
6713 lnum = cap->count0;
6714 if (lnum < 1L)
6715 lnum = 1L;
6716 else if (lnum > curbuf->b_ml.ml_line_count)
6717 lnum = curbuf->b_ml.ml_line_count;
6718 curwin->w_cursor.lnum = lnum;
6719 beginline(BL_SOL | BL_FIX);
6720#ifdef FEAT_FOLDING
6721 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6722 foldOpenCursor();
6723#endif
6724}
6725
6726/*
6727 * CTRL-\ in Normal mode.
6728 */
6729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006730nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6733 {
6734 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006735 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006736 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 restart_edit = 0;
6738#ifdef FEAT_CMDWIN
6739 if (cmdwin_type != 0)
6740 cmdwin_result = Ctrl_C;
6741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (VIsual_active)
6743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006745 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006747 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (cap->nchar == Ctrl_G && p_im)
6749 restart_edit = 'a';
6750 }
6751 else
6752 clearopbeep(cap->oap);
6753}
6754
6755/*
6756 * ESC in Normal mode: beep, but don't flush buffers.
6757 * Don't even beep if we are canceling a command.
6758 */
6759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006760nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 int no_reason;
6763
6764 no_reason = (cap->oap->op_type == OP_NOP
6765 && cap->opcount == 0
6766 && cap->count0 == 0
6767 && cap->oap->regname == 0
6768 && !p_im);
6769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006770 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 if (restart_edit == 0
6773#ifdef FEAT_CMDWIN
6774 && cmdwin_type == 0
6775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006778 {
6779 if (anyBufIsChanged())
6780 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6781 else
6782 msg(_("Type :qa and press <Enter> to exit Vim"));
6783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006785 if (restart_edit != 0)
6786 redraw_mode = TRUE; // remove "-- (insert) --"
6787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006788 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6789 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (!p_im)
6791 restart_edit = 0;
6792#ifdef FEAT_CMDWIN
6793 if (cmdwin_type != 0)
6794 {
6795 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006796 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 return;
6798 }
6799#endif
6800 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006801#ifdef FEAT_CMDWIN
Bram Moolenaar8d696372022-08-21 10:40:07 +01006802 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006803 {
6804 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006805 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6806 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006807 cmdwin_result = K_IGNORE;
6808 return;
6809 }
6810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (VIsual_active)
6813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006814 end_visual_mode(); // stop Visual
6815 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006817 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006819 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006820 {
6821#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006822 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006823 popup_hide_message_win();
6824 else
6825#endif
6826 vim_beep(BO_ESC);
6827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 clearop(cap->oap);
6829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006830 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6831 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006832 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 restart_edit = 'a';
6834}
6835
6836/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006837 * Move the cursor for the "A" command.
6838 */
6839 void
6840set_cursor_for_append_to_line(void)
6841{
6842 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006843 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006844 {
6845 int save_State = State;
6846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 // Pretend Insert mode here to allow the cursor on the
6848 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006849 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006850 coladvance((colnr_T)MAXCOL);
6851 State = save_State;
6852 }
6853 else
6854 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6855}
6856
6857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006859 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 */
6861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006864 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6866 cap->cmdchar = 'i';
6867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006870 {
6871#ifdef FEAT_TERMINAL
6872 if (term_in_normal_mode())
6873 {
6874 end_visual_mode();
6875 clearop(cap->oap);
6876 term_enter_job_mode();
6877 return;
6878 }
6879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006883 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006884 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6885 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006889#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006890 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006891 {
6892 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006893 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006894 return;
6895 }
6896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 else if (!curbuf->b_p_ma && !p_im)
6898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006899 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006900 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006902 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006903 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006904 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006906 else if (cap->cmdchar == K_PS && VIsual_active)
6907 {
6908 pos_T old_pos = curwin->w_cursor;
6909 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006910 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006913 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6914 {
6915 shift_delete_registers();
6916 cap->oap->regname = '1';
6917 }
6918 else
6919 cap->oap->regname = '-';
6920 cap->cmdchar = 'd';
6921 cap->nchar = NUL;
6922 nv_operator(cap);
6923 do_pending_operator(cap, 0, FALSE);
6924 cap->cmdchar = K_PS;
6925
zeertzjq7a732522022-03-14 20:46:41 +00006926 if (*ml_get_cursor() != NUL)
6927 {
6928 if (old_visual_mode == 'V')
6929 {
6930 // In linewise Visual mode insert before the beginning of the
6931 // next line.
6932 // When the last line in the buffer was deleted then create a
6933 // new line, otherwise there is not need to move cursor.
6934 // Detect this by checking if cursor moved above Visual area.
6935 if (curwin->w_cursor.lnum < old_pos.lnum
6936 && curwin->w_cursor.lnum < old_visual.lnum)
6937 {
6938 if (u_save_cursor() == OK)
6939 {
6940 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6941 FALSE);
6942 appended_lines(curwin->w_cursor.lnum++, 1L);
6943 }
6944 }
6945 }
6946 // When the last char in the line was deleted then append.
6947 // Detect this by checking if cursor moved before Visual area.
6948 else if (curwin->w_cursor.col < old_pos.col
6949 && curwin->w_cursor.col < old_visual.col)
6950 inc_cursor();
6951 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006954 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 else if (!checkclearopq(cap->oap))
6957 {
6958 switch (cap->cmdchar)
6959 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006961 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 break;
6963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006964 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006965 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6966 beginline(BL_WHITE);
6967 else
6968 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 break;
6970
Bram Moolenaara1891842017-02-04 21:34:31 +01006971 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 // Bracketed paste works like "a"ppend, unless the cursor is in
6973 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006974 if (curwin->w_cursor.col == 0)
6975 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006976 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 case 'a': // "a"ppend is like "i"nsert on the next character.
6979 // increment coladd when in virtual space, increment the
6980 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (virtual_active()
6982 && (curwin->w_cursor.coladd > 0
6983 || *ml_get_cursor() == NUL
6984 || *ml_get_cursor() == TAB))
6985 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006986 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 inc_cursor();
6988 break;
6989 }
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6992 {
6993 int save_State = State;
6994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006995 // Pretend Insert mode here to allow the cursor on the
6996 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006997 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 coladvance(getviscol());
6999 State = save_State;
7000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7003 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007004 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007006 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007}
7008
7009/*
7010 * Invoke edit() and take care of "restart_edit" and the return value.
7011 */
7012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007013invoke_edit(
7014 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007016 int cmd,
7017 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 int restart_edit_save = 0;
7020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007021 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7022 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7023 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (repl || !stuff_empty())
7025 restart_edit_save = restart_edit;
7026 else
7027 restart_edit_save = 0;
7028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007029 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 restart_edit = 0;
7031
7032 if (edit(cmd, startln, cap->count1))
7033 cap->retval |= CA_COMMAND_BUSY;
7034
7035 if (restart_edit == 0)
7036 restart_edit = restart_edit_save;
7037}
7038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039/*
7040 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7041 */
7042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007043nv_object(
7044 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 int flag;
7047 int include;
7048 char_u *mps_save;
7049
7050 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007053 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 mps_save = curbuf->b_p_mps;
7057 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7058
7059 switch (cap->nchar)
7060 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007061 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 flag = current_word(cap->oap, cap->count1, include, FALSE);
7063 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 flag = current_word(cap->oap, cap->count1, include, TRUE);
7066 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 case '(':
7069 case ')':
7070 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7071 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007072 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 case '{':
7074 case '}':
7075 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7076 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 case ']':
7079 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7080 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 case '>':
7083 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7084 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007085#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 case 't': // "at" = a tag block (xml and html)
7087 // Do not adjust oap->end in do_pending_operator()
7088 // otherwise there are different results for 'dit'
7089 // (note leading whitespace in last line):
7090 // 1) <b> 2) <b>
7091 // foobar foobar
7092 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007093 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007094 flag = current_tagblock(cap->oap, cap->count1, include);
7095 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007096#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 flag = current_par(cap->oap, cap->count1, include, 'p');
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 flag = current_sent(cap->oap, cap->count1, include);
7102 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 case '"': // "a"" = a double quoted string
7104 case '\'': // "a'" = a single quoted string
7105 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007106 flag = current_quote(cap->oap, cap->count1, include,
7107 cap->nchar);
7108 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109#if 0 // TODO
7110 case 'S': // "aS" = a section
7111 case 'f': // "af" = a filename
7112 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113#endif
7114 default:
7115 flag = FAIL;
7116 break;
7117 }
7118
7119 curbuf->b_p_mps = mps_save;
7120 if (flag == FAIL)
7121 clearopbeep(cap->oap);
7122 adjust_cursor_col();
7123 curwin->w_set_curswant = TRUE;
7124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126/*
7127 * "q" command: Start/stop recording.
7128 * "q:", "q/", "q?": edit command-line in command-line window.
7129 */
7130 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007131nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
7133 if (cap->oap->op_type == OP_FORMAT)
7134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 cap->cmdchar = 'g';
7137 cap->nchar = 'q';
7138 nv_operator(cap);
7139 }
7140 else if (!checkclearop(cap->oap))
7141 {
7142#ifdef FEAT_CMDWIN
7143 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7144 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007145 if (cmdwin_type != 0)
7146 {
7147 emsg(_(e_cmdline_window_already_open));
7148 return;
7149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 stuffcharReadbuff(cap->nchar);
7151 stuffcharReadbuff(K_CMDWIN);
7152 }
7153 else
7154#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007155 // (stop) recording into a named register, unless executing a
7156 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007157 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 clearopbeep(cap->oap);
7159 }
7160}
7161
7162/*
7163 * Handle the "@r" command.
7164 */
7165 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007166nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
7168 if (checkclearop(cap->oap))
7169 return;
7170#ifdef FEAT_EVAL
7171 if (cap->nchar == '=')
7172 {
7173 if (get_expr_register() == NUL)
7174 return;
7175 }
7176#endif
7177 while (cap->count1-- && !got_int)
7178 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007179 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
7181 clearopbeep(cap->oap);
7182 break;
7183 }
7184 line_breakcheck();
7185 }
7186}
7187
7188/*
7189 * Handle the CTRL-U and CTRL-D commands.
7190 */
7191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007192nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7195 || (cap->cmdchar == Ctrl_D
7196 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7197 clearopbeep(cap->oap);
7198 else if (!checkclearop(cap->oap))
7199 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7200}
7201
7202/*
7203 * Handle "J" or "gJ" command.
7204 */
7205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007206nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007210 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {
7212 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7215 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007218 if (cap->count0 <= 2)
7219 {
7220 clearopbeep(cap->oap);
7221 return;
7222 }
7223 cap->count0 = curbuf->b_ml.ml_line_count
7224 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007226
7227 prep_redo(cap->oap->regname, cap->count0,
7228 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7229 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
7231}
7232
7233/*
7234 * "P", "gP", "p" and "gp" commands.
7235 */
7236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007237nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007239 nv_put_opt(cap, FALSE);
7240}
7241
7242/*
7243 * "P", "gP", "p" and "gp" commands.
7244 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7245 */
7246 static void
7247nv_put_opt(cmdarg_T *cap, int fix_indent)
7248{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 int regname = 0;
7250 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007251 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007252 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 int dir;
7254 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007255 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
7257 if (cap->oap->op_type != OP_NOP)
7258 {
7259#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007260 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7262 {
7263 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007264 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
7266 else
7267#endif
7268 clearopbeep(cap->oap);
7269 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007270#ifdef FEAT_JOB_CHANNEL
7271 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7272 {
7273 clearopbeep(cap->oap);
7274 }
7275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 else
7277 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007278 if (fix_indent)
7279 {
7280 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7281 ? FORWARD : BACKWARD;
7282 flags |= PUT_FIXINDENT;
7283 }
7284 else
7285 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007286 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7287 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 prep_redo_cmd(cap);
7289 if (cap->cmdchar == 'g')
7290 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007291 else if (cap->cmdchar == 'z')
7292 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (VIsual_active)
7295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007296 // Putting in Visual mode: The put text replaces the selected
7297 // text. First delete the selected text, then put the new text.
7298 // Need to save and restore the registers that the delete
7299 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007300 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007302 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007303#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007305#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007306 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007307 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007308#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 )
7313 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007314 // The delete is going to overwrite the register we want to
7315 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 reg1 = get_register(regname, TRUE);
7317 }
7318
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007319 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 cap->cmdchar = 'd';
7321 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007322 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007323 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 nv_operator(cap);
7325 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007326 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007327 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007329 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 cap->oap->regname = regname;
7331
7332 if (reg1 != NULL)
7333 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007334 // Delete probably changed the register we want to put, save
7335 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 reg2 = get_register(regname, FALSE);
7337 put_register(regname, reg1);
7338 }
7339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007340 // When deleted a linewise Visual area, put the register as
7341 // lines to avoid it joined with the next line. When deletion was
7342 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (VIsual_mode == 'V')
7344 flags |= PUT_LINE;
7345 else if (VIsual_mode == 'v')
7346 flags |= PUT_LINE_SPLIT;
7347 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7348 flags |= PUT_LINE_FORWARD;
7349 dir = BACKWARD;
7350 if ((VIsual_mode != 'V'
7351 && curwin->w_cursor.col < curbuf->b_op_start.col)
7352 || (VIsual_mode == 'V'
7353 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 // cursor is at the end of the line or end of file, put
7355 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007357 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007358 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007360 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007362 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (reg2 != NULL)
7364 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // What to reselect with "gv"? Selecting the just put text seems to
7367 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007368 if (was_visual)
7369 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007370 curbuf->b_visual.vi_start = curbuf->b_op_start;
7371 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007372 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007373 if (*p_sel == 'e')
7374 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007375 }
7376
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007377 // When all lines were selected and deleted do_put() leaves an empty
7378 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007379 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007380 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007381 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007382 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007384 // If the cursor was in that line, move it to the end of the last
7385 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007386 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7387 {
7388 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7389 coladvance((colnr_T)MAXCOL);
7390 }
7391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 auto_format(FALSE, TRUE);
7393 }
7394}
7395
7396/*
7397 * "o" and "O" commands.
7398 */
7399 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007400nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007403 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7405 {
7406 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007407 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
7409 else
7410#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007411 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007413#ifdef FEAT_JOB_CHANNEL
7414 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007415 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 n_opencmd(cap);
7419}
7420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421#ifdef FEAT_NETBEANS_INTG
7422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007423nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 netbeans_keycommand(cap->nchar);
7426}
7427#endif
7428
7429#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007431nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007433 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007436
Bram Moolenaar3918c952005-03-15 22:34:55 +00007437/*
7438 * Trigger CursorHold event.
7439 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7440 * input buffer. "did_cursorhold" is set to avoid retriggering.
7441 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007443nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007444{
7445 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7446 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007447 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007448}