blob: b1335a0373a6b5d89ca4af6f11236e70f2964cb8 [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
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001799 if (p_ch == 0)
1800 return;
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 cursor_off();
1803
1804 len = (int)STRLEN(showcmd_buf);
1805 if (len == 0)
1806 showcmd_is_clear = TRUE;
1807 else
1808 {
1809 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1810 showcmd_is_clear = FALSE;
1811 }
1812
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001813 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1814 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1816
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001817 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818}
1819#endif
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * When "check" is FALSE, prepare for commands that scroll the window.
1823 * When "check" is TRUE, take care of scroll-binding after the window has
1824 * scrolled. Called from normal_cmd() and edit().
1825 */
1826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001827do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 static win_T *old_curwin = NULL;
1830 static linenr_T old_topline = 0;
1831#ifdef FEAT_DIFF
1832 static int old_topfill = 0;
1833#endif
1834 static buf_T *old_buf = NULL;
1835 static colnr_T old_leftcol = 0;
1836
1837 if (check && curwin->w_p_scb)
1838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001839 // If a ":syncbind" command was just used, don't scroll, only reset
1840 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (did_syncbind)
1842 did_syncbind = FALSE;
1843 else if (curwin == old_curwin)
1844 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001845 // Synchronize other windows, as necessary according to
1846 // 'scrollbind'. Don't do this after an ":edit" command, except
1847 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if ((curwin->w_buffer == old_buf
1849#ifdef FEAT_DIFF
1850 || curwin->w_p_diff
1851#endif
1852 )
1853 && (curwin->w_topline != old_topline
1854#ifdef FEAT_DIFF
1855 || curwin->w_topfill != old_topfill
1856#endif
1857 || curwin->w_leftcol != old_leftcol))
1858 {
1859 check_scrollbind(curwin->w_topline - old_topline,
1860 (long)(curwin->w_leftcol - old_leftcol));
1861 }
1862 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001865 // When switching between windows, make sure that the relative
1866 // vertical offset is valid for the new window. The relative
1867 // offset is invalid whenever another 'scrollbind' window has
1868 // scrolled to a point that would force the current window to
1869 // scroll past the beginning or end of its buffer. When the
1870 // resync is performed, some of the other 'scrollbind' windows may
1871 // need to jump so that the current window's relative position is
1872 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1874 }
1875 curwin->w_scbind_pos = curwin->w_topline;
1876 }
1877
1878 old_curwin = curwin;
1879 old_topline = curwin->w_topline;
1880#ifdef FEAT_DIFF
1881 old_topfill = curwin->w_topfill;
1882#endif
1883 old_buf = curwin->w_buffer;
1884 old_leftcol = curwin->w_leftcol;
1885}
1886
1887/*
1888 * Synchronize any windows that have "scrollbind" set, based on the
1889 * number of rows by which the current window has changed
1890 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1891 */
1892 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001893check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int want_ver;
1896 int want_hor;
1897 win_T *old_curwin = curwin;
1898 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 int old_VIsual_select = VIsual_select;
1900 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 colnr_T tgt_leftcol = curwin->w_leftcol;
1902 long topline;
1903 long y;
1904
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001905 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1907#ifdef FEAT_DIFF
1908 want_ver |= old_curwin->w_p_diff;
1909#endif
1910 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1911
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001912 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001914 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001917 // skip original window and windows with 'noscrollbind'
1918 if (curwin == old_curwin || !curwin->w_p_scb)
1919 continue;
1920
1921 // do the vertical scroll
1922 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001925 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
zeertzjq101d57b2022-07-31 18:34:32 +01001927 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
zeertzjq101d57b2022-07-31 18:34:32 +01001929 else
1930#endif
1931 {
1932 curwin->w_scbind_pos += topline_diff;
1933 topline = curwin->w_scbind_pos;
1934 if (topline > curbuf->b_ml.ml_line_count)
1935 topline = curbuf->b_ml.ml_line_count;
1936 if (topline < 1)
1937 topline = 1;
1938
1939 y = topline - curwin->w_topline;
1940 if (y > 0)
1941 scrollup(y, FALSE);
1942 else
1943 scrolldown(-y, FALSE);
1944 }
1945
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001946 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001947 cursor_correct();
1948 curwin->w_redr_status = TRUE;
1949 }
1950
1951 // do the horizontal scroll
1952 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1953 {
1954 curwin->w_leftcol = tgt_leftcol;
1955 leftcol_changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001959 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 VIsual_select = old_VIsual_select;
1961 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 curwin = old_curwin;
1963 curbuf = old_curbuf;
1964}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966/*
1967 * Command character that's ignored.
1968 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001969 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001972nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001974 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975}
1976
1977/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001978 * Command character that doesn't do anything, but unlike nv_ignore() does
1979 * start edit(). Used for "startinsert" executed while starting up.
1980 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001982nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001983{
1984}
1985
1986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 * Command character doesn't exist.
1988 */
1989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001990nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
1992 clearopbeep(cap->oap);
1993}
1994
1995/*
1996 * <Help> and <F1> commands.
1997 */
1998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 if (!checkclearopq(cap->oap))
2002 ex_help(NULL);
2003}
2004
2005/*
2006 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2007 */
2008 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002009nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002011#ifdef FEAT_JOB_CHANNEL
2012 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2013 clearopbeep(cap->oap);
2014 else
2015#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002016 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002017 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002018 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002019 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2020 op_addsub(cap->oap, cap->count1, cap->arg);
2021 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002022 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002023 else if (VIsual_active)
2024 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002025 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002026 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027}
2028
2029/*
2030 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2031 */
2032 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002033nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002036 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 if (mod_mask & MOD_MASK_CTRL)
2038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002039 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002040 if (cap->arg == BACKWARD)
2041 goto_tabpage(-(int)cap->count1);
2042 else
2043 goto_tabpage((int)cap->count0);
2044 }
2045 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002046 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048}
2049
2050/*
2051 * Implementation of "gd" and "gD" command.
2052 */
2053 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002054nv_gd(
2055 oparg_T *oap,
2056 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002057 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 char_u *ptr;
2061
Bram Moolenaard9d30582005-05-18 22:10:28 +00002062 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002063 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002064 == FAIL)
2065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002067 }
2068 else
2069 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002070#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002071 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2072 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002073#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002074 // clear any search statistics
2075 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2076 clear_cmdline = TRUE;
2077 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002078}
2079
2080/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002081 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2082 * otherwise.
2083 */
2084 static int
2085is_ident(char_u *line, int offset)
2086{
2087 int i;
2088 int incomment = FALSE;
2089 int instring = 0;
2090 int prev = 0;
2091
2092 for (i = 0; i < offset && line[i] != NUL; i++)
2093 {
2094 if (instring != 0)
2095 {
2096 if (prev != '\\' && line[i] == instring)
2097 instring = 0;
2098 }
2099 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2100 {
2101 instring = line[i];
2102 }
2103 else
2104 {
2105 if (incomment)
2106 {
2107 if (prev == '*' && line[i] == '/')
2108 incomment = FALSE;
2109 }
2110 else if (prev == '/' && line[i] == '*')
2111 {
2112 incomment = TRUE;
2113 }
2114 else if (prev == '/' && line[i] == '/')
2115 {
2116 return FALSE;
2117 }
2118 }
2119
2120 prev = line[i];
2121 }
2122
2123 return incomment == FALSE && instring == 0;
2124}
2125
2126/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002127 * Search for variable declaration of "ptr[len]".
2128 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2129 * current file ("gD").
2130 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002131 * Return FAIL when not found.
2132 */
2133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134find_decl(
2135 char_u *ptr,
2136 int len,
2137 int locally,
2138 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002139 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002140{
2141 char_u *pat;
2142 pos_T old_pos;
2143 pos_T par_pos;
2144 pos_T found_pos;
2145 int t;
2146 int save_p_ws;
2147 int save_p_scs;
2148 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002149 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002150 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002151 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002152
2153 if ((pat = alloc(len + 7)) == NULL)
2154 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002156 // Put "\V" before the pattern to avoid that the special meaning of "."
2157 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002158 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2159 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 old_pos = curwin->w_cursor;
2161 save_p_ws = p_ws;
2162 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002163 p_ws = FALSE; // don't wrap around end of file now
2164 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002166 // With "gD" go to line 1.
2167 // With "gd" Search back for the start of the current function, then go
2168 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002169 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002171 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002173 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175 else
2176 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002177 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2179 --curwin->w_cursor.lnum;
2180 }
2181 curwin->w_cursor.col = 0;
2182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002183 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002184 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002185 for (;;)
2186 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002187 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002188 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002189 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002190 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002191
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002192 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002193 {
2194 pos_T *pos;
2195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002196 // Check that the block the match is in doesn't end before the
2197 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002198 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2199 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2200 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002201 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 // There can't be a useful match before the end of this block.
2203 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002204 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002205 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002206 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002207 }
2208
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002209 if (t == FAIL)
2210 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002211 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002212 if (found_pos.lnum != 0)
2213 {
2214 curwin->w_cursor = found_pos;
2215 t = OK;
2216 }
2217 break;
2218 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002219 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002221 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002222 ++curwin->w_cursor.lnum;
2223 curwin->w_cursor.col = 0;
2224 continue;
2225 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002226 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002228 // If the current position is not a valid identifier and a previous
2229 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002230 if (!valid && found_pos.lnum != 0)
2231 {
2232 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002233 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 }
2235
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002236 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002237 if (valid && !locally)
2238 break;
2239 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002242 if (found_pos.lnum != 0)
2243 curwin->w_cursor = found_pos;
2244 break;
2245 }
2246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002247 // For finding a local variable and the match is before the "{" or
2248 // inside a comment, continue searching. For K&R style function
2249 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002250 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002251 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002252 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002253 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002254 // Remove SEARCH_START from flags to avoid getting stuck at one
2255 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002256 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002258
2259 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002261 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 curwin->w_cursor = old_pos;
2263 }
2264 else
2265 {
2266 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002267 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 reset_search_dir();
2269 }
2270
2271 vim_free(pat);
2272 p_ws = save_p_ws;
2273 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002274
2275 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276}
2277
2278/*
2279 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2280 * lines rather than lines in the file.
2281 * 'dist' must be positive.
2282 *
2283 * Return OK if able to move cursor, FAIL otherwise.
2284 */
2285 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002286nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 int linelen = linetabsize(ml_get_curline());
2289 int retval = OK;
2290 int atend = FALSE;
2291 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002292 int col_off1; // margin offset for first screen line
2293 int col_off2; // margin offset for wrapped screen line
2294 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002295 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002298 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
2300 col_off1 = curwin_col_off();
2301 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002302 width1 = curwin->w_width - col_off1;
2303 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002304 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002305 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002308 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002309 // Instead of sticking at the last character of the buffer line we
2310 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (curwin->w_curswant == MAXCOL)
2312 {
2313 atend = TRUE;
2314 validate_virtcol();
2315 if (width1 <= 0)
2316 curwin->w_curswant = 0;
2317 else
2318 {
2319 curwin->w_curswant = width1 - 1;
2320 if (curwin->w_virtcol > curwin->w_curswant)
2321 curwin->w_curswant += ((curwin->w_virtcol
2322 - curwin->w_curswant - 1) / width2 + 1) * width2;
2323 }
2324 }
2325 else
2326 {
2327 if (linelen > width1)
2328 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2329 else
2330 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002331 if (curwin->w_curswant >= (colnr_T)n)
2332 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334
2335 while (dist--)
2336 {
2337 if (dir == BACKWARD)
2338 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002339 if ((long)curwin->w_curswant >= width1
2340#ifdef FEAT_FOLDING
2341 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2342#endif
2343 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002344 // Move back within the line. This can give a negative value
2345 // for w_curswant if width1 < width2 (with cpoptions+=n),
2346 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 curwin->w_curswant -= width2;
2348 else
2349 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002350 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002352 // Move to the start of a closed fold. Don't do that when
2353 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (!(fdo_flags & FDO_ALL))
2355 (void)hasFolding(curwin->w_cursor.lnum,
2356 &curwin->w_cursor.lnum, NULL);
2357#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002358 if (curwin->w_cursor.lnum == 1)
2359 {
2360 retval = FAIL;
2361 break;
2362 }
2363 --curwin->w_cursor.lnum;
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 linelen = linetabsize(ml_get_curline());
2366 if (linelen > width1)
2367 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2368 + 1) * width2;
2369 }
2370 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 if (linelen > width1)
2374 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2375 else
2376 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002377 if (curwin->w_curswant + width2 < (colnr_T)n
2378#ifdef FEAT_FOLDING
2379 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2380#endif
2381 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 curwin->w_curswant += width2;
2384 else
2385 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2390 &curwin->w_cursor.lnum);
2391#endif
2392 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2393 {
2394 retval = FAIL;
2395 break;
2396 }
2397 curwin->w_cursor.lnum++;
2398 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002399 // Check if the cursor has moved below the number display
2400 // when width1 < width2 (with cpoptions+=n). Subtract width2
2401 // to get a negative value for w_curswant, which will get
2402 // clipped to column 0.
2403 if (curwin->w_curswant >= width1)
2404 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002405 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 }
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002411 if (virtual_active() && atend)
2412 coladvance(MAXCOL);
2413 else
2414 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2417 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002418 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002419 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002420
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002421 // Check for landing on a character that got split at the end of the
2422 // last line. We want to advance a screenline, not end up in the same
2423 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002425 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002426#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002427 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2428 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002429#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002430
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002431 c = (*mb_ptr2char)(ml_get_cursor());
2432 if (dir == FORWARD && virtcol < curwin->w_curswant
2433 && (curwin->w_curswant <= (colnr_T)width1)
2434 && !vim_isprintc(c) && c > 255)
2435 oneright();
2436
Bram Moolenaar773b1582014-08-29 14:20:51 +02002437 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 && (curwin->w_curswant < (colnr_T)width1
2439 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2440 : ((curwin->w_curswant - width1) % width2
2441 > (colnr_T)width2 / 2)))
2442 --curwin->w_cursor.col;
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448 return retval;
2449}
2450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451/*
2452 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2453 * cap->arg must be TRUE for CTRL-E.
2454 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 if (!checkclearop(cap->oap))
2459 scroll_redraw(cap->arg, cap->count1);
2460}
2461
2462/*
2463 * Scroll "count" lines up or down, and redraw.
2464 */
2465 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002466scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 linenr_T prev_topline = curwin->w_topline;
2469#ifdef FEAT_DIFF
2470 int prev_topfill = curwin->w_topfill;
2471#endif
2472 linenr_T prev_lnum = curwin->w_cursor.lnum;
2473
2474 if (up)
2475 scrollup(count, TRUE);
2476 else
2477 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002478 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2481 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 cursor_correct();
2483 check_cursor_moved(curwin);
2484 curwin->w_valid |= VALID_TOPLINE;
2485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 // If moved back to where we were, at least move the cursor, otherwise
2487 // we get stuck at one position. Don't move the cursor up if the
2488 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 while (curwin->w_topline == prev_topline
2490#ifdef FEAT_DIFF
2491 && curwin->w_topfill == prev_topfill
2492#endif
2493 )
2494 {
2495 if (up)
2496 {
2497 if (curwin->w_cursor.lnum > prev_lnum
2498 || cursor_down(1L, FALSE) == FAIL)
2499 break;
2500 }
2501 else
2502 {
2503 if (curwin->w_cursor.lnum < prev_lnum
2504 || prev_topline == 1L
2505 || cursor_up(1L, FALSE) == FAIL)
2506 break;
2507 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002508 // Mark w_topline as valid, otherwise the screen jumps back at the
2509 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 check_cursor_moved(curwin);
2511 curwin->w_valid |= VALID_TOPLINE;
2512 }
2513 }
2514 if (curwin->w_cursor.lnum != prev_lnum)
2515 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002516 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517}
2518
2519/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002520 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2521 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2522 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002523 */
2524 static int
2525nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2526{
2527 int nchar = *nchar_arg;
2528 long n;
2529
2530 // "z123{nchar}": edit the count before obtaining {nchar}
2531 if (checkclearop(cap->oap))
2532 return FALSE;
2533 n = nchar - '0';
2534
2535 for (;;)
2536 {
2537#ifdef USE_ON_FLY_SCROLL
2538 dont_scroll = TRUE; // disallow scrolling here
2539#endif
2540 ++no_mapping;
2541 ++allow_keys; // no mapping for nchar, but allow key codes
2542 nchar = plain_vgetc();
2543 LANGMAP_ADJUST(nchar, TRUE);
2544 --no_mapping;
2545 --allow_keys;
2546#ifdef FEAT_CMDL_INFO
2547 (void)add_to_showcmd(nchar);
2548#endif
2549 if (nchar == K_DEL || nchar == K_KDEL)
2550 n /= 10;
2551 else if (VIM_ISDIGIT(nchar))
2552 n = n * 10 + (nchar - '0');
2553 else if (nchar == CAR)
2554 {
2555#ifdef FEAT_GUI
2556 need_mouse_correct = TRUE;
2557#endif
2558 win_setheight((int)n);
2559 break;
2560 }
2561 else if (nchar == 'l'
2562 || nchar == 'h'
2563 || nchar == K_LEFT
2564 || nchar == K_RIGHT)
2565 {
2566 cap->count1 = n ? n * cap->count1 : cap->count1;
2567 *nchar_arg = nchar;
2568 return TRUE;
2569 }
2570 else
2571 {
2572 clearopbeep(cap->oap);
2573 break;
2574 }
2575 }
2576 cap->oap->op_type = OP_NOP;
2577 return FALSE;
2578}
2579
2580#ifdef FEAT_SPELL
2581/*
2582 * "zug" and "zuw": undo "zg" and "zw"
2583 * "zg": add good word to word list
2584 * "zw": add wrong word to word list
2585 * "zG": add good word to temp word list
2586 * "zW": add wrong word to temp word list
2587 */
2588 static int
2589nv_zg_zw(cmdarg_T *cap, int nchar)
2590{
2591 char_u *ptr = NULL;
2592 int len;
2593 int undo = FALSE;
2594
2595 if (nchar == 'u')
2596 {
2597 ++no_mapping;
2598 ++allow_keys; // no mapping for nchar, but allow key codes
2599 nchar = plain_vgetc();
2600 LANGMAP_ADJUST(nchar, TRUE);
2601 --no_mapping;
2602 --allow_keys;
2603#ifdef FEAT_CMDL_INFO
2604 (void)add_to_showcmd(nchar);
2605#endif
2606 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2607 {
2608 clearopbeep(cap->oap);
2609 return OK;
2610 }
2611 undo = TRUE;
2612 }
2613
2614 if (checkclearop(cap->oap))
2615 return OK;
2616 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2617 return FAIL;
2618 if (ptr == NULL)
2619 {
2620 pos_T pos = curwin->w_cursor;
2621
2622 // Find bad word under the cursor. When 'spell' is
2623 // off this fails and find_ident_under_cursor() is
2624 // used below.
2625 emsg_off++;
2626 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2627 emsg_off--;
2628 if (len != 0 && curwin->w_cursor.col <= pos.col)
2629 ptr = ml_get_pos(&curwin->w_cursor);
2630 curwin->w_cursor = pos;
2631 }
2632
2633 if (ptr == NULL
2634 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2635 return FAIL;
2636 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2637 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2638 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2639
2640 return OK;
2641}
2642#endif
2643
2644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * Commands that start with "z".
2646 */
2647 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002648nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 long n;
2651 colnr_T col;
2652 int nchar = cap->nchar;
2653#ifdef FEAT_FOLDING
2654 long old_fdl = curwin->w_p_fdl;
2655 int old_fen = curwin->w_p_fen;
2656#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002657 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002659 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (
2663#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2665 // and "zC" only in Visual mode. "zj" and "zk" are motion
2666 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 cap->nchar != 'f' && cap->nchar != 'F'
2668 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2669 && cap->nchar != 'j' && cap->nchar != 'k'
2670 &&
2671#endif
2672 checkclearop(cap->oap))
2673 return;
2674
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002675 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2676 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2678 && cap->count0
2679 && cap->count0 != curwin->w_cursor.lnum)
2680 {
2681 setpcmark();
2682 if (cap->count0 > curbuf->b_ml.ml_line_count)
2683 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2684 else
2685 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002686 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688
2689 switch (nchar)
2690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002691 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 case '+':
2693 if (cap->count0 == 0)
2694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 // No count given: put cursor at the line below screen
2696 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2698 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2699 else
2700 curwin->w_cursor.lnum = curwin->w_botline;
2701 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002702 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 case NL:
2704 case CAR:
2705 case K_KENTER:
2706 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002707 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002710 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002711 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 break;
2713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002719 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002720 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 break;
2722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // "z^", "z-" and "zb": put cursor at bottom of screen
2724 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2725 // when <count> is at bottom of window, and puts that one at
2726 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (cap->count0 != 0)
2728 {
2729 scroll_cursor_bot(0, TRUE);
2730 curwin->w_cursor.lnum = curwin->w_topline;
2731 }
2732 else if (curwin->w_topline == 1)
2733 curwin->w_cursor.lnum = 1;
2734 else
2735 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 case '-':
2738 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002742 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002743 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 break;
2745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002748 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 case 'h':
2753 case K_LEFT:
2754 if (!curwin->w_p_wrap)
2755 {
2756 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2757 curwin->w_leftcol = 0;
2758 else
2759 curwin->w_leftcol -= (colnr_T)cap->count1;
2760 leftcol_changed();
2761 }
2762 break;
2763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002765 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 case 'l':
2770 case K_RIGHT:
2771 if (!curwin->w_p_wrap)
2772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 curwin->w_leftcol += (colnr_T)cap->count1;
2775 leftcol_changed();
2776 }
2777 break;
2778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 case 's': if (!curwin->w_p_wrap)
2781 {
2782#ifdef FEAT_FOLDING
2783 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002784 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 else
2786#endif
2787 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002788 if ((long)col > siso)
2789 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 else
2791 col = 0;
2792 if (curwin->w_leftcol != col)
2793 {
2794 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002795 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 }
2798 break;
2799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 case 'e': if (!curwin->w_p_wrap)
2802 {
2803#ifdef FEAT_FOLDING
2804 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else
2807#endif
2808 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002809 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002810 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 col = 0;
2812 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002813 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (curwin->w_leftcol != col)
2815 {
2816 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002817 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 }
2820 break;
2821
Christian Brabandt2fa93842021-05-30 22:17:25 +02002822 // "zp", "zP" in block mode put without addind trailing spaces
2823 case 'P':
2824 case 'p': nv_put(cap);
2825 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002826 // "zy" Yank without trailing spaces
2827 case 'y': nv_operator(cap);
2828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 // "zF": create fold command
2831 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 case 'F':
2833 case 'f': if (foldManualAllowed(TRUE))
2834 {
2835 cap->nchar = 'f';
2836 nv_operator(cap);
2837 curwin->w_p_fen = TRUE;
2838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2841 {
2842 nv_operator(cap);
2843 finish_op = TRUE;
2844 }
2845 }
2846 else
2847 clearopbeep(cap->oap);
2848 break;
2849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // "zd": delete fold at cursor
2851 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case 'd':
2853 case 'D': if (foldManualAllowed(FALSE))
2854 {
2855 if (VIsual_active)
2856 nv_operator(cap);
2857 else
2858 deleteFold(curwin->w_cursor.lnum,
2859 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2860 }
2861 break;
2862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case 'E': if (foldmethodIsManual(curwin))
2865 {
2866 clearFolding(curwin);
2867 changed_window_setting();
2868 }
2869 else if (foldmethodIsMarker(curwin))
2870 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2871 TRUE, FALSE);
2872 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002873 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 break;
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case 'n': curwin->w_p_fen = FALSE;
2878 break;
2879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002880 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 case 'N': curwin->w_p_fen = TRUE;
2882 break;
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2890 openFold(curwin->w_cursor.lnum, cap->count1);
2891 else
2892 {
2893 closeFold(curwin->w_cursor.lnum, cap->count1);
2894 curwin->w_p_fen = TRUE;
2895 }
2896 break;
2897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2900 openFoldRecurse(curwin->w_cursor.lnum);
2901 else
2902 {
2903 closeFoldRecurse(curwin->w_cursor.lnum);
2904 curwin->w_p_fen = TRUE;
2905 }
2906 break;
2907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 case 'o': if (VIsual_active)
2910 nv_operator(cap);
2911 else
2912 openFold(curwin->w_cursor.lnum, cap->count1);
2913 break;
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 case 'O': if (VIsual_active)
2917 nv_operator(cap);
2918 else
2919 openFoldRecurse(curwin->w_cursor.lnum);
2920 break;
2921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 case 'c': if (VIsual_active)
2924 nv_operator(cap);
2925 else
2926 closeFold(curwin->w_cursor.lnum, cap->count1);
2927 curwin->w_p_fen = TRUE;
2928 break;
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 case 'C': if (VIsual_active)
2932 nv_operator(cap);
2933 else
2934 closeFoldRecurse(curwin->w_cursor.lnum);
2935 curwin->w_p_fen = TRUE;
2936 break;
2937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 case 'v': foldOpenCursor();
2940 break;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 curwin->w_foldinvalid = TRUE; // recompute folds
2945 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 foldOpenCursor();
2947 break;
2948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 curwin->w_foldinvalid = TRUE; // recompute folds
2952 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 break;
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002957 {
2958 curwin->w_p_fdl -= cap->count1;
2959 if (curwin->w_p_fdl < 0)
2960 curwin->w_p_fdl = 0;
2961 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 curwin->w_p_fen = TRUE;
2964 break;
2965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 curwin->w_p_fen = TRUE;
2970 break;
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002973 case 'r': curwin->w_p_fdl += cap->count1;
2974 {
2975 int d = getDeepestNesting();
2976
2977 if (curwin->w_p_fdl >= d)
2978 curwin->w_p_fdl = d;
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 break;
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 break;
2986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 case 'j': // "zj" move to next fold downwards
2988 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2990 cap->count1) == FAIL)
2991 clearopbeep(cap->oap);
2992 break;
2993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002996#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 case 'g': // "zg": add good word to word list
2999 case 'w': // "zw": add wrong word to word list
3000 case 'G': // "zG": add good word to temp word list
3001 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003002 if (nv_zg_zw(cap, nchar) == FAIL)
3003 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003004 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003007 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003008 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003009 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003010#endif
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 default: clearopbeep(cap->oap);
3013 }
3014
3015#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003016 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (old_fen != curwin->w_p_fen)
3018 {
3019# ifdef FEAT_DIFF
3020 win_T *wp;
3021
3022 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 FOR_ALL_WINDOWS(wp)
3026 {
3027 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3028 {
3029 wp->w_p_fen = curwin->w_p_fen;
3030 changed_window_setting_win(wp);
3031 }
3032 }
3033 }
3034# endif
3035 changed_window_setting();
3036 }
3037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003038 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (old_fdl != curwin->w_p_fdl)
3040 newFoldLevel();
3041#endif
3042}
3043
3044#ifdef FEAT_GUI
3045/*
3046 * Vertical scrollbar movement.
3047 */
3048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003049nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 if (cap->oap->op_type != OP_NOP)
3052 clearopbeep(cap->oap);
3053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 gui_do_scroll();
3056}
3057
3058/*
3059 * Horizontal scrollbar movement.
3060 */
3061 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003062nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 if (cap->oap->op_type != OP_NOP)
3065 clearopbeep(cap->oap);
3066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003068 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069}
3070#endif
3071
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003072#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073/*
3074 * Click in GUI tab.
3075 */
3076 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003077nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003078{
3079 if (cap->oap->op_type != OP_NOP)
3080 clearopbeep(cap->oap);
3081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003083 goto_tabpage(current_tab);
3084}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003085
3086/*
3087 * Selected item in tab line menu.
3088 */
3089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003090nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003091{
3092 if (cap->oap->op_type != OP_NOP)
3093 clearopbeep(cap->oap);
3094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003096 handle_tabmenu();
3097}
3098
3099/*
3100 * Handle selecting an item of the GUI tab line menu.
3101 * Used in Normal and Insert mode.
3102 */
3103 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003104handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003105{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003106 switch (current_tabmenu)
3107 {
3108 case TABLINE_MENU_CLOSE:
3109 if (current_tab == 0)
3110 do_cmdline_cmd((char_u *)"tabclose");
3111 else
3112 {
3113 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3114 current_tab);
3115 do_cmdline_cmd(IObuff);
3116 }
3117 break;
3118
3119 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003120 if (current_tab == 0)
3121 do_cmdline_cmd((char_u *)"$tabnew");
3122 else
3123 {
3124 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3125 current_tab - 1);
3126 do_cmdline_cmd(IObuff);
3127 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003128 break;
3129
3130 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003131 if (current_tab == 0)
3132 do_cmdline_cmd((char_u *)"browse $tabnew");
3133 else
3134 {
3135 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3136 current_tab - 1);
3137 do_cmdline_cmd(IObuff);
3138 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003139 break;
3140 }
3141}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003142#endif
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144/*
3145 * "Q" command.
3146 */
3147 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003148nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003150 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003152 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003153 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 do_exmode(FALSE);
3155}
3156
3157/*
3158 * Handle a ":" command.
3159 */
3160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003161nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003163 int old_p_im;
3164 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003165 int is_cmdkey = cap->cmdchar == K_COMMAND
3166 || cap->cmdchar == K_SCRIPT_COMMAND;
3167 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaar957cf672020-11-12 14:21:06 +01003169 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 nv_operator(cap);
3171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 if (cap->oap->op_type != OP_NOP)
3174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 cap->oap->motion_type = MCHAR;
3177 cap->oap->inclusive = FALSE;
3178 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003179 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 stuffcharReadbuff('.');
3183 if (cap->count0 > 1)
3184 {
3185 stuffReadbuff((char_u *)",.+");
3186 stuffnumReadbuff((long)cap->count0 - 1L);
3187 }
3188 }
3189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (KeyTyped)
3192 compute_cmdrow();
3193
3194 old_p_im = p_im;
3195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003196 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003197 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3198 if (is_cmdkey)
3199 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3200 else
3201 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (p_im != old_p_im)
3205 {
3206 if (p_im)
3207 restart_edit = 'i';
3208 else
3209 restart_edit = 0;
3210 }
3211
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003212 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003214 clearop(cap->oap);
3215 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3217 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003218 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3219 || did_emsg
3220 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 clearopbeep(cap->oap);
3223 }
3224}
3225
3226/*
3227 * Handle CTRL-G command.
3228 */
3229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003230nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003235 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 showmode();
3237 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003238 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003239 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 fileinfo((int)cap->count0, FALSE, TRUE);
3241}
3242
3243/*
3244 * Handle CTRL-H <Backspace> command.
3245 */
3246 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003247nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (VIsual_active && VIsual_select)
3250 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003251 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 v_visop(cap);
3253 }
3254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 nv_left(cap);
3256}
3257
3258/*
3259 * CTRL-L: clear screen and redraw.
3260 */
3261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003262nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 if (!checkclearop(cap->oap))
3265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003268 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003269# ifdef FEAT_RELTIME
3270 {
3271 win_T *wp;
3272
3273 FOR_ALL_WINDOWS(wp)
3274 wp->w_s->b_syn_slow = FALSE;
3275 }
3276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003278 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003279#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3280# ifdef VIMDLL
3281 if (!gui.in_use)
3282# endif
3283 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286}
3287
3288/*
3289 * CTRL-O: In Select mode: switch to Visual mode for one command.
3290 * Otherwise: Go to older pcmark.
3291 */
3292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003293nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (VIsual_active && VIsual_select)
3296 {
3297 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003298 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003300 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 cap->count1 = -cap->count1;
3305 nv_pcmark(cap);
3306 }
3307}
3308
3309/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003310 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3311 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 */
3313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 if (!checkclearopq(cap->oap))
3317 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3318 GETF_SETMARK|GETF_ALT, FALSE);
3319}
3320
3321/*
3322 * "Z" commands.
3323 */
3324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003325nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 if (!checkclearopq(cap->oap))
3328 {
3329 switch (cap->nchar)
3330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003331 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 case 'Z': do_cmdline_cmd((char_u *)"x");
3333 break;
3334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003335 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 case 'Q': do_cmdline_cmd((char_u *)"q!");
3337 break;
3338
3339 default: clearopbeep(cap->oap);
3340 }
3341 }
3342}
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3346 */
3347 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003348do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 oparg_T oa;
3351 cmdarg_T ca;
3352
3353 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003354 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 ca.oap = &oa;
3356 ca.cmdchar = c1;
3357 ca.nchar = c2;
3358 nv_ident(&ca);
3359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003362 * 'K' normal-mode command. Get the command to lookup the keyword under the
3363 * cursor.
3364 */
3365 static int
3366nv_K_getcmd(
3367 cmdarg_T *cap,
3368 char_u *kp,
3369 int kp_help,
3370 int kp_ex,
3371 char_u **ptr_arg,
3372 int n,
3373 char_u *buf,
3374 unsigned buflen)
3375{
3376 char_u *ptr = *ptr_arg;
3377 int isman;
3378 int isman_s;
3379
3380 if (kp_help)
3381 {
3382 // in the help buffer
3383 STRCPY(buf, "he! ");
3384 return n;
3385 }
3386
3387 if (kp_ex)
3388 {
3389 // 'keywordprog' is an ex command
3390 if (cap->count0 != 0)
3391 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3392 else
3393 STRCPY(buf, kp);
3394 STRCAT(buf, " ");
3395 return n;
3396 }
3397
3398 // An external command will probably use an argument starting
3399 // with "-" as an option. To avoid trouble we skip the "-".
3400 while (*ptr == '-' && n > 0)
3401 {
3402 ++ptr;
3403 --n;
3404 }
3405 if (n == 0)
3406 {
3407 // found dashes only
3408 emsg(_(e_no_identifier_under_cursor));
3409 vim_free(buf);
3410 *ptr_arg = ptr;
3411 return 0;
3412 }
3413
3414 // When a count is given, turn it into a range. Is this
3415 // really what we want?
3416 isman = (STRCMP(kp, "man") == 0);
3417 isman_s = (STRCMP(kp, "man -s") == 0);
3418 if (cap->count0 != 0 && !(isman || isman_s))
3419 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3420
3421 STRCAT(buf, "! ");
3422 if (cap->count0 == 0 && isman_s)
3423 STRCAT(buf, "man");
3424 else
3425 STRCAT(buf, kp);
3426 STRCAT(buf, " ");
3427 if (cap->count0 != 0 && (isman || isman_s))
3428 {
3429 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3430 STRCAT(buf, " ");
3431 }
3432
3433 *ptr_arg = ptr;
3434 return n;
3435}
3436
3437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 * Handle the commands that use the word under the cursor.
3439 * [g] CTRL-] :ta to current identifier
3440 * [g] 'K' run program for current identifier
3441 * [g] '*' / to current identifier or string
3442 * [g] '#' ? to current identifier or string
3443 * g ']' :tselect for current identifier
3444 */
3445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003446nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
3448 char_u *ptr = NULL;
3449 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003450 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003451 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003453 char_u *kp; // value of 'keywordprg'
3454 int kp_help; // 'keywordprg' is ":he"
3455 int kp_ex; // 'keywordprg' starts with ":"
3456 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003459 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 cmdchar = cap->nchar;
3465 g_cmd = TRUE;
3466 }
3467 else
3468 {
3469 cmdchar = cap->cmdchar;
3470 g_cmd = FALSE;
3471 }
3472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003473 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 cmdchar = '#';
3475
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003476 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3480 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (checkclearopq(cap->oap))
3482 return;
3483 }
3484
3485 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3486 (cmdchar == '*' || cmdchar == '#')
3487 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3488 {
3489 clearop(cap->oap);
3490 return;
3491 }
3492
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003493 // Allocate buffer to put the command in. Inserting backslashes can
3494 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3495 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3497 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3498 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003499 if (kp_help && *skipwhite(ptr) == NUL)
3500 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003501 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003502 return;
3503 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003504 kp_ex = (*kp == ':');
3505 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3506 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (buf == NULL)
3508 return;
3509 buf[0] = NUL;
3510
3511 switch (cmdchar)
3512 {
3513 case '*':
3514 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003515 // Put cursor at start of word, makes search skip the word
3516 // under the cursor.
3517 // Call setpcmark() first, so "*``" puts the cursor back where
3518 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 setpcmark();
3520 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3521
3522 if (!g_cmd && vim_iswordp(ptr))
3523 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003524 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 break;
3526
3527 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003528 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3529 if (n == 0)
3530 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 break;
3532
3533 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003534 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#ifdef FEAT_CSCOPE
3536 if (p_cst)
3537 STRCPY(buf, "cstag ");
3538 else
3539#endif
3540 STRCPY(buf, "ts ");
3541 break;
3542
3543 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003544 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (curbuf->b_help)
3546 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003548 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003549 if (g_cmd)
3550 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003551 else if (cap->count0 == 0)
3552 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003553 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003554 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003558 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003559 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003561 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003562 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003563 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003564 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003565 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003567 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003568 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003569 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 vim_free(buf);
3572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003574 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003575 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003576 {
3577 vim_free(buf);
3578 vim_free(p);
3579 return;
3580 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003581 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003582 STRCAT(buf, p);
3583 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003585 else
3586 {
3587 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003588 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003590 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003591 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003592 {
3593 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003595 aux_ptr = (char_u *)"";
3596 else
3597 aux_ptr = (char_u *)"\\|\"\n[";
3598 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003599 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 aux_ptr = (char_u *)"\\|\"\n*?[";
3601
3602 p = buf + STRLEN(buf);
3603 while (n-- > 0)
3604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003605 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003606 if (vim_strchr(aux_ptr, *ptr) != NULL)
3607 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003608 // When current byte is a part of multibyte character, copy all
3609 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003610 if (has_mbyte)
3611 {
3612 int i;
3613 int len = (*mb_ptr2len)(ptr) - 1;
3614
3615 for (i = 0; i < len && n >= 1; ++i, --n)
3616 *p++ = *ptr++;
3617 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003618 *p++ = *ptr++;
3619 }
3620 *p = NUL;
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003623 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (cmdchar == '*' || cmdchar == '#')
3625 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003626 if (!g_cmd && (has_mbyte
3627 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3628 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003630
3631 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003632 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003634
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003635 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003638 {
3639 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003641 g_tag_at_cursor = FALSE;
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 vim_free(buf);
3645}
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647/*
3648 * Get visually selected text, within one line only.
3649 * Returns FAIL if more than one line selected.
3650 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003651 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003652get_visual_text(
3653 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003654 char_u **pp, // return: start of selected text
3655 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 if (VIsual_mode != 'V')
3658 unadjust_for_sel();
3659 if (VIsual.lnum != curwin->w_cursor.lnum)
3660 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003661 if (cap != NULL)
3662 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return FAIL;
3664 }
3665 if (VIsual_mode == 'V')
3666 {
3667 *pp = ml_get_curline();
3668 *lenp = (int)STRLEN(*pp);
3669 }
3670 else
3671 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003672 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 *pp = ml_get_pos(&curwin->w_cursor);
3675 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3676 }
3677 else
3678 {
3679 *pp = ml_get_pos(&VIsual);
3680 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3681 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003682 if (**pp == NUL)
3683 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003684 if (*lenp > 0)
3685 {
3686 if (has_mbyte)
3687 // Correct the length to include all bytes of the last
3688 // character.
3689 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3690 else if ((*pp)[*lenp - 1] == NUL)
3691 // Do not include a trailing NUL.
3692 *lenp -= 1;
3693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695 reset_VIsual_and_resel();
3696 return OK;
3697}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
3699/*
3700 * CTRL-T: backwards in tag stack
3701 */
3702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003703nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 if (!checkclearopq(cap->oap))
3706 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3707}
3708
3709/*
3710 * Handle scrolling command 'H', 'L' and 'M'.
3711 */
3712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003713nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715 int used = 0;
3716 long n;
3717#ifdef FEAT_FOLDING
3718 linenr_T lnum;
3719#endif
3720 int half;
3721
3722 cap->oap->motion_type = MLINE;
3723 setpcmark();
3724
3725 if (cap->cmdchar == 'L')
3726 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003727 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 curwin->w_cursor.lnum = curwin->w_botline - 1;
3729 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3730 curwin->w_cursor.lnum = 1;
3731 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003732 {
3733#ifdef FEAT_FOLDING
3734 if (hasAnyFolding(curwin))
3735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003736 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003737 for (n = cap->count1 - 1; n > 0
3738 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3739 {
3740 (void)hasFolding(curwin->w_cursor.lnum,
3741 &curwin->w_cursor.lnum, NULL);
3742 --curwin->w_cursor.lnum;
3743 }
3744 }
3745 else
3746#endif
3747 curwin->w_cursor.lnum -= cap->count1 - 1;
3748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750 else
3751 {
3752 if (cap->cmdchar == 'M')
3753 {
3754#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003755 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 used -= diff_check_fill(curwin, curwin->w_topline)
3757 - curwin->w_topfill;
3758#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3761 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3762 {
3763#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003764 // Count half he number of filler lines to be "below this
3765 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3767 + n) / 2 >= half)
3768 {
3769 --n;
3770 break;
3771 }
3772#endif
3773 used += plines(curwin->w_topline + n);
3774 if (used >= half)
3775 break;
3776#ifdef FEAT_FOLDING
3777 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3778 n = lnum - curwin->w_topline;
3779#endif
3780 }
3781 if (n > 0 && used > curwin->w_height)
3782 --n;
3783 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003784 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003787#ifdef FEAT_FOLDING
3788 if (hasAnyFolding(curwin))
3789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003790 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003791 lnum = curwin->w_topline;
3792 while (n-- > 0 && lnum < curwin->w_botline - 1)
3793 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003794 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003795 ++lnum;
3796 }
3797 n = lnum - curwin->w_topline;
3798 }
3799#endif
3800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 curwin->w_cursor.lnum = curwin->w_topline + n;
3802 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3803 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3804 }
3805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003806 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003807 if (cap->oap->op_type == OP_NOP)
3808 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 beginline(BL_SOL | BL_FIX);
3810}
3811
3812/*
3813 * Cursor right commands.
3814 */
3815 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003816nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003819 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003821 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003823 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003824 if (mod_mask & MOD_MASK_CTRL)
3825 cap->arg = TRUE;
3826 nv_wordcmd(cap);
3827 return;
3828 }
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 cap->oap->motion_type = MCHAR;
3831 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003832 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003834 // In virtual edit mode, there's no such thing as "past_line", as lines
3835 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003837 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 for (n = cap->count1; n > 0; --n)
3840 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003841 if ((!past_line && oneright() == FAIL)
3842 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003843 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003845 // <Space> wraps to next line if 'whichwrap' has 's'.
3846 // 'l' wraps to next line if 'whichwrap' has 'l'.
3847 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if ( ((cap->cmdchar == ' '
3849 && vim_strchr(p_ww, 's') != NULL)
3850 || (cap->cmdchar == 'l'
3851 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003852 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 && vim_strchr(p_ww, '>') != NULL))
3854 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003856 // When deleting we also count the NL as a character.
3857 // Set cap->oap->inclusive when last char in the line is
3858 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003859 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003861 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 cap->oap->inclusive = TRUE;
3863 else
3864 {
3865 ++curwin->w_cursor.lnum;
3866 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 curwin->w_set_curswant = TRUE;
3869 cap->oap->inclusive = FALSE;
3870 }
3871 continue;
3872 }
3873 if (cap->oap->op_type == OP_NOP)
3874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003875 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (n == cap->count1)
3877 beep_flush();
3878 }
3879 else
3880 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003881 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 cap->oap->inclusive = TRUE;
3883 }
3884 break;
3885 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003886 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (virtual_active())
3890 oneright();
3891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003894 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 ++curwin->w_cursor.col;
3897 }
3898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900#ifdef FEAT_FOLDING
3901 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3902 && cap->oap->op_type == OP_NOP)
3903 foldOpenCursor();
3904#endif
3905}
3906
3907/*
3908 * Cursor left commands.
3909 *
3910 * Returns TRUE when operator end should not be adjusted.
3911 */
3912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003913nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915 long n;
3916
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003917 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003919 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003920 if (mod_mask & MOD_MASK_CTRL)
3921 cap->arg = 1;
3922 nv_bck_word(cap);
3923 return;
3924 }
3925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 cap->oap->motion_type = MCHAR;
3927 cap->oap->inclusive = FALSE;
3928 for (n = cap->count1; n > 0; --n)
3929 {
3930 if (oneleft() == FAIL)
3931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003932 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3933 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3934 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if ( (((cap->cmdchar == K_BS
3936 || cap->cmdchar == Ctrl_H)
3937 && vim_strchr(p_ww, 'b') != NULL)
3938 || (cap->cmdchar == 'h'
3939 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003940 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 && vim_strchr(p_ww, '<') != NULL))
3942 && curwin->w_cursor.lnum > 1)
3943 {
3944 --(curwin->w_cursor.lnum);
3945 coladvance((colnr_T)MAXCOL);
3946 curwin->w_set_curswant = TRUE;
3947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003948 // When the NL before the first char has to be deleted we
3949 // put the cursor on the NUL after the previous line.
3950 // This is a very special case, be careful!
3951 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if ( (cap->oap->op_type == OP_DELETE
3953 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003954 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003956 char_u *cp = ml_get_cursor();
3957
3958 if (*cp != NUL)
3959 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003960 if (has_mbyte)
3961 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3962 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003963 ++curwin->w_cursor.col;
3964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 cap->retval |= CA_NO_ADJ_OP_END;
3966 }
3967 continue;
3968 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003969 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3971 beep_flush();
3972 break;
3973 }
3974 }
3975#ifdef FEAT_FOLDING
3976 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3977 && cap->oap->op_type == OP_NOP)
3978 foldOpenCursor();
3979#endif
3980}
3981
3982/*
3983 * Cursor up commands.
3984 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3985 */
3986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003987nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003989 if (mod_mask & MOD_MASK_SHIFT)
3990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003991 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003992 cap->arg = BACKWARD;
3993 nv_page(cap);
3994 }
3995 else
3996 {
3997 cap->oap->motion_type = MLINE;
3998 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3999 clearopbeep(cap->oap);
4000 else if (cap->arg)
4001 beginline(BL_WHITE | BL_FIX);
4002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003}
4004
4005/*
4006 * Cursor down commands.
4007 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4008 */
4009 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004010nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004012 if (mod_mask & MOD_MASK_SHIFT)
4013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004015 cap->arg = FORWARD;
4016 nv_page(cap);
4017 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004018#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004020 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4021 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004026 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004027 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 cmdwin_result = CAR;
4029 else
4030#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004031#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004033 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4034 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4035 {
4036 invoke_prompt_callback();
4037 if (restart_edit == 0)
4038 restart_edit = 'a';
4039 }
4040 else
4041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 cap->oap->motion_type = MLINE;
4044 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4045 clearopbeep(cap->oap);
4046 else if (cap->arg)
4047 beginline(BL_WHITE | BL_FIX);
4048 }
4049 }
4050}
4051
4052#ifdef FEAT_SEARCHPATH
4053/*
4054 * Grab the file name under the cursor and edit it.
4055 */
4056 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004057nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004060 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004062 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064 if (curbuf_locked())
4065 {
4066 clearop(cap->oap);
4067 return;
4068 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004069#ifdef FEAT_PROP_POPUP
4070 if (ERROR_IF_TERM_POPUP_WINDOW)
4071 return;
4072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004074 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 if (ptr != NULL)
4077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004078 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004079 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004080 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004082 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004083 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004084 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004085 {
4086 curwin->w_cursor.lnum = lnum;
4087 check_cursor_lnum();
4088 beginline(BL_SOL | BL_FIX);
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 vim_free(ptr);
4091 }
4092 else
4093 clearop(cap->oap);
4094}
4095#endif
4096
4097/*
4098 * <End> command: to end of current line or last line.
4099 */
4100 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004101nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004103 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004105 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004107 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 nv_dollar(cap);
4110}
4111
4112/*
4113 * Handle the "$" command.
4114 */
4115 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004116nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 cap->oap->motion_type = MCHAR;
4119 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004120 // In virtual mode when off the edge of a line and an operator
4121 // is pending (whew!) keep the cursor where it is.
4122 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (!virtual_active() || gchar_cursor() != NUL
4124 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004125 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (cursor_down((long)(cap->count1 - 1),
4127 cap->oap->op_type == OP_NOP) == FAIL)
4128 clearopbeep(cap->oap);
4129#ifdef FEAT_FOLDING
4130 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4131 foldOpenCursor();
4132#endif
4133}
4134
4135/*
4136 * Implementation of '?' and '/' commands.
4137 * If cap->arg is TRUE don't set PC mark.
4138 */
4139 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004140nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004143 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4146 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004147 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 cap->cmdchar = 'g';
4149 cap->nchar = '?';
4150 nv_operator(cap);
4151 return;
4152 }
4153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004154 // When using 'incsearch' the cursor may be moved to set a different search
4155 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004156 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 if (cap->searchbuf == NULL)
4159 {
4160 clearop(oap);
4161 return;
4162 }
4163
Bram Moolenaar46539112015-02-17 15:43:57 +01004164 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004165 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004166 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167}
4168
LemonBoya4399382022-04-09 21:04:08 +01004169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170/*
4171 * Handle "N" and "n" commands.
4172 * cap->arg is SEARCH_REV for "N", 0 for "n".
4173 */
4174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004175nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004177 pos_T old = curwin->w_cursor;
4178 int wrapped = FALSE;
4179 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004180
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004181 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004182 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004183 // Avoid getting stuck on the current cursor position, which can
4184 // happen when an offset is given and the cursor is on the last char
4185 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004186 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004187 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004188 cap->count1 -= 1;
4189 }
LemonBoya4399382022-04-09 21:04:08 +01004190
4191#ifdef FEAT_SEARCH_EXTRA
4192 // Redraw the window to refresh the highlighted matches.
4193 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004194 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196}
4197
4198/*
4199 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4200 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004201 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004203 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004204normal_search(
4205 cmdarg_T *cap,
4206 int dir,
4207 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004208 int opt, // extra flags for do_search()
4209 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004212 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004213#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004214 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 cap->oap->motion_type = MCHAR;
4218 cap->oap->inclusive = FALSE;
4219 cap->oap->use_reg_one = TRUE;
4220 curwin->w_set_curswant = TRUE;
4221
Bram Moolenaara80faa82020-04-12 19:37:17 +02004222 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004223 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004224 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4225 if (wrapped != NULL)
4226 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (i == 0)
4228 clearop(cap->oap);
4229 else
4230 {
4231 if (i == 2)
4232 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234#ifdef FEAT_FOLDING
4235 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4236 foldOpenCursor();
4237#endif
4238 }
LemonBoya4399382022-04-09 21:04:08 +01004239#ifdef FEAT_SEARCH_EXTRA
4240 // Redraw the window to refresh the highlighted matches.
4241 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004242 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004245 // "/$" will put the cursor after the end of the line, may need to
4246 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004248 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249}
4250
4251/*
4252 * Character search commands.
4253 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4254 * ',' and FALSE for ';'.
4255 * cap->nchar is NUL for ',' and ';' (repeat the search)
4256 */
4257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004258nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 int t_cmd;
4261
4262 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4263 t_cmd = TRUE;
4264 else
4265 t_cmd = FALSE;
4266
4267 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4269 clearopbeep(cap->oap);
4270 else
4271 {
4272 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004273 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4275 && (t_cmd || cap->oap->op_type != OP_NOP))
4276 {
4277 colnr_T scol, ecol;
4278
4279 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4280 curwin->w_cursor.coladd = ecol - scol;
4281 }
4282 else
4283 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#ifdef FEAT_FOLDING
4286 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4287 foldOpenCursor();
4288#endif
4289 }
4290}
4291
4292/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004293 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4294 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4295 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4296 * "[m" or "]m" search for prev/next start of (Java) method.
4297 * "[M" or "]M" search for prev/next end of (Java) method.
4298 */
4299 static void
4300nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4301{
4302 pos_T new_pos = {0, 0, 0};
4303 pos_T *pos = NULL; // init for GCC
4304 pos_T prev_pos;
4305 long n;
4306 int findc;
4307 int c;
4308
4309 if (cap->nchar == '*')
4310 cap->nchar = '/';
4311 prev_pos.lnum = 0;
4312 if (cap->nchar == 'm' || cap->nchar == 'M')
4313 {
4314 if (cap->cmdchar == '[')
4315 findc = '{';
4316 else
4317 findc = '}';
4318 n = 9999;
4319 }
4320 else
4321 {
4322 findc = cap->nchar;
4323 n = cap->count1;
4324 }
4325 for ( ; n > 0; --n)
4326 {
4327 if ((pos = findmatchlimit(cap->oap, findc,
4328 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4329 {
4330 if (new_pos.lnum == 0) // nothing found
4331 {
4332 if (cap->nchar != 'm' && cap->nchar != 'M')
4333 clearopbeep(cap->oap);
4334 }
4335 else
4336 pos = &new_pos; // use last one found
4337 break;
4338 }
4339 prev_pos = new_pos;
4340 curwin->w_cursor = *pos;
4341 new_pos = *pos;
4342 }
4343 curwin->w_cursor = *old_pos;
4344
4345 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4346 // brought us to the match for "[m" and "]M" when inside a method.
4347 // Try finding the '{' or '}' we want to be at.
4348 // Also repeat for the given count.
4349 if (cap->nchar == 'm' || cap->nchar == 'M')
4350 {
4351 // norm is TRUE for "]M" and "[m"
4352 int norm = ((findc == '{') == (cap->nchar == 'm'));
4353
4354 n = cap->count1;
4355 // found a match: we were inside a method
4356 if (prev_pos.lnum != 0)
4357 {
4358 pos = &prev_pos;
4359 curwin->w_cursor = prev_pos;
4360 if (norm)
4361 --n;
4362 }
4363 else
4364 pos = NULL;
4365 while (n > 0)
4366 {
4367 for (;;)
4368 {
4369 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4370 {
4371 // if not found anything, that's an error
4372 if (pos == NULL)
4373 clearopbeep(cap->oap);
4374 n = 0;
4375 break;
4376 }
4377 c = gchar_cursor();
4378 if (c == '{' || c == '}')
4379 {
4380 // Must have found end/start of class: use it.
4381 // Or found the place to be at.
4382 if ((c == findc && norm) || (n == 1 && !norm))
4383 {
4384 new_pos = curwin->w_cursor;
4385 pos = &new_pos;
4386 n = 0;
4387 }
4388 // if no match found at all, we started outside of the
4389 // class and we're inside now. Just go on.
4390 else if (new_pos.lnum == 0)
4391 {
4392 new_pos = curwin->w_cursor;
4393 pos = &new_pos;
4394 }
4395 // found start/end of other method: go to match
4396 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004397 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4398 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004399 n = 0;
4400 else
4401 curwin->w_cursor = *pos;
4402 break;
4403 }
4404 }
4405 --n;
4406 }
4407 curwin->w_cursor = *old_pos;
4408 if (pos == NULL && new_pos.lnum != 0)
4409 clearopbeep(cap->oap);
4410 }
4411 if (pos != NULL)
4412 {
4413 setpcmark();
4414 curwin->w_cursor = *pos;
4415 curwin->w_set_curswant = TRUE;
4416#ifdef FEAT_FOLDING
4417 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4418 && cap->oap->op_type == OP_NOP)
4419 foldOpenCursor();
4420#endif
4421 }
4422}
4423
4424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 * "[" and "]" commands.
4426 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4427 */
4428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004429nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004432 pos_T *pos = NULL; // init for GCC
4433 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 int flag;
4435 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 cap->oap->motion_type = MCHAR;
4438 cap->oap->inclusive = FALSE;
4439 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004440 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004443 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if (cap->nchar == 'f')
4445 nv_gotofile(cap);
4446 else
4447#endif
4448
4449#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004450 // Find the occurrence(s) of the identifier or define under cursor
4451 // in current and included files or jump to the first occurrence.
4452 //
4453 // search list jump
4454 // fwd bwd fwd bwd fwd bwd
4455 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4456 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004457 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 char_u *ptr;
4460 int len;
4461
4462 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4463 clearop(cap->oap);
4464 else
4465 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004466 // Make a copy, if the line was changed it will be freed.
4467 ptr = vim_strnsave(ptr, len);
4468 if (ptr == NULL)
4469 return;
4470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 find_pattern_in_path(ptr, 0, len, TRUE,
4472 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4473 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4474 cap->count1,
4475 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4476 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4477 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4478 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004479 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 curwin->w_set_curswant = TRUE;
4481 }
4482 }
4483 else
4484#endif
4485
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004486 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4487 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4488 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4489 // "[m" or "]m" search for prev/next start of (Java) method.
4490 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if ( (cap->cmdchar == '['
4492 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4493 || (cap->cmdchar == ']'
4494 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004495 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004497 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else if (cap->nchar == '[' || cap->nchar == ']')
4499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004500 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 flag = '{';
4502 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004503 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004506 // Imitate strange Vi behaviour: When using "]]" with an operator
4507 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004508 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 (cap->oap->op_type != OP_NOP
4510 && cap->arg == FORWARD && flag == '{')))
4511 clearopbeep(cap->oap);
4512 else
4513 {
4514 if (cap->oap->op_type == OP_NOP)
4515 beginline(BL_WHITE | BL_FIX);
4516#ifdef FEAT_FOLDING
4517 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4518 foldOpenCursor();
4519#endif
4520 }
4521 }
4522
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004523 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else if (cap->nchar == 'p' || cap->nchar == 'P')
4525 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004526 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004529 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else if (cap->nchar == '\'' || cap->nchar == '`')
4531 {
4532 pos = &curwin->w_cursor;
4533 for (n = cap->count1; n > 0; --n)
4534 {
4535 prev_pos = *pos;
4536 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4537 cap->nchar == '\'');
4538 if (pos == NULL)
4539 break;
4540 }
4541 if (pos == NULL)
4542 pos = &prev_pos;
4543 nv_cursormark(cap, cap->nchar == '\'', pos);
4544 }
4545
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004546 // [ or ] followed by a middle mouse click: put selected text with
4547 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004548 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
4550 (void)do_mouse(cap->oap, cap->nchar,
4551 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4552 cap->count1, PUT_FIXINDENT);
4553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004556 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else if (cap->nchar == 'z')
4558 {
4559 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4560 cap->count1) == FAIL)
4561 clearopbeep(cap->oap);
4562 }
4563#endif
4564
4565#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004566 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else if (cap->nchar == 'c')
4568 {
4569 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4570 cap->count1) == FAIL)
4571 clearopbeep(cap->oap);
4572 }
4573#endif
4574
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004575#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004576 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 else if (cap->nchar == 's' || cap->nchar == 'S')
4578 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004579 setpcmark();
4580 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004581 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4582 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004583 {
4584 clearopbeep(cap->oap);
4585 break;
4586 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004587 else
4588 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004589# ifdef FEAT_FOLDING
4590 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4591 foldOpenCursor();
4592# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593 }
4594#endif
4595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004596 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 else
4598 clearopbeep(cap->oap);
4599}
4600
4601/*
4602 * Handle Normal mode "%" command.
4603 */
4604 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004605nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004608#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 linenr_T lnum = curwin->w_cursor.lnum;
4610#endif
4611
4612 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004613 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 if (cap->count0 > 100)
4616 clearopbeep(cap->oap);
4617 else
4618 {
4619 cap->oap->motion_type = MLINE;
4620 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004621 // Round up, so 'normal 100%' always jumps at the line line.
4622 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4623 // overflow on 32-bits, so use a formula with less accuracy
4624 // to avoid overflows.
4625 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4627 / 100L * cap->count0;
4628 else
4629 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4630 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004631 if (curwin->w_cursor.lnum < 1)
4632 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4634 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4635 beginline(BL_SOL | BL_FIX);
4636 }
4637 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 cap->oap->motion_type = MCHAR;
4641 cap->oap->use_reg_one = TRUE;
4642 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4643 clearopbeep(cap->oap);
4644 else
4645 {
4646 setpcmark();
4647 curwin->w_cursor = *pos;
4648 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 }
4653#ifdef FEAT_FOLDING
4654 if (cap->oap->op_type == OP_NOP
4655 && lnum != curwin->w_cursor.lnum
4656 && (fdo_flags & FDO_PERCENT)
4657 && KeyTyped)
4658 foldOpenCursor();
4659#endif
4660}
4661
4662/*
4663 * Handle "(" and ")" commands.
4664 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4665 */
4666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004667nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
4669 cap->oap->motion_type = MCHAR;
4670 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004671 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004672 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 curwin->w_set_curswant = TRUE;
4674
4675 if (findsent(cap->arg, cap->count1) == FAIL)
4676 clearopbeep(cap->oap);
4677 else
4678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004680 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#ifdef FEAT_FOLDING
4683 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4684 foldOpenCursor();
4685#endif
4686 }
4687}
4688
4689/*
4690 * "m" command: Mark a position.
4691 */
4692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004693nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 if (!checkclearop(cap->oap))
4696 {
4697 if (setmark(cap->nchar) == FAIL)
4698 clearopbeep(cap->oap);
4699 }
4700}
4701
4702/*
4703 * "{" and "}" commands.
4704 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4705 */
4706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004707nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 cap->oap->motion_type = MCHAR;
4710 cap->oap->inclusive = FALSE;
4711 cap->oap->use_reg_one = TRUE;
4712 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004713 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 clearopbeep(cap->oap);
4715 else
4716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718#ifdef FEAT_FOLDING
4719 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4720 foldOpenCursor();
4721#endif
4722 }
4723}
4724
4725/*
4726 * "u" command: Undo or make lower case.
4727 */
4728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004729nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004731 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004733 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 cap->cmdchar = 'g';
4735 cap->nchar = 'u';
4736 nv_operator(cap);
4737 }
4738 else
4739 nv_kundo(cap);
4740}
4741
4742/*
4743 * <Undo> command.
4744 */
4745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 if (!checkclearopq(cap->oap))
4749 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004750#ifdef FEAT_JOB_CHANNEL
4751 if (bt_prompt(curbuf))
4752 {
4753 clearopbeep(cap->oap);
4754 return;
4755 }
4756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 u_undo((int)cap->count1);
4758 curwin->w_set_curswant = TRUE;
4759 }
4760}
4761
4762/*
4763 * Handle the "r" command.
4764 */
4765 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004766nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
4768 char_u *ptr;
4769 int had_ctrl_v;
4770 long n;
4771
4772 if (checkclearop(cap->oap))
4773 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004774#ifdef FEAT_JOB_CHANNEL
4775 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4776 {
4777 clearopbeep(cap->oap);
4778 return;
4779 }
4780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (cap->nchar == Ctrl_V)
4784 {
4785 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004786 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (cap->nchar > DEL)
4789 had_ctrl_v = NUL;
4790 }
4791 else
4792 had_ctrl_v = NUL;
4793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004795 if (IS_SPECIAL(cap->nchar))
4796 {
4797 clearopbeep(cap->oap);
4798 return;
4799 }
4800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (VIsual_active)
4803 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004804 if (got_int)
4805 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004806 if (had_ctrl_v)
4807 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004808 // Use a special (negative) number to make a difference between a
4809 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004810 if (cap->nchar == CAR)
4811 cap->nchar = REPLACE_CR_NCHAR;
4812 else if (cap->nchar == NL)
4813 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 nv_operator(cap);
4816 return;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004819 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (virtual_active())
4821 {
4822 if (u_save_cursor() == FAIL)
4823 return;
4824 if (gchar_cursor() == NUL)
4825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004826 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 coladvance_force((colnr_T)(getviscol() + cap->count1));
4828 curwin->w_cursor.col -= cap->count1;
4829 }
4830 else if (gchar_cursor() == TAB)
4831 coladvance_force(getviscol());
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004834 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004836 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004837 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 clearopbeep(cap->oap);
4840 return;
4841 }
4842
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004843 // Replacing with a TAB is done by edit() when it is complicated because
4844 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4845 // Other characters are done below to avoid problems with things like
4846 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4848 {
4849 stuffnumReadbuff(cap->count1);
4850 stuffcharReadbuff('R');
4851 stuffcharReadbuff('\t');
4852 stuffcharReadbuff(ESC);
4853 return;
4854 }
4855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004856 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (u_save_cursor() == FAIL)
4858 return;
4859
4860 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4861 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004862 // Replace character(s) by a single newline.
4863 // Strange vi behaviour: Only one newline is inserted.
4864 // Delete the characters here.
4865 // Insert the newline with an insert command, takes care of
4866 // autoindent. The insert command depends on being on the last
4867 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 stuffcharReadbuff('\r');
4870 stuffcharReadbuff(ESC);
4871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 invoke_edit(cap, TRUE, 'r', FALSE);
4874 }
4875 else
4876 {
4877 prep_redo(cap->oap->regname, cap->count1,
4878 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4879
4880 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (has_mbyte)
4882 {
4883 int old_State = State;
4884
4885 if (cap->ncharC1 != 0)
4886 AppendCharToRedobuff(cap->ncharC1);
4887 if (cap->ncharC2 != 0)
4888 AppendCharToRedobuff(cap->ncharC2);
4889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004890 // This is slow, but it handles replacing a single-byte with a
4891 // multi-byte and the other way around. Also handles adding
4892 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 for (n = cap->count1; n > 0; --n)
4894 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004895 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004896 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4897 {
4898 int c = ins_copychar(curwin->w_cursor.lnum
4899 + (cap->nchar == Ctrl_Y ? -1 : 1));
4900 if (c != NUL)
4901 ins_char(c);
4902 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004903 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004904 ++curwin->w_cursor.col;
4905 }
4906 else
4907 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 State = old_State;
4909 if (cap->ncharC1 != 0)
4910 ins_char(cap->ncharC1);
4911 if (cap->ncharC2 != 0)
4912 ins_char(cap->ncharC2);
4913 }
4914 }
4915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004917 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 for (n = cap->count1; n > 0; --n)
4919 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004920 // Get ptr again, because u_save and/or showmatch() will have
4921 // released the line. This may also happen in ins_copychar().
4922 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004923 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4924 {
4925 int c = ins_copychar(curwin->w_cursor.lnum
4926 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004927
4928 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004929 if (c != NUL)
4930 ptr[curwin->w_cursor.col] = c;
4931 }
4932 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004933 {
4934 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004935 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (p_sm && msg_silent == 0)
4938 showmatch(cap->nchar);
4939 ++curwin->w_cursor.col;
4940 }
4941#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004942 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004944 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar009b2592004-10-24 19:18:58 +00004946 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004947 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004949 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951#endif
4952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 changed_bytes(curwin->w_cursor.lnum,
4955 (colnr_T)(curwin->w_cursor.col - cap->count1));
4956 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 --curwin->w_cursor.col; // cursor on the last replaced char
4958 // if the character on the left of the current cursor is a multi-byte
4959 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (has_mbyte)
4961 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 curbuf->b_op_end = curwin->w_cursor;
4963 curwin->w_set_curswant = TRUE;
4964 set_last_insert(cap->nchar);
4965 }
4966}
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968/*
4969 * 'o': Exchange start and end of Visual area.
4970 * 'O': same, but in block mode exchange left and right corners.
4971 */
4972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 pos_T old_cursor;
4976 colnr_T left, right;
4977
4978 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4979 {
4980 old_cursor = curwin->w_cursor;
4981 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4982 curwin->w_cursor.lnum = VIsual.lnum;
4983 coladvance(left);
4984 VIsual = curwin->w_cursor;
4985
4986 curwin->w_cursor.lnum = old_cursor.lnum;
4987 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004988 // 'selection "exclusive" and cursor at right-bottom corner: move it
4989 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4991 ++curwin->w_curswant;
4992 coladvance(curwin->w_curswant);
4993 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004995 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 curwin->w_cursor.lnum = VIsual.lnum;
4998 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4999 ++right;
5000 coladvance(right);
5001 VIsual = curwin->w_cursor;
5002
5003 curwin->w_cursor.lnum = old_cursor.lnum;
5004 coladvance(left);
5005 curwin->w_curswant = left;
5006 }
5007 }
5008 else
5009 {
5010 old_cursor = curwin->w_cursor;
5011 curwin->w_cursor = VIsual;
5012 VIsual = old_cursor;
5013 curwin->w_set_curswant = TRUE;
5014 }
5015}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017/*
5018 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5019 */
5020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005021nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005023 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 cap->cmdchar = 'c';
5026 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005027 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 VIsual_mode = 'V';
5029 nv_operator(cap);
5030 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005031 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
5033 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005034 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 else
5036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (virtual_active())
5038 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5040 }
5041 }
5042}
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044/*
5045 * "gr".
5046 */
5047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005048nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (VIsual_active)
5051 {
5052 cap->cmdchar = 'r';
5053 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005054 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005056 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
5058 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005059 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else
5061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005062 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005063 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 stuffcharReadbuff(cap->extra_char);
5065 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (virtual_active())
5067 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 invoke_edit(cap, TRUE, 'v', FALSE);
5069 }
5070 }
5071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073/*
5074 * Swap case for "~" command, when it does not work like an operator.
5075 */
5076 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005077n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 long n;
5080 pos_T startpos;
5081 int did_change = 0;
5082#ifdef FEAT_NETBEANS_INTG
5083 pos_T pos;
5084 char_u *ptr;
5085 int count;
5086#endif
5087
5088 if (checkclearopq(cap->oap))
5089 return;
5090
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005091 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 clearopbeep(cap->oap);
5094 return;
5095 }
5096
5097 prep_redo_cmd(cap);
5098
5099 if (u_save_cursor() == FAIL)
5100 return;
5101
5102 startpos = curwin->w_cursor;
5103#ifdef FEAT_NETBEANS_INTG
5104 pos = startpos;
5105#endif
5106 for (n = cap->count1; n > 0; --n)
5107 {
5108 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5109 inc_cursor();
5110 if (gchar_cursor() == NUL)
5111 {
5112 if (vim_strchr(p_ww, '~') != NULL
5113 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5114 {
5115#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005116 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 if (did_change)
5119 {
5120 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005121 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005122 netbeans_removed(curbuf, pos.lnum, pos.col,
5123 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005124 // line may have been flushed, get it again
5125 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005127 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 pos.col = 0;
5130 pos.lnum++;
5131 }
5132#endif
5133 ++curwin->w_cursor.lnum;
5134 curwin->w_cursor.col = 0;
5135 if (n > 1)
5136 {
5137 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5138 break;
5139 u_clearline();
5140 }
5141 }
5142 else
5143 break;
5144 }
5145 }
5146#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005147 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 ptr = ml_get(pos.lnum);
5150 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005151 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5152 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154#endif
5155
5156
5157 check_cursor();
5158 curwin->w_set_curswant = TRUE;
5159 if (did_change)
5160 {
5161 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5162 0L);
5163 curbuf->b_op_start = startpos;
5164 curbuf->b_op_end = curwin->w_cursor;
5165 if (curbuf->b_op_end.col > 0)
5166 --curbuf->b_op_end.col;
5167 }
5168}
5169
5170/*
5171 * Move cursor to mark.
5172 */
5173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005174nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 if (check_mark(pos) == FAIL)
5177 clearop(cap->oap);
5178 else
5179 {
5180 if (cap->cmdchar == '\''
5181 || cap->cmdchar == '`'
5182 || cap->cmdchar == '['
5183 || cap->cmdchar == ']')
5184 setpcmark();
5185 curwin->w_cursor = *pos;
5186 if (flag)
5187 beginline(BL_WHITE | BL_FIX);
5188 else
5189 check_cursor();
5190 }
5191 cap->oap->motion_type = flag ? MLINE : MCHAR;
5192 if (cap->cmdchar == '`')
5193 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005194 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 curwin->w_set_curswant = TRUE;
5196}
5197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198/*
5199 * Handle commands that are operators in Visual mode.
5200 */
5201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005206 // Uppercase means linewise, except in block mode, then "D" deletes till
5207 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (isupper(cap->cmdchar))
5209 {
5210 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005211 {
5212 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5216 curwin->w_curswant = MAXCOL;
5217 }
5218 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5219 nv_operator(cap);
5220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222/*
5223 * "s" and "S" commands.
5224 */
5225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005228#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005229 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005230 if (term_swap_diff() == OK)
5231 return;
5232#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005233#ifdef FEAT_JOB_CHANNEL
5234 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5235 {
5236 clearopbeep(cap->oap);
5237 return;
5238 }
5239#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005243 {
5244 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 cap->cmdchar = 'c';
5248 nv_operator(cap);
5249 }
5250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 nv_optrans(cap);
5252}
5253
5254/*
5255 * Abbreviated commands.
5256 */
5257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005258nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005261 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (VIsual_active)
5265 v_visop(cap);
5266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 nv_optrans(cap);
5268}
5269
5270/*
5271 * Translate a command into another command.
5272 */
5273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005274nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5277 (char_u *)"d$", (char_u *)"c$",
5278 (char_u *)"cl", (char_u *)"cc",
5279 (char_u *)"yy", (char_u *)":s\r"};
5280 static char_u *str = (char_u *)"xXDCsSY&";
5281
5282 if (!checkclearopq(cap->oap))
5283 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005284 // In Vi "2D" doesn't delete the next line. Can't translate it
5285 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005286 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5287 {
5288 cap->oap->start = curwin->w_cursor;
5289 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005290#ifdef FEAT_EVAL
5291 set_op_var(OP_DELETE);
5292#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005293 cap->count1 = 1;
5294 nv_dollar(cap);
5295 finish_op = TRUE;
5296 ResetRedobuff();
5297 AppendCharToRedobuff('D');
5298 }
5299 else
5300 {
5301 if (cap->count0)
5302 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005303 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 cap->opcount = 0;
5307}
5308
5309/*
5310 * "'" and "`" commands. Also for "g'" and "g`".
5311 * cap->arg is TRUE for "'" and "g'".
5312 */
5313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 pos_T *pos;
5317 int c;
5318#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005319 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322
5323 if (cap->cmdchar == 'g')
5324 c = cap->extra_char;
5325 else
5326 c = cap->nchar;
5327 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
5330 if (cap->arg)
5331 {
5332 check_cursor_lnum();
5333 beginline(BL_WHITE | BL_FIX);
5334 }
5335 else
5336 check_cursor();
5337 }
5338 else
5339 nv_cursormark(cap, cap->arg, pos);
5340
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (!virtual_active())
5343 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005344 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345#ifdef FEAT_FOLDING
5346 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005347 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005348 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 && (fdo_flags & FDO_MARK)
5350 && old_KeyTyped)
5351 foldOpenCursor();
5352#endif
5353}
5354
5355/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005356 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 */
5358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005359nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005362#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005364 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367 if (!checkclearopq(cap->oap))
5368 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005369 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5370 {
5371 if (goto_tabpage_lastused() == FAIL)
5372 clearopbeep(cap->oap);
5373 return;
5374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (cap->cmdchar == 'g')
5376 pos = movechangelist((int)cap->count1);
5377 else
5378 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005379 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 curwin->w_set_curswant = TRUE;
5382 check_cursor();
5383 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005384 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 nv_cursormark(cap, FALSE, pos);
5386 else if (cap->cmdchar == 'g')
5387 {
5388 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005389 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005391 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005393 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395 else
5396 clearopbeep(cap->oap);
5397# ifdef FEAT_FOLDING
5398 if (cap->oap->op_type == OP_NOP
5399 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5400 && (fdo_flags & FDO_MARK)
5401 && old_KeyTyped)
5402 foldOpenCursor();
5403# endif
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405}
5406
5407/*
5408 * Handle '"' command.
5409 */
5410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005411nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 if (checkclearop(cap->oap))
5414 return;
5415#ifdef FEAT_EVAL
5416 if (cap->nchar == '=')
5417 cap->nchar = get_expr_register();
5418#endif
5419 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5420 {
5421 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005422 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#ifdef FEAT_EVAL
5424 set_reg_var(cap->oap->regname);
5425#endif
5426 }
5427 else
5428 clearopbeep(cap->oap);
5429}
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431/*
5432 * Handle "v", "V" and "CTRL-V" commands.
5433 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5434 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005435 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 */
5437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005440 if (cap->cmdchar == Ctrl_Q)
5441 cap->cmdchar = Ctrl_V;
5442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005443 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5444 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if (cap->oap->op_type != OP_NOP)
5446 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005447 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005448 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 return;
5450 }
5451
5452 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 else // toggle char/block mode
5458 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 VIsual_mode = cap->cmdchar;
5460 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005461 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005463 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005465 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005468 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 VIsual = curwin->w_cursor;
5472
5473 VIsual_active = TRUE;
5474 VIsual_reselect = TRUE;
5475 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005476 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005479 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005480 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005481 // For V and ^V, we multiply the number of lines even if there
5482 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5484 {
5485 curwin->w_cursor.lnum +=
5486 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005487 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489 VIsual_mode = resel_VIsual_mode;
5490 if (VIsual_mode == 'v')
5491 {
5492 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005493 {
5494 validate_virtcol();
5495 curwin->w_curswant = curwin->w_virtcol
5496 + resel_VIsual_vcol * cap->count0 - 1;
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005499 curwin->w_curswant = resel_VIsual_vcol;
5500 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005502 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 curwin->w_curswant = MAXCOL;
5505 coladvance((colnr_T)MAXCOL);
5506 }
5507 else if (VIsual_mode == Ctrl_V)
5508 {
5509 validate_virtcol();
5510 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005511 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 coladvance(curwin->w_curswant);
5513 }
5514 else
5515 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005516 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518 else
5519 {
5520 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 may_start_select('c');
5523 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005524 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005525 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005526 if (cap->count0 > 0 && --cap->count1 > 0)
5527 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005528 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005529 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5530 nv_right(cap);
5531 else if (VIsual_mode == 'V')
5532 nv_down(cap);
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 }
5536}
5537
5538/*
5539 * Start selection for Shift-movement keys.
5540 */
5541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005542start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005544 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 may_start_select('k');
5546 n_start_visual_mode('v');
5547}
5548
5549/*
5550 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005551 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 */
5553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005554may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005556 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5557 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
5560/*
5561 * Start Visual mode "c".
5562 * Should set VIsual_select before calling this.
5563 */
5564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005565n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005567#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005568 int cursor_line_was_concealed = curwin->w_p_cole > 0
5569 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005570#endif
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 VIsual_mode = c;
5573 VIsual_active = TRUE;
5574 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005575
5576 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005577 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005578 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005579 {
5580 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 VIsual = curwin->w_cursor;
5584
5585#ifdef FEAT_FOLDING
5586 foldAdjustVisual();
5587#endif
5588
LemonBoy2bf52dd2022-04-09 18:17:34 +01005589 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005591#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005592 // Check if redraw is needed after changing the state.
5593 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005594#endif
5595
Bram Moolenaar09df3122006-01-23 22:23:09 +00005596 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 // Make sure the clipboard gets updated. Needed because start and
5600 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 clip_star.vmode = NUL;
5602#endif
5603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 // Only need to redraw this line, unless still need to redraw an old
5605 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005606 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5609 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5610 }
5611}
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614/*
5615 * CTRL-W: Window commands
5616 */
5617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005618nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005620 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005623 cap->cmdchar = ':';
5624 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005625 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005626 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005627 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629}
5630
5631/*
5632 * CTRL-Z: Suspend
5633 */
5634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005635nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005640 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005644 * "gv": Reselect the previous Visual area. If Visual already active,
5645 * exchange previous and current Visual area.
5646 */
5647 static void
5648nv_gv_cmd(cmdarg_T *cap)
5649{
5650 pos_T tpos;
5651 int i;
5652
5653 if (checkclearop(cap->oap))
5654 return;
5655
5656 if (curbuf->b_visual.vi_start.lnum == 0
5657 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5658 || curbuf->b_visual.vi_end.lnum == 0)
5659 {
5660 beep_flush();
5661 return;
5662 }
5663
5664 // set w_cursor to the start of the Visual area, tpos to the end
5665 if (VIsual_active)
5666 {
5667 i = VIsual_mode;
5668 VIsual_mode = curbuf->b_visual.vi_mode;
5669 curbuf->b_visual.vi_mode = i;
5670# ifdef FEAT_EVAL
5671 curbuf->b_visual_mode_eval = i;
5672# endif
5673 i = curwin->w_curswant;
5674 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5675 curbuf->b_visual.vi_curswant = i;
5676
5677 tpos = curbuf->b_visual.vi_end;
5678 curbuf->b_visual.vi_end = curwin->w_cursor;
5679 curwin->w_cursor = curbuf->b_visual.vi_start;
5680 curbuf->b_visual.vi_start = VIsual;
5681 }
5682 else
5683 {
5684 VIsual_mode = curbuf->b_visual.vi_mode;
5685 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5686 tpos = curbuf->b_visual.vi_end;
5687 curwin->w_cursor = curbuf->b_visual.vi_start;
5688 }
5689
5690 VIsual_active = TRUE;
5691 VIsual_reselect = TRUE;
5692
5693 // Set Visual to the start and w_cursor to the end of the Visual
5694 // area. Make sure they are on an existing character.
5695 check_cursor();
5696 VIsual = curwin->w_cursor;
5697 curwin->w_cursor = tpos;
5698 check_cursor();
5699 update_topline();
5700
5701 // When called from normal "g" command: start Select mode when
5702 // 'selectmode' contains "cmd". When called for K_SELECT, always
5703 // start Select mode.
5704 if (cap->arg)
5705 {
5706 VIsual_select = TRUE;
5707 VIsual_select_reg = 0;
5708 }
5709 else
5710 may_start_select('c');
5711 setmouse();
5712#ifdef FEAT_CLIPBOARD
5713 // Make sure the clipboard gets updated. Needed because start and
5714 // end are still the same, and the selection needs to be owned
5715 clip_star.vmode = NUL;
5716#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005717 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005718 showmode();
5719}
5720
5721/*
5722 * "g0", "g^" : Like "0" and "^" but for screen lines.
5723 * "gm": middle of "g0" and "g$".
5724 */
5725 static void
5726nv_g_home_m_cmd(cmdarg_T *cap)
5727{
5728 int i;
5729 int flag = FALSE;
5730
5731 if (cap->nchar == '^')
5732 flag = TRUE;
5733
5734 cap->oap->motion_type = MCHAR;
5735 cap->oap->inclusive = FALSE;
5736 if (curwin->w_p_wrap && curwin->w_width != 0)
5737 {
5738 int width1 = curwin->w_width - curwin_col_off();
5739 int width2 = width1 + curwin_col_off2();
5740
5741 validate_virtcol();
5742 i = 0;
5743 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5744 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5745 }
5746 else
5747 i = curwin->w_leftcol;
5748 // Go to the middle of the screen line. When 'number' or
5749 // 'relativenumber' is on and lines are wrapping the middle can be more
5750 // to the left.
5751 if (cap->nchar == 'm')
5752 i += (curwin->w_width - curwin_col_off()
5753 + ((curwin->w_p_wrap && i > 0)
5754 ? curwin_col_off2() : 0)) / 2;
5755 coladvance((colnr_T)i);
5756 if (flag)
5757 {
5758 do
5759 i = gchar_cursor();
5760 while (VIM_ISWHITE(i) && oneright() == OK);
5761 curwin->w_valid &= ~VALID_WCOL;
5762 }
5763 curwin->w_set_curswant = TRUE;
5764}
5765
5766/*
5767 * "g_": to the last non-blank character in the line or <count> lines
5768 * downward.
5769 */
5770 static void
5771nv_g_underscore_cmd(cmdarg_T *cap)
5772{
5773 char_u *ptr;
5774
5775 cap->oap->motion_type = MCHAR;
5776 cap->oap->inclusive = TRUE;
5777 curwin->w_curswant = MAXCOL;
5778 if (cursor_down((long)(cap->count1 - 1),
5779 cap->oap->op_type == OP_NOP) == FAIL)
5780 {
5781 clearopbeep(cap->oap);
5782 return;
5783 }
5784
5785 ptr = ml_get_curline();
5786
5787 // In Visual mode we may end up after the line.
5788 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5789 --curwin->w_cursor.col;
5790
5791 // Decrease the cursor column until it's on a non-blank.
5792 while (curwin->w_cursor.col > 0
5793 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5794 --curwin->w_cursor.col;
5795 curwin->w_set_curswant = TRUE;
5796 adjust_for_sel(cap);
5797}
5798
5799/*
5800 * "g$" : Like "$" but for screen lines.
5801 */
5802 static void
5803nv_g_dollar_cmd(cmdarg_T *cap)
5804{
5805 oparg_T *oap = cap->oap;
5806 int i;
5807 int col_off = curwin_col_off();
5808
5809 oap->motion_type = MCHAR;
5810 oap->inclusive = TRUE;
5811 if (curwin->w_p_wrap && curwin->w_width != 0)
5812 {
5813 curwin->w_curswant = MAXCOL; // so we stay at the end
5814 if (cap->count1 == 1)
5815 {
5816 int width1 = curwin->w_width - col_off;
5817 int width2 = width1 + curwin_col_off2();
5818
5819 validate_virtcol();
5820 i = width1 - 1;
5821 if (curwin->w_virtcol >= (colnr_T)width1)
5822 i += ((curwin->w_virtcol - width1) / width2 + 1)
5823 * width2;
5824 coladvance((colnr_T)i);
5825
5826 // Make sure we stick in this column.
5827 validate_virtcol();
5828 curwin->w_curswant = curwin->w_virtcol;
5829 curwin->w_set_curswant = FALSE;
5830 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5831 {
5832 // Check for landing on a character that got split at
5833 // the end of the line. We do not want to advance to
5834 // the next screen line.
5835 if (curwin->w_virtcol > (colnr_T)i)
5836 --curwin->w_cursor.col;
5837 }
5838 }
5839 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5840 clearopbeep(oap);
5841 }
5842 else
5843 {
5844 if (cap->count1 > 1)
5845 // if it fails, let the cursor still move to the last char
5846 (void)cursor_down(cap->count1 - 1, FALSE);
5847
5848 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5849 coladvance((colnr_T)i);
5850
5851 // if the character doesn't fit move one back
5852 if (curwin->w_cursor.col > 0
5853 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5854 {
5855 colnr_T vcol;
5856
5857 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5858 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5859 --curwin->w_cursor.col;
5860 }
5861
5862 // Make sure we stick in this column.
5863 validate_virtcol();
5864 curwin->w_curswant = curwin->w_virtcol;
5865 curwin->w_set_curswant = FALSE;
5866 }
5867}
5868
5869/*
5870 * "gi": start Insert at the last position.
5871 */
5872 static void
5873nv_gi_cmd(cmdarg_T *cap)
5874{
5875 int i;
5876
5877 if (curbuf->b_last_insert.lnum != 0)
5878 {
5879 curwin->w_cursor = curbuf->b_last_insert;
5880 check_cursor_lnum();
5881 i = (int)STRLEN(ml_get_curline());
5882 if (curwin->w_cursor.col > (colnr_T)i)
5883 {
5884 if (virtual_active())
5885 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5886 curwin->w_cursor.col = i;
5887 }
5888 }
5889 cap->cmdchar = 'i';
5890 nv_edit(cap);
5891}
5892
5893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 * Commands starting with "g".
5895 */
5896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005897nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 switch (cap->nchar)
5903 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005904 case Ctrl_A:
5905 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005907 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005908 if (!VIsual_active && cap->nchar == Ctrl_A)
5909 vim_mem_profile_dump();
5910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005912 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005913 if (VIsual_active)
5914 {
5915 cap->arg = TRUE;
5916 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005917 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005918 nv_addsub(cap);
5919 }
5920 else
5921 clearopbeep(oap);
5922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005924 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 case 'R':
5926 cap->arg = TRUE;
5927 nv_Replace(cap);
5928 break;
5929
5930 case 'r':
5931 nv_vreplace(cap);
5932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
5934 case '&':
5935 do_cmdline_cmd((char_u *)"%s//~/&");
5936 break;
5937
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005938 // "gv": Reselect the previous Visual area. If Visual already active,
5939 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005941 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005943
5944 // "gV": Don't reselect the previous Visual area after a Select mode
5945 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case 'V':
5947 VIsual_reselect = FALSE;
5948 break;
5949
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005950 // "gh": start Select mode.
5951 // "gH": start Select line mode.
5952 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 case K_BS:
5954 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005955 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 case 'h':
5957 case 'H':
5958 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 cap->cmdchar = cap->nchar + ('v' - 'h');
5960 cap->arg = TRUE;
5961 nv_visual(cap);
5962 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005964 // "gn", "gN" visually select next/previous search match
5965 // "gn" selects next match
5966 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005967 case 'N':
5968 case 'n':
5969 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005970 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005971 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005973 // "gj" and "gk" two new funny movement keys -- up and down
5974 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 case 'j':
5976 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005977 // with 'nowrap' it works just like the normal "j" command.
5978 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 oap->motion_type = MLINE;
5981 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5982 }
5983 else
5984 i = nv_screengo(oap, FORWARD, cap->count1);
5985 if (i == FAIL)
5986 clearopbeep(oap);
5987 break;
5988
5989 case 'k':
5990 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005991 // with 'nowrap' it works just like the normal "k" command.
5992 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 oap->motion_type = MLINE;
5995 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5996 }
5997 else
5998 i = nv_screengo(oap, BACKWARD, cap->count1);
5999 if (i == FAIL)
6000 clearopbeep(oap);
6001 break;
6002
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006003 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 case 'J':
6005 nv_join(cap);
6006 break;
6007
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 // "g0", "g^" : Like "0" and "^" but for screen lines.
6009 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 case '0':
6012 case 'm':
6013 case K_HOME:
6014 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006015 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 break;
6017
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006018 case 'M':
6019 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006020 oap->motion_type = MCHAR;
6021 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006022 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006023 if (cap->count0 > 0 && cap->count0 <= 100)
6024 coladvance((colnr_T)(i * cap->count0 / 100));
6025 else
6026 coladvance((colnr_T)(i / 2));
6027 curwin->w_set_curswant = TRUE;
6028 }
6029 break;
6030
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006031 // "g_": to the last non-blank character in the line or <count> lines
6032 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 break;
6036
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 case '$':
6039 case K_END:
6040 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006041 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 break;
6043
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006044 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 case '*':
6046 case '#':
6047#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006048 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006050 case Ctrl_RSB: // :tag or :tselect for current identifier
6051 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 nv_ident(cap);
6053 break;
6054
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006055 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 case 'e':
6057 case 'E':
6058 oap->motion_type = MCHAR;
6059 curwin->w_set_curswant = TRUE;
6060 oap->inclusive = TRUE;
6061 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6062 clearopbeep(oap);
6063 break;
6064
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006065 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006067 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 break;
6069
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006070 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006072 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 break;
6074
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 case 'I':
6077 beginline(0);
6078 if (!checkclearopq(oap))
6079 invoke_edit(cap, FALSE, 'g', FALSE);
6080 break;
6081
6082#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "gf": goto file, edit file under cursor
6084 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006086 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 nv_gotofile(cap);
6088 break;
6089#endif
6090
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006091 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case '\'':
6093 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006094 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 case '`':
6096 nv_gomark(cap);
6097 break;
6098
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006099 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006101 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 break;
6103
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006104 // "ga": Display the ascii value of the character under the
6105 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 case 'a':
6107 do_ascii(NULL);
6108 break;
6109
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006110 // "g8": Display the bytes used for the UTF-8 character under the
6111 // cursor. It is displayed in hex.
6112 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006114 if (cap->count0 == 8)
6115 utf_find_illegal();
6116 else
6117 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006120 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006121 case '<':
6122 show_sb_text();
6123 break;
6124
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006125 // "gg": Goto the first line in file. With a count it goes to
6126 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 case 'g':
6128 cap->arg = FALSE;
6129 nv_goto(cap);
6130 break;
6131
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006132 // Two-character operators:
6133 // "gq" Format text
6134 // "gw" Format text and keep cursor position
6135 // "g~" Toggle the case of the text.
6136 // "gu" Change text to lower case.
6137 // "gU" Change text to upper case.
6138 // "g?" rot13 encoding
6139 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 case 'q':
6141 case 'w':
6142 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006143 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 case '~':
6145 case 'u':
6146 case 'U':
6147 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006148 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 nv_operator(cap);
6150 break;
6151
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006152 // "gd": Find first occurrence of pattern under the cursor in the
6153 // current function
6154 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 case 'd':
6156 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006157 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 break;
6159
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006160 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 case K_MIDDLEMOUSE:
6162 case K_MIDDLEDRAG:
6163 case K_MIDDLERELEASE:
6164 case K_LEFTMOUSE:
6165 case K_LEFTDRAG:
6166 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006167 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 case K_RIGHTMOUSE:
6169 case K_RIGHTDRAG:
6170 case K_RIGHTRELEASE:
6171 case K_X1MOUSE:
6172 case K_X1DRAG:
6173 case K_X1RELEASE:
6174 case K_X2MOUSE:
6175 case K_X2DRAG:
6176 case K_X2RELEASE:
6177 mod_mask = MOD_MASK_CTRL;
6178 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 case K_IGNORE:
6182 break;
6183
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006184 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 case 'p':
6186 case 'P':
6187 nv_put(cap);
6188 break;
6189
6190#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006191 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 case 'o':
6193 goto_byte(cap->count0);
6194 break;
6195#endif
6196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006197 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006199 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 do_exmode(TRUE);
6201 break;
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 case ',':
6204 nv_pcmark(cap);
6205 break;
6206
6207 case ';':
6208 cap->count1 = -cap->count1;
6209 nv_pcmark(cap);
6210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006213 if (!checkclearop(oap))
6214 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006216 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006217 if (!checkclearop(oap))
6218 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006219 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006220
Bram Moolenaar62a23252020-08-09 14:04:42 +02006221 case TAB:
6222 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6223 clearopbeep(oap);
6224 break;
6225
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006226 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006227 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006228 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006229 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006230 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006231 break;
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 default:
6234 clearopbeep(oap);
6235 break;
6236 }
6237}
6238
6239/*
6240 * Handle "o" and "O" commands.
6241 */
6242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006243n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006245#ifdef FEAT_CONCEAL
6246 linenr_T oldline = curwin->w_cursor.lnum;
6247#endif
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (!checkclearopq(cap->oap))
6250 {
6251#ifdef FEAT_FOLDING
6252 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006253 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 (void)hasFolding(curwin->w_cursor.lnum,
6255 &curwin->w_cursor.lnum, NULL);
6256 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006257 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 (void)hasFolding(curwin->w_cursor.lnum,
6259 NULL, &curwin->w_cursor.lnum);
6260#endif
6261 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6262 (cap->cmdchar == 'O' ? 1 : 0)),
6263 (linenr_T)(curwin->w_cursor.lnum +
6264 (cap->cmdchar == 'o' ? 1 : 0))
6265 ) == OK
6266 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006267 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006268 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006270#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006271 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006272 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006273#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006274#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006275 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006276 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006277 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006278#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006279 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006280 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6281 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6283 }
6284 }
6285}
6286
6287/*
6288 * "." command: redo last change.
6289 */
6290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006291nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 if (!checkclearopq(cap->oap))
6294 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006295 // If "restart_edit" is TRUE, the last but one command is repeated
6296 // instead of the last command (inserting text). This is used for
6297 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6299 clearopbeep(cap->oap);
6300 }
6301}
6302
6303/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006304 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 */
6306 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006307nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006309 if (VIsual_select && VIsual_active)
6310 {
6311 int reg;
6312 // Get register name
6313 ++no_mapping;
6314 ++allow_keys;
6315 reg = plain_vgetc();
6316 LANGMAP_ADJUST(reg, TRUE);
6317 --no_mapping;
6318 --allow_keys;
6319
6320 if (reg == '"')
6321 // the unnamed register is 0
6322 reg = 0;
6323
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006324 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006325 return;
6326 }
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (!checkclearopq(cap->oap))
6329 {
6330 u_redo((int)cap->count1);
6331 curwin->w_set_curswant = TRUE;
6332 }
6333}
6334
6335/*
6336 * Handle "U" command.
6337 */
6338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006339nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006341 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006342 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006344 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 cap->cmdchar = 'g';
6346 cap->nchar = 'U';
6347 nv_operator(cap);
6348 }
6349 else if (!checkclearopq(cap->oap))
6350 {
6351 u_undoline();
6352 curwin->w_set_curswant = TRUE;
6353 }
6354}
6355
6356/*
6357 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6358 * single character.
6359 */
6360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006361nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006363 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006364 {
6365#ifdef FEAT_JOB_CHANNEL
6366 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6367 {
6368 clearopbeep(cap->oap);
6369 return;
6370 }
6371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 else
6375 nv_operator(cap);
6376}
6377
6378/*
6379 * Handle an operator command.
6380 * The actual work is done by do_pending_operator().
6381 */
6382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006383nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 int op_type;
6386
6387 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006388#ifdef FEAT_JOB_CHANNEL
6389 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6390 {
6391 clearopbeep(cap->oap);
6392 return;
6393 }
6394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006396 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 nv_lineop(cap);
6398 else if (!checkclearop(cap->oap))
6399 {
6400 cap->oap->start = curwin->w_cursor;
6401 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006402#ifdef FEAT_EVAL
6403 set_op_var(op_type);
6404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406}
6407
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006408#ifdef FEAT_EVAL
6409/*
6410 * Set v:operator to the characters for "optype".
6411 */
6412 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006413set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006414{
6415 char_u opchars[3];
6416
6417 if (optype == OP_NOP)
6418 set_vim_var_string(VV_OP, NULL, 0);
6419 else
6420 {
6421 opchars[0] = get_op_char(optype);
6422 opchars[1] = get_extra_op_char(optype);
6423 opchars[2] = NUL;
6424 set_vim_var_string(VV_OP, opchars, -1);
6425 }
6426}
6427#endif
6428
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429/*
6430 * Handle linewise operator "dd", "yy", etc.
6431 *
6432 * "_" is is a strange motion command that helps make operators more logical.
6433 * It is actually implemented, but not documented in the real Vi. This motion
6434 * command actually refers to "the current line". Commands like "dd" and "yy"
6435 * are really an alternate form of "d_" and "y_". It does accept a count, so
6436 * "d3_" works to delete 3 lines.
6437 */
6438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006439nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 cap->oap->motion_type = MLINE;
6442 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6443 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006444 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006445 && cap->oap->motion_force != 'v'
6446 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 || cap->oap->op_type == OP_LSHIFT
6448 || cap->oap->op_type == OP_RSHIFT)
6449 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 beginline(BL_WHITE | BL_FIX);
6452}
6453
6454/*
6455 * <Home> command.
6456 */
6457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006458nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006460 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006461 if (mod_mask & MOD_MASK_CTRL)
6462 nv_goto(cap);
6463 else
6464 {
6465 cap->count0 = 1;
6466 nv_pipe(cap);
6467 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006468 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6469 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470}
6471
6472/*
6473 * "|" command.
6474 */
6475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006476nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 cap->oap->motion_type = MCHAR;
6479 cap->oap->inclusive = FALSE;
6480 beginline(0);
6481 if (cap->count0 > 0)
6482 {
6483 coladvance((colnr_T)(cap->count0 - 1));
6484 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6485 }
6486 else
6487 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006488 // keep curswant at the column where we wanted to go, not where
6489 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 curwin->w_set_curswant = FALSE;
6491}
6492
6493/*
6494 * Handle back-word command "b" and "B".
6495 * cap->arg is 1 for "B"
6496 */
6497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006498nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 cap->oap->motion_type = MCHAR;
6501 cap->oap->inclusive = FALSE;
6502 curwin->w_set_curswant = TRUE;
6503 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6504 clearopbeep(cap->oap);
6505#ifdef FEAT_FOLDING
6506 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6507 foldOpenCursor();
6508#endif
6509}
6510
6511/*
6512 * Handle word motion commands "e", "E", "w" and "W".
6513 * cap->arg is TRUE for "E" and "W".
6514 */
6515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006516nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 int n;
6519 int word_end;
6520 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006521 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006523 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6525 word_end = TRUE;
6526 else
6527 word_end = FALSE;
6528 cap->oap->inclusive = word_end;
6529
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006530 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 if (!word_end && cap->oap->op_type == OP_CHANGE)
6532 {
6533 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006534 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006536 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006538 // Reproduce a funny Vi behaviour: "cw" on a blank only
6539 // changes one character, not all blanks until the start of
6540 // the next word. Only do this when the 'w' flag is included
6541 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6543 {
6544 cap->oap->inclusive = TRUE;
6545 cap->oap->motion_type = MCHAR;
6546 return;
6547 }
6548 }
6549 else
6550 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006551 // This is a little strange. To match what the real Vi does,
6552 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6553 // that we are not on a space or a TAB. This seems impolite
6554 // at first, but it's really more what we mean when we say
6555 // 'cw'.
6556 // Another strangeness: When standing on the end of a word
6557 // "ce" will change until the end of the next word, but "cw"
6558 // will change only one character! This is done by setting
6559 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 cap->oap->inclusive = TRUE;
6561 word_end = TRUE;
6562 flag = TRUE;
6563 }
6564 }
6565 }
6566
6567 cap->oap->motion_type = MCHAR;
6568 curwin->w_set_curswant = TRUE;
6569 if (word_end)
6570 n = end_word(cap->count1, cap->arg, flag, FALSE);
6571 else
6572 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006574 // Don't leave the cursor on the NUL past the end of line. Unless we
6575 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006576 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006577 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
6579 if (n == FAIL && cap->oap->op_type == OP_NOP)
6580 clearopbeep(cap->oap);
6581 else
6582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584#ifdef FEAT_FOLDING
6585 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6586 foldOpenCursor();
6587#endif
6588 }
6589}
6590
6591/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006592 * Used after a movement command: If the cursor ends up on the NUL after the
6593 * end of the line, may move it back to the last character and make the motion
6594 * inclusive.
6595 */
6596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006597adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006598{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006599 // The cursor cannot remain on the NUL when:
6600 // - the column is > 0
6601 // - not in Visual mode or 'selection' is "o"
6602 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006603 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006604 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006605 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006606 {
6607 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006608 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006609 if (has_mbyte)
6610 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006611 oap->inclusive = TRUE;
6612 }
6613}
6614
6615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 * "0" and "^" commands.
6617 * cap->arg is the argument for beginline().
6618 */
6619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006620nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 cap->oap->motion_type = MCHAR;
6623 cap->oap->inclusive = FALSE;
6624 beginline(cap->arg);
6625#ifdef FEAT_FOLDING
6626 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6627 foldOpenCursor();
6628#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006629 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6630 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631}
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633/*
6634 * In exclusive Visual mode, may include the last character.
6635 */
6636 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006637adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006640 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if (has_mbyte)
6643 inc_cursor();
6644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 ++curwin->w_cursor.col;
6646 cap->oap->inclusive = FALSE;
6647 }
6648}
6649
6650/*
6651 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6652 * Should check VIsual_mode before calling this.
6653 * Returns TRUE when backed up to the previous line.
6654 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006655 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 pos_T *pp;
6659
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006660 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006662 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 pp = &curwin->w_cursor;
6664 else
6665 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 if (pp->coladd > 0)
6667 --pp->coladd;
6668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (pp->col > 0)
6670 {
6671 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006672 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 }
6674 else if (pp->lnum > 1)
6675 {
6676 --pp->lnum;
6677 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6678 return TRUE;
6679 }
6680 }
6681 return FALSE;
6682}
6683
6684/*
6685 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6686 */
6687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006688nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006691 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006693 VIsual_select_reg = 0;
6694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else if (VIsual_reselect)
6696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006697 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 cap->arg = TRUE;
6699 nv_g_cmd(cap);
6700 }
6701}
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704/*
6705 * "G", "gg", CTRL-END, CTRL-HOME.
6706 * cap->arg is TRUE for "G".
6707 */
6708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 linenr_T lnum;
6712
6713 if (cap->arg)
6714 lnum = curbuf->b_ml.ml_line_count;
6715 else
6716 lnum = 1L;
6717 cap->oap->motion_type = MLINE;
6718 setpcmark();
6719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006720 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (cap->count0 != 0)
6722 lnum = cap->count0;
6723 if (lnum < 1L)
6724 lnum = 1L;
6725 else if (lnum > curbuf->b_ml.ml_line_count)
6726 lnum = curbuf->b_ml.ml_line_count;
6727 curwin->w_cursor.lnum = lnum;
6728 beginline(BL_SOL | BL_FIX);
6729#ifdef FEAT_FOLDING
6730 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6731 foldOpenCursor();
6732#endif
6733}
6734
6735/*
6736 * CTRL-\ in Normal mode.
6737 */
6738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006739nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6742 {
6743 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006744 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 restart_edit = 0;
6747#ifdef FEAT_CMDWIN
6748 if (cmdwin_type != 0)
6749 cmdwin_result = Ctrl_C;
6750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (VIsual_active)
6752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006753 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006754 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006756 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (cap->nchar == Ctrl_G && p_im)
6758 restart_edit = 'a';
6759 }
6760 else
6761 clearopbeep(cap->oap);
6762}
6763
6764/*
6765 * ESC in Normal mode: beep, but don't flush buffers.
6766 * Don't even beep if we are canceling a command.
6767 */
6768 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006769nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771 int no_reason;
6772
6773 no_reason = (cap->oap->op_type == OP_NOP
6774 && cap->opcount == 0
6775 && cap->count0 == 0
6776 && cap->oap->regname == 0
6777 && !p_im);
6778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006779 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 if (restart_edit == 0
6782#ifdef FEAT_CMDWIN
6783 && cmdwin_type == 0
6784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006787 {
6788 if (anyBufIsChanged())
6789 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6790 else
6791 msg(_("Type :qa and press <Enter> to exit Vim"));
6792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006794 if (restart_edit != 0)
6795 redraw_mode = TRUE; // remove "-- (insert) --"
6796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006797 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6798 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (!p_im)
6800 restart_edit = 0;
6801#ifdef FEAT_CMDWIN
6802 if (cmdwin_type != 0)
6803 {
6804 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 return;
6807 }
6808#endif
6809 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006810#ifdef FEAT_CMDWIN
6811 else if (cmdwin_type != 0 && ex_normal_busy)
6812 {
6813 // When :normal runs out of characters while in the command line window
6814 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6815 // loop.
6816 cmdwin_result = K_IGNORE;
6817 return;
6818 }
6819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (VIsual_active)
6822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006823 end_visual_mode(); // stop Visual
6824 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006826 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006828 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006829 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 clearop(cap->oap);
6831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006832 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6833 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006834 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 restart_edit = 'a';
6836}
6837
6838/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006839 * Move the cursor for the "A" command.
6840 */
6841 void
6842set_cursor_for_append_to_line(void)
6843{
6844 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006845 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006846 {
6847 int save_State = State;
6848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006849 // Pretend Insert mode here to allow the cursor on the
6850 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006851 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006852 coladvance((colnr_T)MAXCOL);
6853 State = save_State;
6854 }
6855 else
6856 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6857}
6858
6859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006861 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 */
6863 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006864nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6868 cap->cmdchar = 'i';
6869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006870 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006872 {
6873#ifdef FEAT_TERMINAL
6874 if (term_in_normal_mode())
6875 {
6876 end_visual_mode();
6877 clearop(cap->oap);
6878 term_enter_job_mode();
6879 return;
6880 }
6881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006885 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006886 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6887 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006891#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006892 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006893 {
6894 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006895 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006896 return;
6897 }
6898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 else if (!curbuf->b_p_ma && !p_im)
6900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006902 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006904 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006906 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006908 else if (cap->cmdchar == K_PS && VIsual_active)
6909 {
6910 pos_T old_pos = curwin->w_cursor;
6911 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006912 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006914 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006915 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6916 {
6917 shift_delete_registers();
6918 cap->oap->regname = '1';
6919 }
6920 else
6921 cap->oap->regname = '-';
6922 cap->cmdchar = 'd';
6923 cap->nchar = NUL;
6924 nv_operator(cap);
6925 do_pending_operator(cap, 0, FALSE);
6926 cap->cmdchar = K_PS;
6927
zeertzjq7a732522022-03-14 20:46:41 +00006928 if (*ml_get_cursor() != NUL)
6929 {
6930 if (old_visual_mode == 'V')
6931 {
6932 // In linewise Visual mode insert before the beginning of the
6933 // next line.
6934 // When the last line in the buffer was deleted then create a
6935 // new line, otherwise there is not need to move cursor.
6936 // Detect this by checking if cursor moved above Visual area.
6937 if (curwin->w_cursor.lnum < old_pos.lnum
6938 && curwin->w_cursor.lnum < old_visual.lnum)
6939 {
6940 if (u_save_cursor() == OK)
6941 {
6942 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6943 FALSE);
6944 appended_lines(curwin->w_cursor.lnum++, 1L);
6945 }
6946 }
6947 }
6948 // When the last char in the line was deleted then append.
6949 // Detect this by checking if cursor moved before Visual area.
6950 else if (curwin->w_cursor.col < old_pos.col
6951 && curwin->w_cursor.col < old_visual.col)
6952 inc_cursor();
6953 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006956 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 else if (!checkclearopq(cap->oap))
6959 {
6960 switch (cap->cmdchar)
6961 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006962 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006963 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 break;
6965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006967 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6968 beginline(BL_WHITE);
6969 else
6970 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 break;
6972
Bram Moolenaara1891842017-02-04 21:34:31 +01006973 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 // Bracketed paste works like "a"ppend, unless the cursor is in
6975 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006976 if (curwin->w_cursor.col == 0)
6977 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 case 'a': // "a"ppend is like "i"nsert on the next character.
6981 // increment coladd when in virtual space, increment the
6982 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (virtual_active()
6984 && (curwin->w_cursor.coladd > 0
6985 || *ml_get_cursor() == NUL
6986 || *ml_get_cursor() == TAB))
6987 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006988 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 inc_cursor();
6990 break;
6991 }
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6994 {
6995 int save_State = State;
6996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006997 // Pretend Insert mode here to allow the cursor on the
6998 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006999 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 coladvance(getviscol());
7001 State = save_State;
7002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7005 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007006 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007008 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009}
7010
7011/*
7012 * Invoke edit() and take care of "restart_edit" and the return value.
7013 */
7014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007015invoke_edit(
7016 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007018 int cmd,
7019 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 int restart_edit_save = 0;
7022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007023 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7024 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7025 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (repl || !stuff_empty())
7027 restart_edit_save = restart_edit;
7028 else
7029 restart_edit_save = 0;
7030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007031 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 restart_edit = 0;
7033
7034 if (edit(cmd, startln, cap->count1))
7035 cap->retval |= CA_COMMAND_BUSY;
7036
7037 if (restart_edit == 0)
7038 restart_edit = restart_edit_save;
7039}
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041/*
7042 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7043 */
7044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007045nv_object(
7046 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 int flag;
7049 int include;
7050 char_u *mps_save;
7051
7052 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007053 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 mps_save = curbuf->b_p_mps;
7059 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7060
7061 switch (cap->nchar)
7062 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007063 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 flag = current_word(cap->oap, cap->count1, include, FALSE);
7065 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007066 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 flag = current_word(cap->oap, cap->count1, include, TRUE);
7068 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 case '(':
7071 case ')':
7072 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7073 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 case '{':
7076 case '}':
7077 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7078 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 case ']':
7081 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7082 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 case '>':
7085 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7086 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007087#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case 't': // "at" = a tag block (xml and html)
7089 // Do not adjust oap->end in do_pending_operator()
7090 // otherwise there are different results for 'dit'
7091 // (note leading whitespace in last line):
7092 // 1) <b> 2) <b>
7093 // foobar foobar
7094 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007095 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007096 flag = current_tagblock(cap->oap, cap->count1, include);
7097 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007098#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 flag = current_par(cap->oap, cap->count1, include, 'p');
7101 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007102 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 flag = current_sent(cap->oap, cap->count1, include);
7104 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 case '"': // "a"" = a double quoted string
7106 case '\'': // "a'" = a single quoted string
7107 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007108 flag = current_quote(cap->oap, cap->count1, include,
7109 cap->nchar);
7110 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007111#if 0 // TODO
7112 case 'S': // "aS" = a section
7113 case 'f': // "af" = a filename
7114 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115#endif
7116 default:
7117 flag = FAIL;
7118 break;
7119 }
7120
7121 curbuf->b_p_mps = mps_save;
7122 if (flag == FAIL)
7123 clearopbeep(cap->oap);
7124 adjust_cursor_col();
7125 curwin->w_set_curswant = TRUE;
7126}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128/*
7129 * "q" command: Start/stop recording.
7130 * "q:", "q/", "q?": edit command-line in command-line window.
7131 */
7132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007133nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
7135 if (cap->oap->op_type == OP_FORMAT)
7136 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007137 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 cap->cmdchar = 'g';
7139 cap->nchar = 'q';
7140 nv_operator(cap);
7141 }
7142 else if (!checkclearop(cap->oap))
7143 {
7144#ifdef FEAT_CMDWIN
7145 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7146 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007147 if (cmdwin_type != 0)
7148 {
7149 emsg(_(e_cmdline_window_already_open));
7150 return;
7151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 stuffcharReadbuff(cap->nchar);
7153 stuffcharReadbuff(K_CMDWIN);
7154 }
7155 else
7156#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007157 // (stop) recording into a named register, unless executing a
7158 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007159 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 clearopbeep(cap->oap);
7161 }
7162}
7163
7164/*
7165 * Handle the "@r" command.
7166 */
7167 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007168nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 if (checkclearop(cap->oap))
7171 return;
7172#ifdef FEAT_EVAL
7173 if (cap->nchar == '=')
7174 {
7175 if (get_expr_register() == NUL)
7176 return;
7177 }
7178#endif
7179 while (cap->count1-- && !got_int)
7180 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007181 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
7183 clearopbeep(cap->oap);
7184 break;
7185 }
7186 line_breakcheck();
7187 }
7188}
7189
7190/*
7191 * Handle the CTRL-U and CTRL-D commands.
7192 */
7193 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007194nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7197 || (cap->cmdchar == Ctrl_D
7198 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7199 clearopbeep(cap->oap);
7200 else if (!checkclearop(cap->oap))
7201 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7202}
7203
7204/*
7205 * Handle "J" or "gJ" command.
7206 */
7207 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007208nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007210 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007212 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7217 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007219 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007220 if (cap->count0 <= 2)
7221 {
7222 clearopbeep(cap->oap);
7223 return;
7224 }
7225 cap->count0 = curbuf->b_ml.ml_line_count
7226 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007228
7229 prep_redo(cap->oap->regname, cap->count0,
7230 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7231 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
7233}
7234
7235/*
7236 * "P", "gP", "p" and "gp" commands.
7237 */
7238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007239nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007241 nv_put_opt(cap, FALSE);
7242}
7243
7244/*
7245 * "P", "gP", "p" and "gp" commands.
7246 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7247 */
7248 static void
7249nv_put_opt(cmdarg_T *cap, int fix_indent)
7250{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 int regname = 0;
7252 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007253 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007254 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 int dir;
7256 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007257 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 if (cap->oap->op_type != OP_NOP)
7260 {
7261#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007262 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7264 {
7265 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007266 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 else
7269#endif
7270 clearopbeep(cap->oap);
7271 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007272#ifdef FEAT_JOB_CHANNEL
7273 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7274 {
7275 clearopbeep(cap->oap);
7276 }
7277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
7279 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007280 if (fix_indent)
7281 {
7282 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7283 ? FORWARD : BACKWARD;
7284 flags |= PUT_FIXINDENT;
7285 }
7286 else
7287 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007288 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7289 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 prep_redo_cmd(cap);
7291 if (cap->cmdchar == 'g')
7292 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007293 else if (cap->cmdchar == 'z')
7294 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 if (VIsual_active)
7297 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007298 // Putting in Visual mode: The put text replaces the selected
7299 // text. First delete the selected text, then put the new text.
7300 // Need to save and restore the registers that the delete
7301 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007302 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007304 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007305#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007307#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007308 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007309 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
7314 )
7315 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007316 // The delete is going to overwrite the register we want to
7317 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 reg1 = get_register(regname, TRUE);
7319 }
7320
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007321 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 cap->cmdchar = 'd';
7323 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007324 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007325 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 nv_operator(cap);
7327 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007328 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007329 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007331 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 cap->oap->regname = regname;
7333
7334 if (reg1 != NULL)
7335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // Delete probably changed the register we want to put, save
7337 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 reg2 = get_register(regname, FALSE);
7339 put_register(regname, reg1);
7340 }
7341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 // When deleted a linewise Visual area, put the register as
7343 // lines to avoid it joined with the next line. When deletion was
7344 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 if (VIsual_mode == 'V')
7346 flags |= PUT_LINE;
7347 else if (VIsual_mode == 'v')
7348 flags |= PUT_LINE_SPLIT;
7349 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7350 flags |= PUT_LINE_FORWARD;
7351 dir = BACKWARD;
7352 if ((VIsual_mode != 'V'
7353 && curwin->w_cursor.col < curbuf->b_op_start.col)
7354 || (VIsual_mode == 'V'
7355 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007356 // cursor is at the end of the line or end of file, put
7357 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007359 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007360 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007362 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (reg2 != NULL)
7366 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // What to reselect with "gv"? Selecting the just put text seems to
7369 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370 if (was_visual)
7371 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007372 curbuf->b_visual.vi_start = curbuf->b_op_start;
7373 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007374 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007375 if (*p_sel == 'e')
7376 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007377 }
7378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007379 // When all lines were selected and deleted do_put() leaves an empty
7380 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007381 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007382 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007383 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007384 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007386 // If the cursor was in that line, move it to the end of the last
7387 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007388 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7389 {
7390 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7391 coladvance((colnr_T)MAXCOL);
7392 }
7393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 auto_format(FALSE, TRUE);
7395 }
7396}
7397
7398/*
7399 * "o" and "O" commands.
7400 */
7401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007402nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
7404#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007405 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7407 {
7408 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007409 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 }
7411 else
7412#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007413 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007415#ifdef FEAT_JOB_CHANNEL
7416 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007417 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 n_opencmd(cap);
7421}
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423#ifdef FEAT_NETBEANS_INTG
7424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007425nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
7427 netbeans_keycommand(cap->nchar);
7428}
7429#endif
7430
7431#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007433nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007435 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436}
7437#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007438
Bram Moolenaar3918c952005-03-15 22:34:55 +00007439/*
7440 * Trigger CursorHold event.
7441 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7442 * input buffer. "did_cursorhold" is set to avoid retriggering.
7443 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007445nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007446{
7447 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7448 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007449 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007450}