blob: 5c7bc2e9702123cb4c23c0801ab1995619ab4b72 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
26
27/*
28 * nv_*(): functions called to handle Normal and Visual mode commands.
29 * n_*(): functions called to handle Normal mode commands.
30 * v_*(): functions called to handle Visual mode commands.
31 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static void nv_ignore(cmdarg_T *cap);
33static void nv_nop(cmdarg_T *cap);
34static void nv_error(cmdarg_T *cap);
35static void nv_help(cmdarg_T *cap);
36static void nv_addsub(cmdarg_T *cap);
37static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_ver_scrollbar(cmdarg_T *cap);
41static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000043#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void nv_tabline(cmdarg_T *cap);
45static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000046#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010047static void nv_exmode(cmdarg_T *cap);
48static void nv_colon(cmdarg_T *cap);
49static void nv_ctrlg(cmdarg_T *cap);
50static void nv_ctrlh(cmdarg_T *cap);
51static void nv_clear(cmdarg_T *cap);
52static void nv_ctrlo(cmdarg_T *cap);
53static void nv_hat(cmdarg_T *cap);
54static void nv_Zet(cmdarg_T *cap);
55static void nv_ident(cmdarg_T *cap);
56static void nv_tagpop(cmdarg_T *cap);
57static void nv_scroll(cmdarg_T *cap);
58static void nv_right(cmdarg_T *cap);
59static void nv_left(cmdarg_T *cap);
60static void nv_up(cmdarg_T *cap);
61static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void nv_end(cmdarg_T *cap);
63static void nv_dollar(cmdarg_T *cap);
64static void nv_search(cmdarg_T *cap);
65static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020066static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010067static void nv_csearch(cmdarg_T *cap);
68static void nv_brackets(cmdarg_T *cap);
69static void nv_percent(cmdarg_T *cap);
70static void nv_brace(cmdarg_T *cap);
71static void nv_mark(cmdarg_T *cap);
72static void nv_findpar(cmdarg_T *cap);
73static void nv_undo(cmdarg_T *cap);
74static void nv_kundo(cmdarg_T *cap);
75static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010077static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
78static void v_visop(cmdarg_T *cap);
79static void nv_subst(cmdarg_T *cap);
80static void nv_abbrev(cmdarg_T *cap);
81static void nv_optrans(cmdarg_T *cap);
82static void nv_gomark(cmdarg_T *cap);
83static void nv_pcmark(cmdarg_T *cap);
84static void nv_regname(cmdarg_T *cap);
85static void nv_visual(cmdarg_T *cap);
86static void n_start_visual_mode(int c);
87static void nv_window(cmdarg_T *cap);
88static void nv_suspend(cmdarg_T *cap);
89static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000091static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_Undo(cmdarg_T *cap);
93static void nv_tilde(cmdarg_T *cap);
94static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_lineop(cmdarg_T *cap);
99static void nv_home(cmdarg_T *cap);
100static void nv_pipe(cmdarg_T *cap);
101static void nv_bck_word(cmdarg_T *cap);
102static void nv_wordcmd(cmdarg_T *cap);
103static void nv_beginline(cmdarg_T *cap);
104static void adjust_cursor(oparg_T *oap);
105static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_select(cmdarg_T *cap);
107static void nv_goto(cmdarg_T *cap);
108static void nv_normal(cmdarg_T *cap);
109static void nv_esc(cmdarg_T *oap);
110static void nv_edit(cmdarg_T *cap);
111static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100112static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void nv_record(cmdarg_T *cap);
114static void nv_at(cmdarg_T *cap);
115static void nv_halfpage(cmdarg_T *cap);
116static void nv_join(cmdarg_T *cap);
117static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200118static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#endif
123#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
ichizok672776d2022-01-31 12:27:18 +0000128// Declare nv_cmds[].
129#define DO_DECLARE_NVCMD
130#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000132// Include the lookuptable generated by create_nvcmdidx.vim.
133#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135/*
136 * Search for a command in the commands table.
137 * Returns -1 for invalid command.
138 */
139 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100140find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141{
142 int i;
143 int idx;
144 int top, bot;
145 int c;
146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100147 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 if (cmdchar >= 0x100)
149 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100151 // We use the absolute value of the character. Special keys have a
152 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 if (cmdchar < 0)
154 cmdchar = -cmdchar;
155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100156 // If the character is in the first part: The character is the index into
157 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 if (cmdchar <= nv_max_linear)
159 return nv_cmd_idx[cmdchar];
160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100161 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 bot = nv_max_linear + 1;
163 top = NV_CMDS_SIZE - 1;
164 idx = -1;
165 while (bot <= top)
166 {
167 i = (top + bot) / 2;
168 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
169 if (c < 0)
170 c = -c;
171 if (cmdchar == c)
172 {
173 idx = nv_cmd_idx[i];
174 break;
175 }
176 if (cmdchar > c)
177 bot = i + 1;
178 else
179 top = i - 1;
180 }
181 return idx;
182}
183
184/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100185 * If currently editing a cmdline or text is locked: beep and give an error
186 * message, return TRUE.
187 */
188 static int
189check_text_locked(oparg_T *oap)
190{
191 if (text_locked())
192 {
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
196 }
197 return FALSE;
198}
199
200/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000201 * Handle the count before a normal command and set cap->count0.
202 */
203 static int
204normal_cmd_get_count(
205 cmdarg_T *cap,
206 int c,
207 int toplevel UNUSED,
208 int set_prevcount UNUSED,
209 int *ctrl_w,
210 int *need_flushbuf UNUSED)
211{
212getcount:
213 if (!(VIsual_active && VIsual_select))
214 {
215 // Handle a count before a command and compute ca.count0.
216 // Note that '0' is a command and not the start of a count, but it's
217 // part of a count after other digits.
218 while ((c >= '1' && c <= '9')
219 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
220 || c == '0')))
221 {
222 if (c == K_DEL || c == K_KDEL)
223 {
224 cap->count0 /= 10;
225#ifdef FEAT_CMDL_INFO
226 del_from_showcmd(4); // delete the digit and ~@%
227#endif
228 }
229 else if (cap->count0 > 99999999L)
230 {
231 cap->count0 = 999999999L;
232 }
233 else
234 {
235 cap->count0 = cap->count0 * 10 + (c - '0');
236 }
237#ifdef FEAT_EVAL
238 // Set v:count here, when called from main() and not a stuffed
239 // command, so that v:count can be used in an expression mapping
240 // right after the count. Do set it for redo.
241 if (toplevel && readbuf1_empty())
242 set_vcount_ca(cap, &set_prevcount);
243#endif
244 if (*ctrl_w)
245 {
246 ++no_mapping;
247 ++allow_keys; // no mapping for nchar, but keys
248 }
249 ++no_zero_mapping; // don't map zero here
250 c = plain_vgetc();
251 LANGMAP_ADJUST(c, TRUE);
252 --no_zero_mapping;
253 if (*ctrl_w)
254 {
255 --no_mapping;
256 --allow_keys;
257 }
258#ifdef FEAT_CMDL_INFO
259 *need_flushbuf |= add_to_showcmd(c);
260#endif
261 }
262
263 // If we got CTRL-W there may be a/another count
264 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
265 {
266 *ctrl_w = TRUE;
267 cap->opcount = cap->count0; // remember first count
268 cap->count0 = 0;
269 ++no_mapping;
270 ++allow_keys; // no mapping for nchar, but keys
271 c = plain_vgetc(); // get next character
272 LANGMAP_ADJUST(c, TRUE);
273 --no_mapping;
274 --allow_keys;
275#ifdef FEAT_CMDL_INFO
276 *need_flushbuf |= add_to_showcmd(c);
277#endif
278 goto getcount; // jump back
279 }
280 }
281
282 if (c == K_CURSORHOLD)
283 {
284 // Save the count values so that ca.opcount and ca.count0 are exactly
285 // the same when coming back here after handling K_CURSORHOLD.
286 cap->oap->prev_opcount = cap->opcount;
287 cap->oap->prev_count0 = cap->count0;
288 }
289 else if (cap->opcount != 0)
290 {
291 // If we're in the middle of an operator (including after entering a
292 // yank buffer with '"') AND we had a count before the operator, then
293 // that count overrides the current value of ca.count0.
294 // What this means effectively, is that commands like "3dw" get turned
295 // into "d3w" which makes things fall into place pretty neatly.
296 // If you give a count before AND after the operator, they are
297 // multiplied.
298 if (cap->count0)
299 {
300 if (cap->opcount >= 999999999L / cap->count0)
301 cap->count0 = 999999999L;
302 else
303 cap->count0 *= cap->opcount;
304 }
305 else
306 cap->count0 = cap->opcount;
307 }
308
309 // Always remember the count. It will be set to zero (on the next call,
310 // above) when there is no pending operator.
311 // When called from main(), save the count for use by the "count" built-in
312 // variable.
313 cap->opcount = cap->count0;
314 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
315
316#ifdef FEAT_EVAL
317 // Only set v:count when called from main() and not a stuffed command.
318 // Do set it for redo.
319 if (toplevel && readbuf1_empty())
320 set_vcount(cap->count0, cap->count1, set_prevcount);
321#endif
322
323 return c;
324}
325
326/*
327 * Returns TRUE if the normal command (cap) needs a second character.
328 */
329 static int
330normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
331{
332 return ((cmd_flags & NV_NCH)
333 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
334 && cap->oap->op_type == OP_NOP)
335 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
336 || (cap->cmdchar == 'q'
337 && cap->oap->op_type == OP_NOP
338 && reg_recording == 0
339 && reg_executing == 0)
340 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
341 && (cap->oap->op_type != OP_NOP || VIsual_active))));
342}
343
344/*
345 * Get one or more additional characters for a normal command.
346 * Return the updated command index (if changed).
347 */
348 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000349normal_cmd_get_more_chars(
350 int idx_arg,
351 cmdarg_T *cap,
352 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000353{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000354 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000355 int c;
356 int *cp;
357 int repl = FALSE; // get character for replace mode
358 int lit = FALSE; // get extra character literally
359 int langmap_active = FALSE; // using :lmap mappings
360 int lang; // getting a text character
361#ifdef HAVE_INPUT_METHOD
362 int save_smd; // saved value of p_smd
363#endif
364
365 ++no_mapping;
366 ++allow_keys; // no mapping for nchar, but allow key codes
367 // Don't generate a CursorHold event here, most commands can't handle
368 // it, e.g., nv_replace(), nv_csearch().
369 did_cursorhold = TRUE;
370 if (cap->cmdchar == 'g')
371 {
372 /*
373 * For 'g' get the next character now, so that we can check for
374 * "gr", "g'" and "g`".
375 */
376 cap->nchar = plain_vgetc();
377 LANGMAP_ADJUST(cap->nchar, TRUE);
378#ifdef FEAT_CMDL_INFO
379 *need_flushbuf |= add_to_showcmd(cap->nchar);
380#endif
381 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
382 || cap->nchar == Ctrl_BSL)
383 {
384 cp = &cap->extra_char; // need to get a third character
385 if (cap->nchar != 'r')
386 lit = TRUE; // get it literally
387 else
388 repl = TRUE; // get it in replace mode
389 }
390 else
391 cp = NULL; // no third character needed
392 }
393 else
394 {
395 if (cap->cmdchar == 'r') // get it in replace mode
396 repl = TRUE;
397 cp = &cap->nchar;
398 }
399 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
400
401 /*
402 * Get a second or third character.
403 */
404 if (cp != NULL)
405 {
406 if (repl)
407 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100408 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000409#ifdef CURSOR_SHAPE
410 ui_cursor_shape(); // show different cursor shape
411#endif
412 }
413 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
414 {
415 // Allow mappings defined with ":lmap".
416 --no_mapping;
417 --allow_keys;
418 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100419 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000420 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100421 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000422 langmap_active = TRUE;
423 }
424#ifdef HAVE_INPUT_METHOD
425 save_smd = p_smd;
426 p_smd = FALSE; // Don't let the IM code show the mode here
427 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
428 im_set_active(TRUE);
429#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100432 MAY_WANT_TO_LOG_THIS;
433
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000434 // Disable bracketed paste and modifyOtherKeys here, we won't
435 // recognize the escape sequences with 'esckeys' off.
436 out_str(T_BD);
437 out_str(T_CTE);
438 }
439
440 *cp = plain_vgetc();
441
Bram Moolenaar24959102022-05-07 20:01:16 +0100442 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100444 MAY_WANT_TO_LOG_THIS;
445
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000446 // Re-enable bracketed paste mode and modifyOtherKeys
447 out_str(T_BE);
448 out_str(T_CTI);
449 }
450
451 if (langmap_active)
452 {
453 // Undo the decrement done above
454 ++no_mapping;
455 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100456 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000457 }
458#ifdef HAVE_INPUT_METHOD
459 if (lang)
460 {
461 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
462 im_save_status(&curbuf->b_p_iminsert);
463 im_set_active(FALSE);
464 }
465 p_smd = save_smd;
466#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100467 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000468#ifdef FEAT_CMDL_INFO
469 *need_flushbuf |= add_to_showcmd(*cp);
470#endif
471
472 if (!lit)
473 {
474#ifdef FEAT_DIGRAPHS
475 // Typing CTRL-K gets a digraph.
476 if (*cp == Ctrl_K
477 && ((nv_cmds[idx].cmd_flags & NV_LANG)
478 || cp == &cap->extra_char)
479 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
480 {
481 c = get_digraph(FALSE);
482 if (c > 0)
483 {
484 *cp = c;
485# ifdef FEAT_CMDL_INFO
486 // Guessing how to update showcmd here...
487 del_from_showcmd(3);
488 *need_flushbuf |= add_to_showcmd(*cp);
489# endif
490 }
491 }
492#endif
493
494 // adjust chars > 127, except after "tTfFr" commands
495 LANGMAP_ADJUST(*cp, !lang);
496#ifdef FEAT_RIGHTLEFT
497 // adjust Hebrew mapped char
498 if (p_hkmap && lang && KeyTyped)
499 *cp = hkmap(*cp);
500#endif
501 }
502
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000503 // When the next character is CTRL-\ a following CTRL-N means the
504 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000505 if (cp == &cap->extra_char
506 && cap->nchar == Ctrl_BSL
507 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
508 {
509 cap->cmdchar = Ctrl_BSL;
510 cap->nchar = cap->extra_char;
511 idx = find_command(cap->cmdchar);
512 }
513 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
514 cap->oap->op_type = get_op_type(*cp, NUL);
515 else if (*cp == Ctrl_BSL)
516 {
517 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
518
519 // There is a busy wait here when typing "f<C-\>" and then
520 // something different from CTRL-N. Can't be avoided.
521 while ((c = vpeekc()) <= 0 && towait > 0L)
522 {
523 do_sleep(towait > 50L ? 50L : towait, FALSE);
524 towait -= 50L;
525 }
526 if (c > 0)
527 {
528 c = plain_vgetc();
529 if (c != Ctrl_N && c != Ctrl_G)
530 vungetc(c);
531 else
532 {
533 cap->cmdchar = Ctrl_BSL;
534 cap->nchar = c;
535 idx = find_command(cap->cmdchar);
536 }
537 }
538 }
539
540 // When getting a text character and the next character is a
541 // multi-byte character, it could be a composing character.
542 // However, don't wait for it to arrive. Also, do enable mapping,
543 // because if it's put back with vungetc() it's too late to apply
544 // mapping.
545 --no_mapping;
546 while (enc_utf8 && lang && (c = vpeekc()) > 0
547 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
548 {
549 c = plain_vgetc();
550 if (!utf_iscomposing(c))
551 {
552 vungetc(c); // it wasn't, put it back
553 break;
554 }
555 else if (cap->ncharC1 == 0)
556 cap->ncharC1 = c;
557 else
558 cap->ncharC2 = c;
559 }
560 ++no_mapping;
561 }
562 --no_mapping;
563 --allow_keys;
564
565 return idx;
566}
567
568/*
569 * Returns TRUE if after processing a normal mode command, need to wait for a
570 * moment when a message is displayed that will be overwritten by the mode
571 * message.
572 */
573 static int
574normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
575{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000576 // In Visual mode and with "^O" in Insert mode, a short message will be
577 // overwritten by the mode message. Wait a bit, until a key is hit.
578 // In Visual mode, it's more important to keep the Visual area updated
579 // than keeping a message (e.g. from a /pat search).
580 // Only do this if the command was typed, not from a mapping.
581 // Don't wait when emsg_silent is non-zero.
582 // Also wait a bit after an error message, e.g. for "^O:".
583 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000584 return ( ((p_smd
585 && msg_silent == 0
586 && (restart_edit != 0
587 || (VIsual_active
588 && old_pos->lnum == curwin->w_cursor.lnum
589 && old_pos->col == curwin->w_cursor.col)
590 )
591 && (clear_cmdline
592 || redraw_cmdline)
593 && (msg_didout || (msg_didany && msg_scroll))
594 && !msg_nowait
595 && KeyTyped)
596 || (restart_edit != 0
597 && !VIsual_active
598 && (msg_scroll
599 || emsg_on_display)))
600 && cap->oap->regname == 0
601 && !(cap->retval & CA_COMMAND_BUSY)
602 && stuff_empty()
603 && typebuf_typed()
604 && emsg_silent == 0
605 && !in_assert_fails
606 && !did_wait_return
607 && cap->oap->op_type == OP_NOP);
608}
609
610/*
611 * After processing a normal mode command, wait for a moment when a message is
612 * displayed that will be overwritten by the mode message.
613 */
614 static void
615normal_cmd_wait_for_msg(void)
616{
617 int save_State = State;
618
619 // Draw the cursor with the right shape here
620 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100621 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000622
623 // If need to redraw, and there is a "keep_msg", redraw before the
624 // delay
625 if (must_redraw && keep_msg != NULL && !emsg_on_display)
626 {
627 char_u *kmsg;
628
629 kmsg = keep_msg;
630 keep_msg = NULL;
631 // Showmode() will clear keep_msg, but we want to use it anyway.
632 // First update w_topline.
633 setcursor();
634 update_screen(0);
635 // now reset it, otherwise it's put in the history again
636 keep_msg = kmsg;
637
638 kmsg = vim_strsave(keep_msg);
639 if (kmsg != NULL)
640 {
641 msg_attr((char *)kmsg, keep_msg_attr);
642 vim_free(kmsg);
643 }
644 }
645 setcursor();
646#ifdef CURSOR_SHAPE
647 ui_cursor_shape(); // may show different cursor shape
648#endif
649 cursor_on();
650 out_flush();
651 if (msg_scroll || emsg_on_display)
652 ui_delay(1003L, TRUE); // wait at least one second
653 ui_delay(3003L, FALSE); // wait up to three seconds
654 State = save_State;
655
656 msg_scroll = FALSE;
657 emsg_on_display = FALSE;
658}
659
660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 * Execute a command in Normal mode.
662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100664normal_cmd(
665 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000672 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100673 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 static int old_mapped_len = 0;
676 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000677 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200678 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaara80faa82020-04-12 19:37:17 +0200680 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Use a count remembered from before entering an operator. After typing
684 // "3d" we return from normal_cmd() and come back here, the "3" is
685 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ca.opcount = opcount;
687
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000688 // If there is an operator pending, then the command we take this time
689 // will terminate it. Finish_op tells us to finish the operation before
690 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#ifdef CURSOR_SHAPE
692 c = finish_op;
693#endif
694 finish_op = (oap->op_type != OP_NOP);
695#ifdef CURSOR_SHAPE
696 if (finish_op != c)
697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100698 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# ifdef FEAT_MOUSESHAPE
700 update_mouseshape(-1);
701# endif
702 }
703#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100704 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100706 // When not finishing an operator and no register name typed, reset the
707 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000711#ifdef FEAT_EVAL
712 set_prevcount = TRUE;
713#endif
714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100716 // Restore counts from before receiving K_CURSORHOLD. This means after
717 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
718 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000719 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
720 {
721 ca.opcount = oap->prev_opcount;
722 ca.count0 = oap->prev_count0;
723 oap->prev_opcount = 0;
724 oap->prev_count0 = 0;
725 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaar24959102022-05-07 20:01:16 +0100729 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100731 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100734#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // Set v:count here, when called from main() and not a stuffed
736 // command, so that v:count can be used in an expression mapping
737 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100738 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100739 set_vcount_ca(&ca, &set_prevcount);
740#endif
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 /*
743 * Get the command character from the user.
744 */
745 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100746 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000748 // If a mapping was started in Visual or Select mode, remember the length
749 // of the mapping. This is used below to not return to Insert mode for as
750 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (restart_edit == 0)
752 old_mapped_len = 0;
753 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000754 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 old_mapped_len = typebuf_maplen();
756
757 if (c == NUL)
758 c = K_ZERO;
759
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000760 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (VIsual_active
762 && VIsual_select
763 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
764 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000765 int len;
766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
768 // 'insertmode' is set) fake a "d"elete command, Insert mode will
769 // restart automatically.
770 // Insert the typed character in the typeahead buffer, so that it can
771 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000772 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
773
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000774 // When recording and gotchars() was called the character will be
775 // recorded again, remove the previous recording.
776 if (KeyTyped)
777 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000778
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000779 if (restart_edit != 0)
780 c = 'd';
781 else
782 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 msg_nowait = TRUE; // don't delay going to insert mode
784 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000787 // If the window was made so small that nothing shows, make it at least one
788 // line and one column when typing a command.
789 if (KeyTyped && !KeyStuffed)
790 win_ensure_size();
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#ifdef FEAT_CMDL_INFO
793 need_flushbuf = add_to_showcmd(c);
794#endif
795
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000796 // Get the command count
797 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
798 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000800 // Find the command character in the table of commands.
801 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (ctrl_w)
803 {
804 ca.nchar = c;
805 ca.cmdchar = Ctrl_W;
806 }
807 else
808 ca.cmdchar = c;
809 idx = find_command(ca.cmdchar);
810 if (idx < 0)
811 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100812 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 clearopbeep(oap);
814 goto normal_end;
815 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000816
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100817 if ((nv_cmds[idx].cmd_flags & NV_NCW)
818 && (check_text_locked(oap) || curbuf_locked()))
819 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000820 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000822 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (VIsual_active)
824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (km_stopsel
827 && (nv_cmds[idx].cmd_flags & NV_STS)
828 && !(mod_mask & MOD_MASK_SHIFT))
829 {
830 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100831 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (km_startsel)
836 {
837 if (nv_cmds[idx].cmd_flags & NV_SS)
838 {
839 unshift_special(&ca);
840 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000841 if (idx < 0)
842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100843 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 clearopbeep(oap);
845 goto normal_end;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
849 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854#ifdef FEAT_RIGHTLEFT
855 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
856 && (nv_cmds[idx].cmd_flags & NV_RL))
857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 // Invert horizontal movements and operations. Only when typed by the
859 // user directly, not when the result of a mapping or "x" translated
860 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 switch (ca.cmdchar)
862 {
863 case 'l': ca.cmdchar = 'h'; break;
864 case K_RIGHT: ca.cmdchar = K_LEFT; break;
865 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
866 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
867 case 'h': ca.cmdchar = 'l'; break;
868 case K_LEFT: ca.cmdchar = K_RIGHT; break;
869 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
870 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
871 case '>': ca.cmdchar = '<'; break;
872 case '<': ca.cmdchar = '>'; break;
873 }
874 idx = find_command(ca.cmdchar);
875 }
876#endif
877
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000878 // Get additional characters if we need them.
879 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
880 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000883 // Flush the showcmd characters onto the screen so we can see them while
884 // the command is being executed. Only do this when the shown command was
885 // actually displayed, otherwise this will slow down a lot when executing
886 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (need_flushbuf)
888 out_flush();
889#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000890 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200891 {
892 if (ex_normal_busy)
893 did_cursorhold = save_did_cursorhold;
894 else
895 did_cursorhold = FALSE;
896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaar24959102022-05-07 20:01:16 +0100898 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900 if (ca.nchar == ESC)
901 {
902 clearop(oap);
903 if (restart_edit == 0 && goto_im())
904 restart_edit = 'a';
905 goto normal_end;
906 }
907
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000908 if (ca.cmdchar != K_IGNORE)
909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000911 msg_col = 0;
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100914 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 // When 'keymodel' contains "startsel" some keys start Select/Visual
917 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (!VIsual_active && km_startsel)
919 {
920 if (nv_cmds[idx].cmd_flags & NV_SS)
921 {
922 start_selection();
923 unshift_special(&ca);
924 idx = find_command(ca.cmdchar);
925 }
926 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
927 && (mod_mask & MOD_MASK_SHIFT))
928 {
929 start_selection();
930 mod_mask &= ~MOD_MASK_SHIFT;
931 }
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000934 // Execute the command!
935 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 ca.arg = nv_cmds[idx].cmd_arg;
937 (nv_cmds[idx].cmd_func)(&ca);
938
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000939 // If we didn't start or finish an operator, reset oap->regname, unless we
940 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (!finish_op
942 && !oap->op_type
943 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
944 {
945 clearop(oap);
946#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200947 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949 }
950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100951 // Get the length of mapped chars again after typing a count, second
952 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000953 if (old_mapped_len > 0)
954 old_mapped_len = typebuf_maplen();
955
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000956 // If an operation is pending, handle it. But not for K_IGNORE or
957 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200958 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100959 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000961 // Wait for a moment when a message is displayed that will be overwritten
962 // by the mode message.
963 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
964 normal_cmd_wait_for_msg();
965
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000966 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967normal_end:
968
969 msg_nowait = FALSE;
970
Bram Moolenaarcc613032020-06-07 21:31:18 +0200971#ifdef FEAT_EVAL
972 if (finish_op)
973 reset_reg_var();
974#endif
975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#ifdef CURSOR_SHAPE
978 c = finish_op;
979#endif
980 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100981 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100983 // Redraw the cursor with another shape, if we were in Operator-pending
984 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (c || ca.cmdchar == 'r')
986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100987 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# ifdef FEAT_MOUSESHAPE
989 update_mouseshape(-1);
990# endif
991 }
992#endif
993
994#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000995 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100996 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 clear_showcmd();
998#endif
999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 vim_free(ca.searchbuf);
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (has_mbyte)
1004 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (curwin->w_p_scb && toplevel)
1007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001008 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 do_check_scrollbind(TRUE);
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar860cae12010-06-05 23:22:07 +02001012 if (curwin->w_p_crb && toplevel)
1013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001015 do_check_cursorbind();
1016 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001018#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001020 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001021 restart_edit = 0;
1022#endif
1023
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001024 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1025 // if still inside a mapping that started in Visual mode).
1026 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1029 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !(ca.retval & CA_COMMAND_BUSY)
1031 && stuff_empty()
1032 && oap->regname == 0)
1033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_VIsual_select == 1)
1035 {
1036 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001037 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 showmode();
1039 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001040 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001042 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)edit(restart_edit, FALSE, 1L);
1044 }
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (restart_VIsual_select == 2)
1047 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 opcount = ca.opcount;
1051}
1052
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053#ifdef FEAT_EVAL
1054/*
1055 * Set v:count and v:count1 according to "cap".
1056 * Set v:prevcount only when "set_prevcount" is TRUE.
1057 */
1058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001060{
1061 long count = cap->count0;
1062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064 if (cap->opcount != 0)
1065 count = cap->opcount * (count == 0 ? 1 : count);
1066 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068}
1069#endif
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001072 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * if not.
1074 */
1075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 static int did_check = FALSE;
1079
1080 if (full_screen)
1081 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001082 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 did_check = TRUE;
1085 }
1086}
1087
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001088#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1089/*
1090 * Call yank_do_autocmd() for "regname".
1091 */
1092 static void
1093call_yank_do_autocmd(int regname)
1094{
1095 oparg_T oa;
1096 yankreg_T *reg;
1097
1098 clear_oparg(&oa);
1099 oa.regname = regname;
1100 oa.op_type = OP_YANK;
1101 oa.is_VIsual = TRUE;
1102 reg = get_register(regname, TRUE);
1103 yank_do_autocmd(&oa, reg);
1104 free_register(reg);
1105}
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001109 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001110 * This function or the next should ALWAYS be called to end Visual mode, except
1111 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114end_visual_mode()
1115{
1116 end_visual_mode_keep_button();
1117 reset_held_button();
1118}
1119
1120 void
1121end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001124 // If we are using the clipboard, then remember what was selected in case
1125 // we need to paste it somewhere while we still own the selection.
1126 // Only do this when the clipboard is already owned. Don't want to grab
1127 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (clip_star.available && clip_star.owned)
1129 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001130
1131# if defined(FEAT_EVAL)
1132 // Emit a TextYankPost for the automatic copy of the selection into the
1133 // star and/or plus register.
1134 if (has_textyankpost())
1135 {
1136 if (clip_isautosel_star())
1137 call_yank_do_autocmd('*');
1138 if (clip_isautosel_plus())
1139 call_yank_do_autocmd('+');
1140 }
1141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 setmouse();
1146 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001149 curbuf->b_visual.vi_mode = VIsual_mode;
1150 curbuf->b_visual.vi_start = VIsual;
1151 curbuf->b_visual.vi_end = curwin->w_cursor;
1152 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153#ifdef FEAT_EVAL
1154 curbuf->b_visual_mode_eval = VIsual_mode;
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!virtual_active())
1157 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001158 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001160 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001161 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162}
1163
1164/*
1165 * Reset VIsual_active and VIsual_reselect.
1166 */
1167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001168reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 if (VIsual_active)
1171 {
1172 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001173 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 VIsual_reselect = FALSE;
1176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect if it's set.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 VIsual_reselect = FALSE;
1189 }
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001192 void
1193restore_visual_mode(void)
1194{
1195 if (VIsual_mode_orig != NUL)
1196 {
1197 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1198 VIsual_mode_orig = NUL;
1199 }
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Check for a balloon-eval special item to include when searching for an
1204 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1205 * Returns TRUE if the character at "*ptr" should be included.
1206 * "dir" is FORWARD or BACKWARD, the direction of searching.
1207 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1208 * "bnp" points to a counter for square brackets.
1209 */
1210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001211find_is_eval_item(
1212 char_u *ptr,
1213 int *colp,
1214 int *bnp,
1215 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1219 ++*bnp;
1220 if (*bnp > 0)
1221 {
1222 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1223 --*bnp;
1224 return TRUE;
1225 }
1226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (*ptr == '.')
1229 return TRUE;
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1233 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1234 {
1235 *colp += dir;
1236 return TRUE;
1237 }
1238 return FALSE;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241/*
1242 * Find the identifier under or to the right of the cursor.
1243 * "find_type" can have one of three values:
1244 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * FIND_STRING: find any non-white text
1246 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001247 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *
1249 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001250 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * This doesn't match the real Vi but I like it a little better and it
1254 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001255 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * When FIND_IDENT isn't defined, we backup until a blank.
1257 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001258 * Returns the length of the text, or zero if no text is found.
1259 * If text is found, a pointer to the text is put in "*text". This
1260 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
1262 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001263find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267}
1268
1269/*
1270 * Like find_ident_under_cursor(), but for any window and any position.
1271 * However: Uses 'iskeyword' from the current window!.
1272 */
1273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274find_ident_at_pos(
1275 win_T *wp,
1276 linenr_T lnum,
1277 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001278 char_u **text,
1279 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001280 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001283 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int this_class = 0;
1286 int prev_class;
1287 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001288 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001290 // if i == 0: try to find an identifier
1291 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1293 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1294 {
1295 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (has_mbyte)
1300 {
1301 while (ptr[col] != NUL)
1302 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001303 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 this_class = mb_get_class(ptr + col);
1307 if (this_class != 0 && (i == 1 || this_class != 1))
1308 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 )
1317 ++col;
1318
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001319 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (has_mbyte)
1326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1329 this_class = mb_get_class((char_u *)"a");
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001332 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1335 prev_class = mb_get_class(ptr + prevcol);
1336 if (this_class != prev_class
1337 && (i == 0
1338 || prev_class == 0
1339 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && (!(find_type & FIND_EVAL)
1341 || prevcol == 0
1342 || !find_is_eval_item(ptr + prevcol, &prevcol,
1343 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 )
1345 break;
1346 col = prevcol;
1347 }
1348
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001349 // If we don't want just any old text, or we've found an
1350 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (this_class > 2)
1352 this_class = 2;
1353 if (!(find_type & FIND_STRING) || this_class == 2)
1354 break;
1355 }
1356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 while (col > 0
1359 && ((i == 0
1360 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001361 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 && (!(find_type & FIND_IDENT)
1363 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || ((find_type & FIND_EVAL)
1365 && col > 1
1366 && find_is_eval_item(ptr + col - 1, &col,
1367 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ))
1369 --col;
1370
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001371 // If we don't want just any old text, or we've found an
1372 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1374 break;
1375 }
1376 }
1377
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001378 if (ptr[col] == NUL || (i == 0
1379 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001381 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001382 if ((find_type & FIND_NOERROR) == 0)
1383 {
1384 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001385 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return 0;
1390 }
1391 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001392 *text = ptr;
1393 if (textcol != NULL)
1394 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 bn = 0;
1400 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (has_mbyte)
1403 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001404 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 this_class = mb_get_class(ptr);
1406 while (ptr[col] != NUL
1407 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1408 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || ((find_type & FIND_EVAL)
1410 && col <= (int)startcol
1411 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001413 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001417 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 || ((find_type & FIND_EVAL)
1419 && col <= (int)startcol
1420 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 return col;
1425}
1426
1427/*
1428 * Prepare for redo of a normal command.
1429 */
1430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001431prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 prep_redo(cap->oap->regname, cap->count0,
1434 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1435}
1436
1437/*
1438 * Prepare for redo of any command.
1439 * Note that only the last argument can be a multi-byte char.
1440 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001442prep_redo(
1443 int regname,
1444 long num,
1445 int cmd1,
1446 int cmd2,
1447 int cmd3,
1448 int cmd4,
1449 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001451 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1452}
1453
1454/*
1455 * Prepare for redo of any command with extra count after "cmd2".
1456 */
1457 void
1458prep_redo_num2(
1459 int regname,
1460 long num1,
1461 int cmd1,
1462 int cmd2,
1463 long num2,
1464 int cmd3,
1465 int cmd4,
1466 int cmd5)
1467{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001469
1470#ifdef FEAT_EVAL
1471 // Put info about a mapping in the redo buffer, so that "." will use the
1472 // same script context.
1473 may_add_last_used_map_to_redobuff();
1474#endif
1475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
1478 AppendCharToRedobuff('"');
1479 AppendCharToRedobuff(regname);
1480 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001481 if (num1 != 0)
1482 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (cmd1 != NUL)
1484 AppendCharToRedobuff(cmd1);
1485 if (cmd2 != NUL)
1486 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001487 if (num2 != 0)
1488 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (cmd3 != NUL)
1490 AppendCharToRedobuff(cmd3);
1491 if (cmd4 != NUL)
1492 AppendCharToRedobuff(cmd4);
1493 if (cmd5 != NUL)
1494 AppendCharToRedobuff(cmd5);
1495}
1496
1497/*
1498 * check for operator active and clear it
1499 *
1500 * return TRUE if operator was active
1501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 if (oap->op_type == OP_NOP)
1506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
1511/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001512 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001514 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001519 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001526clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 oap->op_type = OP_NOP;
1529 oap->regname = 0;
1530 oap->motion_force = NUL;
1531 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001532 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 clearop(oap);
1539 beep_flush();
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
1543 * Remove the shift modifier from a special key.
1544 */
1545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 switch (cap->cmdchar)
1549 {
1550 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1551 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1552 case K_S_UP: cap->cmdchar = K_UP; break;
1553 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1554 case K_S_HOME: cap->cmdchar = K_HOME; break;
1555 case K_S_END: cap->cmdchar = K_END; break;
1556 }
1557 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001560/*
1561 * If the mode is currently displayed clear the command line or update the
1562 * command displayed.
1563 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566{
1567 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001569#ifdef FEAT_CMDL_INFO
1570 else
1571 clear_showcmd();
1572#endif
1573}
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1576/*
1577 * Routines for displaying a partly typed command
1578 */
1579
kylo252ae6f1d82022-02-16 19:24:07 +00001580#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001582static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583static int showcmd_is_clear = TRUE;
1584static int showcmd_visual = FALSE;
1585
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001586static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001589clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
1591 if (!p_sc)
1592 return;
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (VIsual_active && !char_avail())
1595 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001596 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 long lines;
1598 colnr_T leftcol, rightcol;
1599 linenr_T top, bot;
1600
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001601 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001602 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 top = VIsual.lnum;
1605 bot = curwin->w_cursor.lnum;
1606 }
1607 else
1608 {
1609 top = curwin->w_cursor.lnum;
1610 bot = VIsual.lnum;
1611 }
1612# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001613 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001614 (void)hasFolding(top, &top, NULL);
1615 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616# endif
1617 lines = bot - top + 1;
1618
1619 if (VIsual_mode == Ctrl_V)
1620 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001621# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001622 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001623 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001625 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001626 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001627 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001631 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001632 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1635 (long)(rightcol - leftcol + 1));
1636 }
1637 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1638 sprintf((char *)showcmd_buf, "%ld", lines);
1639 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001640 {
1641 char_u *s, *e;
1642 int l;
1643 int bytes = 0;
1644 int chars = 0;
1645
1646 if (cursor_bot)
1647 {
1648 s = ml_get_pos(&VIsual);
1649 e = ml_get_cursor();
1650 }
1651 else
1652 {
1653 s = ml_get_cursor();
1654 e = ml_get_pos(&VIsual);
1655 }
1656 while ((*p_sel != 'e') ? s <= e : s < e)
1657 {
1658 l = (*mb_ptr2len)(s);
1659 if (l == 0)
1660 {
1661 ++bytes;
1662 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001663 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001664 }
1665 bytes += l;
1666 ++chars;
1667 s += l;
1668 }
1669 if (bytes == chars)
1670 sprintf((char *)showcmd_buf, "%d", chars);
1671 else
1672 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1673 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001674 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 showcmd_visual = TRUE;
1676 }
1677 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 showcmd_buf[0] = NUL;
1680 showcmd_visual = FALSE;
1681
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001682 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (showcmd_is_clear)
1684 return;
1685 }
1686
1687 display_showcmd();
1688}
1689
1690/*
1691 * Add 'c' to string of shown command chars.
1692 * Return TRUE if output has been written (and setcursor() has been called).
1693 */
1694 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001695add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 char_u *p;
1698 int old_len;
1699 int extra_len;
1700 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 int i;
1702 static int ignore[] =
1703 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001704#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1706 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001707#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001708 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001709 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1711 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001712 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001714 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 0
1716 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar09df3122006-01-23 22:23:09 +00001718 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return FALSE;
1720
1721 if (showcmd_visual)
1722 {
1723 showcmd_buf[0] = NUL;
1724 showcmd_visual = FALSE;
1725 }
1726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001727 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (IS_SPECIAL(c))
1729 for (i = 0; ignore[i] != 0; ++i)
1730 if (ignore[i] == c)
1731 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001734 if (*p == ' ')
1735 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 old_len = (int)STRLEN(showcmd_buf);
1737 extra_len = (int)STRLEN(p);
1738 overflow = old_len + extra_len - SHOWCMD_COLS;
1739 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001740 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1741 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 STRCAT(showcmd_buf, p);
1743
1744 if (char_avail())
1745 return FALSE;
1746
1747 display_showcmd();
1748
1749 return TRUE;
1750}
1751
1752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 if (!add_to_showcmd(c))
1756 setcursor();
1757}
1758
1759/*
1760 * Delete 'len' characters from the end of the shown command.
1761 */
1762 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001763del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 int old_len;
1766
1767 if (!p_sc)
1768 return;
1769
1770 old_len = (int)STRLEN(showcmd_buf);
1771 if (len > old_len)
1772 len = old_len;
1773 showcmd_buf[old_len - len] = NUL;
1774
1775 if (!char_avail())
1776 display_showcmd();
1777}
1778
1779/*
1780 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1781 * something and there is a partial mapping.
1782 */
1783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 if (p_sc)
1787 STRCPY(old_showcmd_buf, showcmd_buf);
1788}
1789
1790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001791pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 if (!p_sc)
1794 return;
1795
1796 STRCPY(showcmd_buf, old_showcmd_buf);
1797
1798 display_showcmd();
1799}
1800
1801 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001802display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 int len;
1805
1806 cursor_off();
1807
1808 len = (int)STRLEN(showcmd_buf);
1809 if (len == 0)
1810 showcmd_is_clear = TRUE;
1811 else
1812 {
1813 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1814 showcmd_is_clear = FALSE;
1815 }
1816
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001817 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1818 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001821 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822}
1823#endif
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825/*
1826 * When "check" is FALSE, prepare for commands that scroll the window.
1827 * When "check" is TRUE, take care of scroll-binding after the window has
1828 * scrolled. Called from normal_cmd() and edit().
1829 */
1830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001831do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
1833 static win_T *old_curwin = NULL;
1834 static linenr_T old_topline = 0;
1835#ifdef FEAT_DIFF
1836 static int old_topfill = 0;
1837#endif
1838 static buf_T *old_buf = NULL;
1839 static colnr_T old_leftcol = 0;
1840
1841 if (check && curwin->w_p_scb)
1842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001843 // If a ":syncbind" command was just used, don't scroll, only reset
1844 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (did_syncbind)
1846 did_syncbind = FALSE;
1847 else if (curwin == old_curwin)
1848 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001849 // Synchronize other windows, as necessary according to
1850 // 'scrollbind'. Don't do this after an ":edit" command, except
1851 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if ((curwin->w_buffer == old_buf
1853#ifdef FEAT_DIFF
1854 || curwin->w_p_diff
1855#endif
1856 )
1857 && (curwin->w_topline != old_topline
1858#ifdef FEAT_DIFF
1859 || curwin->w_topfill != old_topfill
1860#endif
1861 || curwin->w_leftcol != old_leftcol))
1862 {
1863 check_scrollbind(curwin->w_topline - old_topline,
1864 (long)(curwin->w_leftcol - old_leftcol));
1865 }
1866 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001869 // When switching between windows, make sure that the relative
1870 // vertical offset is valid for the new window. The relative
1871 // offset is invalid whenever another 'scrollbind' window has
1872 // scrolled to a point that would force the current window to
1873 // scroll past the beginning or end of its buffer. When the
1874 // resync is performed, some of the other 'scrollbind' windows may
1875 // need to jump so that the current window's relative position is
1876 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1878 }
1879 curwin->w_scbind_pos = curwin->w_topline;
1880 }
1881
1882 old_curwin = curwin;
1883 old_topline = curwin->w_topline;
1884#ifdef FEAT_DIFF
1885 old_topfill = curwin->w_topfill;
1886#endif
1887 old_buf = curwin->w_buffer;
1888 old_leftcol = curwin->w_leftcol;
1889}
1890
1891/*
1892 * Synchronize any windows that have "scrollbind" set, based on the
1893 * number of rows by which the current window has changed
1894 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1895 */
1896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001897check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int want_ver;
1900 int want_hor;
1901 win_T *old_curwin = curwin;
1902 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int old_VIsual_select = VIsual_select;
1904 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 colnr_T tgt_leftcol = curwin->w_leftcol;
1906 long topline;
1907 long y;
1908
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001909 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1911#ifdef FEAT_DIFF
1912 want_ver |= old_curwin->w_p_diff;
1913#endif
1914 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1915
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001916 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001918 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001921 // skip original window and windows with 'noscrollbind'
1922 if (curwin == old_curwin || !curwin->w_p_scb)
1923 continue;
1924
1925 // do the vertical scroll
1926 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001929 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
zeertzjq101d57b2022-07-31 18:34:32 +01001931 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
zeertzjq101d57b2022-07-31 18:34:32 +01001933 else
1934#endif
1935 {
1936 curwin->w_scbind_pos += topline_diff;
1937 topline = curwin->w_scbind_pos;
1938 if (topline > curbuf->b_ml.ml_line_count)
1939 topline = curbuf->b_ml.ml_line_count;
1940 if (topline < 1)
1941 topline = 1;
1942
1943 y = topline - curwin->w_topline;
1944 if (y > 0)
1945 scrollup(y, FALSE);
1946 else
1947 scrolldown(-y, FALSE);
1948 }
1949
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001950 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001951 cursor_correct();
1952 curwin->w_redr_status = TRUE;
1953 }
1954
1955 // do the horizontal scroll
1956 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1957 {
1958 curwin->w_leftcol = tgt_leftcol;
1959 leftcol_changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 }
1962
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001963 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 VIsual_select = old_VIsual_select;
1965 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 curwin = old_curwin;
1967 curbuf = old_curbuf;
1968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970/*
1971 * Command character that's ignored.
1972 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001973 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001978 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979}
1980
1981/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001982 * Command character that doesn't do anything, but unlike nv_ignore() does
1983 * start edit(). Used for "startinsert" executed while starting up.
1984 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001986nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001987{
1988}
1989
1990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 * Command character doesn't exist.
1992 */
1993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 clearopbeep(cap->oap);
1997}
1998
1999/*
2000 * <Help> and <F1> commands.
2001 */
2002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002003nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 if (!checkclearopq(cap->oap))
2006 ex_help(NULL);
2007}
2008
2009/*
2010 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2011 */
2012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002013nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002015#ifdef FEAT_JOB_CHANNEL
2016 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2017 clearopbeep(cap->oap);
2018 else
2019#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002020 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002021 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002022 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002023 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2024 op_addsub(cap->oap, cap->count1, cap->arg);
2025 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002026 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002027 else if (VIsual_active)
2028 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002029 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002030 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031}
2032
2033/*
2034 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2035 */
2036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002040 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 if (mod_mask & MOD_MASK_CTRL)
2042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 if (cap->arg == BACKWARD)
2045 goto_tabpage(-(int)cap->count1);
2046 else
2047 goto_tabpage((int)cap->count0);
2048 }
2049 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002050 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
2054/*
2055 * Implementation of "gd" and "gD" command.
2056 */
2057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002058nv_gd(
2059 oparg_T *oap,
2060 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 char_u *ptr;
2065
Bram Moolenaard9d30582005-05-18 22:10:28 +00002066 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002067 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002068 == FAIL)
2069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002071 }
2072 else
2073 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002074#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002075 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2076 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002077#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002078 // clear any search statistics
2079 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2080 clear_cmdline = TRUE;
2081 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002082}
2083
2084/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002085 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2086 * otherwise.
2087 */
2088 static int
2089is_ident(char_u *line, int offset)
2090{
2091 int i;
2092 int incomment = FALSE;
2093 int instring = 0;
2094 int prev = 0;
2095
2096 for (i = 0; i < offset && line[i] != NUL; i++)
2097 {
2098 if (instring != 0)
2099 {
2100 if (prev != '\\' && line[i] == instring)
2101 instring = 0;
2102 }
2103 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2104 {
2105 instring = line[i];
2106 }
2107 else
2108 {
2109 if (incomment)
2110 {
2111 if (prev == '*' && line[i] == '/')
2112 incomment = FALSE;
2113 }
2114 else if (prev == '/' && line[i] == '*')
2115 {
2116 incomment = TRUE;
2117 }
2118 else if (prev == '/' && line[i] == '/')
2119 {
2120 return FALSE;
2121 }
2122 }
2123
2124 prev = line[i];
2125 }
2126
2127 return incomment == FALSE && instring == 0;
2128}
2129
2130/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002131 * Search for variable declaration of "ptr[len]".
2132 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2133 * current file ("gD").
2134 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002135 * Return FAIL when not found.
2136 */
2137 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002138find_decl(
2139 char_u *ptr,
2140 int len,
2141 int locally,
2142 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002143 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002144{
2145 char_u *pat;
2146 pos_T old_pos;
2147 pos_T par_pos;
2148 pos_T found_pos;
2149 int t;
2150 int save_p_ws;
2151 int save_p_scs;
2152 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002153 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002154 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002155 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002156
2157 if ((pat = alloc(len + 7)) == NULL)
2158 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002160 // Put "\V" before the pattern to avoid that the special meaning of "."
2161 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002162 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2163 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 old_pos = curwin->w_cursor;
2165 save_p_ws = p_ws;
2166 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002167 p_ws = FALSE; // don't wrap around end of file now
2168 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002170 // With "gD" go to line 1.
2171 // With "gd" Search back for the start of the current function, then go
2172 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002173 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002175 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002177 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179 else
2180 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002181 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2183 --curwin->w_cursor.lnum;
2184 }
2185 curwin->w_cursor.col = 0;
2186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002188 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002189 for (;;)
2190 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002191 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002192 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002193 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002196 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002197 {
2198 pos_T *pos;
2199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // Check that the block the match is in doesn't end before the
2201 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002202 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2203 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2204 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002205 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 // There can't be a useful match before the end of this block.
2207 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002208 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002209 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002210 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002211 }
2212
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002213 if (t == FAIL)
2214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002215 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002216 if (found_pos.lnum != 0)
2217 {
2218 curwin->w_cursor = found_pos;
2219 t = OK;
2220 }
2221 break;
2222 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002223 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002226 ++curwin->w_cursor.lnum;
2227 curwin->w_cursor.col = 0;
2228 continue;
2229 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002230 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // If the current position is not a valid identifier and a previous
2233 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 if (!valid && found_pos.lnum != 0)
2235 {
2236 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002238 }
2239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002240 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002241 if (valid && !locally)
2242 break;
2243 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002246 if (found_pos.lnum != 0)
2247 curwin->w_cursor = found_pos;
2248 break;
2249 }
2250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // For finding a local variable and the match is before the "{" or
2252 // inside a comment, continue searching. For K&R style function
2253 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002254 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002255 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002256 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002257 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 // Remove SEARCH_START from flags to avoid getting stuck at one
2259 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002260 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002262
2263 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002265 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curwin->w_cursor = old_pos;
2267 }
2268 else
2269 {
2270 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 reset_search_dir();
2273 }
2274
2275 vim_free(pat);
2276 p_ws = save_p_ws;
2277 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002278
2279 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280}
2281
2282/*
2283 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2284 * lines rather than lines in the file.
2285 * 'dist' must be positive.
2286 *
2287 * Return OK if able to move cursor, FAIL otherwise.
2288 */
2289 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002290nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 int linelen = linetabsize(ml_get_curline());
2293 int retval = OK;
2294 int atend = FALSE;
2295 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002296 int col_off1; // margin offset for first screen line
2297 int col_off2; // margin offset for wrapped screen line
2298 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002299 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002302 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 col_off1 = curwin_col_off();
2305 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002306 width1 = curwin->w_width - col_off1;
2307 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002308 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002309 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002312 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002313 // Instead of sticking at the last character of the buffer line we
2314 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (curwin->w_curswant == MAXCOL)
2316 {
2317 atend = TRUE;
2318 validate_virtcol();
2319 if (width1 <= 0)
2320 curwin->w_curswant = 0;
2321 else
2322 {
2323 curwin->w_curswant = width1 - 1;
2324 if (curwin->w_virtcol > curwin->w_curswant)
2325 curwin->w_curswant += ((curwin->w_virtcol
2326 - curwin->w_curswant - 1) / width2 + 1) * width2;
2327 }
2328 }
2329 else
2330 {
2331 if (linelen > width1)
2332 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2333 else
2334 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002335 if (curwin->w_curswant >= (colnr_T)n)
2336 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338
2339 while (dist--)
2340 {
2341 if (dir == BACKWARD)
2342 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002343 if ((long)curwin->w_curswant >= width1
2344#ifdef FEAT_FOLDING
2345 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2346#endif
2347 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002348 // Move back within the line. This can give a negative value
2349 // for w_curswant if width1 < width2 (with cpoptions+=n),
2350 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 curwin->w_curswant -= width2;
2352 else
2353 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002354 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002356 // Move to the start of a closed fold. Don't do that when
2357 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (!(fdo_flags & FDO_ALL))
2359 (void)hasFolding(curwin->w_cursor.lnum,
2360 &curwin->w_cursor.lnum, NULL);
2361#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002362 if (curwin->w_cursor.lnum == 1)
2363 {
2364 retval = FAIL;
2365 break;
2366 }
2367 --curwin->w_cursor.lnum;
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 linelen = linetabsize(ml_get_curline());
2370 if (linelen > width1)
2371 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2372 + 1) * width2;
2373 }
2374 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 if (linelen > width1)
2378 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2379 else
2380 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002381 if (curwin->w_curswant + width2 < (colnr_T)n
2382#ifdef FEAT_FOLDING
2383 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2384#endif
2385 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 curwin->w_curswant += width2;
2388 else
2389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002390 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002392 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2394 &curwin->w_cursor.lnum);
2395#endif
2396 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2397 {
2398 retval = FAIL;
2399 break;
2400 }
2401 curwin->w_cursor.lnum++;
2402 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002403 // Check if the cursor has moved below the number display
2404 // when width1 < width2 (with cpoptions+=n). Subtract width2
2405 // to get a negative value for w_curswant, which will get
2406 // clipped to column 0.
2407 if (curwin->w_curswant >= width1)
2408 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002409 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411 }
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002415 if (virtual_active() && atend)
2416 coladvance(MAXCOL);
2417 else
2418 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2421 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002422 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002423 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002424
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002425 // Check for landing on a character that got split at the end of the
2426 // last line. We want to advance a screenline, not end up in the same
2427 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002429 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002430#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002431 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2432 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002433#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002434
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002435 c = (*mb_ptr2char)(ml_get_cursor());
2436 if (dir == FORWARD && virtcol < curwin->w_curswant
2437 && (curwin->w_curswant <= (colnr_T)width1)
2438 && !vim_isprintc(c) && c > 255)
2439 oneright();
2440
Bram Moolenaar773b1582014-08-29 14:20:51 +02002441 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 && (curwin->w_curswant < (colnr_T)width1
2443 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2444 : ((curwin->w_curswant - width1) % width2
2445 > (colnr_T)width2 / 2)))
2446 --curwin->w_cursor.col;
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002450 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002451 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 return retval;
2454}
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456/*
2457 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2458 * cap->arg must be TRUE for CTRL-E.
2459 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 if (!checkclearop(cap->oap))
2464 scroll_redraw(cap->arg, cap->count1);
2465}
2466
2467/*
2468 * Scroll "count" lines up or down, and redraw.
2469 */
2470 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002471scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002474 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_DIFF
2476 int prev_topfill = curwin->w_topfill;
2477#endif
2478 linenr_T prev_lnum = curwin->w_cursor.lnum;
2479
2480 if (up)
2481 scrollup(count, TRUE);
2482 else
2483 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002484 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2487 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 cursor_correct();
2489 check_cursor_moved(curwin);
2490 curwin->w_valid |= VALID_TOPLINE;
2491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002492 // If moved back to where we were, at least move the cursor, otherwise
2493 // we get stuck at one position. Don't move the cursor up if the
2494 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002496 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497#ifdef FEAT_DIFF
2498 && curwin->w_topfill == prev_topfill
2499#endif
2500 )
2501 {
2502 if (up)
2503 {
2504 if (curwin->w_cursor.lnum > prev_lnum
2505 || cursor_down(1L, FALSE) == FAIL)
2506 break;
2507 }
2508 else
2509 {
2510 if (curwin->w_cursor.lnum < prev_lnum
2511 || prev_topline == 1L
2512 || cursor_up(1L, FALSE) == FAIL)
2513 break;
2514 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002515 // Mark w_topline as valid, otherwise the screen jumps back at the
2516 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 check_cursor_moved(curwin);
2518 curwin->w_valid |= VALID_TOPLINE;
2519 }
2520 }
2521 if (curwin->w_cursor.lnum != prev_lnum)
2522 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002523 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524}
2525
2526/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002527 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2528 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2529 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002530 */
2531 static int
2532nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2533{
2534 int nchar = *nchar_arg;
2535 long n;
2536
2537 // "z123{nchar}": edit the count before obtaining {nchar}
2538 if (checkclearop(cap->oap))
2539 return FALSE;
2540 n = nchar - '0';
2541
2542 for (;;)
2543 {
2544#ifdef USE_ON_FLY_SCROLL
2545 dont_scroll = TRUE; // disallow scrolling here
2546#endif
2547 ++no_mapping;
2548 ++allow_keys; // no mapping for nchar, but allow key codes
2549 nchar = plain_vgetc();
2550 LANGMAP_ADJUST(nchar, TRUE);
2551 --no_mapping;
2552 --allow_keys;
2553#ifdef FEAT_CMDL_INFO
2554 (void)add_to_showcmd(nchar);
2555#endif
2556 if (nchar == K_DEL || nchar == K_KDEL)
2557 n /= 10;
2558 else if (VIM_ISDIGIT(nchar))
2559 n = n * 10 + (nchar - '0');
2560 else if (nchar == CAR)
2561 {
2562#ifdef FEAT_GUI
2563 need_mouse_correct = TRUE;
2564#endif
2565 win_setheight((int)n);
2566 break;
2567 }
2568 else if (nchar == 'l'
2569 || nchar == 'h'
2570 || nchar == K_LEFT
2571 || nchar == K_RIGHT)
2572 {
2573 cap->count1 = n ? n * cap->count1 : cap->count1;
2574 *nchar_arg = nchar;
2575 return TRUE;
2576 }
2577 else
2578 {
2579 clearopbeep(cap->oap);
2580 break;
2581 }
2582 }
2583 cap->oap->op_type = OP_NOP;
2584 return FALSE;
2585}
2586
2587#ifdef FEAT_SPELL
2588/*
2589 * "zug" and "zuw": undo "zg" and "zw"
2590 * "zg": add good word to word list
2591 * "zw": add wrong word to word list
2592 * "zG": add good word to temp word list
2593 * "zW": add wrong word to temp word list
2594 */
2595 static int
2596nv_zg_zw(cmdarg_T *cap, int nchar)
2597{
2598 char_u *ptr = NULL;
2599 int len;
2600 int undo = FALSE;
2601
2602 if (nchar == 'u')
2603 {
2604 ++no_mapping;
2605 ++allow_keys; // no mapping for nchar, but allow key codes
2606 nchar = plain_vgetc();
2607 LANGMAP_ADJUST(nchar, TRUE);
2608 --no_mapping;
2609 --allow_keys;
2610#ifdef FEAT_CMDL_INFO
2611 (void)add_to_showcmd(nchar);
2612#endif
2613 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2614 {
2615 clearopbeep(cap->oap);
2616 return OK;
2617 }
2618 undo = TRUE;
2619 }
2620
2621 if (checkclearop(cap->oap))
2622 return OK;
2623 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2624 return FAIL;
2625 if (ptr == NULL)
2626 {
2627 pos_T pos = curwin->w_cursor;
2628
2629 // Find bad word under the cursor. When 'spell' is
2630 // off this fails and find_ident_under_cursor() is
2631 // used below.
2632 emsg_off++;
2633 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2634 emsg_off--;
2635 if (len != 0 && curwin->w_cursor.col <= pos.col)
2636 ptr = ml_get_pos(&curwin->w_cursor);
2637 curwin->w_cursor = pos;
2638 }
2639
2640 if (ptr == NULL
2641 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2642 return FAIL;
2643 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2644 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2645 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2646
2647 return OK;
2648}
2649#endif
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * Commands that start with "z".
2653 */
2654 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002655nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 long n;
2658 colnr_T col;
2659 int nchar = cap->nchar;
2660#ifdef FEAT_FOLDING
2661 long old_fdl = curwin->w_p_fdl;
2662 int old_fen = curwin->w_p_fen;
2663#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002664 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002666 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (
2670#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2672 // and "zC" only in Visual mode. "zj" and "zk" are motion
2673 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 cap->nchar != 'f' && cap->nchar != 'F'
2675 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2676 && cap->nchar != 'j' && cap->nchar != 'k'
2677 &&
2678#endif
2679 checkclearop(cap->oap))
2680 return;
2681
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002682 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2683 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2685 && cap->count0
2686 && cap->count0 != curwin->w_cursor.lnum)
2687 {
2688 setpcmark();
2689 if (cap->count0 > curbuf->b_ml.ml_line_count)
2690 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2691 else
2692 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002693 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695
2696 switch (nchar)
2697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 case '+':
2700 if (cap->count0 == 0)
2701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002702 // No count given: put cursor at the line below screen
2703 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2705 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2706 else
2707 curwin->w_cursor.lnum = curwin->w_botline;
2708 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 case NL:
2711 case CAR:
2712 case K_KENTER:
2713 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002717 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002718 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 break;
2720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002726 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002727 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 break;
2729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // "z^", "z-" and "zb": put cursor at bottom of screen
2731 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2732 // when <count> is at bottom of window, and puts that one at
2733 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (cap->count0 != 0)
2735 {
2736 scroll_cursor_bot(0, TRUE);
2737 curwin->w_cursor.lnum = curwin->w_topline;
2738 }
2739 else if (curwin->w_topline == 1)
2740 curwin->w_cursor.lnum = 1;
2741 else
2742 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 case '-':
2745 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002749 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002750 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 break;
2752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002755 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 case 'h':
2760 case K_LEFT:
2761 if (!curwin->w_p_wrap)
2762 {
2763 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2764 curwin->w_leftcol = 0;
2765 else
2766 curwin->w_leftcol -= (colnr_T)cap->count1;
2767 leftcol_changed();
2768 }
2769 break;
2770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002772 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 case 'l':
2777 case K_RIGHT:
2778 if (!curwin->w_p_wrap)
2779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 curwin->w_leftcol += (colnr_T)cap->count1;
2782 leftcol_changed();
2783 }
2784 break;
2785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 case 's': if (!curwin->w_p_wrap)
2788 {
2789#ifdef FEAT_FOLDING
2790 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 else
2793#endif
2794 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002795 if ((long)col > siso)
2796 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 else
2798 col = 0;
2799 if (curwin->w_leftcol != col)
2800 {
2801 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002802 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 }
2805 break;
2806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002807 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 case 'e': if (!curwin->w_p_wrap)
2809 {
2810#ifdef FEAT_FOLDING
2811 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002812 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 else
2814#endif
2815 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002816 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002817 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 col = 0;
2819 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002820 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (curwin->w_leftcol != col)
2822 {
2823 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002824 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826 }
2827 break;
2828
Christian Brabandt2fa93842021-05-30 22:17:25 +02002829 // "zp", "zP" in block mode put without addind trailing spaces
2830 case 'P':
2831 case 'p': nv_put(cap);
2832 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002833 // "zy" Yank without trailing spaces
2834 case 'y': nv_operator(cap);
2835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // "zF": create fold command
2838 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 case 'F':
2840 case 'f': if (foldManualAllowed(TRUE))
2841 {
2842 cap->nchar = 'f';
2843 nv_operator(cap);
2844 curwin->w_p_fen = TRUE;
2845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2848 {
2849 nv_operator(cap);
2850 finish_op = TRUE;
2851 }
2852 }
2853 else
2854 clearopbeep(cap->oap);
2855 break;
2856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // "zd": delete fold at cursor
2858 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case 'd':
2860 case 'D': if (foldManualAllowed(FALSE))
2861 {
2862 if (VIsual_active)
2863 nv_operator(cap);
2864 else
2865 deleteFold(curwin->w_cursor.lnum,
2866 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2867 }
2868 break;
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case 'E': if (foldmethodIsManual(curwin))
2872 {
2873 clearFolding(curwin);
2874 changed_window_setting();
2875 }
2876 else if (foldmethodIsMarker(curwin))
2877 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2878 TRUE, FALSE);
2879 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002880 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 break;
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case 'n': curwin->w_p_fen = FALSE;
2885 break;
2886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case 'N': curwin->w_p_fen = TRUE;
2889 break;
2890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2893 break;
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2897 openFold(curwin->w_cursor.lnum, cap->count1);
2898 else
2899 {
2900 closeFold(curwin->w_cursor.lnum, cap->count1);
2901 curwin->w_p_fen = TRUE;
2902 }
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2907 openFoldRecurse(curwin->w_cursor.lnum);
2908 else
2909 {
2910 closeFoldRecurse(curwin->w_cursor.lnum);
2911 curwin->w_p_fen = TRUE;
2912 }
2913 break;
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 case 'o': if (VIsual_active)
2917 nv_operator(cap);
2918 else
2919 openFold(curwin->w_cursor.lnum, cap->count1);
2920 break;
2921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 case 'O': if (VIsual_active)
2924 nv_operator(cap);
2925 else
2926 openFoldRecurse(curwin->w_cursor.lnum);
2927 break;
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 case 'c': if (VIsual_active)
2931 nv_operator(cap);
2932 else
2933 closeFold(curwin->w_cursor.lnum, cap->count1);
2934 curwin->w_p_fen = TRUE;
2935 break;
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 case 'C': if (VIsual_active)
2939 nv_operator(cap);
2940 else
2941 closeFoldRecurse(curwin->w_cursor.lnum);
2942 curwin->w_p_fen = TRUE;
2943 break;
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 case 'v': foldOpenCursor();
2947 break;
2948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // "zx": re-apply 'foldlevel' and open folds at the cursor
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 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 foldOpenCursor();
2954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 curwin->w_foldinvalid = TRUE; // recompute folds
2959 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 break;
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002964 {
2965 curwin->w_p_fdl -= cap->count1;
2966 if (curwin->w_p_fdl < 0)
2967 curwin->w_p_fdl = 0;
2968 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 curwin->w_p_fen = TRUE;
2971 break;
2972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 curwin->w_p_fen = TRUE;
2977 break;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002980 case 'r': curwin->w_p_fdl += cap->count1;
2981 {
2982 int d = getDeepestNesting();
2983
2984 if (curwin->w_p_fdl >= d)
2985 curwin->w_p_fdl = d;
2986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 break;
2988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 break;
2993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 case 'j': // "zj" move to next fold downwards
2995 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2997 cap->count1) == FAIL)
2998 clearopbeep(cap->oap);
2999 break;
3000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003003#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 case 'g': // "zg": add good word to word list
3006 case 'w': // "zw": add wrong word to word list
3007 case 'G': // "zG": add good word to temp word list
3008 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003009 if (nv_zg_zw(cap, nchar) == FAIL)
3010 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003011 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003014 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003015 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003017#endif
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 default: clearopbeep(cap->oap);
3020 }
3021
3022#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (old_fen != curwin->w_p_fen)
3025 {
3026# ifdef FEAT_DIFF
3027 win_T *wp;
3028
3029 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003031 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 FOR_ALL_WINDOWS(wp)
3033 {
3034 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3035 {
3036 wp->w_p_fen = curwin->w_p_fen;
3037 changed_window_setting_win(wp);
3038 }
3039 }
3040 }
3041# endif
3042 changed_window_setting();
3043 }
3044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (old_fdl != curwin->w_p_fdl)
3047 newFoldLevel();
3048#endif
3049}
3050
3051#ifdef FEAT_GUI
3052/*
3053 * Vertical scrollbar movement.
3054 */
3055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003056nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 if (cap->oap->op_type != OP_NOP)
3059 clearopbeep(cap->oap);
3060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003061 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 gui_do_scroll();
3063}
3064
3065/*
3066 * Horizontal scrollbar movement.
3067 */
3068 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003069nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 if (cap->oap->op_type != OP_NOP)
3072 clearopbeep(cap->oap);
3073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003075 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077#endif
3078
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003079#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003080/*
3081 * Click in GUI tab.
3082 */
3083 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003084nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003085{
3086 if (cap->oap->op_type != OP_NOP)
3087 clearopbeep(cap->oap);
3088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003090 goto_tabpage(current_tab);
3091}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003092
3093/*
3094 * Selected item in tab line menu.
3095 */
3096 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003097nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003098{
3099 if (cap->oap->op_type != OP_NOP)
3100 clearopbeep(cap->oap);
3101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003103 handle_tabmenu();
3104}
3105
3106/*
3107 * Handle selecting an item of the GUI tab line menu.
3108 * Used in Normal and Insert mode.
3109 */
3110 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003111handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003112{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003113 switch (current_tabmenu)
3114 {
3115 case TABLINE_MENU_CLOSE:
3116 if (current_tab == 0)
3117 do_cmdline_cmd((char_u *)"tabclose");
3118 else
3119 {
3120 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3121 current_tab);
3122 do_cmdline_cmd(IObuff);
3123 }
3124 break;
3125
3126 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003127 if (current_tab == 0)
3128 do_cmdline_cmd((char_u *)"$tabnew");
3129 else
3130 {
3131 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3132 current_tab - 1);
3133 do_cmdline_cmd(IObuff);
3134 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003135 break;
3136
3137 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003138 if (current_tab == 0)
3139 do_cmdline_cmd((char_u *)"browse $tabnew");
3140 else
3141 {
3142 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3143 current_tab - 1);
3144 do_cmdline_cmd(IObuff);
3145 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003146 break;
3147 }
3148}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003149#endif
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151/*
3152 * "Q" command.
3153 */
3154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003155nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003157 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003159 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003160 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 do_exmode(FALSE);
3162}
3163
3164/*
3165 * Handle a ":" command.
3166 */
3167 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003168nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003170 int old_p_im;
3171 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003172 int is_cmdkey = cap->cmdchar == K_COMMAND
3173 || cap->cmdchar == K_SCRIPT_COMMAND;
3174 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaar957cf672020-11-12 14:21:06 +01003176 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 nv_operator(cap);
3178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 if (cap->oap->op_type != OP_NOP)
3181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 cap->oap->motion_type = MCHAR;
3184 cap->oap->inclusive = FALSE;
3185 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003186 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 stuffcharReadbuff('.');
3190 if (cap->count0 > 1)
3191 {
3192 stuffReadbuff((char_u *)",.+");
3193 stuffnumReadbuff((long)cap->count0 - 1L);
3194 }
3195 }
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (KeyTyped)
3199 compute_cmdrow();
3200
3201 old_p_im = p_im;
3202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003204 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3205 if (is_cmdkey)
3206 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3207 else
3208 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (p_im != old_p_im)
3212 {
3213 if (p_im)
3214 restart_edit = 'i';
3215 else
3216 restart_edit = 0;
3217 }
3218
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003219 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003221 clearop(cap->oap);
3222 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3224 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003225 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3226 || did_emsg
3227 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003228 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 clearopbeep(cap->oap);
3230 }
3231}
3232
3233/*
3234 * Handle CTRL-G command.
3235 */
3236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003237nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003239 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
3241 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003242 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 showmode();
3244 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003245 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003246 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 fileinfo((int)cap->count0, FALSE, TRUE);
3248}
3249
3250/*
3251 * Handle CTRL-H <Backspace> command.
3252 */
3253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (VIsual_active && VIsual_select)
3257 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003258 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 v_visop(cap);
3260 }
3261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 nv_left(cap);
3263}
3264
3265/*
3266 * CTRL-L: clear screen and redraw.
3267 */
3268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 if (!checkclearop(cap->oap))
3272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003275 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003276# ifdef FEAT_RELTIME
3277 {
3278 win_T *wp;
3279
3280 FOR_ALL_WINDOWS(wp)
3281 wp->w_s->b_syn_slow = FALSE;
3282 }
3283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003285 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003286#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3287# ifdef VIMDLL
3288 if (!gui.in_use)
3289# endif
3290 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293}
3294
3295/*
3296 * CTRL-O: In Select mode: switch to Visual mode for one command.
3297 * Otherwise: Go to older pcmark.
3298 */
3299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (VIsual_active && VIsual_select)
3303 {
3304 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003305 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003307 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 cap->count1 = -cap->count1;
3312 nv_pcmark(cap);
3313 }
3314}
3315
3316/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003317 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3318 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
3320 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003321nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 if (!checkclearopq(cap->oap))
3324 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3325 GETF_SETMARK|GETF_ALT, FALSE);
3326}
3327
3328/*
3329 * "Z" commands.
3330 */
3331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 if (!checkclearopq(cap->oap))
3335 {
3336 switch (cap->nchar)
3337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 case 'Z': do_cmdline_cmd((char_u *)"x");
3340 break;
3341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003342 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 case 'Q': do_cmdline_cmd((char_u *)"q!");
3344 break;
3345
3346 default: clearopbeep(cap->oap);
3347 }
3348 }
3349}
3350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351/*
3352 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3353 */
3354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003355do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 oparg_T oa;
3358 cmdarg_T ca;
3359
3360 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003361 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 ca.oap = &oa;
3363 ca.cmdchar = c1;
3364 ca.nchar = c2;
3365 nv_ident(&ca);
3366}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
3368/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003369 * 'K' normal-mode command. Get the command to lookup the keyword under the
3370 * cursor.
3371 */
3372 static int
3373nv_K_getcmd(
3374 cmdarg_T *cap,
3375 char_u *kp,
3376 int kp_help,
3377 int kp_ex,
3378 char_u **ptr_arg,
3379 int n,
3380 char_u *buf,
3381 unsigned buflen)
3382{
3383 char_u *ptr = *ptr_arg;
3384 int isman;
3385 int isman_s;
3386
3387 if (kp_help)
3388 {
3389 // in the help buffer
3390 STRCPY(buf, "he! ");
3391 return n;
3392 }
3393
3394 if (kp_ex)
3395 {
3396 // 'keywordprog' is an ex command
3397 if (cap->count0 != 0)
3398 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3399 else
3400 STRCPY(buf, kp);
3401 STRCAT(buf, " ");
3402 return n;
3403 }
3404
3405 // An external command will probably use an argument starting
3406 // with "-" as an option. To avoid trouble we skip the "-".
3407 while (*ptr == '-' && n > 0)
3408 {
3409 ++ptr;
3410 --n;
3411 }
3412 if (n == 0)
3413 {
3414 // found dashes only
3415 emsg(_(e_no_identifier_under_cursor));
3416 vim_free(buf);
3417 *ptr_arg = ptr;
3418 return 0;
3419 }
3420
3421 // When a count is given, turn it into a range. Is this
3422 // really what we want?
3423 isman = (STRCMP(kp, "man") == 0);
3424 isman_s = (STRCMP(kp, "man -s") == 0);
3425 if (cap->count0 != 0 && !(isman || isman_s))
3426 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3427
3428 STRCAT(buf, "! ");
3429 if (cap->count0 == 0 && isman_s)
3430 STRCAT(buf, "man");
3431 else
3432 STRCAT(buf, kp);
3433 STRCAT(buf, " ");
3434 if (cap->count0 != 0 && (isman || isman_s))
3435 {
3436 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3437 STRCAT(buf, " ");
3438 }
3439
3440 *ptr_arg = ptr;
3441 return n;
3442}
3443
3444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * Handle the commands that use the word under the cursor.
3446 * [g] CTRL-] :ta to current identifier
3447 * [g] 'K' run program for current identifier
3448 * [g] '*' / to current identifier or string
3449 * [g] '#' ? to current identifier or string
3450 * g ']' :tselect for current identifier
3451 */
3452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003453nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 char_u *ptr = NULL;
3456 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003457 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003458 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 char_u *kp; // value of 'keywordprg'
3461 int kp_help; // 'keywordprg' is ":he"
3462 int kp_ex; // 'keywordprg' starts with ":"
3463 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003466 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003469 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 cmdchar = cap->nchar;
3472 g_cmd = TRUE;
3473 }
3474 else
3475 {
3476 cmdchar = cap->cmdchar;
3477 g_cmd = FALSE;
3478 }
3479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 cmdchar = '#';
3482
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003483 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3487 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (checkclearopq(cap->oap))
3489 return;
3490 }
3491
3492 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3493 (cmdchar == '*' || cmdchar == '#')
3494 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3495 {
3496 clearop(cap->oap);
3497 return;
3498 }
3499
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 // Allocate buffer to put the command in. Inserting backslashes can
3501 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3502 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3504 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3505 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003506 if (kp_help && *skipwhite(ptr) == NUL)
3507 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003508 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003509 return;
3510 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003511 kp_ex = (*kp == ':');
3512 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3513 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (buf == NULL)
3515 return;
3516 buf[0] = NUL;
3517
3518 switch (cmdchar)
3519 {
3520 case '*':
3521 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003522 // Put cursor at start of word, makes search skip the word
3523 // under the cursor.
3524 // Call setpcmark() first, so "*``" puts the cursor back where
3525 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 setpcmark();
3527 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3528
3529 if (!g_cmd && vim_iswordp(ptr))
3530 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003531 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533
3534 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003535 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3536 if (n == 0)
3537 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 break;
3539
3540 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003541 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#ifdef FEAT_CSCOPE
3543 if (p_cst)
3544 STRCPY(buf, "cstag ");
3545 else
3546#endif
3547 STRCPY(buf, "ts ");
3548 break;
3549
3550 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003551 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (curbuf->b_help)
3553 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003555 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003556 if (g_cmd)
3557 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003558 else if (cap->count0 == 0)
3559 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003560 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003561 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003565 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003566 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003568 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003569 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003570 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003571 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003572 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003573 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003574 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003575 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003576 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 vim_free(buf);
3579 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003581 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003582 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003583 {
3584 vim_free(buf);
3585 vim_free(p);
3586 return;
3587 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003588 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 STRCAT(buf, p);
3590 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003592 else
3593 {
3594 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003595 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003597 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003598 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003599 {
3600 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003601 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003602 aux_ptr = (char_u *)"";
3603 else
3604 aux_ptr = (char_u *)"\\|\"\n[";
3605 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003606 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 aux_ptr = (char_u *)"\\|\"\n*?[";
3608
3609 p = buf + STRLEN(buf);
3610 while (n-- > 0)
3611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003612 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003613 if (vim_strchr(aux_ptr, *ptr) != NULL)
3614 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003615 // When current byte is a part of multibyte character, copy all
3616 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003617 if (has_mbyte)
3618 {
3619 int i;
3620 int len = (*mb_ptr2len)(ptr) - 1;
3621
3622 for (i = 0; i < len && n >= 1; ++i, --n)
3623 *p++ = *ptr++;
3624 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003625 *p++ = *ptr++;
3626 }
3627 *p = NUL;
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003630 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (cmdchar == '*' || cmdchar == '#')
3632 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003633 if (!g_cmd && (has_mbyte
3634 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3635 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003637
3638 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003639 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003641
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003642 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003645 {
3646 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003648 g_tag_at_cursor = FALSE;
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 vim_free(buf);
3652}
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654/*
3655 * Get visually selected text, within one line only.
3656 * Returns FAIL if more than one line selected.
3657 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003658 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003659get_visual_text(
3660 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 char_u **pp, // return: start of selected text
3662 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 if (VIsual_mode != 'V')
3665 unadjust_for_sel();
3666 if (VIsual.lnum != curwin->w_cursor.lnum)
3667 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003668 if (cap != NULL)
3669 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671 }
3672 if (VIsual_mode == 'V')
3673 {
3674 *pp = ml_get_curline();
3675 *lenp = (int)STRLEN(*pp);
3676 }
3677 else
3678 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003679 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 *pp = ml_get_pos(&curwin->w_cursor);
3682 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3683 }
3684 else
3685 {
3686 *pp = ml_get_pos(&VIsual);
3687 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3688 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003689 if (**pp == NUL)
3690 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003691 if (*lenp > 0)
3692 {
3693 if (has_mbyte)
3694 // Correct the length to include all bytes of the last
3695 // character.
3696 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3697 else if ((*pp)[*lenp - 1] == NUL)
3698 // Do not include a trailing NUL.
3699 *lenp -= 1;
3700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702 reset_VIsual_and_resel();
3703 return OK;
3704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
3706/*
3707 * CTRL-T: backwards in tag stack
3708 */
3709 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 if (!checkclearopq(cap->oap))
3713 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3714}
3715
3716/*
3717 * Handle scrolling command 'H', 'L' and 'M'.
3718 */
3719 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003720nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 int used = 0;
3723 long n;
3724#ifdef FEAT_FOLDING
3725 linenr_T lnum;
3726#endif
3727 int half;
3728
3729 cap->oap->motion_type = MLINE;
3730 setpcmark();
3731
3732 if (cap->cmdchar == 'L')
3733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003734 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 curwin->w_cursor.lnum = curwin->w_botline - 1;
3736 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3737 curwin->w_cursor.lnum = 1;
3738 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003739 {
3740#ifdef FEAT_FOLDING
3741 if (hasAnyFolding(curwin))
3742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003743 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003744 for (n = cap->count1 - 1; n > 0
3745 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3746 {
3747 (void)hasFolding(curwin->w_cursor.lnum,
3748 &curwin->w_cursor.lnum, NULL);
3749 --curwin->w_cursor.lnum;
3750 }
3751 }
3752 else
3753#endif
3754 curwin->w_cursor.lnum -= cap->count1 - 1;
3755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
3759 if (cap->cmdchar == 'M')
3760 {
3761#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003762 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 used -= diff_check_fill(curwin, curwin->w_topline)
3764 - curwin->w_topfill;
3765#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3768 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3769 {
3770#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003771 // Count half he number of filler lines to be "below this
3772 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3774 + n) / 2 >= half)
3775 {
3776 --n;
3777 break;
3778 }
3779#endif
3780 used += plines(curwin->w_topline + n);
3781 if (used >= half)
3782 break;
3783#ifdef FEAT_FOLDING
3784 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3785 n = lnum - curwin->w_topline;
3786#endif
3787 }
3788 if (n > 0 && used > curwin->w_height)
3789 --n;
3790 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003791 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003794#ifdef FEAT_FOLDING
3795 if (hasAnyFolding(curwin))
3796 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003797 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003798 lnum = curwin->w_topline;
3799 while (n-- > 0 && lnum < curwin->w_botline - 1)
3800 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003801 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003802 ++lnum;
3803 }
3804 n = lnum - curwin->w_topline;
3805 }
3806#endif
3807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 curwin->w_cursor.lnum = curwin->w_topline + n;
3809 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3810 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3811 }
3812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003814 if (cap->oap->op_type == OP_NOP)
3815 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 beginline(BL_SOL | BL_FIX);
3817}
3818
3819/*
3820 * Cursor right commands.
3821 */
3822 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003823nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824{
3825 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003826 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003828 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003830 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003831 if (mod_mask & MOD_MASK_CTRL)
3832 cap->arg = TRUE;
3833 nv_wordcmd(cap);
3834 return;
3835 }
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 cap->oap->motion_type = MCHAR;
3838 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003839 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003841 // In virtual edit mode, there's no such thing as "past_line", as lines
3842 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003844 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
3846 for (n = cap->count1; n > 0; --n)
3847 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003848 if ((!past_line && oneright() == FAIL)
3849 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003850 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003852 // <Space> wraps to next line if 'whichwrap' has 's'.
3853 // 'l' wraps to next line if 'whichwrap' has 'l'.
3854 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if ( ((cap->cmdchar == ' '
3856 && vim_strchr(p_ww, 's') != NULL)
3857 || (cap->cmdchar == 'l'
3858 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003859 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 && vim_strchr(p_ww, '>') != NULL))
3861 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003863 // When deleting we also count the NL as a character.
3864 // Set cap->oap->inclusive when last char in the line is
3865 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003866 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003868 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 cap->oap->inclusive = TRUE;
3870 else
3871 {
3872 ++curwin->w_cursor.lnum;
3873 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 curwin->w_set_curswant = TRUE;
3876 cap->oap->inclusive = FALSE;
3877 }
3878 continue;
3879 }
3880 if (cap->oap->op_type == OP_NOP)
3881 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003882 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (n == cap->count1)
3884 beep_flush();
3885 }
3886 else
3887 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003888 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 cap->oap->inclusive = TRUE;
3890 }
3891 break;
3892 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003893 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
3895 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (virtual_active())
3897 oneright();
3898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003901 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 ++curwin->w_cursor.col;
3904 }
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907#ifdef FEAT_FOLDING
3908 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3909 && cap->oap->op_type == OP_NOP)
3910 foldOpenCursor();
3911#endif
3912}
3913
3914/*
3915 * Cursor left commands.
3916 *
3917 * Returns TRUE when operator end should not be adjusted.
3918 */
3919 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003920nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
3922 long n;
3923
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003924 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003926 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003927 if (mod_mask & MOD_MASK_CTRL)
3928 cap->arg = 1;
3929 nv_bck_word(cap);
3930 return;
3931 }
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 cap->oap->motion_type = MCHAR;
3934 cap->oap->inclusive = FALSE;
3935 for (n = cap->count1; n > 0; --n)
3936 {
3937 if (oneleft() == FAIL)
3938 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003939 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3940 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3941 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if ( (((cap->cmdchar == K_BS
3943 || cap->cmdchar == Ctrl_H)
3944 && vim_strchr(p_ww, 'b') != NULL)
3945 || (cap->cmdchar == 'h'
3946 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003947 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 && vim_strchr(p_ww, '<') != NULL))
3949 && curwin->w_cursor.lnum > 1)
3950 {
3951 --(curwin->w_cursor.lnum);
3952 coladvance((colnr_T)MAXCOL);
3953 curwin->w_set_curswant = TRUE;
3954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003955 // When the NL before the first char has to be deleted we
3956 // put the cursor on the NUL after the previous line.
3957 // This is a very special case, be careful!
3958 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if ( (cap->oap->op_type == OP_DELETE
3960 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003961 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003963 char_u *cp = ml_get_cursor();
3964
3965 if (*cp != NUL)
3966 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003967 if (has_mbyte)
3968 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3969 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003970 ++curwin->w_cursor.col;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 cap->retval |= CA_NO_ADJ_OP_END;
3973 }
3974 continue;
3975 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003976 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3978 beep_flush();
3979 break;
3980 }
3981 }
3982#ifdef FEAT_FOLDING
3983 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3984 && cap->oap->op_type == OP_NOP)
3985 foldOpenCursor();
3986#endif
3987}
3988
3989/*
3990 * Cursor up commands.
3991 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3992 */
3993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003994nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 if (mod_mask & MOD_MASK_SHIFT)
3997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003998 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003999 cap->arg = BACKWARD;
4000 nv_page(cap);
4001 }
4002 else
4003 {
4004 cap->oap->motion_type = MLINE;
4005 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4006 clearopbeep(cap->oap);
4007 else if (cap->arg)
4008 beginline(BL_WHITE | BL_FIX);
4009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010}
4011
4012/*
4013 * Cursor down commands.
4014 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4015 */
4016 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004017nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004019 if (mod_mask & MOD_MASK_SHIFT)
4020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004021 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004022 cap->arg = FORWARD;
4023 nv_page(cap);
4024 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004025#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004026 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004027 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4028 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004030 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004033 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 cmdwin_result = CAR;
4035 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004036#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004037 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004038 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4039 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4040 {
4041 invoke_prompt_callback();
4042 if (restart_edit == 0)
4043 restart_edit = 'a';
4044 }
4045 else
4046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
4048 cap->oap->motion_type = MLINE;
4049 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4050 clearopbeep(cap->oap);
4051 else if (cap->arg)
4052 beginline(BL_WHITE | BL_FIX);
4053 }
4054 }
4055}
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057/*
4058 * Grab the file name under the cursor and edit it.
4059 */
4060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004061nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004064 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004066 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 if (curbuf_locked())
4069 {
4070 clearop(cap->oap);
4071 return;
4072 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004073#ifdef FEAT_PROP_POPUP
4074 if (ERROR_IF_TERM_POPUP_WINDOW)
4075 return;
4076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004078 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
4080 if (ptr != NULL)
4081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004082 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004083 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004084 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004086 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004087 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004088 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004089 {
4090 curwin->w_cursor.lnum = lnum;
4091 check_cursor_lnum();
4092 beginline(BL_SOL | BL_FIX);
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 vim_free(ptr);
4095 }
4096 else
4097 clearop(cap->oap);
4098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100/*
4101 * <End> command: to end of current line or last line.
4102 */
4103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004104nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004108 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004110 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 nv_dollar(cap);
4113}
4114
4115/*
4116 * Handle the "$" command.
4117 */
4118 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004119nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 cap->oap->motion_type = MCHAR;
4122 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004123 // In virtual mode when off the edge of a line and an operator
4124 // is pending (whew!) keep the cursor where it is.
4125 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (!virtual_active() || gchar_cursor() != NUL
4127 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004128 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (cursor_down((long)(cap->count1 - 1),
4130 cap->oap->op_type == OP_NOP) == FAIL)
4131 clearopbeep(cap->oap);
4132#ifdef FEAT_FOLDING
4133 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4134 foldOpenCursor();
4135#endif
4136}
4137
4138/*
4139 * Implementation of '?' and '/' commands.
4140 * If cap->arg is TRUE don't set PC mark.
4141 */
4142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004143nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
4145 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004146 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4149 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004150 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 cap->cmdchar = 'g';
4152 cap->nchar = '?';
4153 nv_operator(cap);
4154 return;
4155 }
4156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004157 // When using 'incsearch' the cursor may be moved to set a different search
4158 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004159 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 if (cap->searchbuf == NULL)
4162 {
4163 clearop(oap);
4164 return;
4165 }
4166
Bram Moolenaar46539112015-02-17 15:43:57 +01004167 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004168 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004169 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170}
4171
LemonBoya4399382022-04-09 21:04:08 +01004172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173/*
4174 * Handle "N" and "n" commands.
4175 * cap->arg is SEARCH_REV for "N", 0 for "n".
4176 */
4177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004178nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004180 pos_T old = curwin->w_cursor;
4181 int wrapped = FALSE;
4182 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004183
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004184 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004185 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004186 // Avoid getting stuck on the current cursor position, which can
4187 // happen when an offset is given and the cursor is on the last char
4188 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004189 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004190 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004191 cap->count1 -= 1;
4192 }
LemonBoya4399382022-04-09 21:04:08 +01004193
4194#ifdef FEAT_SEARCH_EXTRA
4195 // Redraw the window to refresh the highlighted matches.
4196 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004197 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199}
4200
4201/*
4202 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4203 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004204 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004206 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004207normal_search(
4208 cmdarg_T *cap,
4209 int dir,
4210 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004211 int opt, // extra flags for do_search()
4212 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004215 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004216#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004217 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 cap->oap->motion_type = MCHAR;
4221 cap->oap->inclusive = FALSE;
4222 cap->oap->use_reg_one = TRUE;
4223 curwin->w_set_curswant = TRUE;
4224
Bram Moolenaara80faa82020-04-12 19:37:17 +02004225 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004226 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004227 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4228 if (wrapped != NULL)
4229 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (i == 0)
4231 clearop(cap->oap);
4232 else
4233 {
4234 if (i == 2)
4235 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#ifdef FEAT_FOLDING
4238 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4239 foldOpenCursor();
4240#endif
4241 }
LemonBoya4399382022-04-09 21:04:08 +01004242#ifdef FEAT_SEARCH_EXTRA
4243 // Redraw the window to refresh the highlighted matches.
4244 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004245 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004248 // "/$" will put the cursor after the end of the line, may need to
4249 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004251 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252}
4253
4254/*
4255 * Character search commands.
4256 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4257 * ',' and FALSE for ';'.
4258 * cap->nchar is NUL for ',' and ';' (repeat the search)
4259 */
4260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004261nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 int t_cmd;
4264
4265 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4266 t_cmd = TRUE;
4267 else
4268 t_cmd = FALSE;
4269
4270 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4272 clearopbeep(cap->oap);
4273 else
4274 {
4275 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004276 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4278 && (t_cmd || cap->oap->op_type != OP_NOP))
4279 {
4280 colnr_T scol, ecol;
4281
4282 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4283 curwin->w_cursor.coladd = ecol - scol;
4284 }
4285 else
4286 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288#ifdef FEAT_FOLDING
4289 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4290 foldOpenCursor();
4291#endif
4292 }
4293}
4294
4295/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004296 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4297 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4298 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4299 * "[m" or "]m" search for prev/next start of (Java) method.
4300 * "[M" or "]M" search for prev/next end of (Java) method.
4301 */
4302 static void
4303nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4304{
4305 pos_T new_pos = {0, 0, 0};
4306 pos_T *pos = NULL; // init for GCC
4307 pos_T prev_pos;
4308 long n;
4309 int findc;
4310 int c;
4311
4312 if (cap->nchar == '*')
4313 cap->nchar = '/';
4314 prev_pos.lnum = 0;
4315 if (cap->nchar == 'm' || cap->nchar == 'M')
4316 {
4317 if (cap->cmdchar == '[')
4318 findc = '{';
4319 else
4320 findc = '}';
4321 n = 9999;
4322 }
4323 else
4324 {
4325 findc = cap->nchar;
4326 n = cap->count1;
4327 }
4328 for ( ; n > 0; --n)
4329 {
4330 if ((pos = findmatchlimit(cap->oap, findc,
4331 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4332 {
4333 if (new_pos.lnum == 0) // nothing found
4334 {
4335 if (cap->nchar != 'm' && cap->nchar != 'M')
4336 clearopbeep(cap->oap);
4337 }
4338 else
4339 pos = &new_pos; // use last one found
4340 break;
4341 }
4342 prev_pos = new_pos;
4343 curwin->w_cursor = *pos;
4344 new_pos = *pos;
4345 }
4346 curwin->w_cursor = *old_pos;
4347
4348 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4349 // brought us to the match for "[m" and "]M" when inside a method.
4350 // Try finding the '{' or '}' we want to be at.
4351 // Also repeat for the given count.
4352 if (cap->nchar == 'm' || cap->nchar == 'M')
4353 {
4354 // norm is TRUE for "]M" and "[m"
4355 int norm = ((findc == '{') == (cap->nchar == 'm'));
4356
4357 n = cap->count1;
4358 // found a match: we were inside a method
4359 if (prev_pos.lnum != 0)
4360 {
4361 pos = &prev_pos;
4362 curwin->w_cursor = prev_pos;
4363 if (norm)
4364 --n;
4365 }
4366 else
4367 pos = NULL;
4368 while (n > 0)
4369 {
4370 for (;;)
4371 {
4372 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4373 {
4374 // if not found anything, that's an error
4375 if (pos == NULL)
4376 clearopbeep(cap->oap);
4377 n = 0;
4378 break;
4379 }
4380 c = gchar_cursor();
4381 if (c == '{' || c == '}')
4382 {
4383 // Must have found end/start of class: use it.
4384 // Or found the place to be at.
4385 if ((c == findc && norm) || (n == 1 && !norm))
4386 {
4387 new_pos = curwin->w_cursor;
4388 pos = &new_pos;
4389 n = 0;
4390 }
4391 // if no match found at all, we started outside of the
4392 // class and we're inside now. Just go on.
4393 else if (new_pos.lnum == 0)
4394 {
4395 new_pos = curwin->w_cursor;
4396 pos = &new_pos;
4397 }
4398 // found start/end of other method: go to match
4399 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004400 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4401 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004402 n = 0;
4403 else
4404 curwin->w_cursor = *pos;
4405 break;
4406 }
4407 }
4408 --n;
4409 }
4410 curwin->w_cursor = *old_pos;
4411 if (pos == NULL && new_pos.lnum != 0)
4412 clearopbeep(cap->oap);
4413 }
4414 if (pos != NULL)
4415 {
4416 setpcmark();
4417 curwin->w_cursor = *pos;
4418 curwin->w_set_curswant = TRUE;
4419#ifdef FEAT_FOLDING
4420 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4421 && cap->oap->op_type == OP_NOP)
4422 foldOpenCursor();
4423#endif
4424 }
4425}
4426
4427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 * "[" and "]" commands.
4429 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4430 */
4431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004432nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004435 pos_T *pos = NULL; // init for GCC
4436 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 int flag;
4438 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 cap->oap->motion_type = MCHAR;
4441 cap->oap->inclusive = FALSE;
4442 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004443 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004445 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (cap->nchar == 'f')
4447 nv_gotofile(cap);
4448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004451 // Find the occurrence(s) of the identifier or define under cursor
4452 // in current and included files or jump to the first occurrence.
4453 //
4454 // search list jump
4455 // fwd bwd fwd bwd fwd bwd
4456 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4457 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004458 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
4460 char_u *ptr;
4461 int len;
4462
4463 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4464 clearop(cap->oap);
4465 else
4466 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004467 // Make a copy, if the line was changed it will be freed.
4468 ptr = vim_strnsave(ptr, len);
4469 if (ptr == NULL)
4470 return;
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 find_pattern_in_path(ptr, 0, len, TRUE,
4473 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4474 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4475 cap->count1,
4476 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4477 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4478 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4479 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004480 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 curwin->w_set_curswant = TRUE;
4482 }
4483 }
4484 else
4485#endif
4486
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004487 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4488 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4489 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4490 // "[m" or "]m" search for prev/next start of (Java) method.
4491 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if ( (cap->cmdchar == '['
4493 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4494 || (cap->cmdchar == ']'
4495 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004496 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004498 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 else if (cap->nchar == '[' || cap->nchar == ']')
4500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004501 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 flag = '{';
4503 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004504 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004507 // Imitate strange Vi behaviour: When using "]]" with an operator
4508 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004509 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 (cap->oap->op_type != OP_NOP
4511 && cap->arg == FORWARD && flag == '{')))
4512 clearopbeep(cap->oap);
4513 else
4514 {
4515 if (cap->oap->op_type == OP_NOP)
4516 beginline(BL_WHITE | BL_FIX);
4517#ifdef FEAT_FOLDING
4518 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4519 foldOpenCursor();
4520#endif
4521 }
4522 }
4523
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004524 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 else if (cap->nchar == 'p' || cap->nchar == 'P')
4526 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004527 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004530 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 else if (cap->nchar == '\'' || cap->nchar == '`')
4532 {
4533 pos = &curwin->w_cursor;
4534 for (n = cap->count1; n > 0; --n)
4535 {
4536 prev_pos = *pos;
4537 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4538 cap->nchar == '\'');
4539 if (pos == NULL)
4540 break;
4541 }
4542 if (pos == NULL)
4543 pos = &prev_pos;
4544 nv_cursormark(cap, cap->nchar == '\'', pos);
4545 }
4546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004547 // [ or ] followed by a middle mouse click: put selected text with
4548 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004549 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 (void)do_mouse(cap->oap, cap->nchar,
4552 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4553 cap->count1, PUT_FIXINDENT);
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004557 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 else if (cap->nchar == 'z')
4559 {
4560 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4561 cap->count1) == FAIL)
4562 clearopbeep(cap->oap);
4563 }
4564#endif
4565
4566#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004567 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 else if (cap->nchar == 'c')
4569 {
4570 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4571 cap->count1) == FAIL)
4572 clearopbeep(cap->oap);
4573 }
4574#endif
4575
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004576#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004577 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004578 else if (cap->nchar == 's' || cap->nchar == 'S')
4579 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004580 setpcmark();
4581 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004582 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4583 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004584 {
4585 clearopbeep(cap->oap);
4586 break;
4587 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004588 else
4589 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004590# ifdef FEAT_FOLDING
4591 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4592 foldOpenCursor();
4593# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594 }
4595#endif
4596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 clearopbeep(cap->oap);
4600}
4601
4602/*
4603 * Handle Normal mode "%" command.
4604 */
4605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004606nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
4608 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004609#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 linenr_T lnum = curwin->w_cursor.lnum;
4611#endif
4612
4613 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004614 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 if (cap->count0 > 100)
4617 clearopbeep(cap->oap);
4618 else
4619 {
4620 cap->oap->motion_type = MLINE;
4621 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004622 // Round up, so 'normal 100%' always jumps at the line line.
4623 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4624 // overflow on 32-bits, so use a formula with less accuracy
4625 // to avoid overflows.
4626 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4628 / 100L * cap->count0;
4629 else
4630 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4631 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004632 if (curwin->w_cursor.lnum < 1)
4633 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4635 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4636 beginline(BL_SOL | BL_FIX);
4637 }
4638 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004639 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 cap->oap->motion_type = MCHAR;
4642 cap->oap->use_reg_one = TRUE;
4643 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4644 clearopbeep(cap->oap);
4645 else
4646 {
4647 setpcmark();
4648 curwin->w_cursor = *pos;
4649 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 }
4654#ifdef FEAT_FOLDING
4655 if (cap->oap->op_type == OP_NOP
4656 && lnum != curwin->w_cursor.lnum
4657 && (fdo_flags & FDO_PERCENT)
4658 && KeyTyped)
4659 foldOpenCursor();
4660#endif
4661}
4662
4663/*
4664 * Handle "(" and ")" commands.
4665 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4666 */
4667 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004668nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670 cap->oap->motion_type = MCHAR;
4671 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004673 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 curwin->w_set_curswant = TRUE;
4675
4676 if (findsent(cap->arg, cap->count1) == FAIL)
4677 clearopbeep(cap->oap);
4678 else
4679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004680 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004681 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#ifdef FEAT_FOLDING
4684 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4685 foldOpenCursor();
4686#endif
4687 }
4688}
4689
4690/*
4691 * "m" command: Mark a position.
4692 */
4693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004694nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 if (!checkclearop(cap->oap))
4697 {
4698 if (setmark(cap->nchar) == FAIL)
4699 clearopbeep(cap->oap);
4700 }
4701}
4702
4703/*
4704 * "{" and "}" commands.
4705 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4706 */
4707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 cap->oap->motion_type = MCHAR;
4711 cap->oap->inclusive = FALSE;
4712 cap->oap->use_reg_one = TRUE;
4713 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004714 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 clearopbeep(cap->oap);
4716 else
4717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719#ifdef FEAT_FOLDING
4720 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4721 foldOpenCursor();
4722#endif
4723 }
4724}
4725
4726/*
4727 * "u" command: Undo or make lower case.
4728 */
4729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004730nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004732 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004734 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 cap->cmdchar = 'g';
4736 cap->nchar = 'u';
4737 nv_operator(cap);
4738 }
4739 else
4740 nv_kundo(cap);
4741}
4742
4743/*
4744 * <Undo> command.
4745 */
4746 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004747nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 if (!checkclearopq(cap->oap))
4750 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004751#ifdef FEAT_JOB_CHANNEL
4752 if (bt_prompt(curbuf))
4753 {
4754 clearopbeep(cap->oap);
4755 return;
4756 }
4757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 u_undo((int)cap->count1);
4759 curwin->w_set_curswant = TRUE;
4760 }
4761}
4762
4763/*
4764 * Handle the "r" command.
4765 */
4766 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004767nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 char_u *ptr;
4770 int had_ctrl_v;
4771 long n;
4772
4773 if (checkclearop(cap->oap))
4774 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004775#ifdef FEAT_JOB_CHANNEL
4776 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4777 {
4778 clearopbeep(cap->oap);
4779 return;
4780 }
4781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (cap->nchar == Ctrl_V)
4785 {
4786 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004787 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (cap->nchar > DEL)
4790 had_ctrl_v = NUL;
4791 }
4792 else
4793 had_ctrl_v = NUL;
4794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004795 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004796 if (IS_SPECIAL(cap->nchar))
4797 {
4798 clearopbeep(cap->oap);
4799 return;
4800 }
4801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (VIsual_active)
4804 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004805 if (got_int)
4806 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004807 if (had_ctrl_v)
4808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004809 // Use a special (negative) number to make a difference between a
4810 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004811 if (cap->nchar == CAR)
4812 cap->nchar = REPLACE_CR_NCHAR;
4813 else if (cap->nchar == NL)
4814 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 nv_operator(cap);
4817 return;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004820 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (virtual_active())
4822 {
4823 if (u_save_cursor() == FAIL)
4824 return;
4825 if (gchar_cursor() == NUL)
4826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 coladvance_force((colnr_T)(getviscol() + cap->count1));
4829 curwin->w_cursor.col -= cap->count1;
4830 }
4831 else if (gchar_cursor() == TAB)
4832 coladvance_force(getviscol());
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004837 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004838 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 clearopbeep(cap->oap);
4841 return;
4842 }
4843
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004844 // Replacing with a TAB is done by edit() when it is complicated because
4845 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4846 // Other characters are done below to avoid problems with things like
4847 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4849 {
4850 stuffnumReadbuff(cap->count1);
4851 stuffcharReadbuff('R');
4852 stuffcharReadbuff('\t');
4853 stuffcharReadbuff(ESC);
4854 return;
4855 }
4856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004857 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (u_save_cursor() == FAIL)
4859 return;
4860
4861 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4862 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004863 // Replace character(s) by a single newline.
4864 // Strange vi behaviour: Only one newline is inserted.
4865 // Delete the characters here.
4866 // Insert the newline with an insert command, takes care of
4867 // autoindent. The insert command depends on being on the last
4868 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 stuffcharReadbuff('\r');
4871 stuffcharReadbuff(ESC);
4872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004873 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 invoke_edit(cap, TRUE, 'r', FALSE);
4875 }
4876 else
4877 {
4878 prep_redo(cap->oap->regname, cap->count1,
4879 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4880
4881 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (has_mbyte)
4883 {
4884 int old_State = State;
4885
4886 if (cap->ncharC1 != 0)
4887 AppendCharToRedobuff(cap->ncharC1);
4888 if (cap->ncharC2 != 0)
4889 AppendCharToRedobuff(cap->ncharC2);
4890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004891 // This is slow, but it handles replacing a single-byte with a
4892 // multi-byte and the other way around. Also handles adding
4893 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 for (n = cap->count1; n > 0; --n)
4895 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004896 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004897 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4898 {
4899 int c = ins_copychar(curwin->w_cursor.lnum
4900 + (cap->nchar == Ctrl_Y ? -1 : 1));
4901 if (c != NUL)
4902 ins_char(c);
4903 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004905 ++curwin->w_cursor.col;
4906 }
4907 else
4908 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 State = old_State;
4910 if (cap->ncharC1 != 0)
4911 ins_char(cap->ncharC1);
4912 if (cap->ncharC2 != 0)
4913 ins_char(cap->ncharC2);
4914 }
4915 }
4916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004918 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 for (n = cap->count1; n > 0; --n)
4920 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004921 // Get ptr again, because u_save and/or showmatch() will have
4922 // released the line. This may also happen in ins_copychar().
4923 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004924 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4925 {
4926 int c = ins_copychar(curwin->w_cursor.lnum
4927 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004928
4929 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004930 if (c != NUL)
4931 ptr[curwin->w_cursor.col] = c;
4932 }
4933 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004934 {
4935 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004936 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (p_sm && msg_silent == 0)
4939 showmatch(cap->nchar);
4940 ++curwin->w_cursor.col;
4941 }
4942#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004943 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004945 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaar009b2592004-10-24 19:18:58 +00004947 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004948 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004950 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952#endif
4953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 changed_bytes(curwin->w_cursor.lnum,
4956 (colnr_T)(curwin->w_cursor.col - cap->count1));
4957 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004958 --curwin->w_cursor.col; // cursor on the last replaced char
4959 // if the character on the left of the current cursor is a multi-byte
4960 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (has_mbyte)
4962 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 curbuf->b_op_end = curwin->w_cursor;
4964 curwin->w_set_curswant = TRUE;
4965 set_last_insert(cap->nchar);
4966 }
4967}
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969/*
4970 * 'o': Exchange start and end of Visual area.
4971 * 'O': same, but in block mode exchange left and right corners.
4972 */
4973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004974v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
4976 pos_T old_cursor;
4977 colnr_T left, right;
4978
4979 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4980 {
4981 old_cursor = curwin->w_cursor;
4982 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4983 curwin->w_cursor.lnum = VIsual.lnum;
4984 coladvance(left);
4985 VIsual = curwin->w_cursor;
4986
4987 curwin->w_cursor.lnum = old_cursor.lnum;
4988 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004989 // 'selection "exclusive" and cursor at right-bottom corner: move it
4990 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4992 ++curwin->w_curswant;
4993 coladvance(curwin->w_curswant);
4994 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004996 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 curwin->w_cursor.lnum = VIsual.lnum;
4999 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5000 ++right;
5001 coladvance(right);
5002 VIsual = curwin->w_cursor;
5003
5004 curwin->w_cursor.lnum = old_cursor.lnum;
5005 coladvance(left);
5006 curwin->w_curswant = left;
5007 }
5008 }
5009 else
5010 {
5011 old_cursor = curwin->w_cursor;
5012 curwin->w_cursor = VIsual;
5013 VIsual = old_cursor;
5014 curwin->w_set_curswant = TRUE;
5015 }
5016}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018/*
5019 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5020 */
5021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005022nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005024 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
5026 cap->cmdchar = 'c';
5027 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 VIsual_mode = 'V';
5030 nv_operator(cap);
5031 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005032 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005035 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
5037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (virtual_active())
5039 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5041 }
5042 }
5043}
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045/*
5046 * "gr".
5047 */
5048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005049nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (VIsual_active)
5052 {
5053 cap->cmdchar = 'r';
5054 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005055 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005057 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005060 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
5062 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005063 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005064 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 stuffcharReadbuff(cap->extra_char);
5066 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (virtual_active())
5068 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 invoke_edit(cap, TRUE, 'v', FALSE);
5070 }
5071 }
5072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074/*
5075 * Swap case for "~" command, when it does not work like an operator.
5076 */
5077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005078n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 long n;
5081 pos_T startpos;
5082 int did_change = 0;
5083#ifdef FEAT_NETBEANS_INTG
5084 pos_T pos;
5085 char_u *ptr;
5086 int count;
5087#endif
5088
5089 if (checkclearopq(cap->oap))
5090 return;
5091
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005092 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 clearopbeep(cap->oap);
5095 return;
5096 }
5097
5098 prep_redo_cmd(cap);
5099
5100 if (u_save_cursor() == FAIL)
5101 return;
5102
5103 startpos = curwin->w_cursor;
5104#ifdef FEAT_NETBEANS_INTG
5105 pos = startpos;
5106#endif
5107 for (n = cap->count1; n > 0; --n)
5108 {
5109 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5110 inc_cursor();
5111 if (gchar_cursor() == NUL)
5112 {
5113 if (vim_strchr(p_ww, '~') != NULL
5114 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5115 {
5116#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005117 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 if (did_change)
5120 {
5121 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005122 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005123 netbeans_removed(curbuf, pos.lnum, pos.col,
5124 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005125 // line may have been flushed, get it again
5126 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005128 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 pos.col = 0;
5131 pos.lnum++;
5132 }
5133#endif
5134 ++curwin->w_cursor.lnum;
5135 curwin->w_cursor.col = 0;
5136 if (n > 1)
5137 {
5138 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5139 break;
5140 u_clearline();
5141 }
5142 }
5143 else
5144 break;
5145 }
5146 }
5147#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005148 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 ptr = ml_get(pos.lnum);
5151 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005152 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5153 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155#endif
5156
5157
5158 check_cursor();
5159 curwin->w_set_curswant = TRUE;
5160 if (did_change)
5161 {
5162 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5163 0L);
5164 curbuf->b_op_start = startpos;
5165 curbuf->b_op_end = curwin->w_cursor;
5166 if (curbuf->b_op_end.col > 0)
5167 --curbuf->b_op_end.col;
5168 }
5169}
5170
5171/*
5172 * Move cursor to mark.
5173 */
5174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005175nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
5177 if (check_mark(pos) == FAIL)
5178 clearop(cap->oap);
5179 else
5180 {
5181 if (cap->cmdchar == '\''
5182 || cap->cmdchar == '`'
5183 || cap->cmdchar == '['
5184 || cap->cmdchar == ']')
5185 setpcmark();
5186 curwin->w_cursor = *pos;
5187 if (flag)
5188 beginline(BL_WHITE | BL_FIX);
5189 else
5190 check_cursor();
5191 }
5192 cap->oap->motion_type = flag ? MLINE : MCHAR;
5193 if (cap->cmdchar == '`')
5194 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005195 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 curwin->w_set_curswant = TRUE;
5197}
5198
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199/*
5200 * Handle commands that are operators in Visual mode.
5201 */
5202 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005203v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
5205 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005207 // Uppercase means linewise, except in block mode, then "D" deletes till
5208 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (isupper(cap->cmdchar))
5210 {
5211 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005212 {
5213 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5217 curwin->w_curswant = MAXCOL;
5218 }
5219 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5220 nv_operator(cap);
5221}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223/*
5224 * "s" and "S" commands.
5225 */
5226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005227nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005229#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005230 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005231 if (term_swap_diff() == OK)
5232 return;
5233#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005234#ifdef FEAT_JOB_CHANNEL
5235 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5236 {
5237 clearopbeep(cap->oap);
5238 return;
5239 }
5240#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005241 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
5243 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005244 {
5245 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 cap->cmdchar = 'c';
5249 nv_operator(cap);
5250 }
5251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 nv_optrans(cap);
5253}
5254
5255/*
5256 * Abbreviated commands.
5257 */
5258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005259nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
5261 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005262 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005264 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 if (VIsual_active)
5266 v_visop(cap);
5267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 nv_optrans(cap);
5269}
5270
5271/*
5272 * Translate a command into another command.
5273 */
5274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005275nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5278 (char_u *)"d$", (char_u *)"c$",
5279 (char_u *)"cl", (char_u *)"cc",
5280 (char_u *)"yy", (char_u *)":s\r"};
5281 static char_u *str = (char_u *)"xXDCsSY&";
5282
5283 if (!checkclearopq(cap->oap))
5284 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005285 // In Vi "2D" doesn't delete the next line. Can't translate it
5286 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005287 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5288 {
5289 cap->oap->start = curwin->w_cursor;
5290 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005291#ifdef FEAT_EVAL
5292 set_op_var(OP_DELETE);
5293#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005294 cap->count1 = 1;
5295 nv_dollar(cap);
5296 finish_op = TRUE;
5297 ResetRedobuff();
5298 AppendCharToRedobuff('D');
5299 }
5300 else
5301 {
5302 if (cap->count0)
5303 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005304 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307 cap->opcount = 0;
5308}
5309
5310/*
5311 * "'" and "`" commands. Also for "g'" and "g`".
5312 * cap->arg is TRUE for "'" and "g'".
5313 */
5314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 pos_T *pos;
5318 int c;
5319#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005320 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#endif
5323
5324 if (cap->cmdchar == 'g')
5325 c = cap->extra_char;
5326 else
5327 c = cap->nchar;
5328 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005329 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 if (cap->arg)
5332 {
5333 check_cursor_lnum();
5334 beginline(BL_WHITE | BL_FIX);
5335 }
5336 else
5337 check_cursor();
5338 }
5339 else
5340 nv_cursormark(cap, cap->arg, pos);
5341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005342 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (!virtual_active())
5344 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005345 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#ifdef FEAT_FOLDING
5347 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005348 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005349 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 && (fdo_flags & FDO_MARK)
5351 && old_KeyTyped)
5352 foldOpenCursor();
5353#endif
5354}
5355
5356/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005357 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 */
5359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005360nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005363#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005365 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
5368 if (!checkclearopq(cap->oap))
5369 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005370 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5371 {
5372 if (goto_tabpage_lastused() == FAIL)
5373 clearopbeep(cap->oap);
5374 return;
5375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (cap->cmdchar == 'g')
5377 pos = movechangelist((int)cap->count1);
5378 else
5379 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005380 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 curwin->w_set_curswant = TRUE;
5383 check_cursor();
5384 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005385 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 nv_cursormark(cap, FALSE, pos);
5387 else if (cap->cmdchar == 'g')
5388 {
5389 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005390 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005392 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005394 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396 else
5397 clearopbeep(cap->oap);
5398# ifdef FEAT_FOLDING
5399 if (cap->oap->op_type == OP_NOP
5400 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5401 && (fdo_flags & FDO_MARK)
5402 && old_KeyTyped)
5403 foldOpenCursor();
5404# endif
5405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406}
5407
5408/*
5409 * Handle '"' command.
5410 */
5411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005412nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 if (checkclearop(cap->oap))
5415 return;
5416#ifdef FEAT_EVAL
5417 if (cap->nchar == '=')
5418 cap->nchar = get_expr_register();
5419#endif
5420 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5421 {
5422 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005423 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#ifdef FEAT_EVAL
5425 set_reg_var(cap->oap->regname);
5426#endif
5427 }
5428 else
5429 clearopbeep(cap->oap);
5430}
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432/*
5433 * Handle "v", "V" and "CTRL-V" commands.
5434 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5435 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005436 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 */
5438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005439nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005441 if (cap->cmdchar == Ctrl_Q)
5442 cap->cmdchar = Ctrl_V;
5443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5445 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (cap->oap->op_type != OP_NOP)
5447 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005448 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005449 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 return;
5451 }
5452
5453 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005454 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005458 else // toggle char/block mode
5459 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 VIsual_mode = cap->cmdchar;
5461 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005462 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005464 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
5468 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005469 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005471 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 VIsual = curwin->w_cursor;
5473
5474 VIsual_active = TRUE;
5475 VIsual_reselect = TRUE;
5476 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005477 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005480 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005481 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005482 // For V and ^V, we multiply the number of lines even if there
5483 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5485 {
5486 curwin->w_cursor.lnum +=
5487 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005488 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490 VIsual_mode = resel_VIsual_mode;
5491 if (VIsual_mode == 'v')
5492 {
5493 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005494 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005495 update_curswant_force();
5496 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005499 curwin->w_curswant = resel_VIsual_vcol;
5500 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005502 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 curwin->w_curswant = MAXCOL;
5505 coladvance((colnr_T)MAXCOL);
5506 }
5507 else if (VIsual_mode == Ctrl_V)
5508 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005509 update_curswant_force();
5510 curwin->w_curswant += + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 coladvance(curwin->w_curswant);
5512 }
5513 else
5514 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005515 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517 else
5518 {
5519 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 may_start_select('c');
5522 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005523 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005525 if (cap->count0 > 0 && --cap->count1 > 0)
5526 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005528 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5529 nv_right(cap);
5530 else if (VIsual_mode == 'V')
5531 nv_down(cap);
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 }
5535}
5536
5537/*
5538 * Start selection for Shift-movement keys.
5539 */
5540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005541start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 may_start_select('k');
5545 n_start_visual_mode('v');
5546}
5547
5548/*
5549 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005550 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 */
5552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005553may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005555 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5556 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558
5559/*
5560 * Start Visual mode "c".
5561 * Should set VIsual_select before calling this.
5562 */
5563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005564n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005566#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005567 int cursor_line_was_concealed = curwin->w_p_cole > 0
5568 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005569#endif
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 VIsual_mode = c;
5572 VIsual_active = TRUE;
5573 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005574
5575 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005576 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005577 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005578 {
5579 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 VIsual = curwin->w_cursor;
5583
5584#ifdef FEAT_FOLDING
5585 foldAdjustVisual();
5586#endif
5587
LemonBoy2bf52dd2022-04-09 18:17:34 +01005588 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005590#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005591 // Check if redraw is needed after changing the state.
5592 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005593#endif
5594
Bram Moolenaar09df3122006-01-23 22:23:09 +00005595 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 // Make sure the clipboard gets updated. Needed because start and
5599 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 clip_star.vmode = NUL;
5601#endif
5602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 // Only need to redraw this line, unless still need to redraw an old
5604 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005605 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5608 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5609 }
5610}
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613/*
5614 * CTRL-W: Window commands
5615 */
5616 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005617nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005619 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005622 cap->cmdchar = ':';
5623 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005624 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005625 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005626 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630/*
5631 * CTRL-Z: Suspend
5632 */
5633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005634nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005638 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005639 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005643 * "gv": Reselect the previous Visual area. If Visual already active,
5644 * exchange previous and current Visual area.
5645 */
5646 static void
5647nv_gv_cmd(cmdarg_T *cap)
5648{
5649 pos_T tpos;
5650 int i;
5651
5652 if (checkclearop(cap->oap))
5653 return;
5654
5655 if (curbuf->b_visual.vi_start.lnum == 0
5656 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5657 || curbuf->b_visual.vi_end.lnum == 0)
5658 {
5659 beep_flush();
5660 return;
5661 }
5662
5663 // set w_cursor to the start of the Visual area, tpos to the end
5664 if (VIsual_active)
5665 {
5666 i = VIsual_mode;
5667 VIsual_mode = curbuf->b_visual.vi_mode;
5668 curbuf->b_visual.vi_mode = i;
5669# ifdef FEAT_EVAL
5670 curbuf->b_visual_mode_eval = i;
5671# endif
5672 i = curwin->w_curswant;
5673 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5674 curbuf->b_visual.vi_curswant = i;
5675
5676 tpos = curbuf->b_visual.vi_end;
5677 curbuf->b_visual.vi_end = curwin->w_cursor;
5678 curwin->w_cursor = curbuf->b_visual.vi_start;
5679 curbuf->b_visual.vi_start = VIsual;
5680 }
5681 else
5682 {
5683 VIsual_mode = curbuf->b_visual.vi_mode;
5684 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5685 tpos = curbuf->b_visual.vi_end;
5686 curwin->w_cursor = curbuf->b_visual.vi_start;
5687 }
5688
5689 VIsual_active = TRUE;
5690 VIsual_reselect = TRUE;
5691
5692 // Set Visual to the start and w_cursor to the end of the Visual
5693 // area. Make sure they are on an existing character.
5694 check_cursor();
5695 VIsual = curwin->w_cursor;
5696 curwin->w_cursor = tpos;
5697 check_cursor();
5698 update_topline();
5699
5700 // When called from normal "g" command: start Select mode when
5701 // 'selectmode' contains "cmd". When called for K_SELECT, always
5702 // start Select mode.
5703 if (cap->arg)
5704 {
5705 VIsual_select = TRUE;
5706 VIsual_select_reg = 0;
5707 }
5708 else
5709 may_start_select('c');
5710 setmouse();
5711#ifdef FEAT_CLIPBOARD
5712 // Make sure the clipboard gets updated. Needed because start and
5713 // end are still the same, and the selection needs to be owned
5714 clip_star.vmode = NUL;
5715#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005716 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005717 showmode();
5718}
5719
5720/*
5721 * "g0", "g^" : Like "0" and "^" but for screen lines.
5722 * "gm": middle of "g0" and "g$".
5723 */
5724 static void
5725nv_g_home_m_cmd(cmdarg_T *cap)
5726{
5727 int i;
5728 int flag = FALSE;
5729
5730 if (cap->nchar == '^')
5731 flag = TRUE;
5732
5733 cap->oap->motion_type = MCHAR;
5734 cap->oap->inclusive = FALSE;
5735 if (curwin->w_p_wrap && curwin->w_width != 0)
5736 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005737 int width1 = curwin->w_width - curwin_col_off();
5738 int width2 = width1 + curwin_col_off2();
5739 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005740
5741 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005742 virtcol = curwin->w_virtcol
5743#ifdef FEAT_PROP_POPUP
5744 - curwin->w_virtcol_first_char
5745#endif
5746 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005747 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005748 if (virtcol >= (colnr_T)width1 && width2 > 0)
5749 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005750 }
5751 else
5752 i = curwin->w_leftcol;
5753 // Go to the middle of the screen line. When 'number' or
5754 // 'relativenumber' is on and lines are wrapping the middle can be more
5755 // to the left.
5756 if (cap->nchar == 'm')
5757 i += (curwin->w_width - curwin_col_off()
5758 + ((curwin->w_p_wrap && i > 0)
5759 ? curwin_col_off2() : 0)) / 2;
5760 coladvance((colnr_T)i);
5761 if (flag)
5762 {
5763 do
5764 i = gchar_cursor();
5765 while (VIM_ISWHITE(i) && oneright() == OK);
5766 curwin->w_valid &= ~VALID_WCOL;
5767 }
5768 curwin->w_set_curswant = TRUE;
5769}
5770
5771/*
5772 * "g_": to the last non-blank character in the line or <count> lines
5773 * downward.
5774 */
5775 static void
5776nv_g_underscore_cmd(cmdarg_T *cap)
5777{
5778 char_u *ptr;
5779
5780 cap->oap->motion_type = MCHAR;
5781 cap->oap->inclusive = TRUE;
5782 curwin->w_curswant = MAXCOL;
5783 if (cursor_down((long)(cap->count1 - 1),
5784 cap->oap->op_type == OP_NOP) == FAIL)
5785 {
5786 clearopbeep(cap->oap);
5787 return;
5788 }
5789
5790 ptr = ml_get_curline();
5791
5792 // In Visual mode we may end up after the line.
5793 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5794 --curwin->w_cursor.col;
5795
5796 // Decrease the cursor column until it's on a non-blank.
5797 while (curwin->w_cursor.col > 0
5798 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5799 --curwin->w_cursor.col;
5800 curwin->w_set_curswant = TRUE;
5801 adjust_for_sel(cap);
5802}
5803
5804/*
5805 * "g$" : Like "$" but for screen lines.
5806 */
5807 static void
5808nv_g_dollar_cmd(cmdarg_T *cap)
5809{
5810 oparg_T *oap = cap->oap;
5811 int i;
5812 int col_off = curwin_col_off();
5813
5814 oap->motion_type = MCHAR;
5815 oap->inclusive = TRUE;
5816 if (curwin->w_p_wrap && curwin->w_width != 0)
5817 {
5818 curwin->w_curswant = MAXCOL; // so we stay at the end
5819 if (cap->count1 == 1)
5820 {
5821 int width1 = curwin->w_width - col_off;
5822 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005823 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005824
5825 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005826 virtcol = curwin->w_virtcol
5827#ifdef FEAT_PROP_POPUP
5828 - curwin->w_virtcol_first_char
5829#endif
5830 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005831 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005832 if (virtcol >= (colnr_T)width1)
5833 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005834 * width2;
5835 coladvance((colnr_T)i);
5836
5837 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005838 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005839 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5840 {
5841 // Check for landing on a character that got split at
5842 // the end of the line. We do not want to advance to
5843 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005844 if (curwin->w_virtcol
5845#ifdef FEAT_PROP_POPUP
5846 - curwin->w_virtcol_first_char
5847#endif
5848 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005849 --curwin->w_cursor.col;
5850 }
5851 }
5852 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5853 clearopbeep(oap);
5854 }
5855 else
5856 {
5857 if (cap->count1 > 1)
5858 // if it fails, let the cursor still move to the last char
5859 (void)cursor_down(cap->count1 - 1, FALSE);
5860
5861 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5862 coladvance((colnr_T)i);
5863
5864 // if the character doesn't fit move one back
5865 if (curwin->w_cursor.col > 0
5866 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5867 {
5868 colnr_T vcol;
5869
5870 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5871 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5872 --curwin->w_cursor.col;
5873 }
5874
5875 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005876 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005877 }
5878}
5879
5880/*
5881 * "gi": start Insert at the last position.
5882 */
5883 static void
5884nv_gi_cmd(cmdarg_T *cap)
5885{
5886 int i;
5887
5888 if (curbuf->b_last_insert.lnum != 0)
5889 {
5890 curwin->w_cursor = curbuf->b_last_insert;
5891 check_cursor_lnum();
5892 i = (int)STRLEN(ml_get_curline());
5893 if (curwin->w_cursor.col > (colnr_T)i)
5894 {
5895 if (virtual_active())
5896 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5897 curwin->w_cursor.col = i;
5898 }
5899 }
5900 cap->cmdchar = 'i';
5901 nv_edit(cap);
5902}
5903
5904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 * Commands starting with "g".
5906 */
5907 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005908nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 switch (cap->nchar)
5914 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005915 case Ctrl_A:
5916 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005918 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005919 if (!VIsual_active && cap->nchar == Ctrl_A)
5920 vim_mem_profile_dump();
5921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005923 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005924 if (VIsual_active)
5925 {
5926 cap->arg = TRUE;
5927 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005928 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005929 nv_addsub(cap);
5930 }
5931 else
5932 clearopbeep(oap);
5933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005935 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 case 'R':
5937 cap->arg = TRUE;
5938 nv_Replace(cap);
5939 break;
5940
5941 case 'r':
5942 nv_vreplace(cap);
5943 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 case '&':
5946 do_cmdline_cmd((char_u *)"%s//~/&");
5947 break;
5948
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005949 // "gv": Reselect the previous Visual area. If Visual already active,
5950 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005952 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005954
5955 // "gV": Don't reselect the previous Visual area after a Select mode
5956 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 case 'V':
5958 VIsual_reselect = FALSE;
5959 break;
5960
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005961 // "gh": start Select mode.
5962 // "gH": start Select line mode.
5963 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 case K_BS:
5965 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005966 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 case 'h':
5968 case 'H':
5969 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 cap->cmdchar = cap->nchar + ('v' - 'h');
5971 cap->arg = TRUE;
5972 nv_visual(cap);
5973 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005975 // "gn", "gN" visually select next/previous search match
5976 // "gn" selects next match
5977 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005978 case 'N':
5979 case 'n':
5980 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005981 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005984 // "gj" and "gk" two new funny movement keys -- up and down
5985 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 case 'j':
5987 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005988 // with 'nowrap' it works just like the normal "j" command.
5989 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 {
5991 oap->motion_type = MLINE;
5992 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5993 }
5994 else
5995 i = nv_screengo(oap, FORWARD, cap->count1);
5996 if (i == FAIL)
5997 clearopbeep(oap);
5998 break;
5999
6000 case 'k':
6001 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006002 // with 'nowrap' it works just like the normal "k" command.
6003 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 {
6005 oap->motion_type = MLINE;
6006 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6007 }
6008 else
6009 i = nv_screengo(oap, BACKWARD, cap->count1);
6010 if (i == FAIL)
6011 clearopbeep(oap);
6012 break;
6013
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006014 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 case 'J':
6016 nv_join(cap);
6017 break;
6018
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006019 // "g0", "g^" : Like "0" and "^" but for screen lines.
6020 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 case '0':
6023 case 'm':
6024 case K_HOME:
6025 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006026 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 break;
6028
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006029 case 'M':
6030 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006031 oap->motion_type = MCHAR;
6032 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006033 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006034 if (cap->count0 > 0 && cap->count0 <= 100)
6035 coladvance((colnr_T)(i * cap->count0 / 100));
6036 else
6037 coladvance((colnr_T)(i / 2));
6038 curwin->w_set_curswant = TRUE;
6039 }
6040 break;
6041
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006042 // "g_": to the last non-blank character in the line or <count> lines
6043 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006045 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 break;
6047
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 case '$':
6050 case K_END:
6051 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006052 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 break;
6054
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006055 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 case '*':
6057 case '#':
6058#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006059 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006061 case Ctrl_RSB: // :tag or :tselect for current identifier
6062 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 nv_ident(cap);
6064 break;
6065
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006066 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 case 'e':
6068 case 'E':
6069 oap->motion_type = MCHAR;
6070 curwin->w_set_curswant = TRUE;
6071 oap->inclusive = TRUE;
6072 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6073 clearopbeep(oap);
6074 break;
6075
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006076 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006078 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 break;
6080
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006081 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 break;
6085
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006086 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 case 'I':
6088 beginline(0);
6089 if (!checkclearopq(oap))
6090 invoke_edit(cap, FALSE, 'g', FALSE);
6091 break;
6092
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006093 // "gf": goto file, edit file under cursor
6094 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006096 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 nv_gotofile(cap);
6098 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006100 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 case '\'':
6102 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006103 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 case '`':
6105 nv_gomark(cap);
6106 break;
6107
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006108 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006110 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 break;
6112
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006113 // "ga": Display the ascii value of the character under the
6114 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 case 'a':
6116 do_ascii(NULL);
6117 break;
6118
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006119 // "g8": Display the bytes used for the UTF-8 character under the
6120 // cursor. It is displayed in hex.
6121 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006123 if (cap->count0 == 8)
6124 utf_find_illegal();
6125 else
6126 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006129 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006130 case '<':
6131 show_sb_text();
6132 break;
6133
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006134 // "gg": Goto the first line in file. With a count it goes to
6135 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 case 'g':
6137 cap->arg = FALSE;
6138 nv_goto(cap);
6139 break;
6140
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006141 // Two-character operators:
6142 // "gq" Format text
6143 // "gw" Format text and keep cursor position
6144 // "g~" Toggle the case of the text.
6145 // "gu" Change text to lower case.
6146 // "gU" Change text to upper case.
6147 // "g?" rot13 encoding
6148 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 case 'q':
6150 case 'w':
6151 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006152 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 case '~':
6154 case 'u':
6155 case 'U':
6156 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006157 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 nv_operator(cap);
6159 break;
6160
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006161 // "gd": Find first occurrence of pattern under the cursor in the
6162 // current function
6163 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 case 'd':
6165 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006166 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 break;
6168
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006169 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 case K_MIDDLEMOUSE:
6171 case K_MIDDLEDRAG:
6172 case K_MIDDLERELEASE:
6173 case K_LEFTMOUSE:
6174 case K_LEFTDRAG:
6175 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006176 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 case K_RIGHTMOUSE:
6178 case K_RIGHTDRAG:
6179 case K_RIGHTRELEASE:
6180 case K_X1MOUSE:
6181 case K_X1DRAG:
6182 case K_X1RELEASE:
6183 case K_X2MOUSE:
6184 case K_X2DRAG:
6185 case K_X2RELEASE:
6186 mod_mask = MOD_MASK_CTRL;
6187 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 case K_IGNORE:
6191 break;
6192
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006193 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 case 'p':
6195 case 'P':
6196 nv_put(cap);
6197 break;
6198
6199#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006200 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 case 'o':
6202 goto_byte(cap->count0);
6203 break;
6204#endif
6205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006206 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006208 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 do_exmode(TRUE);
6210 break;
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 case ',':
6213 nv_pcmark(cap);
6214 break;
6215
6216 case ';':
6217 cap->count1 = -cap->count1;
6218 nv_pcmark(cap);
6219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006222 if (!checkclearop(oap))
6223 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006225 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006226 if (!checkclearop(oap))
6227 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006228 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229
Bram Moolenaar62a23252020-08-09 14:04:42 +02006230 case TAB:
6231 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6232 clearopbeep(oap);
6233 break;
6234
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006235 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006237 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006238 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006239 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006240 break;
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 default:
6243 clearopbeep(oap);
6244 break;
6245 }
6246}
6247
6248/*
6249 * Handle "o" and "O" commands.
6250 */
6251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006252n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006254#ifdef FEAT_CONCEAL
6255 linenr_T oldline = curwin->w_cursor.lnum;
6256#endif
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (!checkclearopq(cap->oap))
6259 {
6260#ifdef FEAT_FOLDING
6261 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006262 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 (void)hasFolding(curwin->w_cursor.lnum,
6264 &curwin->w_cursor.lnum, NULL);
6265 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006266 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 (void)hasFolding(curwin->w_cursor.lnum,
6268 NULL, &curwin->w_cursor.lnum);
6269#endif
6270 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6271 (cap->cmdchar == 'O' ? 1 : 0)),
6272 (linenr_T)(curwin->w_cursor.lnum +
6273 (cap->cmdchar == 'o' ? 1 : 0))
6274 ) == OK
6275 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006276 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006277 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006279#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006280 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006281 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006282#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006283#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006284 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006285 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006286 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006287#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006288 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006289 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6290 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6292 }
6293 }
6294}
6295
6296/*
6297 * "." command: redo last change.
6298 */
6299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006300nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 if (!checkclearopq(cap->oap))
6303 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006304 // If "restart_edit" is TRUE, the last but one command is repeated
6305 // instead of the last command (inserting text). This is used for
6306 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6308 clearopbeep(cap->oap);
6309 }
6310}
6311
6312/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006313 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 */
6315 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006316nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006318 if (VIsual_select && VIsual_active)
6319 {
6320 int reg;
6321 // Get register name
6322 ++no_mapping;
6323 ++allow_keys;
6324 reg = plain_vgetc();
6325 LANGMAP_ADJUST(reg, TRUE);
6326 --no_mapping;
6327 --allow_keys;
6328
6329 if (reg == '"')
6330 // the unnamed register is 0
6331 reg = 0;
6332
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006333 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006334 return;
6335 }
6336
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (!checkclearopq(cap->oap))
6338 {
6339 u_redo((int)cap->count1);
6340 curwin->w_set_curswant = TRUE;
6341 }
6342}
6343
6344/*
6345 * Handle "U" command.
6346 */
6347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006348nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006350 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006351 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006353 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 cap->cmdchar = 'g';
6355 cap->nchar = 'U';
6356 nv_operator(cap);
6357 }
6358 else if (!checkclearopq(cap->oap))
6359 {
6360 u_undoline();
6361 curwin->w_set_curswant = TRUE;
6362 }
6363}
6364
6365/*
6366 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6367 * single character.
6368 */
6369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006370nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006372 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006373 {
6374#ifdef FEAT_JOB_CHANNEL
6375 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6376 {
6377 clearopbeep(cap->oap);
6378 return;
6379 }
6380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 else
6384 nv_operator(cap);
6385}
6386
6387/*
6388 * Handle an operator command.
6389 * The actual work is done by do_pending_operator().
6390 */
6391 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006392nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 int op_type;
6395
6396 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006397#ifdef FEAT_JOB_CHANNEL
6398 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6399 {
6400 clearopbeep(cap->oap);
6401 return;
6402 }
6403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006405 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 nv_lineop(cap);
6407 else if (!checkclearop(cap->oap))
6408 {
6409 cap->oap->start = curwin->w_cursor;
6410 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006411#ifdef FEAT_EVAL
6412 set_op_var(op_type);
6413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415}
6416
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006417#ifdef FEAT_EVAL
6418/*
6419 * Set v:operator to the characters for "optype".
6420 */
6421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006422set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006423{
6424 char_u opchars[3];
6425
6426 if (optype == OP_NOP)
6427 set_vim_var_string(VV_OP, NULL, 0);
6428 else
6429 {
6430 opchars[0] = get_op_char(optype);
6431 opchars[1] = get_extra_op_char(optype);
6432 opchars[2] = NUL;
6433 set_vim_var_string(VV_OP, opchars, -1);
6434 }
6435}
6436#endif
6437
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438/*
6439 * Handle linewise operator "dd", "yy", etc.
6440 *
6441 * "_" is is a strange motion command that helps make operators more logical.
6442 * It is actually implemented, but not documented in the real Vi. This motion
6443 * command actually refers to "the current line". Commands like "dd" and "yy"
6444 * are really an alternate form of "d_" and "y_". It does accept a count, so
6445 * "d3_" works to delete 3 lines.
6446 */
6447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 cap->oap->motion_type = MLINE;
6451 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6452 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006453 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006454 && cap->oap->motion_force != 'v'
6455 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 || cap->oap->op_type == OP_LSHIFT
6457 || cap->oap->op_type == OP_RSHIFT)
6458 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 beginline(BL_WHITE | BL_FIX);
6461}
6462
6463/*
6464 * <Home> command.
6465 */
6466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006467nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006469 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006470 if (mod_mask & MOD_MASK_CTRL)
6471 nv_goto(cap);
6472 else
6473 {
6474 cap->count0 = 1;
6475 nv_pipe(cap);
6476 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006477 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6478 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479}
6480
6481/*
6482 * "|" command.
6483 */
6484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006485nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 cap->oap->motion_type = MCHAR;
6488 cap->oap->inclusive = FALSE;
6489 beginline(0);
6490 if (cap->count0 > 0)
6491 {
6492 coladvance((colnr_T)(cap->count0 - 1));
6493 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6494 }
6495 else
6496 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006497 // keep curswant at the column where we wanted to go, not where
6498 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 curwin->w_set_curswant = FALSE;
6500}
6501
6502/*
6503 * Handle back-word command "b" and "B".
6504 * cap->arg is 1 for "B"
6505 */
6506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006507nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 cap->oap->motion_type = MCHAR;
6510 cap->oap->inclusive = FALSE;
6511 curwin->w_set_curswant = TRUE;
6512 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6513 clearopbeep(cap->oap);
6514#ifdef FEAT_FOLDING
6515 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6516 foldOpenCursor();
6517#endif
6518}
6519
6520/*
6521 * Handle word motion commands "e", "E", "w" and "W".
6522 * cap->arg is TRUE for "E" and "W".
6523 */
6524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006525nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 int n;
6528 int word_end;
6529 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006530 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006532 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6534 word_end = TRUE;
6535 else
6536 word_end = FALSE;
6537 cap->oap->inclusive = word_end;
6538
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006539 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (!word_end && cap->oap->op_type == OP_CHANGE)
6541 {
6542 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006543 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006545 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006547 // Reproduce a funny Vi behaviour: "cw" on a blank only
6548 // changes one character, not all blanks until the start of
6549 // the next word. Only do this when the 'w' flag is included
6550 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6552 {
6553 cap->oap->inclusive = TRUE;
6554 cap->oap->motion_type = MCHAR;
6555 return;
6556 }
6557 }
6558 else
6559 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006560 // This is a little strange. To match what the real Vi does,
6561 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6562 // that we are not on a space or a TAB. This seems impolite
6563 // at first, but it's really more what we mean when we say
6564 // 'cw'.
6565 // Another strangeness: When standing on the end of a word
6566 // "ce" will change until the end of the next word, but "cw"
6567 // will change only one character! This is done by setting
6568 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 cap->oap->inclusive = TRUE;
6570 word_end = TRUE;
6571 flag = TRUE;
6572 }
6573 }
6574 }
6575
6576 cap->oap->motion_type = MCHAR;
6577 curwin->w_set_curswant = TRUE;
6578 if (word_end)
6579 n = end_word(cap->count1, cap->arg, flag, FALSE);
6580 else
6581 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006583 // Don't leave the cursor on the NUL past the end of line. Unless we
6584 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006585 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006586 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
6588 if (n == FAIL && cap->oap->op_type == OP_NOP)
6589 clearopbeep(cap->oap);
6590 else
6591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593#ifdef FEAT_FOLDING
6594 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6595 foldOpenCursor();
6596#endif
6597 }
6598}
6599
6600/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006601 * Used after a movement command: If the cursor ends up on the NUL after the
6602 * end of the line, may move it back to the last character and make the motion
6603 * inclusive.
6604 */
6605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006607{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006608 // The cursor cannot remain on the NUL when:
6609 // - the column is > 0
6610 // - not in Visual mode or 'selection' is "o"
6611 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006612 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006613 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006614 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006615 {
6616 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006618 if (has_mbyte)
6619 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006620 oap->inclusive = TRUE;
6621 }
6622}
6623
6624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 * "0" and "^" commands.
6626 * cap->arg is the argument for beginline().
6627 */
6628 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006629nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 cap->oap->motion_type = MCHAR;
6632 cap->oap->inclusive = FALSE;
6633 beginline(cap->arg);
6634#ifdef FEAT_FOLDING
6635 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6636 foldOpenCursor();
6637#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006638 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6639 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640}
6641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642/*
6643 * In exclusive Visual mode, may include the last character.
6644 */
6645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006646adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006649 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (has_mbyte)
6652 inc_cursor();
6653 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 ++curwin->w_cursor.col;
6655 cap->oap->inclusive = FALSE;
6656 }
6657}
6658
6659/*
6660 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6661 * Should check VIsual_mode before calling this.
6662 * Returns TRUE when backed up to the previous line.
6663 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006665unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 pos_T *pp;
6668
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006669 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006671 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 pp = &curwin->w_cursor;
6673 else
6674 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (pp->coladd > 0)
6676 --pp->coladd;
6677 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 if (pp->col > 0)
6679 {
6680 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006681 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
6683 else if (pp->lnum > 1)
6684 {
6685 --pp->lnum;
6686 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6687 return TRUE;
6688 }
6689 }
6690 return FALSE;
6691}
6692
6693/*
6694 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6695 */
6696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006697nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006702 VIsual_select_reg = 0;
6703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 else if (VIsual_reselect)
6705 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006706 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 cap->arg = TRUE;
6708 nv_g_cmd(cap);
6709 }
6710}
6711
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713/*
6714 * "G", "gg", CTRL-END, CTRL-HOME.
6715 * cap->arg is TRUE for "G".
6716 */
6717 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006718nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 linenr_T lnum;
6721
6722 if (cap->arg)
6723 lnum = curbuf->b_ml.ml_line_count;
6724 else
6725 lnum = 1L;
6726 cap->oap->motion_type = MLINE;
6727 setpcmark();
6728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006729 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (cap->count0 != 0)
6731 lnum = cap->count0;
6732 if (lnum < 1L)
6733 lnum = 1L;
6734 else if (lnum > curbuf->b_ml.ml_line_count)
6735 lnum = curbuf->b_ml.ml_line_count;
6736 curwin->w_cursor.lnum = lnum;
6737 beginline(BL_SOL | BL_FIX);
6738#ifdef FEAT_FOLDING
6739 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6740 foldOpenCursor();
6741#endif
6742}
6743
6744/*
6745 * CTRL-\ in Normal mode.
6746 */
6747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006748nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6751 {
6752 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006753 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (cmdwin_type != 0)
6757 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (VIsual_active)
6759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006760 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006761 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006763 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (cap->nchar == Ctrl_G && p_im)
6765 restart_edit = 'a';
6766 }
6767 else
6768 clearopbeep(cap->oap);
6769}
6770
6771/*
6772 * ESC in Normal mode: beep, but don't flush buffers.
6773 * Don't even beep if we are canceling a command.
6774 */
6775 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006776nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 int no_reason;
6779
6780 no_reason = (cap->oap->op_type == OP_NOP
6781 && cap->opcount == 0
6782 && cap->count0 == 0
6783 && cap->oap->regname == 0
6784 && !p_im);
6785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006786 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006788 if (restart_edit == 0 && cmdwin_type == 0
6789 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006790 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006791 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6792
Bram Moolenaar5939c352022-09-24 12:50:45 +01006793 // The user may accidentally do "vim file | grep word" and then
6794 // CTRL-C doesn't show anything. With a changed buffer give the
6795 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006796 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006797 {
6798 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6799
6800 if (out_redir)
6801 mch_errmsg(ms);
6802 else
6803 msg(ms);
6804 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006805 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006806 {
6807 if (out_redir)
6808 {
6809 got_int = FALSE;
6810 do_cmdline_cmd((char_u *)"qa");
6811 }
6812 else
6813 msg(_("Type :qa and press <Enter> to exit Vim"));
6814 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006817 if (restart_edit != 0)
6818 redraw_mode = TRUE; // remove "-- (insert) --"
6819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006820 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6821 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (!p_im)
6823 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (cmdwin_type != 0)
6825 {
6826 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006827 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 return;
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006831 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006832 {
6833 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006834 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6835 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006836 cmdwin_result = K_IGNORE;
6837 return;
6838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (VIsual_active)
6841 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006842 end_visual_mode(); // stop Visual
6843 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006845 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006847 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006848 {
6849#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006850 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006851 popup_hide_message_win();
6852 else
6853#endif
6854 vim_beep(BO_ESC);
6855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 clearop(cap->oap);
6857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006858 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6859 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006860 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 restart_edit = 'a';
6862}
6863
6864/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006865 * Move the cursor for the "A" command.
6866 */
6867 void
6868set_cursor_for_append_to_line(void)
6869{
6870 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006871 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006872 {
6873 int save_State = State;
6874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006875 // Pretend Insert mode here to allow the cursor on the
6876 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006877 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006878 coladvance((colnr_T)MAXCOL);
6879 State = save_State;
6880 }
6881 else
6882 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6883}
6884
6885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006887 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
6889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006890nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006892 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6894 cap->cmdchar = 'i';
6895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006896 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006898 {
6899#ifdef FEAT_TERMINAL
6900 if (term_in_normal_mode())
6901 {
6902 end_visual_mode();
6903 clearop(cap->oap);
6904 term_enter_job_mode();
6905 return;
6906 }
6907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006911 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006912 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6913 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006917#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006918 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006919 {
6920 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006921 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006922 return;
6923 }
6924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 else if (!curbuf->b_p_ma && !p_im)
6926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006928 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006930 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006931 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006932 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006934 else if (cap->cmdchar == K_PS && VIsual_active)
6935 {
6936 pos_T old_pos = curwin->w_cursor;
6937 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006938 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006940 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006941 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6942 {
6943 shift_delete_registers();
6944 cap->oap->regname = '1';
6945 }
6946 else
6947 cap->oap->regname = '-';
6948 cap->cmdchar = 'd';
6949 cap->nchar = NUL;
6950 nv_operator(cap);
6951 do_pending_operator(cap, 0, FALSE);
6952 cap->cmdchar = K_PS;
6953
zeertzjq7a732522022-03-14 20:46:41 +00006954 if (*ml_get_cursor() != NUL)
6955 {
6956 if (old_visual_mode == 'V')
6957 {
6958 // In linewise Visual mode insert before the beginning of the
6959 // next line.
6960 // When the last line in the buffer was deleted then create a
6961 // new line, otherwise there is not need to move cursor.
6962 // Detect this by checking if cursor moved above Visual area.
6963 if (curwin->w_cursor.lnum < old_pos.lnum
6964 && curwin->w_cursor.lnum < old_visual.lnum)
6965 {
6966 if (u_save_cursor() == OK)
6967 {
6968 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6969 FALSE);
6970 appended_lines(curwin->w_cursor.lnum++, 1L);
6971 }
6972 }
6973 }
6974 // When the last char in the line was deleted then append.
6975 // Detect this by checking if cursor moved before Visual area.
6976 else if (curwin->w_cursor.col < old_pos.col
6977 && curwin->w_cursor.col < old_visual.col)
6978 inc_cursor();
6979 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006981 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006982 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 else if (!checkclearopq(cap->oap))
6985 {
6986 switch (cap->cmdchar)
6987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006989 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 break;
6991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006992 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006993 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6994 beginline(BL_WHITE);
6995 else
6996 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 break;
6998
Bram Moolenaara1891842017-02-04 21:34:31 +01006999 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 // Bracketed paste works like "a"ppend, unless the cursor is in
7001 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007002 if (curwin->w_cursor.col == 0)
7003 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007004 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007006 case 'a': // "a"ppend is like "i"nsert on the next character.
7007 // increment coladd when in virtual space, increment the
7008 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (virtual_active()
7010 && (curwin->w_cursor.coladd > 0
7011 || *ml_get_cursor() == NUL
7012 || *ml_get_cursor() == TAB))
7013 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007014 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 inc_cursor();
7016 break;
7017 }
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7020 {
7021 int save_State = State;
7022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007023 // Pretend Insert mode here to allow the cursor on the
7024 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007025 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 coladvance(getviscol());
7027 State = save_State;
7028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7031 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007032 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007033 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007034 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035}
7036
7037/*
7038 * Invoke edit() and take care of "restart_edit" and the return value.
7039 */
7040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007041invoke_edit(
7042 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007043 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007044 int cmd,
7045 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 int restart_edit_save = 0;
7048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007049 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7050 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7051 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 if (repl || !stuff_empty())
7053 restart_edit_save = restart_edit;
7054 else
7055 restart_edit_save = 0;
7056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 restart_edit = 0;
7059
7060 if (edit(cmd, startln, cap->count1))
7061 cap->retval |= CA_COMMAND_BUSY;
7062
7063 if (restart_edit == 0)
7064 restart_edit = restart_edit_save;
7065}
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067/*
7068 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7069 */
7070 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007071nv_object(
7072 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 int flag;
7075 int include;
7076 char_u *mps_save;
7077
7078 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 mps_save = curbuf->b_p_mps;
7085 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7086
7087 switch (cap->nchar)
7088 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 flag = current_word(cap->oap, cap->count1, include, FALSE);
7091 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 flag = current_word(cap->oap, cap->count1, include, TRUE);
7094 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007095 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 case '(':
7097 case ')':
7098 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 case '{':
7102 case '}':
7103 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7104 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 case ']':
7107 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7108 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 case '>':
7111 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7112 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007113#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007114 case 't': // "at" = a tag block (xml and html)
7115 // Do not adjust oap->end in do_pending_operator()
7116 // otherwise there are different results for 'dit'
7117 // (note leading whitespace in last line):
7118 // 1) <b> 2) <b>
7119 // foobar foobar
7120 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007121 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007122 flag = current_tagblock(cap->oap, cap->count1, include);
7123 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007124#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 flag = current_par(cap->oap, cap->count1, include, 'p');
7127 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007128 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 flag = current_sent(cap->oap, cap->count1, include);
7130 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 case '"': // "a"" = a double quoted string
7132 case '\'': // "a'" = a single quoted string
7133 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007134 flag = current_quote(cap->oap, cap->count1, include,
7135 cap->nchar);
7136 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007137#if 0 // TODO
7138 case 'S': // "aS" = a section
7139 case 'f': // "af" = a filename
7140 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141#endif
7142 default:
7143 flag = FAIL;
7144 break;
7145 }
7146
7147 curbuf->b_p_mps = mps_save;
7148 if (flag == FAIL)
7149 clearopbeep(cap->oap);
7150 adjust_cursor_col();
7151 curwin->w_set_curswant = TRUE;
7152}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154/*
7155 * "q" command: Start/stop recording.
7156 * "q:", "q/", "q?": edit command-line in command-line window.
7157 */
7158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007159nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 if (cap->oap->op_type == OP_FORMAT)
7162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 cap->cmdchar = 'g';
7165 cap->nchar = 'q';
7166 nv_operator(cap);
7167 }
7168 else if (!checkclearop(cap->oap))
7169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7171 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007172 if (cmdwin_type != 0)
7173 {
7174 emsg(_(e_cmdline_window_already_open));
7175 return;
7176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 stuffcharReadbuff(cap->nchar);
7178 stuffcharReadbuff(K_CMDWIN);
7179 }
7180 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 // (stop) recording into a named register, unless executing a
7182 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007183 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 clearopbeep(cap->oap);
7185 }
7186}
7187
7188/*
7189 * Handle the "@r" command.
7190 */
7191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007192nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 if (checkclearop(cap->oap))
7195 return;
7196#ifdef FEAT_EVAL
7197 if (cap->nchar == '=')
7198 {
7199 if (get_expr_register() == NUL)
7200 return;
7201 }
7202#endif
7203 while (cap->count1-- && !got_int)
7204 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007205 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
7207 clearopbeep(cap->oap);
7208 break;
7209 }
7210 line_breakcheck();
7211 }
7212}
7213
7214/*
7215 * Handle the CTRL-U and CTRL-D commands.
7216 */
7217 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007218nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
7220 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7221 || (cap->cmdchar == Ctrl_D
7222 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7223 clearopbeep(cap->oap);
7224 else if (!checkclearop(cap->oap))
7225 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7226}
7227
7228/*
7229 * Handle "J" or "gJ" command.
7230 */
7231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007232nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007234 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007236 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
7238 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007239 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7241 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007243 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007244 if (cap->count0 <= 2)
7245 {
7246 clearopbeep(cap->oap);
7247 return;
7248 }
7249 cap->count0 = curbuf->b_ml.ml_line_count
7250 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007252
7253 prep_redo(cap->oap->regname, cap->count0,
7254 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7255 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257}
7258
7259/*
7260 * "P", "gP", "p" and "gp" commands.
7261 */
7262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007263nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007265 nv_put_opt(cap, FALSE);
7266}
7267
7268/*
7269 * "P", "gP", "p" and "gp" commands.
7270 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7271 */
7272 static void
7273nv_put_opt(cmdarg_T *cap, int fix_indent)
7274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int regname = 0;
7276 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007277 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007278 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 int dir;
7280 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007281 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 if (cap->oap->op_type != OP_NOP)
7284 {
7285#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007286 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7288 {
7289 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007290 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292 else
7293#endif
7294 clearopbeep(cap->oap);
7295 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007296#ifdef FEAT_JOB_CHANNEL
7297 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7298 {
7299 clearopbeep(cap->oap);
7300 }
7301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 else
7303 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007304 if (fix_indent)
7305 {
7306 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7307 ? FORWARD : BACKWARD;
7308 flags |= PUT_FIXINDENT;
7309 }
7310 else
7311 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007312 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7313 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 prep_redo_cmd(cap);
7315 if (cap->cmdchar == 'g')
7316 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007317 else if (cap->cmdchar == 'z')
7318 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 if (VIsual_active)
7321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 // Putting in Visual mode: The put text replaces the selected
7323 // text. First delete the selected text, then put the new text.
7324 // Need to save and restore the registers that the delete
7325 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007326 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007328 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007329#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007331#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007332 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007333 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007334#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338 )
7339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007340 // The delete is going to overwrite the register we want to
7341 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 reg1 = get_register(regname, TRUE);
7343 }
7344
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007345 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 cap->cmdchar = 'd';
7347 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007348 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007349 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 nv_operator(cap);
7351 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007352 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007353 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 cap->oap->regname = regname;
7357
7358 if (reg1 != NULL)
7359 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007360 // Delete probably changed the register we want to put, save
7361 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 reg2 = get_register(regname, FALSE);
7363 put_register(regname, reg1);
7364 }
7365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // When deleted a linewise Visual area, put the register as
7367 // lines to avoid it joined with the next line. When deletion was
7368 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (VIsual_mode == 'V')
7370 flags |= PUT_LINE;
7371 else if (VIsual_mode == 'v')
7372 flags |= PUT_LINE_SPLIT;
7373 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7374 flags |= PUT_LINE_FORWARD;
7375 dir = BACKWARD;
7376 if ((VIsual_mode != 'V'
7377 && curwin->w_cursor.col < curbuf->b_op_start.col)
7378 || (VIsual_mode == 'V'
7379 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007380 // cursor is at the end of the line or end of file, put
7381 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007383 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007384 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007386 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007388 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 if (reg2 != NULL)
7390 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // What to reselect with "gv"? Selecting the just put text seems to
7393 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007394 if (was_visual)
7395 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007396 curbuf->b_visual.vi_start = curbuf->b_op_start;
7397 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007398 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007399 if (*p_sel == 'e')
7400 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007401 }
7402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007403 // When all lines were selected and deleted do_put() leaves an empty
7404 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007405 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007406 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007407 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007408 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007410 // If the cursor was in that line, move it to the end of the last
7411 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007412 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7413 {
7414 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7415 coladvance((colnr_T)MAXCOL);
7416 }
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 auto_format(FALSE, TRUE);
7419 }
7420}
7421
7422/*
7423 * "o" and "O" commands.
7424 */
7425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007426nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007429 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7431 {
7432 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007433 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 }
7435 else
7436#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007437 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007439#ifdef FEAT_JOB_CHANNEL
7440 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007441 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 n_opencmd(cap);
7445}
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447#ifdef FEAT_NETBEANS_INTG
7448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007449nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450{
7451 netbeans_keycommand(cap->nchar);
7452}
7453#endif
7454
7455#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007457nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007459 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460}
7461#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007462
Bram Moolenaar3918c952005-03-15 22:34:55 +00007463/*
7464 * Trigger CursorHold event.
7465 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7466 * input buffer. "did_cursorhold" is set to avoid retriggering.
7467 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007469nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007470{
7471 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7472 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007473 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007474}