blob: 6fcdaeb78efeec3fbe54ff52518ce6aacefdfb08 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052/*
4053 * Grab the file name under the cursor and edit it.
4054 */
4055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004059 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004061 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063 if (curbuf_locked())
4064 {
4065 clearop(cap->oap);
4066 return;
4067 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004068#ifdef FEAT_PROP_POPUP
4069 if (ERROR_IF_TERM_POPUP_WINDOW)
4070 return;
4071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004073 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 if (ptr != NULL)
4076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004077 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004078 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004079 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004081 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004082 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004083 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004084 {
4085 curwin->w_cursor.lnum = lnum;
4086 check_cursor_lnum();
4087 beginline(BL_SOL | BL_FIX);
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 vim_free(ptr);
4090 }
4091 else
4092 clearop(cap->oap);
4093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095/*
4096 * <End> command: to end of current line or last line.
4097 */
4098 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004099nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004103 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 nv_dollar(cap);
4108}
4109
4110/*
4111 * Handle the "$" command.
4112 */
4113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004114nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 cap->oap->motion_type = MCHAR;
4117 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004118 // In virtual mode when off the edge of a line and an operator
4119 // is pending (whew!) keep the cursor where it is.
4120 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (!virtual_active() || gchar_cursor() != NUL
4122 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004123 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (cursor_down((long)(cap->count1 - 1),
4125 cap->oap->op_type == OP_NOP) == FAIL)
4126 clearopbeep(cap->oap);
4127#ifdef FEAT_FOLDING
4128 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4129 foldOpenCursor();
4130#endif
4131}
4132
4133/*
4134 * Implementation of '?' and '/' commands.
4135 * If cap->arg is TRUE don't set PC mark.
4136 */
4137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004138nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
4140 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004141 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004145 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 cap->cmdchar = 'g';
4147 cap->nchar = '?';
4148 nv_operator(cap);
4149 return;
4150 }
4151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004152 // When using 'incsearch' the cursor may be moved to set a different search
4153 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004154 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 if (cap->searchbuf == NULL)
4157 {
4158 clearop(oap);
4159 return;
4160 }
4161
Bram Moolenaar46539112015-02-17 15:43:57 +01004162 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004163 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004164 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165}
4166
LemonBoya4399382022-04-09 21:04:08 +01004167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168/*
4169 * Handle "N" and "n" commands.
4170 * cap->arg is SEARCH_REV for "N", 0 for "n".
4171 */
4172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004173nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004175 pos_T old = curwin->w_cursor;
4176 int wrapped = FALSE;
4177 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004178
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004179 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004181 // Avoid getting stuck on the current cursor position, which can
4182 // happen when an offset is given and the cursor is on the last char
4183 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004184 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004185 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004186 cap->count1 -= 1;
4187 }
LemonBoya4399382022-04-09 21:04:08 +01004188
4189#ifdef FEAT_SEARCH_EXTRA
4190 // Redraw the window to refresh the highlighted matches.
4191 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004192 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
4196/*
4197 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4198 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004199 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004201 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004202normal_search(
4203 cmdarg_T *cap,
4204 int dir,
4205 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004206 int opt, // extra flags for do_search()
4207 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004210 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004211#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004212 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 cap->oap->motion_type = MCHAR;
4216 cap->oap->inclusive = FALSE;
4217 cap->oap->use_reg_one = TRUE;
4218 curwin->w_set_curswant = TRUE;
4219
Bram Moolenaara80faa82020-04-12 19:37:17 +02004220 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004221 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004222 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4223 if (wrapped != NULL)
4224 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 if (i == 0)
4226 clearop(cap->oap);
4227 else
4228 {
4229 if (i == 2)
4230 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#ifdef FEAT_FOLDING
4233 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4234 foldOpenCursor();
4235#endif
4236 }
LemonBoya4399382022-04-09 21:04:08 +01004237#ifdef FEAT_SEARCH_EXTRA
4238 // Redraw the window to refresh the highlighted matches.
4239 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004240 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004243 // "/$" will put the cursor after the end of the line, may need to
4244 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004246 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247}
4248
4249/*
4250 * Character search commands.
4251 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4252 * ',' and FALSE for ';'.
4253 * cap->nchar is NUL for ',' and ';' (repeat the search)
4254 */
4255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004256nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 int t_cmd;
4259
4260 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4261 t_cmd = TRUE;
4262 else
4263 t_cmd = FALSE;
4264
4265 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4267 clearopbeep(cap->oap);
4268 else
4269 {
4270 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004271 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4273 && (t_cmd || cap->oap->op_type != OP_NOP))
4274 {
4275 colnr_T scol, ecol;
4276
4277 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4278 curwin->w_cursor.coladd = ecol - scol;
4279 }
4280 else
4281 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#ifdef FEAT_FOLDING
4284 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4285 foldOpenCursor();
4286#endif
4287 }
4288}
4289
4290/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004291 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4292 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4293 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4294 * "[m" or "]m" search for prev/next start of (Java) method.
4295 * "[M" or "]M" search for prev/next end of (Java) method.
4296 */
4297 static void
4298nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4299{
4300 pos_T new_pos = {0, 0, 0};
4301 pos_T *pos = NULL; // init for GCC
4302 pos_T prev_pos;
4303 long n;
4304 int findc;
4305 int c;
4306
4307 if (cap->nchar == '*')
4308 cap->nchar = '/';
4309 prev_pos.lnum = 0;
4310 if (cap->nchar == 'm' || cap->nchar == 'M')
4311 {
4312 if (cap->cmdchar == '[')
4313 findc = '{';
4314 else
4315 findc = '}';
4316 n = 9999;
4317 }
4318 else
4319 {
4320 findc = cap->nchar;
4321 n = cap->count1;
4322 }
4323 for ( ; n > 0; --n)
4324 {
4325 if ((pos = findmatchlimit(cap->oap, findc,
4326 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4327 {
4328 if (new_pos.lnum == 0) // nothing found
4329 {
4330 if (cap->nchar != 'm' && cap->nchar != 'M')
4331 clearopbeep(cap->oap);
4332 }
4333 else
4334 pos = &new_pos; // use last one found
4335 break;
4336 }
4337 prev_pos = new_pos;
4338 curwin->w_cursor = *pos;
4339 new_pos = *pos;
4340 }
4341 curwin->w_cursor = *old_pos;
4342
4343 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4344 // brought us to the match for "[m" and "]M" when inside a method.
4345 // Try finding the '{' or '}' we want to be at.
4346 // Also repeat for the given count.
4347 if (cap->nchar == 'm' || cap->nchar == 'M')
4348 {
4349 // norm is TRUE for "]M" and "[m"
4350 int norm = ((findc == '{') == (cap->nchar == 'm'));
4351
4352 n = cap->count1;
4353 // found a match: we were inside a method
4354 if (prev_pos.lnum != 0)
4355 {
4356 pos = &prev_pos;
4357 curwin->w_cursor = prev_pos;
4358 if (norm)
4359 --n;
4360 }
4361 else
4362 pos = NULL;
4363 while (n > 0)
4364 {
4365 for (;;)
4366 {
4367 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4368 {
4369 // if not found anything, that's an error
4370 if (pos == NULL)
4371 clearopbeep(cap->oap);
4372 n = 0;
4373 break;
4374 }
4375 c = gchar_cursor();
4376 if (c == '{' || c == '}')
4377 {
4378 // Must have found end/start of class: use it.
4379 // Or found the place to be at.
4380 if ((c == findc && norm) || (n == 1 && !norm))
4381 {
4382 new_pos = curwin->w_cursor;
4383 pos = &new_pos;
4384 n = 0;
4385 }
4386 // if no match found at all, we started outside of the
4387 // class and we're inside now. Just go on.
4388 else if (new_pos.lnum == 0)
4389 {
4390 new_pos = curwin->w_cursor;
4391 pos = &new_pos;
4392 }
4393 // found start/end of other method: go to match
4394 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004395 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4396 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004397 n = 0;
4398 else
4399 curwin->w_cursor = *pos;
4400 break;
4401 }
4402 }
4403 --n;
4404 }
4405 curwin->w_cursor = *old_pos;
4406 if (pos == NULL && new_pos.lnum != 0)
4407 clearopbeep(cap->oap);
4408 }
4409 if (pos != NULL)
4410 {
4411 setpcmark();
4412 curwin->w_cursor = *pos;
4413 curwin->w_set_curswant = TRUE;
4414#ifdef FEAT_FOLDING
4415 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4416 && cap->oap->op_type == OP_NOP)
4417 foldOpenCursor();
4418#endif
4419 }
4420}
4421
4422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 * "[" and "]" commands.
4424 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4425 */
4426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004427nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004430 pos_T *pos = NULL; // init for GCC
4431 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 int flag;
4433 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 cap->oap->motion_type = MCHAR;
4436 cap->oap->inclusive = FALSE;
4437 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004438 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004440 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (cap->nchar == 'f')
4442 nv_gotofile(cap);
4443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004446 // Find the occurrence(s) of the identifier or define under cursor
4447 // in current and included files or jump to the first occurrence.
4448 //
4449 // search list jump
4450 // fwd bwd fwd bwd fwd bwd
4451 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4452 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004453 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 char_u *ptr;
4456 int len;
4457
4458 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4459 clearop(cap->oap);
4460 else
4461 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004462 // Make a copy, if the line was changed it will be freed.
4463 ptr = vim_strnsave(ptr, len);
4464 if (ptr == NULL)
4465 return;
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 find_pattern_in_path(ptr, 0, len, TRUE,
4468 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4469 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4470 cap->count1,
4471 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4472 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4473 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4474 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004475 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 curwin->w_set_curswant = TRUE;
4477 }
4478 }
4479 else
4480#endif
4481
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004482 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4483 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4484 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4485 // "[m" or "]m" search for prev/next start of (Java) method.
4486 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if ( (cap->cmdchar == '['
4488 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4489 || (cap->cmdchar == ']'
4490 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004491 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004493 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 else if (cap->nchar == '[' || cap->nchar == ']')
4495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004496 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 flag = '{';
4498 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004502 // Imitate strange Vi behaviour: When using "]]" with an operator
4503 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004504 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 (cap->oap->op_type != OP_NOP
4506 && cap->arg == FORWARD && flag == '{')))
4507 clearopbeep(cap->oap);
4508 else
4509 {
4510 if (cap->oap->op_type == OP_NOP)
4511 beginline(BL_WHITE | BL_FIX);
4512#ifdef FEAT_FOLDING
4513 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4514 foldOpenCursor();
4515#endif
4516 }
4517 }
4518
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004519 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 else if (cap->nchar == 'p' || cap->nchar == 'P')
4521 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004522 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004525 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 else if (cap->nchar == '\'' || cap->nchar == '`')
4527 {
4528 pos = &curwin->w_cursor;
4529 for (n = cap->count1; n > 0; --n)
4530 {
4531 prev_pos = *pos;
4532 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4533 cap->nchar == '\'');
4534 if (pos == NULL)
4535 break;
4536 }
4537 if (pos == NULL)
4538 pos = &prev_pos;
4539 nv_cursormark(cap, cap->nchar == '\'', pos);
4540 }
4541
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004542 // [ or ] followed by a middle mouse click: put selected text with
4543 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004544 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
4546 (void)do_mouse(cap->oap, cap->nchar,
4547 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4548 cap->count1, PUT_FIXINDENT);
4549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004552 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else if (cap->nchar == 'z')
4554 {
4555 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4556 cap->count1) == FAIL)
4557 clearopbeep(cap->oap);
4558 }
4559#endif
4560
4561#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004562 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else if (cap->nchar == 'c')
4564 {
4565 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4566 cap->count1) == FAIL)
4567 clearopbeep(cap->oap);
4568 }
4569#endif
4570
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004571#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004572 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 else if (cap->nchar == 's' || cap->nchar == 'S')
4574 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004575 setpcmark();
4576 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004577 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4578 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004579 {
4580 clearopbeep(cap->oap);
4581 break;
4582 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004583 else
4584 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004585# ifdef FEAT_FOLDING
4586 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4587 foldOpenCursor();
4588# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589 }
4590#endif
4591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004592 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 else
4594 clearopbeep(cap->oap);
4595}
4596
4597/*
4598 * Handle Normal mode "%" command.
4599 */
4600 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004601nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004604#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 linenr_T lnum = curwin->w_cursor.lnum;
4606#endif
4607
4608 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004609 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 if (cap->count0 > 100)
4612 clearopbeep(cap->oap);
4613 else
4614 {
4615 cap->oap->motion_type = MLINE;
4616 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004617 // Round up, so 'normal 100%' always jumps at the line line.
4618 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4619 // overflow on 32-bits, so use a formula with less accuracy
4620 // to avoid overflows.
4621 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4623 / 100L * cap->count0;
4624 else
4625 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4626 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004627 if (curwin->w_cursor.lnum < 1)
4628 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4630 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4631 beginline(BL_SOL | BL_FIX);
4632 }
4633 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004634 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
4636 cap->oap->motion_type = MCHAR;
4637 cap->oap->use_reg_one = TRUE;
4638 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4639 clearopbeep(cap->oap);
4640 else
4641 {
4642 setpcmark();
4643 curwin->w_cursor = *pos;
4644 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 }
4649#ifdef FEAT_FOLDING
4650 if (cap->oap->op_type == OP_NOP
4651 && lnum != curwin->w_cursor.lnum
4652 && (fdo_flags & FDO_PERCENT)
4653 && KeyTyped)
4654 foldOpenCursor();
4655#endif
4656}
4657
4658/*
4659 * Handle "(" and ")" commands.
4660 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4661 */
4662 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004663nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 cap->oap->motion_type = MCHAR;
4666 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004667 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004668 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 curwin->w_set_curswant = TRUE;
4670
4671 if (findsent(cap->arg, cap->count1) == FAIL)
4672 clearopbeep(cap->oap);
4673 else
4674 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004675 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004676 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#ifdef FEAT_FOLDING
4679 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4680 foldOpenCursor();
4681#endif
4682 }
4683}
4684
4685/*
4686 * "m" command: Mark a position.
4687 */
4688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 if (!checkclearop(cap->oap))
4692 {
4693 if (setmark(cap->nchar) == FAIL)
4694 clearopbeep(cap->oap);
4695 }
4696}
4697
4698/*
4699 * "{" and "}" commands.
4700 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4701 */
4702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004703nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 cap->oap->motion_type = MCHAR;
4706 cap->oap->inclusive = FALSE;
4707 cap->oap->use_reg_one = TRUE;
4708 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004709 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 clearopbeep(cap->oap);
4711 else
4712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#ifdef FEAT_FOLDING
4715 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4716 foldOpenCursor();
4717#endif
4718 }
4719}
4720
4721/*
4722 * "u" command: Undo or make lower case.
4723 */
4724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004725nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004727 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004729 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 cap->cmdchar = 'g';
4731 cap->nchar = 'u';
4732 nv_operator(cap);
4733 }
4734 else
4735 nv_kundo(cap);
4736}
4737
4738/*
4739 * <Undo> command.
4740 */
4741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004742nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 if (!checkclearopq(cap->oap))
4745 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004746#ifdef FEAT_JOB_CHANNEL
4747 if (bt_prompt(curbuf))
4748 {
4749 clearopbeep(cap->oap);
4750 return;
4751 }
4752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 u_undo((int)cap->count1);
4754 curwin->w_set_curswant = TRUE;
4755 }
4756}
4757
4758/*
4759 * Handle the "r" command.
4760 */
4761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004762nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 char_u *ptr;
4765 int had_ctrl_v;
4766 long n;
4767
4768 if (checkclearop(cap->oap))
4769 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004770#ifdef FEAT_JOB_CHANNEL
4771 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4772 {
4773 clearopbeep(cap->oap);
4774 return;
4775 }
4776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004778 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (cap->nchar == Ctrl_V)
4780 {
4781 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004782 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (cap->nchar > DEL)
4785 had_ctrl_v = NUL;
4786 }
4787 else
4788 had_ctrl_v = NUL;
4789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004790 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004791 if (IS_SPECIAL(cap->nchar))
4792 {
4793 clearopbeep(cap->oap);
4794 return;
4795 }
4796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004797 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (VIsual_active)
4799 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004800 if (got_int)
4801 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004802 if (had_ctrl_v)
4803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004804 // Use a special (negative) number to make a difference between a
4805 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004806 if (cap->nchar == CAR)
4807 cap->nchar = REPLACE_CR_NCHAR;
4808 else if (cap->nchar == NL)
4809 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 nv_operator(cap);
4812 return;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004815 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (virtual_active())
4817 {
4818 if (u_save_cursor() == FAIL)
4819 return;
4820 if (gchar_cursor() == NUL)
4821 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004822 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 coladvance_force((colnr_T)(getviscol() + cap->count1));
4824 curwin->w_cursor.col -= cap->count1;
4825 }
4826 else if (gchar_cursor() == TAB)
4827 coladvance_force(getviscol());
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004832 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004833 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 clearopbeep(cap->oap);
4836 return;
4837 }
4838
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004839 // Replacing with a TAB is done by edit() when it is complicated because
4840 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4841 // Other characters are done below to avoid problems with things like
4842 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4844 {
4845 stuffnumReadbuff(cap->count1);
4846 stuffcharReadbuff('R');
4847 stuffcharReadbuff('\t');
4848 stuffcharReadbuff(ESC);
4849 return;
4850 }
4851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004852 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (u_save_cursor() == FAIL)
4854 return;
4855
4856 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4857 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004858 // Replace character(s) by a single newline.
4859 // Strange vi behaviour: Only one newline is inserted.
4860 // Delete the characters here.
4861 // Insert the newline with an insert command, takes care of
4862 // autoindent. The insert command depends on being on the last
4863 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004864 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 stuffcharReadbuff('\r');
4866 stuffcharReadbuff(ESC);
4867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 invoke_edit(cap, TRUE, 'r', FALSE);
4870 }
4871 else
4872 {
4873 prep_redo(cap->oap->regname, cap->count1,
4874 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4875
4876 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (has_mbyte)
4878 {
4879 int old_State = State;
4880
4881 if (cap->ncharC1 != 0)
4882 AppendCharToRedobuff(cap->ncharC1);
4883 if (cap->ncharC2 != 0)
4884 AppendCharToRedobuff(cap->ncharC2);
4885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004886 // This is slow, but it handles replacing a single-byte with a
4887 // multi-byte and the other way around. Also handles adding
4888 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 for (n = cap->count1; n > 0; --n)
4890 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004891 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004892 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4893 {
4894 int c = ins_copychar(curwin->w_cursor.lnum
4895 + (cap->nchar == Ctrl_Y ? -1 : 1));
4896 if (c != NUL)
4897 ins_char(c);
4898 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004899 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004900 ++curwin->w_cursor.col;
4901 }
4902 else
4903 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 State = old_State;
4905 if (cap->ncharC1 != 0)
4906 ins_char(cap->ncharC1);
4907 if (cap->ncharC2 != 0)
4908 ins_char(cap->ncharC2);
4909 }
4910 }
4911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004913 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 for (n = cap->count1; n > 0; --n)
4915 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004916 // Get ptr again, because u_save and/or showmatch() will have
4917 // released the line. This may also happen in ins_copychar().
4918 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004919 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4920 {
4921 int c = ins_copychar(curwin->w_cursor.lnum
4922 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004923
4924 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004925 if (c != NUL)
4926 ptr[curwin->w_cursor.col] = c;
4927 }
4928 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004929 {
4930 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004931 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (p_sm && msg_silent == 0)
4934 showmatch(cap->nchar);
4935 ++curwin->w_cursor.col;
4936 }
4937#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004938 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004940 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar009b2592004-10-24 19:18:58 +00004942 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004943 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004945 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947#endif
4948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004949 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 changed_bytes(curwin->w_cursor.lnum,
4951 (colnr_T)(curwin->w_cursor.col - cap->count1));
4952 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 --curwin->w_cursor.col; // cursor on the last replaced char
4954 // if the character on the left of the current cursor is a multi-byte
4955 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (has_mbyte)
4957 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 curbuf->b_op_end = curwin->w_cursor;
4959 curwin->w_set_curswant = TRUE;
4960 set_last_insert(cap->nchar);
4961 }
4962}
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964/*
4965 * 'o': Exchange start and end of Visual area.
4966 * 'O': same, but in block mode exchange left and right corners.
4967 */
4968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004969v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
4971 pos_T old_cursor;
4972 colnr_T left, right;
4973
4974 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4975 {
4976 old_cursor = curwin->w_cursor;
4977 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4978 curwin->w_cursor.lnum = VIsual.lnum;
4979 coladvance(left);
4980 VIsual = curwin->w_cursor;
4981
4982 curwin->w_cursor.lnum = old_cursor.lnum;
4983 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004984 // 'selection "exclusive" and cursor at right-bottom corner: move it
4985 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4987 ++curwin->w_curswant;
4988 coladvance(curwin->w_curswant);
4989 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004991 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 curwin->w_cursor.lnum = VIsual.lnum;
4994 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4995 ++right;
4996 coladvance(right);
4997 VIsual = curwin->w_cursor;
4998
4999 curwin->w_cursor.lnum = old_cursor.lnum;
5000 coladvance(left);
5001 curwin->w_curswant = left;
5002 }
5003 }
5004 else
5005 {
5006 old_cursor = curwin->w_cursor;
5007 curwin->w_cursor = VIsual;
5008 VIsual = old_cursor;
5009 curwin->w_set_curswant = TRUE;
5010 }
5011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013/*
5014 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5015 */
5016 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005017nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005019 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
5021 cap->cmdchar = 'c';
5022 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005023 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 VIsual_mode = 'V';
5025 nv_operator(cap);
5026 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005027 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005030 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 else
5032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (virtual_active())
5034 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5036 }
5037 }
5038}
5039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040/*
5041 * "gr".
5042 */
5043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005044nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (VIsual_active)
5047 {
5048 cap->cmdchar = 'r';
5049 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005050 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005052 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005055 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 else
5057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005058 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005059 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 stuffcharReadbuff(cap->extra_char);
5061 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (virtual_active())
5063 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 invoke_edit(cap, TRUE, 'v', FALSE);
5065 }
5066 }
5067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069/*
5070 * Swap case for "~" command, when it does not work like an operator.
5071 */
5072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005073n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
5075 long n;
5076 pos_T startpos;
5077 int did_change = 0;
5078#ifdef FEAT_NETBEANS_INTG
5079 pos_T pos;
5080 char_u *ptr;
5081 int count;
5082#endif
5083
5084 if (checkclearopq(cap->oap))
5085 return;
5086
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005087 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 clearopbeep(cap->oap);
5090 return;
5091 }
5092
5093 prep_redo_cmd(cap);
5094
5095 if (u_save_cursor() == FAIL)
5096 return;
5097
5098 startpos = curwin->w_cursor;
5099#ifdef FEAT_NETBEANS_INTG
5100 pos = startpos;
5101#endif
5102 for (n = cap->count1; n > 0; --n)
5103 {
5104 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5105 inc_cursor();
5106 if (gchar_cursor() == NUL)
5107 {
5108 if (vim_strchr(p_ww, '~') != NULL
5109 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5110 {
5111#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005112 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 if (did_change)
5115 {
5116 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005117 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005118 netbeans_removed(curbuf, pos.lnum, pos.col,
5119 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005120 // line may have been flushed, get it again
5121 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005123 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 pos.col = 0;
5126 pos.lnum++;
5127 }
5128#endif
5129 ++curwin->w_cursor.lnum;
5130 curwin->w_cursor.col = 0;
5131 if (n > 1)
5132 {
5133 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5134 break;
5135 u_clearline();
5136 }
5137 }
5138 else
5139 break;
5140 }
5141 }
5142#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005143 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 ptr = ml_get(pos.lnum);
5146 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005147 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5148 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150#endif
5151
5152
5153 check_cursor();
5154 curwin->w_set_curswant = TRUE;
5155 if (did_change)
5156 {
5157 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5158 0L);
5159 curbuf->b_op_start = startpos;
5160 curbuf->b_op_end = curwin->w_cursor;
5161 if (curbuf->b_op_end.col > 0)
5162 --curbuf->b_op_end.col;
5163 }
5164}
5165
5166/*
5167 * Move cursor to mark.
5168 */
5169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 if (check_mark(pos) == FAIL)
5173 clearop(cap->oap);
5174 else
5175 {
5176 if (cap->cmdchar == '\''
5177 || cap->cmdchar == '`'
5178 || cap->cmdchar == '['
5179 || cap->cmdchar == ']')
5180 setpcmark();
5181 curwin->w_cursor = *pos;
5182 if (flag)
5183 beginline(BL_WHITE | BL_FIX);
5184 else
5185 check_cursor();
5186 }
5187 cap->oap->motion_type = flag ? MLINE : MCHAR;
5188 if (cap->cmdchar == '`')
5189 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005190 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 curwin->w_set_curswant = TRUE;
5192}
5193
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194/*
5195 * Handle commands that are operators in Visual mode.
5196 */
5197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 // Uppercase means linewise, except in block mode, then "D" deletes till
5203 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (isupper(cap->cmdchar))
5205 {
5206 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005207 {
5208 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5212 curwin->w_curswant = MAXCOL;
5213 }
5214 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5215 nv_operator(cap);
5216}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218/*
5219 * "s" and "S" commands.
5220 */
5221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005224#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005225 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005226 if (term_swap_diff() == OK)
5227 return;
5228#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005229#ifdef FEAT_JOB_CHANNEL
5230 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5231 {
5232 clearopbeep(cap->oap);
5233 return;
5234 }
5235#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005236 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005239 {
5240 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 cap->cmdchar = 'c';
5244 nv_operator(cap);
5245 }
5246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 nv_optrans(cap);
5248}
5249
5250/*
5251 * Abbreviated commands.
5252 */
5253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005257 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005259 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (VIsual_active)
5261 v_visop(cap);
5262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 nv_optrans(cap);
5264}
5265
5266/*
5267 * Translate a command into another command.
5268 */
5269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005270nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5273 (char_u *)"d$", (char_u *)"c$",
5274 (char_u *)"cl", (char_u *)"cc",
5275 (char_u *)"yy", (char_u *)":s\r"};
5276 static char_u *str = (char_u *)"xXDCsSY&";
5277
5278 if (!checkclearopq(cap->oap))
5279 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005280 // In Vi "2D" doesn't delete the next line. Can't translate it
5281 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005282 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5283 {
5284 cap->oap->start = curwin->w_cursor;
5285 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005286#ifdef FEAT_EVAL
5287 set_op_var(OP_DELETE);
5288#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005289 cap->count1 = 1;
5290 nv_dollar(cap);
5291 finish_op = TRUE;
5292 ResetRedobuff();
5293 AppendCharToRedobuff('D');
5294 }
5295 else
5296 {
5297 if (cap->count0)
5298 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005299 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302 cap->opcount = 0;
5303}
5304
5305/*
5306 * "'" and "`" commands. Also for "g'" and "g`".
5307 * cap->arg is TRUE for "'" and "g'".
5308 */
5309 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005310nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
5312 pos_T *pos;
5313 int c;
5314#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005315 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005316 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
5318
5319 if (cap->cmdchar == 'g')
5320 c = cap->extra_char;
5321 else
5322 c = cap->nchar;
5323 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005324 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 if (cap->arg)
5327 {
5328 check_cursor_lnum();
5329 beginline(BL_WHITE | BL_FIX);
5330 }
5331 else
5332 check_cursor();
5333 }
5334 else
5335 nv_cursormark(cap, cap->arg, pos);
5336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005337 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (!virtual_active())
5339 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005340 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#ifdef FEAT_FOLDING
5342 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005343 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005344 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 && (fdo_flags & FDO_MARK)
5346 && old_KeyTyped)
5347 foldOpenCursor();
5348#endif
5349}
5350
5351/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005352 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 */
5354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005358#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005360 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 if (!checkclearopq(cap->oap))
5364 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005365 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5366 {
5367 if (goto_tabpage_lastused() == FAIL)
5368 clearopbeep(cap->oap);
5369 return;
5370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (cap->cmdchar == 'g')
5372 pos = movechangelist((int)cap->count1);
5373 else
5374 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005375 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 curwin->w_set_curswant = TRUE;
5378 check_cursor();
5379 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005380 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 nv_cursormark(cap, FALSE, pos);
5382 else if (cap->cmdchar == 'g')
5383 {
5384 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005385 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005387 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005389 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 else
5392 clearopbeep(cap->oap);
5393# ifdef FEAT_FOLDING
5394 if (cap->oap->op_type == OP_NOP
5395 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5396 && (fdo_flags & FDO_MARK)
5397 && old_KeyTyped)
5398 foldOpenCursor();
5399# endif
5400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401}
5402
5403/*
5404 * Handle '"' command.
5405 */
5406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005407nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 if (checkclearop(cap->oap))
5410 return;
5411#ifdef FEAT_EVAL
5412 if (cap->nchar == '=')
5413 cap->nchar = get_expr_register();
5414#endif
5415 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5416 {
5417 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005418 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#ifdef FEAT_EVAL
5420 set_reg_var(cap->oap->regname);
5421#endif
5422 }
5423 else
5424 clearopbeep(cap->oap);
5425}
5426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427/*
5428 * Handle "v", "V" and "CTRL-V" commands.
5429 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5430 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005431 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 */
5433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005434nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005436 if (cap->cmdchar == Ctrl_Q)
5437 cap->cmdchar = Ctrl_V;
5438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005439 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5440 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (cap->oap->op_type != OP_NOP)
5442 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005443 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return;
5446 }
5447
5448 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005449 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005451 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 else // toggle char/block mode
5454 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 VIsual_mode = cap->cmdchar;
5456 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005457 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005459 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005464 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 VIsual = curwin->w_cursor;
5468
5469 VIsual_active = TRUE;
5470 VIsual_reselect = TRUE;
5471 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005472 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005475 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005476 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005477 // For V and ^V, we multiply the number of lines even if there
5478 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5480 {
5481 curwin->w_cursor.lnum +=
5482 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005483 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485 VIsual_mode = resel_VIsual_mode;
5486 if (VIsual_mode == 'v')
5487 {
5488 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005489 {
5490 validate_virtcol();
5491 curwin->w_curswant = curwin->w_virtcol
5492 + resel_VIsual_vcol * cap->count0 - 1;
5493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005495 curwin->w_curswant = resel_VIsual_vcol;
5496 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005498 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
5500 curwin->w_curswant = MAXCOL;
5501 coladvance((colnr_T)MAXCOL);
5502 }
5503 else if (VIsual_mode == Ctrl_V)
5504 {
5505 validate_virtcol();
5506 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005507 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 coladvance(curwin->w_curswant);
5509 }
5510 else
5511 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005512 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 }
5514 else
5515 {
5516 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005517 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 may_start_select('c');
5519 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005520 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005522 if (cap->count0 > 0 && --cap->count1 > 0)
5523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005525 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5526 nv_right(cap);
5527 else if (VIsual_mode == 'V')
5528 nv_down(cap);
5529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531 }
5532}
5533
5534/*
5535 * Start selection for Shift-movement keys.
5536 */
5537 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005538start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005540 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 may_start_select('k');
5542 n_start_visual_mode('v');
5543}
5544
5545/*
5546 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005547 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 */
5549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005550may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005552 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5553 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554}
5555
5556/*
5557 * Start Visual mode "c".
5558 * Should set VIsual_select before calling this.
5559 */
5560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005561n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005563#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005564 int cursor_line_was_concealed = curwin->w_p_cole > 0
5565 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005566#endif
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 VIsual_mode = c;
5569 VIsual_active = TRUE;
5570 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005571
5572 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005573 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005574 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005575 {
5576 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 VIsual = curwin->w_cursor;
5580
5581#ifdef FEAT_FOLDING
5582 foldAdjustVisual();
5583#endif
5584
LemonBoy2bf52dd2022-04-09 18:17:34 +01005585 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005587#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005588 // Check if redraw is needed after changing the state.
5589 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005590#endif
5591
Bram Moolenaar09df3122006-01-23 22:23:09 +00005592 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005595 // Make sure the clipboard gets updated. Needed because start and
5596 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 clip_star.vmode = NUL;
5598#endif
5599
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005600 // Only need to redraw this line, unless still need to redraw an old
5601 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005602 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5605 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5606 }
5607}
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
5610/*
5611 * CTRL-W: Window commands
5612 */
5613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005614nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005616 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005617 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005618 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005619 cap->cmdchar = ':';
5620 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005621 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005622 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005623 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005624 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626
5627/*
5628 * CTRL-Z: Suspend
5629 */
5630 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005631nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005635 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005636 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637}
5638
5639/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005640 * "gv": Reselect the previous Visual area. If Visual already active,
5641 * exchange previous and current Visual area.
5642 */
5643 static void
5644nv_gv_cmd(cmdarg_T *cap)
5645{
5646 pos_T tpos;
5647 int i;
5648
5649 if (checkclearop(cap->oap))
5650 return;
5651
5652 if (curbuf->b_visual.vi_start.lnum == 0
5653 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5654 || curbuf->b_visual.vi_end.lnum == 0)
5655 {
5656 beep_flush();
5657 return;
5658 }
5659
5660 // set w_cursor to the start of the Visual area, tpos to the end
5661 if (VIsual_active)
5662 {
5663 i = VIsual_mode;
5664 VIsual_mode = curbuf->b_visual.vi_mode;
5665 curbuf->b_visual.vi_mode = i;
5666# ifdef FEAT_EVAL
5667 curbuf->b_visual_mode_eval = i;
5668# endif
5669 i = curwin->w_curswant;
5670 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5671 curbuf->b_visual.vi_curswant = i;
5672
5673 tpos = curbuf->b_visual.vi_end;
5674 curbuf->b_visual.vi_end = curwin->w_cursor;
5675 curwin->w_cursor = curbuf->b_visual.vi_start;
5676 curbuf->b_visual.vi_start = VIsual;
5677 }
5678 else
5679 {
5680 VIsual_mode = curbuf->b_visual.vi_mode;
5681 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5682 tpos = curbuf->b_visual.vi_end;
5683 curwin->w_cursor = curbuf->b_visual.vi_start;
5684 }
5685
5686 VIsual_active = TRUE;
5687 VIsual_reselect = TRUE;
5688
5689 // Set Visual to the start and w_cursor to the end of the Visual
5690 // area. Make sure they are on an existing character.
5691 check_cursor();
5692 VIsual = curwin->w_cursor;
5693 curwin->w_cursor = tpos;
5694 check_cursor();
5695 update_topline();
5696
5697 // When called from normal "g" command: start Select mode when
5698 // 'selectmode' contains "cmd". When called for K_SELECT, always
5699 // start Select mode.
5700 if (cap->arg)
5701 {
5702 VIsual_select = TRUE;
5703 VIsual_select_reg = 0;
5704 }
5705 else
5706 may_start_select('c');
5707 setmouse();
5708#ifdef FEAT_CLIPBOARD
5709 // Make sure the clipboard gets updated. Needed because start and
5710 // end are still the same, and the selection needs to be owned
5711 clip_star.vmode = NUL;
5712#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005713 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005714 showmode();
5715}
5716
5717/*
5718 * "g0", "g^" : Like "0" and "^" but for screen lines.
5719 * "gm": middle of "g0" and "g$".
5720 */
5721 static void
5722nv_g_home_m_cmd(cmdarg_T *cap)
5723{
5724 int i;
5725 int flag = FALSE;
5726
5727 if (cap->nchar == '^')
5728 flag = TRUE;
5729
5730 cap->oap->motion_type = MCHAR;
5731 cap->oap->inclusive = FALSE;
5732 if (curwin->w_p_wrap && curwin->w_width != 0)
5733 {
5734 int width1 = curwin->w_width - curwin_col_off();
5735 int width2 = width1 + curwin_col_off2();
5736
5737 validate_virtcol();
5738 i = 0;
5739 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5740 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5741 }
5742 else
5743 i = curwin->w_leftcol;
5744 // Go to the middle of the screen line. When 'number' or
5745 // 'relativenumber' is on and lines are wrapping the middle can be more
5746 // to the left.
5747 if (cap->nchar == 'm')
5748 i += (curwin->w_width - curwin_col_off()
5749 + ((curwin->w_p_wrap && i > 0)
5750 ? curwin_col_off2() : 0)) / 2;
5751 coladvance((colnr_T)i);
5752 if (flag)
5753 {
5754 do
5755 i = gchar_cursor();
5756 while (VIM_ISWHITE(i) && oneright() == OK);
5757 curwin->w_valid &= ~VALID_WCOL;
5758 }
5759 curwin->w_set_curswant = TRUE;
5760}
5761
5762/*
5763 * "g_": to the last non-blank character in the line or <count> lines
5764 * downward.
5765 */
5766 static void
5767nv_g_underscore_cmd(cmdarg_T *cap)
5768{
5769 char_u *ptr;
5770
5771 cap->oap->motion_type = MCHAR;
5772 cap->oap->inclusive = TRUE;
5773 curwin->w_curswant = MAXCOL;
5774 if (cursor_down((long)(cap->count1 - 1),
5775 cap->oap->op_type == OP_NOP) == FAIL)
5776 {
5777 clearopbeep(cap->oap);
5778 return;
5779 }
5780
5781 ptr = ml_get_curline();
5782
5783 // In Visual mode we may end up after the line.
5784 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5785 --curwin->w_cursor.col;
5786
5787 // Decrease the cursor column until it's on a non-blank.
5788 while (curwin->w_cursor.col > 0
5789 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5790 --curwin->w_cursor.col;
5791 curwin->w_set_curswant = TRUE;
5792 adjust_for_sel(cap);
5793}
5794
5795/*
5796 * "g$" : Like "$" but for screen lines.
5797 */
5798 static void
5799nv_g_dollar_cmd(cmdarg_T *cap)
5800{
5801 oparg_T *oap = cap->oap;
5802 int i;
5803 int col_off = curwin_col_off();
5804
5805 oap->motion_type = MCHAR;
5806 oap->inclusive = TRUE;
5807 if (curwin->w_p_wrap && curwin->w_width != 0)
5808 {
5809 curwin->w_curswant = MAXCOL; // so we stay at the end
5810 if (cap->count1 == 1)
5811 {
5812 int width1 = curwin->w_width - col_off;
5813 int width2 = width1 + curwin_col_off2();
5814
5815 validate_virtcol();
5816 i = width1 - 1;
5817 if (curwin->w_virtcol >= (colnr_T)width1)
5818 i += ((curwin->w_virtcol - width1) / width2 + 1)
5819 * width2;
5820 coladvance((colnr_T)i);
5821
5822 // Make sure we stick in this column.
5823 validate_virtcol();
5824 curwin->w_curswant = curwin->w_virtcol;
5825 curwin->w_set_curswant = FALSE;
5826 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5827 {
5828 // Check for landing on a character that got split at
5829 // the end of the line. We do not want to advance to
5830 // the next screen line.
5831 if (curwin->w_virtcol > (colnr_T)i)
5832 --curwin->w_cursor.col;
5833 }
5834 }
5835 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5836 clearopbeep(oap);
5837 }
5838 else
5839 {
5840 if (cap->count1 > 1)
5841 // if it fails, let the cursor still move to the last char
5842 (void)cursor_down(cap->count1 - 1, FALSE);
5843
5844 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5845 coladvance((colnr_T)i);
5846
5847 // if the character doesn't fit move one back
5848 if (curwin->w_cursor.col > 0
5849 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5850 {
5851 colnr_T vcol;
5852
5853 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5854 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5855 --curwin->w_cursor.col;
5856 }
5857
5858 // Make sure we stick in this column.
5859 validate_virtcol();
5860 curwin->w_curswant = curwin->w_virtcol;
5861 curwin->w_set_curswant = FALSE;
5862 }
5863}
5864
5865/*
5866 * "gi": start Insert at the last position.
5867 */
5868 static void
5869nv_gi_cmd(cmdarg_T *cap)
5870{
5871 int i;
5872
5873 if (curbuf->b_last_insert.lnum != 0)
5874 {
5875 curwin->w_cursor = curbuf->b_last_insert;
5876 check_cursor_lnum();
5877 i = (int)STRLEN(ml_get_curline());
5878 if (curwin->w_cursor.col > (colnr_T)i)
5879 {
5880 if (virtual_active())
5881 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5882 curwin->w_cursor.col = i;
5883 }
5884 }
5885 cap->cmdchar = 'i';
5886 nv_edit(cap);
5887}
5888
5889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 * Commands starting with "g".
5891 */
5892 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005893nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 switch (cap->nchar)
5899 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005900 case Ctrl_A:
5901 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005903 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005904 if (!VIsual_active && cap->nchar == Ctrl_A)
5905 vim_mem_profile_dump();
5906 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005908 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005909 if (VIsual_active)
5910 {
5911 cap->arg = TRUE;
5912 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005913 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005914 nv_addsub(cap);
5915 }
5916 else
5917 clearopbeep(oap);
5918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005920 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 case 'R':
5922 cap->arg = TRUE;
5923 nv_Replace(cap);
5924 break;
5925
5926 case 'r':
5927 nv_vreplace(cap);
5928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
5930 case '&':
5931 do_cmdline_cmd((char_u *)"%s//~/&");
5932 break;
5933
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005934 // "gv": Reselect the previous Visual area. If Visual already active,
5935 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005937 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005939
5940 // "gV": Don't reselect the previous Visual area after a Select mode
5941 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case 'V':
5943 VIsual_reselect = FALSE;
5944 break;
5945
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005946 // "gh": start Select mode.
5947 // "gH": start Select line mode.
5948 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case K_BS:
5950 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005951 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 case 'h':
5953 case 'H':
5954 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 cap->cmdchar = cap->nchar + ('v' - 'h');
5956 cap->arg = TRUE;
5957 nv_visual(cap);
5958 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005960 // "gn", "gN" visually select next/previous search match
5961 // "gn" selects next match
5962 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005963 case 'N':
5964 case 'n':
5965 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005966 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005969 // "gj" and "gk" two new funny movement keys -- up and down
5970 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 case 'j':
5972 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005973 // with 'nowrap' it works just like the normal "j" command.
5974 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {
5976 oap->motion_type = MLINE;
5977 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5978 }
5979 else
5980 i = nv_screengo(oap, FORWARD, cap->count1);
5981 if (i == FAIL)
5982 clearopbeep(oap);
5983 break;
5984
5985 case 'k':
5986 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005987 // with 'nowrap' it works just like the normal "k" command.
5988 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {
5990 oap->motion_type = MLINE;
5991 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5992 }
5993 else
5994 i = nv_screengo(oap, BACKWARD, cap->count1);
5995 if (i == FAIL)
5996 clearopbeep(oap);
5997 break;
5998
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005999 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 case 'J':
6001 nv_join(cap);
6002 break;
6003
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006004 // "g0", "g^" : Like "0" and "^" but for screen lines.
6005 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 case '0':
6008 case 'm':
6009 case K_HOME:
6010 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006011 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 break;
6013
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006014 case 'M':
6015 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006016 oap->motion_type = MCHAR;
6017 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006018 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006019 if (cap->count0 > 0 && cap->count0 <= 100)
6020 coladvance((colnr_T)(i * cap->count0 / 100));
6021 else
6022 coladvance((colnr_T)(i / 2));
6023 curwin->w_set_curswant = TRUE;
6024 }
6025 break;
6026
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006027 // "g_": to the last non-blank character in the line or <count> lines
6028 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 break;
6032
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 case '$':
6035 case K_END:
6036 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 break;
6039
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006040 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 case '*':
6042 case '#':
6043#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006044 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006046 case Ctrl_RSB: // :tag or :tselect for current identifier
6047 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 nv_ident(cap);
6049 break;
6050
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006051 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 case 'e':
6053 case 'E':
6054 oap->motion_type = MCHAR;
6055 curwin->w_set_curswant = TRUE;
6056 oap->inclusive = TRUE;
6057 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6058 clearopbeep(oap);
6059 break;
6060
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006061 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006063 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 break;
6065
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006066 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006068 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 break;
6070
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006071 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 case 'I':
6073 beginline(0);
6074 if (!checkclearopq(oap))
6075 invoke_edit(cap, FALSE, 'g', FALSE);
6076 break;
6077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "gf": goto file, edit file under cursor
6079 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006081 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 nv_gotofile(cap);
6083 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006085 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 case '\'':
6087 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006088 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 case '`':
6090 nv_gomark(cap);
6091 break;
6092
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006093 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006095 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 break;
6097
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006098 // "ga": Display the ascii value of the character under the
6099 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 case 'a':
6101 do_ascii(NULL);
6102 break;
6103
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006104 // "g8": Display the bytes used for the UTF-8 character under the
6105 // cursor. It is displayed in hex.
6106 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006108 if (cap->count0 == 8)
6109 utf_find_illegal();
6110 else
6111 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006114 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006115 case '<':
6116 show_sb_text();
6117 break;
6118
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006119 // "gg": Goto the first line in file. With a count it goes to
6120 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 case 'g':
6122 cap->arg = FALSE;
6123 nv_goto(cap);
6124 break;
6125
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006126 // Two-character operators:
6127 // "gq" Format text
6128 // "gw" Format text and keep cursor position
6129 // "g~" Toggle the case of the text.
6130 // "gu" Change text to lower case.
6131 // "gU" Change text to upper case.
6132 // "g?" rot13 encoding
6133 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 case 'q':
6135 case 'w':
6136 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006137 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 case '~':
6139 case 'u':
6140 case 'U':
6141 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006142 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 nv_operator(cap);
6144 break;
6145
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006146 // "gd": Find first occurrence of pattern under the cursor in the
6147 // current function
6148 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 case 'd':
6150 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006151 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 break;
6153
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006154 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 case K_MIDDLEMOUSE:
6156 case K_MIDDLEDRAG:
6157 case K_MIDDLERELEASE:
6158 case K_LEFTMOUSE:
6159 case K_LEFTDRAG:
6160 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006161 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 case K_RIGHTMOUSE:
6163 case K_RIGHTDRAG:
6164 case K_RIGHTRELEASE:
6165 case K_X1MOUSE:
6166 case K_X1DRAG:
6167 case K_X1RELEASE:
6168 case K_X2MOUSE:
6169 case K_X2DRAG:
6170 case K_X2RELEASE:
6171 mod_mask = MOD_MASK_CTRL;
6172 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175 case K_IGNORE:
6176 break;
6177
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006178 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 case 'p':
6180 case 'P':
6181 nv_put(cap);
6182 break;
6183
6184#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006185 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 case 'o':
6187 goto_byte(cap->count0);
6188 break;
6189#endif
6190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006191 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006193 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 do_exmode(TRUE);
6195 break;
6196
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 case ',':
6198 nv_pcmark(cap);
6199 break;
6200
6201 case ';':
6202 cap->count1 = -cap->count1;
6203 nv_pcmark(cap);
6204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006207 if (!checkclearop(oap))
6208 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006209 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006210 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006211 if (!checkclearop(oap))
6212 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006213 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214
Bram Moolenaar62a23252020-08-09 14:04:42 +02006215 case TAB:
6216 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6217 clearopbeep(oap);
6218 break;
6219
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006220 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006221 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006222 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006223 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006224 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006225 break;
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 default:
6228 clearopbeep(oap);
6229 break;
6230 }
6231}
6232
6233/*
6234 * Handle "o" and "O" commands.
6235 */
6236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006237n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006239#ifdef FEAT_CONCEAL
6240 linenr_T oldline = curwin->w_cursor.lnum;
6241#endif
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (!checkclearopq(cap->oap))
6244 {
6245#ifdef FEAT_FOLDING
6246 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006247 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 (void)hasFolding(curwin->w_cursor.lnum,
6249 &curwin->w_cursor.lnum, NULL);
6250 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006251 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 (void)hasFolding(curwin->w_cursor.lnum,
6253 NULL, &curwin->w_cursor.lnum);
6254#endif
6255 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6256 (cap->cmdchar == 'O' ? 1 : 0)),
6257 (linenr_T)(curwin->w_cursor.lnum +
6258 (cap->cmdchar == 'o' ? 1 : 0))
6259 ) == OK
6260 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006261 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006262 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006264#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006265 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006266 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006267#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006268#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006269 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006271 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006272#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006273 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006274 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6275 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6277 }
6278 }
6279}
6280
6281/*
6282 * "." command: redo last change.
6283 */
6284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006285nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 if (!checkclearopq(cap->oap))
6288 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006289 // If "restart_edit" is TRUE, the last but one command is repeated
6290 // instead of the last command (inserting text). This is used for
6291 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6293 clearopbeep(cap->oap);
6294 }
6295}
6296
6297/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006298 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 */
6300 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006301nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006303 if (VIsual_select && VIsual_active)
6304 {
6305 int reg;
6306 // Get register name
6307 ++no_mapping;
6308 ++allow_keys;
6309 reg = plain_vgetc();
6310 LANGMAP_ADJUST(reg, TRUE);
6311 --no_mapping;
6312 --allow_keys;
6313
6314 if (reg == '"')
6315 // the unnamed register is 0
6316 reg = 0;
6317
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006318 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006319 return;
6320 }
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (!checkclearopq(cap->oap))
6323 {
6324 u_redo((int)cap->count1);
6325 curwin->w_set_curswant = TRUE;
6326 }
6327}
6328
6329/*
6330 * Handle "U" command.
6331 */
6332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006333nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006335 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006336 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006338 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 cap->cmdchar = 'g';
6340 cap->nchar = 'U';
6341 nv_operator(cap);
6342 }
6343 else if (!checkclearopq(cap->oap))
6344 {
6345 u_undoline();
6346 curwin->w_set_curswant = TRUE;
6347 }
6348}
6349
6350/*
6351 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6352 * single character.
6353 */
6354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006355nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006357 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006358 {
6359#ifdef FEAT_JOB_CHANNEL
6360 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6361 {
6362 clearopbeep(cap->oap);
6363 return;
6364 }
6365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 else
6369 nv_operator(cap);
6370}
6371
6372/*
6373 * Handle an operator command.
6374 * The actual work is done by do_pending_operator().
6375 */
6376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 int op_type;
6380
6381 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006382#ifdef FEAT_JOB_CHANNEL
6383 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6384 {
6385 clearopbeep(cap->oap);
6386 return;
6387 }
6388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006390 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 nv_lineop(cap);
6392 else if (!checkclearop(cap->oap))
6393 {
6394 cap->oap->start = curwin->w_cursor;
6395 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006396#ifdef FEAT_EVAL
6397 set_op_var(op_type);
6398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400}
6401
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006402#ifdef FEAT_EVAL
6403/*
6404 * Set v:operator to the characters for "optype".
6405 */
6406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006407set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006408{
6409 char_u opchars[3];
6410
6411 if (optype == OP_NOP)
6412 set_vim_var_string(VV_OP, NULL, 0);
6413 else
6414 {
6415 opchars[0] = get_op_char(optype);
6416 opchars[1] = get_extra_op_char(optype);
6417 opchars[2] = NUL;
6418 set_vim_var_string(VV_OP, opchars, -1);
6419 }
6420}
6421#endif
6422
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423/*
6424 * Handle linewise operator "dd", "yy", etc.
6425 *
6426 * "_" is is a strange motion command that helps make operators more logical.
6427 * It is actually implemented, but not documented in the real Vi. This motion
6428 * command actually refers to "the current line". Commands like "dd" and "yy"
6429 * are really an alternate form of "d_" and "y_". It does accept a count, so
6430 * "d3_" works to delete 3 lines.
6431 */
6432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006433nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 cap->oap->motion_type = MLINE;
6436 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6437 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006439 && cap->oap->motion_force != 'v'
6440 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 || cap->oap->op_type == OP_LSHIFT
6442 || cap->oap->op_type == OP_RSHIFT)
6443 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006444 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 beginline(BL_WHITE | BL_FIX);
6446}
6447
6448/*
6449 * <Home> command.
6450 */
6451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006452nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006454 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006455 if (mod_mask & MOD_MASK_CTRL)
6456 nv_goto(cap);
6457 else
6458 {
6459 cap->count0 = 1;
6460 nv_pipe(cap);
6461 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006462 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6463 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465
6466/*
6467 * "|" command.
6468 */
6469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006470nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 cap->oap->motion_type = MCHAR;
6473 cap->oap->inclusive = FALSE;
6474 beginline(0);
6475 if (cap->count0 > 0)
6476 {
6477 coladvance((colnr_T)(cap->count0 - 1));
6478 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6479 }
6480 else
6481 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006482 // keep curswant at the column where we wanted to go, not where
6483 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 curwin->w_set_curswant = FALSE;
6485}
6486
6487/*
6488 * Handle back-word command "b" and "B".
6489 * cap->arg is 1 for "B"
6490 */
6491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006492nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 cap->oap->motion_type = MCHAR;
6495 cap->oap->inclusive = FALSE;
6496 curwin->w_set_curswant = TRUE;
6497 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6498 clearopbeep(cap->oap);
6499#ifdef FEAT_FOLDING
6500 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6501 foldOpenCursor();
6502#endif
6503}
6504
6505/*
6506 * Handle word motion commands "e", "E", "w" and "W".
6507 * cap->arg is TRUE for "E" and "W".
6508 */
6509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006510nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 int n;
6513 int word_end;
6514 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006515 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006517 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6519 word_end = TRUE;
6520 else
6521 word_end = FALSE;
6522 cap->oap->inclusive = word_end;
6523
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006524 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (!word_end && cap->oap->op_type == OP_CHANGE)
6526 {
6527 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006530 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006532 // Reproduce a funny Vi behaviour: "cw" on a blank only
6533 // changes one character, not all blanks until the start of
6534 // the next word. Only do this when the 'w' flag is included
6535 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6537 {
6538 cap->oap->inclusive = TRUE;
6539 cap->oap->motion_type = MCHAR;
6540 return;
6541 }
6542 }
6543 else
6544 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006545 // This is a little strange. To match what the real Vi does,
6546 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6547 // that we are not on a space or a TAB. This seems impolite
6548 // at first, but it's really more what we mean when we say
6549 // 'cw'.
6550 // Another strangeness: When standing on the end of a word
6551 // "ce" will change until the end of the next word, but "cw"
6552 // will change only one character! This is done by setting
6553 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 cap->oap->inclusive = TRUE;
6555 word_end = TRUE;
6556 flag = TRUE;
6557 }
6558 }
6559 }
6560
6561 cap->oap->motion_type = MCHAR;
6562 curwin->w_set_curswant = TRUE;
6563 if (word_end)
6564 n = end_word(cap->count1, cap->arg, flag, FALSE);
6565 else
6566 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006568 // Don't leave the cursor on the NUL past the end of line. Unless we
6569 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006570 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006571 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 if (n == FAIL && cap->oap->op_type == OP_NOP)
6574 clearopbeep(cap->oap);
6575 else
6576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578#ifdef FEAT_FOLDING
6579 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6580 foldOpenCursor();
6581#endif
6582 }
6583}
6584
6585/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006586 * Used after a movement command: If the cursor ends up on the NUL after the
6587 * end of the line, may move it back to the last character and make the motion
6588 * inclusive.
6589 */
6590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006591adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006592{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006593 // The cursor cannot remain on the NUL when:
6594 // - the column is > 0
6595 // - not in Visual mode or 'selection' is "o"
6596 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006598 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006599 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006600 {
6601 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006603 if (has_mbyte)
6604 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006605 oap->inclusive = TRUE;
6606 }
6607}
6608
6609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 * "0" and "^" commands.
6611 * cap->arg is the argument for beginline().
6612 */
6613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 cap->oap->motion_type = MCHAR;
6617 cap->oap->inclusive = FALSE;
6618 beginline(cap->arg);
6619#ifdef FEAT_FOLDING
6620 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6621 foldOpenCursor();
6622#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006623 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6624 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
6628 * In exclusive Visual mode, may include the last character.
6629 */
6630 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006631adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006634 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (has_mbyte)
6637 inc_cursor();
6638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 ++curwin->w_cursor.col;
6640 cap->oap->inclusive = FALSE;
6641 }
6642}
6643
6644/*
6645 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6646 * Should check VIsual_mode before calling this.
6647 * Returns TRUE when backed up to the previous line.
6648 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006650unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 pos_T *pp;
6653
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006654 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006656 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 pp = &curwin->w_cursor;
6658 else
6659 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if (pp->coladd > 0)
6661 --pp->coladd;
6662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (pp->col > 0)
6664 {
6665 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006666 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
6668 else if (pp->lnum > 1)
6669 {
6670 --pp->lnum;
6671 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6672 return TRUE;
6673 }
6674 }
6675 return FALSE;
6676}
6677
6678/*
6679 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6680 */
6681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006687 VIsual_select_reg = 0;
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 else if (VIsual_reselect)
6690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006691 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 cap->arg = TRUE;
6693 nv_g_cmd(cap);
6694 }
6695}
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
6698/*
6699 * "G", "gg", CTRL-END, CTRL-HOME.
6700 * cap->arg is TRUE for "G".
6701 */
6702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 linenr_T lnum;
6706
6707 if (cap->arg)
6708 lnum = curbuf->b_ml.ml_line_count;
6709 else
6710 lnum = 1L;
6711 cap->oap->motion_type = MLINE;
6712 setpcmark();
6713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006714 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (cap->count0 != 0)
6716 lnum = cap->count0;
6717 if (lnum < 1L)
6718 lnum = 1L;
6719 else if (lnum > curbuf->b_ml.ml_line_count)
6720 lnum = curbuf->b_ml.ml_line_count;
6721 curwin->w_cursor.lnum = lnum;
6722 beginline(BL_SOL | BL_FIX);
6723#ifdef FEAT_FOLDING
6724 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6725 foldOpenCursor();
6726#endif
6727}
6728
6729/*
6730 * CTRL-\ in Normal mode.
6731 */
6732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006733nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6736 {
6737 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006738 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 restart_edit = 0;
6741#ifdef FEAT_CMDWIN
6742 if (cmdwin_type != 0)
6743 cmdwin_result = Ctrl_C;
6744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (VIsual_active)
6746 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006747 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006748 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006750 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (cap->nchar == Ctrl_G && p_im)
6752 restart_edit = 'a';
6753 }
6754 else
6755 clearopbeep(cap->oap);
6756}
6757
6758/*
6759 * ESC in Normal mode: beep, but don't flush buffers.
6760 * Don't even beep if we are canceling a command.
6761 */
6762 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006763nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 int no_reason;
6766
6767 no_reason = (cap->oap->op_type == OP_NOP
6768 && cap->opcount == 0
6769 && cap->count0 == 0
6770 && cap->oap->regname == 0
6771 && !p_im);
6772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006773 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 if (restart_edit == 0
6776#ifdef FEAT_CMDWIN
6777 && cmdwin_type == 0
6778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006781 {
6782 if (anyBufIsChanged())
6783 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6784 else
6785 msg(_("Type :qa and press <Enter> to exit Vim"));
6786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006788 if (restart_edit != 0)
6789 redraw_mode = TRUE; // remove "-- (insert) --"
6790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006791 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6792 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (!p_im)
6794 restart_edit = 0;
6795#ifdef FEAT_CMDWIN
6796 if (cmdwin_type != 0)
6797 {
6798 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 return;
6801 }
6802#endif
6803 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006804#ifdef FEAT_CMDWIN
Bram Moolenaar8d696372022-08-21 10:40:07 +01006805 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006806 {
6807 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006808 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6809 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006810 cmdwin_result = K_IGNORE;
6811 return;
6812 }
6813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (VIsual_active)
6816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006817 end_visual_mode(); // stop Visual
6818 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006820 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006822 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006823 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 clearop(cap->oap);
6825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006826 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6827 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006828 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 restart_edit = 'a';
6830}
6831
6832/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006833 * Move the cursor for the "A" command.
6834 */
6835 void
6836set_cursor_for_append_to_line(void)
6837{
6838 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006839 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006840 {
6841 int save_State = State;
6842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006843 // Pretend Insert mode here to allow the cursor on the
6844 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006845 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006846 coladvance((colnr_T)MAXCOL);
6847 State = save_State;
6848 }
6849 else
6850 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6851}
6852
6853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006855 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 */
6857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006860 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6862 cap->cmdchar = 'i';
6863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006864 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006866 {
6867#ifdef FEAT_TERMINAL
6868 if (term_in_normal_mode())
6869 {
6870 end_visual_mode();
6871 clearop(cap->oap);
6872 term_enter_job_mode();
6873 return;
6874 }
6875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006879 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006880 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6881 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006885#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006886 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006887 {
6888 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006889 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006890 return;
6891 }
6892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 else if (!curbuf->b_p_ma && !p_im)
6894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006895 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006896 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006898 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006899 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006900 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006902 else if (cap->cmdchar == K_PS && VIsual_active)
6903 {
6904 pos_T old_pos = curwin->w_cursor;
6905 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006906 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006908 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006909 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6910 {
6911 shift_delete_registers();
6912 cap->oap->regname = '1';
6913 }
6914 else
6915 cap->oap->regname = '-';
6916 cap->cmdchar = 'd';
6917 cap->nchar = NUL;
6918 nv_operator(cap);
6919 do_pending_operator(cap, 0, FALSE);
6920 cap->cmdchar = K_PS;
6921
zeertzjq7a732522022-03-14 20:46:41 +00006922 if (*ml_get_cursor() != NUL)
6923 {
6924 if (old_visual_mode == 'V')
6925 {
6926 // In linewise Visual mode insert before the beginning of the
6927 // next line.
6928 // When the last line in the buffer was deleted then create a
6929 // new line, otherwise there is not need to move cursor.
6930 // Detect this by checking if cursor moved above Visual area.
6931 if (curwin->w_cursor.lnum < old_pos.lnum
6932 && curwin->w_cursor.lnum < old_visual.lnum)
6933 {
6934 if (u_save_cursor() == OK)
6935 {
6936 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6937 FALSE);
6938 appended_lines(curwin->w_cursor.lnum++, 1L);
6939 }
6940 }
6941 }
6942 // When the last char in the line was deleted then append.
6943 // Detect this by checking if cursor moved before Visual area.
6944 else if (curwin->w_cursor.col < old_pos.col
6945 && curwin->w_cursor.col < old_visual.col)
6946 inc_cursor();
6947 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006949 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006950 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 else if (!checkclearopq(cap->oap))
6953 {
6954 switch (cap->cmdchar)
6955 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006956 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006957 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 break;
6959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006961 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6962 beginline(BL_WHITE);
6963 else
6964 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 break;
6966
Bram Moolenaara1891842017-02-04 21:34:31 +01006967 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // Bracketed paste works like "a"ppend, unless the cursor is in
6969 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006970 if (curwin->w_cursor.col == 0)
6971 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 case 'a': // "a"ppend is like "i"nsert on the next character.
6975 // increment coladd when in virtual space, increment the
6976 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (virtual_active()
6978 && (curwin->w_cursor.coladd > 0
6979 || *ml_get_cursor() == NUL
6980 || *ml_get_cursor() == TAB))
6981 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006982 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 inc_cursor();
6984 break;
6985 }
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6988 {
6989 int save_State = State;
6990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 // Pretend Insert mode here to allow the cursor on the
6992 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006993 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 coladvance(getviscol());
6995 State = save_State;
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6999 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007000 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007002 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003}
7004
7005/*
7006 * Invoke edit() and take care of "restart_edit" and the return value.
7007 */
7008 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007009invoke_edit(
7010 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007012 int cmd,
7013 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 int restart_edit_save = 0;
7016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7018 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7019 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (repl || !stuff_empty())
7021 restart_edit_save = restart_edit;
7022 else
7023 restart_edit_save = 0;
7024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007025 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 restart_edit = 0;
7027
7028 if (edit(cmd, startln, cap->count1))
7029 cap->retval |= CA_COMMAND_BUSY;
7030
7031 if (restart_edit == 0)
7032 restart_edit = restart_edit_save;
7033}
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035/*
7036 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7037 */
7038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007039nv_object(
7040 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
7042 int flag;
7043 int include;
7044 char_u *mps_save;
7045
7046 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007047 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007049 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 mps_save = curbuf->b_p_mps;
7053 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7054
7055 switch (cap->nchar)
7056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 flag = current_word(cap->oap, cap->count1, include, FALSE);
7059 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 flag = current_word(cap->oap, cap->count1, include, TRUE);
7062 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007063 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 case '(':
7065 case ')':
7066 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7067 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 case '{':
7070 case '}':
7071 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7072 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 case ']':
7075 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7076 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 case '>':
7079 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7080 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007081#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007082 case 't': // "at" = a tag block (xml and html)
7083 // Do not adjust oap->end in do_pending_operator()
7084 // otherwise there are different results for 'dit'
7085 // (note leading whitespace in last line):
7086 // 1) <b> 2) <b>
7087 // foobar foobar
7088 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007089 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007090 flag = current_tagblock(cap->oap, cap->count1, include);
7091 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007092#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 flag = current_par(cap->oap, cap->count1, include, 'p');
7095 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007096 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 flag = current_sent(cap->oap, cap->count1, include);
7098 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 case '"': // "a"" = a double quoted string
7100 case '\'': // "a'" = a single quoted string
7101 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007102 flag = current_quote(cap->oap, cap->count1, include,
7103 cap->nchar);
7104 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105#if 0 // TODO
7106 case 'S': // "aS" = a section
7107 case 'f': // "af" = a filename
7108 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#endif
7110 default:
7111 flag = FAIL;
7112 break;
7113 }
7114
7115 curbuf->b_p_mps = mps_save;
7116 if (flag == FAIL)
7117 clearopbeep(cap->oap);
7118 adjust_cursor_col();
7119 curwin->w_set_curswant = TRUE;
7120}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
7122/*
7123 * "q" command: Start/stop recording.
7124 * "q:", "q/", "q?": edit command-line in command-line window.
7125 */
7126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007127nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 if (cap->oap->op_type == OP_FORMAT)
7130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 cap->cmdchar = 'g';
7133 cap->nchar = 'q';
7134 nv_operator(cap);
7135 }
7136 else if (!checkclearop(cap->oap))
7137 {
7138#ifdef FEAT_CMDWIN
7139 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7140 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007141 if (cmdwin_type != 0)
7142 {
7143 emsg(_(e_cmdline_window_already_open));
7144 return;
7145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 stuffcharReadbuff(cap->nchar);
7147 stuffcharReadbuff(K_CMDWIN);
7148 }
7149 else
7150#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007151 // (stop) recording into a named register, unless executing a
7152 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007153 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 clearopbeep(cap->oap);
7155 }
7156}
7157
7158/*
7159 * Handle the "@r" command.
7160 */
7161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007162nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 if (checkclearop(cap->oap))
7165 return;
7166#ifdef FEAT_EVAL
7167 if (cap->nchar == '=')
7168 {
7169 if (get_expr_register() == NUL)
7170 return;
7171 }
7172#endif
7173 while (cap->count1-- && !got_int)
7174 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007175 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177 clearopbeep(cap->oap);
7178 break;
7179 }
7180 line_breakcheck();
7181 }
7182}
7183
7184/*
7185 * Handle the CTRL-U and CTRL-D commands.
7186 */
7187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007188nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7191 || (cap->cmdchar == Ctrl_D
7192 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7193 clearopbeep(cap->oap);
7194 else if (!checkclearop(cap->oap))
7195 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7196}
7197
7198/*
7199 * Handle "J" or "gJ" command.
7200 */
7201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007202nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007204 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007206 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
7208 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007209 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7211 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007214 if (cap->count0 <= 2)
7215 {
7216 clearopbeep(cap->oap);
7217 return;
7218 }
7219 cap->count0 = curbuf->b_ml.ml_line_count
7220 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007222
7223 prep_redo(cap->oap->regname, cap->count0,
7224 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7225 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227}
7228
7229/*
7230 * "P", "gP", "p" and "gp" commands.
7231 */
7232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007233nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007235 nv_put_opt(cap, FALSE);
7236}
7237
7238/*
7239 * "P", "gP", "p" and "gp" commands.
7240 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7241 */
7242 static void
7243nv_put_opt(cmdarg_T *cap, int fix_indent)
7244{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 int regname = 0;
7246 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007247 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007248 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 int dir;
7250 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007251 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 if (cap->oap->op_type != OP_NOP)
7254 {
7255#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007256 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7258 {
7259 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007260 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262 else
7263#endif
7264 clearopbeep(cap->oap);
7265 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007266#ifdef FEAT_JOB_CHANNEL
7267 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7268 {
7269 clearopbeep(cap->oap);
7270 }
7271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 else
7273 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007274 if (fix_indent)
7275 {
7276 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7277 ? FORWARD : BACKWARD;
7278 flags |= PUT_FIXINDENT;
7279 }
7280 else
7281 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007282 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7283 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 prep_redo_cmd(cap);
7285 if (cap->cmdchar == 'g')
7286 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007287 else if (cap->cmdchar == 'z')
7288 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 if (VIsual_active)
7291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007292 // Putting in Visual mode: The put text replaces the selected
7293 // text. First delete the selected text, then put the new text.
7294 // Need to save and restore the registers that the delete
7295 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007296 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007298 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007299#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007301#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007302 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007303 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007304#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307
7308 )
7309 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007310 // The delete is going to overwrite the register we want to
7311 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 reg1 = get_register(regname, TRUE);
7313 }
7314
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007315 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 cap->cmdchar = 'd';
7317 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007318 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007319 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 nv_operator(cap);
7321 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007322 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007323 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007325 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 cap->oap->regname = regname;
7327
7328 if (reg1 != NULL)
7329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007330 // Delete probably changed the register we want to put, save
7331 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 reg2 = get_register(regname, FALSE);
7333 put_register(regname, reg1);
7334 }
7335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // When deleted a linewise Visual area, put the register as
7337 // lines to avoid it joined with the next line. When deletion was
7338 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 if (VIsual_mode == 'V')
7340 flags |= PUT_LINE;
7341 else if (VIsual_mode == 'v')
7342 flags |= PUT_LINE_SPLIT;
7343 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7344 flags |= PUT_LINE_FORWARD;
7345 dir = BACKWARD;
7346 if ((VIsual_mode != 'V'
7347 && curwin->w_cursor.col < curbuf->b_op_start.col)
7348 || (VIsual_mode == 'V'
7349 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007350 // cursor is at the end of the line or end of file, put
7351 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007354 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007356 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (reg2 != NULL)
7360 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007362 // What to reselect with "gv"? Selecting the just put text seems to
7363 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007364 if (was_visual)
7365 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007366 curbuf->b_visual.vi_start = curbuf->b_op_start;
7367 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007369 if (*p_sel == 'e')
7370 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007371 }
7372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // When all lines were selected and deleted do_put() leaves an empty
7374 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007375 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007376 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007377 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007378 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007380 // If the cursor was in that line, move it to the end of the last
7381 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007382 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7383 {
7384 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7385 coladvance((colnr_T)MAXCOL);
7386 }
7387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 auto_format(FALSE, TRUE);
7389 }
7390}
7391
7392/*
7393 * "o" and "O" commands.
7394 */
7395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007396nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007399 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7401 {
7402 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007403 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405 else
7406#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007407 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007409#ifdef FEAT_JOB_CHANNEL
7410 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007411 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 n_opencmd(cap);
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#ifdef FEAT_NETBEANS_INTG
7418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007419nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 netbeans_keycommand(cap->nchar);
7422}
7423#endif
7424
7425#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007427nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007429 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007432
Bram Moolenaar3918c952005-03-15 22:34:55 +00007433/*
7434 * Trigger CursorHold event.
7435 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7436 * input buffer. "did_cursorhold" is set to avoid retriggering.
7437 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007439nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007440{
7441 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7442 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007443 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007444}