blob: 96f83d3f316acfce2d4ccb87fb60f7966e01953c [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 Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_record(cmdarg_T *cap);
116static void nv_at(cmdarg_T *cap);
117static void nv_halfpage(cmdarg_T *cap);
118static void nv_join(cmdarg_T *cap);
119static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200120static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
ichizok672776d2022-01-31 12:27:18 +0000130// Declare nv_cmds[].
131#define DO_DECLARE_NVCMD
132#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000134// Include the lookuptable generated by create_nvcmdidx.vim.
135#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
138 * Search for a command in the commands table.
139 * Returns -1 for invalid command.
140 */
141 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100142find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
144 int i;
145 int idx;
146 int top, bot;
147 int c;
148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 if (cmdchar >= 0x100)
151 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100153 // We use the absolute value of the character. Special keys have a
154 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (cmdchar < 0)
156 cmdchar = -cmdchar;
157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100158 // If the character is in the first part: The character is the index into
159 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (cmdchar <= nv_max_linear)
161 return nv_cmd_idx[cmdchar];
162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 bot = nv_max_linear + 1;
165 top = NV_CMDS_SIZE - 1;
166 idx = -1;
167 while (bot <= top)
168 {
169 i = (top + bot) / 2;
170 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
171 if (c < 0)
172 c = -c;
173 if (cmdchar == c)
174 {
175 idx = nv_cmd_idx[i];
176 break;
177 }
178 if (cmdchar > c)
179 bot = i + 1;
180 else
181 top = i - 1;
182 }
183 return idx;
184}
185
186/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000187 * Handle the count before a normal command and set cap->count0.
188 */
189 static int
190normal_cmd_get_count(
191 cmdarg_T *cap,
192 int c,
193 int toplevel UNUSED,
194 int set_prevcount UNUSED,
195 int *ctrl_w,
196 int *need_flushbuf UNUSED)
197{
198getcount:
199 if (!(VIsual_active && VIsual_select))
200 {
201 // Handle a count before a command and compute ca.count0.
202 // Note that '0' is a command and not the start of a count, but it's
203 // part of a count after other digits.
204 while ((c >= '1' && c <= '9')
205 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
206 || c == '0')))
207 {
208 if (c == K_DEL || c == K_KDEL)
209 {
210 cap->count0 /= 10;
211#ifdef FEAT_CMDL_INFO
212 del_from_showcmd(4); // delete the digit and ~@%
213#endif
214 }
215 else if (cap->count0 > 99999999L)
216 {
217 cap->count0 = 999999999L;
218 }
219 else
220 {
221 cap->count0 = cap->count0 * 10 + (c - '0');
222 }
223#ifdef FEAT_EVAL
224 // Set v:count here, when called from main() and not a stuffed
225 // command, so that v:count can be used in an expression mapping
226 // right after the count. Do set it for redo.
227 if (toplevel && readbuf1_empty())
228 set_vcount_ca(cap, &set_prevcount);
229#endif
230 if (*ctrl_w)
231 {
232 ++no_mapping;
233 ++allow_keys; // no mapping for nchar, but keys
234 }
235 ++no_zero_mapping; // don't map zero here
236 c = plain_vgetc();
237 LANGMAP_ADJUST(c, TRUE);
238 --no_zero_mapping;
239 if (*ctrl_w)
240 {
241 --no_mapping;
242 --allow_keys;
243 }
244#ifdef FEAT_CMDL_INFO
245 *need_flushbuf |= add_to_showcmd(c);
246#endif
247 }
248
249 // If we got CTRL-W there may be a/another count
250 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
251 {
252 *ctrl_w = TRUE;
253 cap->opcount = cap->count0; // remember first count
254 cap->count0 = 0;
255 ++no_mapping;
256 ++allow_keys; // no mapping for nchar, but keys
257 c = plain_vgetc(); // get next character
258 LANGMAP_ADJUST(c, TRUE);
259 --no_mapping;
260 --allow_keys;
261#ifdef FEAT_CMDL_INFO
262 *need_flushbuf |= add_to_showcmd(c);
263#endif
264 goto getcount; // jump back
265 }
266 }
267
268 if (c == K_CURSORHOLD)
269 {
270 // Save the count values so that ca.opcount and ca.count0 are exactly
271 // the same when coming back here after handling K_CURSORHOLD.
272 cap->oap->prev_opcount = cap->opcount;
273 cap->oap->prev_count0 = cap->count0;
274 }
275 else if (cap->opcount != 0)
276 {
277 // If we're in the middle of an operator (including after entering a
278 // yank buffer with '"') AND we had a count before the operator, then
279 // that count overrides the current value of ca.count0.
280 // What this means effectively, is that commands like "3dw" get turned
281 // into "d3w" which makes things fall into place pretty neatly.
282 // If you give a count before AND after the operator, they are
283 // multiplied.
284 if (cap->count0)
285 {
286 if (cap->opcount >= 999999999L / cap->count0)
287 cap->count0 = 999999999L;
288 else
289 cap->count0 *= cap->opcount;
290 }
291 else
292 cap->count0 = cap->opcount;
293 }
294
295 // Always remember the count. It will be set to zero (on the next call,
296 // above) when there is no pending operator.
297 // When called from main(), save the count for use by the "count" built-in
298 // variable.
299 cap->opcount = cap->count0;
300 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
301
302#ifdef FEAT_EVAL
303 // Only set v:count when called from main() and not a stuffed command.
304 // Do set it for redo.
305 if (toplevel && readbuf1_empty())
306 set_vcount(cap->count0, cap->count1, set_prevcount);
307#endif
308
309 return c;
310}
311
312/*
313 * Returns TRUE if the normal command (cap) needs a second character.
314 */
315 static int
316normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
317{
318 return ((cmd_flags & NV_NCH)
319 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
320 && cap->oap->op_type == OP_NOP)
321 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
322 || (cap->cmdchar == 'q'
323 && cap->oap->op_type == OP_NOP
324 && reg_recording == 0
325 && reg_executing == 0)
326 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
327 && (cap->oap->op_type != OP_NOP || VIsual_active))));
328}
329
330/*
331 * Get one or more additional characters for a normal command.
332 * Return the updated command index (if changed).
333 */
334 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000335normal_cmd_get_more_chars(
336 int idx_arg,
337 cmdarg_T *cap,
338 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000339{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000340 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000341 int c;
342 int *cp;
343 int repl = FALSE; // get character for replace mode
344 int lit = FALSE; // get extra character literally
345 int langmap_active = FALSE; // using :lmap mappings
346 int lang; // getting a text character
347#ifdef HAVE_INPUT_METHOD
348 int save_smd; // saved value of p_smd
349#endif
350
351 ++no_mapping;
352 ++allow_keys; // no mapping for nchar, but allow key codes
353 // Don't generate a CursorHold event here, most commands can't handle
354 // it, e.g., nv_replace(), nv_csearch().
355 did_cursorhold = TRUE;
356 if (cap->cmdchar == 'g')
357 {
358 /*
359 * For 'g' get the next character now, so that we can check for
360 * "gr", "g'" and "g`".
361 */
362 cap->nchar = plain_vgetc();
363 LANGMAP_ADJUST(cap->nchar, TRUE);
364#ifdef FEAT_CMDL_INFO
365 *need_flushbuf |= add_to_showcmd(cap->nchar);
366#endif
367 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
368 || cap->nchar == Ctrl_BSL)
369 {
370 cp = &cap->extra_char; // need to get a third character
371 if (cap->nchar != 'r')
372 lit = TRUE; // get it literally
373 else
374 repl = TRUE; // get it in replace mode
375 }
376 else
377 cp = NULL; // no third character needed
378 }
379 else
380 {
381 if (cap->cmdchar == 'r') // get it in replace mode
382 repl = TRUE;
383 cp = &cap->nchar;
384 }
385 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
386
387 /*
388 * Get a second or third character.
389 */
390 if (cp != NULL)
391 {
392 if (repl)
393 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100394 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000395#ifdef CURSOR_SHAPE
396 ui_cursor_shape(); // show different cursor shape
397#endif
398 }
399 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
400 {
401 // Allow mappings defined with ":lmap".
402 --no_mapping;
403 --allow_keys;
404 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100405 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000406 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100407 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000408 langmap_active = TRUE;
409 }
410#ifdef HAVE_INPUT_METHOD
411 save_smd = p_smd;
412 p_smd = FALSE; // Don't let the IM code show the mode here
413 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
414 im_set_active(TRUE);
415#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100416 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000417 {
418#ifdef FEAT_JOB_CHANNEL
419 ch_log_output = TRUE;
420#endif
421 // Disable bracketed paste and modifyOtherKeys here, we won't
422 // recognize the escape sequences with 'esckeys' off.
423 out_str(T_BD);
424 out_str(T_CTE);
425 }
426
427 *cp = plain_vgetc();
428
Bram Moolenaar24959102022-05-07 20:01:16 +0100429 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000430 {
431#ifdef FEAT_JOB_CHANNEL
432 ch_log_output = TRUE;
433#endif
434 // Re-enable bracketed paste mode and modifyOtherKeys
435 out_str(T_BE);
436 out_str(T_CTI);
437 }
438
439 if (langmap_active)
440 {
441 // Undo the decrement done above
442 ++no_mapping;
443 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100444 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000445 }
446#ifdef HAVE_INPUT_METHOD
447 if (lang)
448 {
449 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
450 im_save_status(&curbuf->b_p_iminsert);
451 im_set_active(FALSE);
452 }
453 p_smd = save_smd;
454#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100455 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000456#ifdef FEAT_CMDL_INFO
457 *need_flushbuf |= add_to_showcmd(*cp);
458#endif
459
460 if (!lit)
461 {
462#ifdef FEAT_DIGRAPHS
463 // Typing CTRL-K gets a digraph.
464 if (*cp == Ctrl_K
465 && ((nv_cmds[idx].cmd_flags & NV_LANG)
466 || cp == &cap->extra_char)
467 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
468 {
469 c = get_digraph(FALSE);
470 if (c > 0)
471 {
472 *cp = c;
473# ifdef FEAT_CMDL_INFO
474 // Guessing how to update showcmd here...
475 del_from_showcmd(3);
476 *need_flushbuf |= add_to_showcmd(*cp);
477# endif
478 }
479 }
480#endif
481
482 // adjust chars > 127, except after "tTfFr" commands
483 LANGMAP_ADJUST(*cp, !lang);
484#ifdef FEAT_RIGHTLEFT
485 // adjust Hebrew mapped char
486 if (p_hkmap && lang && KeyTyped)
487 *cp = hkmap(*cp);
488#endif
489 }
490
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000491 // When the next character is CTRL-\ a following CTRL-N means the
492 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000493 if (cp == &cap->extra_char
494 && cap->nchar == Ctrl_BSL
495 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
496 {
497 cap->cmdchar = Ctrl_BSL;
498 cap->nchar = cap->extra_char;
499 idx = find_command(cap->cmdchar);
500 }
501 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
502 cap->oap->op_type = get_op_type(*cp, NUL);
503 else if (*cp == Ctrl_BSL)
504 {
505 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
506
507 // There is a busy wait here when typing "f<C-\>" and then
508 // something different from CTRL-N. Can't be avoided.
509 while ((c = vpeekc()) <= 0 && towait > 0L)
510 {
511 do_sleep(towait > 50L ? 50L : towait, FALSE);
512 towait -= 50L;
513 }
514 if (c > 0)
515 {
516 c = plain_vgetc();
517 if (c != Ctrl_N && c != Ctrl_G)
518 vungetc(c);
519 else
520 {
521 cap->cmdchar = Ctrl_BSL;
522 cap->nchar = c;
523 idx = find_command(cap->cmdchar);
524 }
525 }
526 }
527
528 // When getting a text character and the next character is a
529 // multi-byte character, it could be a composing character.
530 // However, don't wait for it to arrive. Also, do enable mapping,
531 // because if it's put back with vungetc() it's too late to apply
532 // mapping.
533 --no_mapping;
534 while (enc_utf8 && lang && (c = vpeekc()) > 0
535 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
536 {
537 c = plain_vgetc();
538 if (!utf_iscomposing(c))
539 {
540 vungetc(c); // it wasn't, put it back
541 break;
542 }
543 else if (cap->ncharC1 == 0)
544 cap->ncharC1 = c;
545 else
546 cap->ncharC2 = c;
547 }
548 ++no_mapping;
549 }
550 --no_mapping;
551 --allow_keys;
552
553 return idx;
554}
555
556/*
557 * Returns TRUE if after processing a normal mode command, need to wait for a
558 * moment when a message is displayed that will be overwritten by the mode
559 * message.
560 */
561 static int
562normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
563{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000564 // In Visual mode and with "^O" in Insert mode, a short message will be
565 // overwritten by the mode message. Wait a bit, until a key is hit.
566 // In Visual mode, it's more important to keep the Visual area updated
567 // than keeping a message (e.g. from a /pat search).
568 // Only do this if the command was typed, not from a mapping.
569 // Don't wait when emsg_silent is non-zero.
570 // Also wait a bit after an error message, e.g. for "^O:".
571 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000572 return ( ((p_smd
573 && msg_silent == 0
574 && (restart_edit != 0
575 || (VIsual_active
576 && old_pos->lnum == curwin->w_cursor.lnum
577 && old_pos->col == curwin->w_cursor.col)
578 )
579 && (clear_cmdline
580 || redraw_cmdline)
581 && (msg_didout || (msg_didany && msg_scroll))
582 && !msg_nowait
583 && KeyTyped)
584 || (restart_edit != 0
585 && !VIsual_active
586 && (msg_scroll
587 || emsg_on_display)))
588 && cap->oap->regname == 0
589 && !(cap->retval & CA_COMMAND_BUSY)
590 && stuff_empty()
591 && typebuf_typed()
592 && emsg_silent == 0
593 && !in_assert_fails
594 && !did_wait_return
595 && cap->oap->op_type == OP_NOP);
596}
597
598/*
599 * After processing a normal mode command, wait for a moment when a message is
600 * displayed that will be overwritten by the mode message.
601 */
602 static void
603normal_cmd_wait_for_msg(void)
604{
605 int save_State = State;
606
607 // Draw the cursor with the right shape here
608 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100609 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000610
611 // If need to redraw, and there is a "keep_msg", redraw before the
612 // delay
613 if (must_redraw && keep_msg != NULL && !emsg_on_display)
614 {
615 char_u *kmsg;
616
617 kmsg = keep_msg;
618 keep_msg = NULL;
619 // Showmode() will clear keep_msg, but we want to use it anyway.
620 // First update w_topline.
621 setcursor();
622 update_screen(0);
623 // now reset it, otherwise it's put in the history again
624 keep_msg = kmsg;
625
626 kmsg = vim_strsave(keep_msg);
627 if (kmsg != NULL)
628 {
629 msg_attr((char *)kmsg, keep_msg_attr);
630 vim_free(kmsg);
631 }
632 }
633 setcursor();
634#ifdef CURSOR_SHAPE
635 ui_cursor_shape(); // may show different cursor shape
636#endif
637 cursor_on();
638 out_flush();
639 if (msg_scroll || emsg_on_display)
640 ui_delay(1003L, TRUE); // wait at least one second
641 ui_delay(3003L, FALSE); // wait up to three seconds
642 State = save_State;
643
644 msg_scroll = FALSE;
645 emsg_on_display = FALSE;
646}
647
648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * Execute a command in Normal mode.
650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652normal_cmd(
653 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100654 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100658 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000660 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100661 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 static int old_mapped_len = 0;
664 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000665 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200666 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaara80faa82020-04-12 19:37:17 +0200668 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100671 // Use a count remembered from before entering an operator. After typing
672 // "3d" we return from normal_cmd() and come back here, the "3" is
673 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 ca.opcount = opcount;
675
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000676 // If there is an operator pending, then the command we take this time
677 // will terminate it. Finish_op tells us to finish the operation before
678 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef CURSOR_SHAPE
680 c = finish_op;
681#endif
682 finish_op = (oap->op_type != OP_NOP);
683#ifdef CURSOR_SHAPE
684 if (finish_op != c)
685 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687# ifdef FEAT_MOUSESHAPE
688 update_mouseshape(-1);
689# endif
690 }
691#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100692 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100694 // When not finishing an operator and no register name typed, reset the
695 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000699#ifdef FEAT_EVAL
700 set_prevcount = TRUE;
701#endif
702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // Restore counts from before receiving K_CURSORHOLD. This means after
705 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
706 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000707 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
708 {
709 ca.opcount = oap->prev_opcount;
710 ca.count0 = oap->prev_count0;
711 oap->prev_opcount = 0;
712 oap->prev_count0 = 0;
713 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaar24959102022-05-07 20:01:16 +0100717 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#endif
721
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100722#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100723 // Set v:count here, when called from main() and not a stuffed
724 // command, so that v:count can be used in an expression mapping
725 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100726 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100727 set_vcount_ca(&ca, &set_prevcount);
728#endif
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 /*
731 * Get the command character from the user.
732 */
733 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100734 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000736 // If a mapping was started in Visual or Select mode, remember the length
737 // of the mapping. This is used below to not return to Insert mode for as
738 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (restart_edit == 0)
740 old_mapped_len = 0;
741 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000742 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 old_mapped_len = typebuf_maplen();
744
745 if (c == NUL)
746 c = K_ZERO;
747
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000748 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 if (VIsual_active
750 && VIsual_select
751 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
752 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000753 int len;
754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100755 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
756 // 'insertmode' is set) fake a "d"elete command, Insert mode will
757 // restart automatically.
758 // Insert the typed character in the typeahead buffer, so that it can
759 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000760 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
761
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000762 // When recording and gotchars() was called the character will be
763 // recorded again, remove the previous recording.
764 if (KeyTyped)
765 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000766
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000767 if (restart_edit != 0)
768 c = 'd';
769 else
770 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 msg_nowait = TRUE; // don't delay going to insert mode
772 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000775 // If the window was made so small that nothing shows, make it at least one
776 // line and one column when typing a command.
777 if (KeyTyped && !KeyStuffed)
778 win_ensure_size();
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef FEAT_CMDL_INFO
781 need_flushbuf = add_to_showcmd(c);
782#endif
783
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000784 // Get the command count
785 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
786 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000788 // Find the command character in the table of commands.
789 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (ctrl_w)
791 {
792 ca.nchar = c;
793 ca.cmdchar = Ctrl_W;
794 }
795 else
796 ca.cmdchar = c;
797 idx = find_command(ca.cmdchar);
798 if (idx < 0)
799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100800 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 clearopbeep(oap);
802 goto normal_end;
803 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000804
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000805 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000809 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 goto normal_end;
811 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000812 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
813 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000815 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (VIsual_active)
817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (km_stopsel
820 && (nv_cmds[idx].cmd_flags & NV_STS)
821 && !(mod_mask & MOD_MASK_SHIFT))
822 {
823 end_visual_mode();
824 redraw_curbuf_later(INVERTED);
825 }
826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (km_startsel)
829 {
830 if (nv_cmds[idx].cmd_flags & NV_SS)
831 {
832 unshift_special(&ca);
833 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000834 if (idx < 0)
835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000837 clearopbeep(oap);
838 goto normal_end;
839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
842 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 }
845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847#ifdef FEAT_RIGHTLEFT
848 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
849 && (nv_cmds[idx].cmd_flags & NV_RL))
850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 // Invert horizontal movements and operations. Only when typed by the
852 // user directly, not when the result of a mapping or "x" translated
853 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 switch (ca.cmdchar)
855 {
856 case 'l': ca.cmdchar = 'h'; break;
857 case K_RIGHT: ca.cmdchar = K_LEFT; break;
858 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
859 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
860 case 'h': ca.cmdchar = 'l'; break;
861 case K_LEFT: ca.cmdchar = K_RIGHT; break;
862 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
863 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
864 case '>': ca.cmdchar = '<'; break;
865 case '<': ca.cmdchar = '>'; break;
866 }
867 idx = find_command(ca.cmdchar);
868 }
869#endif
870
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000871 // Get additional characters if we need them.
872 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
873 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000876 // Flush the showcmd characters onto the screen so we can see them while
877 // the command is being executed. Only do this when the shown command was
878 // actually displayed, otherwise this will slow down a lot when executing
879 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (need_flushbuf)
881 out_flush();
882#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000883 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200884 {
885 if (ex_normal_busy)
886 did_cursorhold = save_did_cursorhold;
887 else
888 did_cursorhold = FALSE;
889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaar24959102022-05-07 20:01:16 +0100891 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 if (ca.nchar == ESC)
894 {
895 clearop(oap);
896 if (restart_edit == 0 && goto_im())
897 restart_edit = 'a';
898 goto normal_end;
899 }
900
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000901 if (ca.cmdchar != K_IGNORE)
902 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100903 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000904 msg_col = 0;
905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100907 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 // When 'keymodel' contains "startsel" some keys start Select/Visual
910 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (!VIsual_active && km_startsel)
912 {
913 if (nv_cmds[idx].cmd_flags & NV_SS)
914 {
915 start_selection();
916 unshift_special(&ca);
917 idx = find_command(ca.cmdchar);
918 }
919 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
920 && (mod_mask & MOD_MASK_SHIFT))
921 {
922 start_selection();
923 mod_mask &= ~MOD_MASK_SHIFT;
924 }
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000927 // Execute the command!
928 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 ca.arg = nv_cmds[idx].cmd_arg;
930 (nv_cmds[idx].cmd_func)(&ca);
931
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000932 // If we didn't start or finish an operator, reset oap->regname, unless we
933 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (!finish_op
935 && !oap->op_type
936 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
937 {
938 clearop(oap);
939#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200940 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#endif
942 }
943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100944 // Get the length of mapped chars again after typing a count, second
945 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000946 if (old_mapped_len > 0)
947 old_mapped_len = typebuf_maplen();
948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000949 // If an operation is pending, handle it. But not for K_IGNORE or
950 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200951 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100952 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000954 // Wait for a moment when a message is displayed that will be overwritten
955 // by the mode message.
956 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
957 normal_cmd_wait_for_msg();
958
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000959 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960normal_end:
961
962 msg_nowait = FALSE;
963
Bram Moolenaarcc613032020-06-07 21:31:18 +0200964#ifdef FEAT_EVAL
965 if (finish_op)
966 reset_reg_var();
967#endif
968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#ifdef CURSOR_SHAPE
971 c = finish_op;
972#endif
973 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100974 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Redraw the cursor with another shape, if we were in Operator-pending
977 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (c || ca.cmdchar == 'r')
979 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100980 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# ifdef FEAT_MOUSESHAPE
982 update_mouseshape(-1);
983# endif
984 }
985#endif
986
987#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000988 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100989 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 clear_showcmd();
991#endif
992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100993 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 vim_free(ca.searchbuf);
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (has_mbyte)
997 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (curwin->w_p_scb && toplevel)
1000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 do_check_scrollbind(TRUE);
1003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar860cae12010-06-05 23:22:07 +02001005 if (curwin->w_p_crb && toplevel)
1006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001007 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001008 do_check_cursorbind();
1009 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001010
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001011#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001013 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001014 restart_edit = 0;
1015#endif
1016
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001017 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1018 // if still inside a mapping that started in Visual mode).
1019 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1022 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !(ca.retval & CA_COMMAND_BUSY)
1024 && stuff_empty()
1025 && oap->regname == 0)
1026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (restart_VIsual_select == 1)
1028 {
1029 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001030 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 showmode();
1032 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001033 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001035 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 (void)edit(restart_edit, FALSE, 1L);
1037 }
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (restart_VIsual_select == 2)
1040 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 opcount = ca.opcount;
1044}
1045
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001046#ifdef FEAT_EVAL
1047/*
1048 * Set v:count and v:count1 according to "cap".
1049 * Set v:prevcount only when "set_prevcount" is TRUE.
1050 */
1051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001052set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053{
1054 long count = cap->count0;
1055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001056 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001057 if (cap->opcount != 0)
1058 count = cap->opcount * (count == 0 ? 1 : count);
1059 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001060 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001061}
1062#endif
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001065 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * if not.
1067 */
1068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001069check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
1071 static int did_check = FALSE;
1072
1073 if (full_screen)
1074 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001075 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001076 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 did_check = TRUE;
1078 }
1079}
1080
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001081#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1082/*
1083 * Call yank_do_autocmd() for "regname".
1084 */
1085 static void
1086call_yank_do_autocmd(int regname)
1087{
1088 oparg_T oa;
1089 yankreg_T *reg;
1090
1091 clear_oparg(&oa);
1092 oa.regname = regname;
1093 oa.op_type = OP_YANK;
1094 oa.is_VIsual = TRUE;
1095 reg = get_register(regname, TRUE);
1096 yank_do_autocmd(&oa, reg);
1097 free_register(reg);
1098}
1099#endif
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001102 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001103 * This function or the next should ALWAYS be called to end Visual mode, except
1104 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
1106 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001107end_visual_mode()
1108{
1109 end_visual_mode_keep_button();
1110 reset_held_button();
1111}
1112
1113 void
1114end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001117 // If we are using the clipboard, then remember what was selected in case
1118 // we need to paste it somewhere while we still own the selection.
1119 // Only do this when the clipboard is already owned. Don't want to grab
1120 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (clip_star.available && clip_star.owned)
1122 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001123
1124# if defined(FEAT_EVAL)
1125 // Emit a TextYankPost for the automatic copy of the selection into the
1126 // star and/or plus register.
1127 if (has_textyankpost())
1128 {
1129 if (clip_isautosel_star())
1130 call_yank_do_autocmd('*');
1131 if (clip_isautosel_plus())
1132 call_yank_do_autocmd('+');
1133 }
1134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif
1136
1137 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 setmouse();
1139 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001141 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001142 curbuf->b_visual.vi_mode = VIsual_mode;
1143 curbuf->b_visual.vi_start = VIsual;
1144 curbuf->b_visual.vi_end = curwin->w_cursor;
1145 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#ifdef FEAT_EVAL
1147 curbuf->b_visual_mode_eval = VIsual_mode;
1148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (!virtual_active())
1150 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001151 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001153 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001154 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155}
1156
1157/*
1158 * Reset VIsual_active and VIsual_reselect.
1159 */
1160 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001161reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
1163 if (VIsual_active)
1164 {
1165 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 VIsual_reselect = FALSE;
1169}
1170
1171/*
1172 * Reset VIsual_active and VIsual_reselect if it's set.
1173 */
1174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001175reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 if (VIsual_active)
1178 {
1179 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001180 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 VIsual_reselect = FALSE;
1182 }
1183}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001185 void
1186restore_visual_mode(void)
1187{
1188 if (VIsual_mode_orig != NUL)
1189 {
1190 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1191 VIsual_mode_orig = NUL;
1192 }
1193}
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195/*
1196 * Check for a balloon-eval special item to include when searching for an
1197 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1198 * Returns TRUE if the character at "*ptr" should be included.
1199 * "dir" is FORWARD or BACKWARD, the direction of searching.
1200 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1201 * "bnp" points to a counter for square brackets.
1202 */
1203 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001204find_is_eval_item(
1205 char_u *ptr,
1206 int *colp,
1207 int *bnp,
1208 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1212 ++*bnp;
1213 if (*bnp > 0)
1214 {
1215 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1216 --*bnp;
1217 return TRUE;
1218 }
1219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (*ptr == '.')
1222 return TRUE;
1223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001224 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1226 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1227 {
1228 *colp += dir;
1229 return TRUE;
1230 }
1231 return FALSE;
1232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234/*
1235 * Find the identifier under or to the right of the cursor.
1236 * "find_type" can have one of three values:
1237 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001238 * FIND_STRING: find any non-white text
1239 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001240 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 *
1242 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001243 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 * This doesn't match the real Vi but I like it a little better and it
1247 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001248 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 * When FIND_IDENT isn't defined, we backup until a blank.
1250 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001251 * Returns the length of the text, or zero if no text is found.
1252 * If text is found, a pointer to the text is put in "*text". This
1253 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 */
1255 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260}
1261
1262/*
1263 * Like find_ident_under_cursor(), but for any window and any position.
1264 * However: Uses 'iskeyword' from the current window!.
1265 */
1266 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001267find_ident_at_pos(
1268 win_T *wp,
1269 linenr_T lnum,
1270 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001271 char_u **text,
1272 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001273 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001276 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int this_class = 0;
1279 int prev_class;
1280 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001281 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001283 // if i == 0: try to find an identifier
1284 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1286 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1287 {
1288 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001289 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
1291 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (has_mbyte)
1293 {
1294 while (ptr[col] != NUL)
1295 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1298 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 this_class = mb_get_class(ptr + col);
1300 if (this_class != 0 && (i == 1 || this_class != 1))
1301 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001302 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 }
1305 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001307 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 )
1310 ++col;
1311
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001312 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001316 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (has_mbyte)
1319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1322 this_class = mb_get_class((char_u *)"a");
1323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001325 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
1327 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1328 prev_class = mb_get_class(ptr + prevcol);
1329 if (this_class != prev_class
1330 && (i == 0
1331 || prev_class == 0
1332 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && (!(find_type & FIND_EVAL)
1334 || prevcol == 0
1335 || !find_is_eval_item(ptr + prevcol, &prevcol,
1336 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 )
1338 break;
1339 col = prevcol;
1340 }
1341
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001342 // If we don't want just any old text, or we've found an
1343 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (this_class > 2)
1345 this_class = 2;
1346 if (!(find_type & FIND_STRING) || this_class == 2)
1347 break;
1348 }
1349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 while (col > 0
1352 && ((i == 0
1353 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001354 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 && (!(find_type & FIND_IDENT)
1356 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 || ((find_type & FIND_EVAL)
1358 && col > 1
1359 && find_is_eval_item(ptr + col - 1, &col,
1360 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 ))
1362 --col;
1363
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001364 // If we don't want just any old text, or we've found an
1365 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1367 break;
1368 }
1369 }
1370
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001371 if (ptr[col] == NUL || (i == 0
1372 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001374 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001375 if ((find_type & FIND_NOERROR) == 0)
1376 {
1377 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001378 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001379 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001380 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return 0;
1383 }
1384 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 *text = ptr;
1386 if (textcol != NULL)
1387 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001390 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 bn = 0;
1393 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (has_mbyte)
1396 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 this_class = mb_get_class(ptr);
1399 while (ptr[col] != NUL
1400 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1401 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 || ((find_type & FIND_EVAL)
1403 && col <= (int)startcol
1404 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001406 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001410 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 || ((find_type & FIND_EVAL)
1412 && col <= (int)startcol
1413 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 return col;
1418}
1419
1420/*
1421 * Prepare for redo of a normal command.
1422 */
1423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001424prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 prep_redo(cap->oap->regname, cap->count0,
1427 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1428}
1429
1430/*
1431 * Prepare for redo of any command.
1432 * Note that only the last argument can be a multi-byte char.
1433 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001435prep_redo(
1436 int regname,
1437 long num,
1438 int cmd1,
1439 int cmd2,
1440 int cmd3,
1441 int cmd4,
1442 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001444 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1445}
1446
1447/*
1448 * Prepare for redo of any command with extra count after "cmd2".
1449 */
1450 void
1451prep_redo_num2(
1452 int regname,
1453 long num1,
1454 int cmd1,
1455 int cmd2,
1456 long num2,
1457 int cmd3,
1458 int cmd4,
1459 int cmd5)
1460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001462 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 AppendCharToRedobuff('"');
1465 AppendCharToRedobuff(regname);
1466 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001467 if (num1 != 0)
1468 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (cmd1 != NUL)
1470 AppendCharToRedobuff(cmd1);
1471 if (cmd2 != NUL)
1472 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001473 if (num2 != 0)
1474 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (cmd3 != NUL)
1476 AppendCharToRedobuff(cmd3);
1477 if (cmd4 != NUL)
1478 AppendCharToRedobuff(cmd4);
1479 if (cmd5 != NUL)
1480 AppendCharToRedobuff(cmd5);
1481}
1482
1483/*
1484 * check for operator active and clear it
1485 *
1486 * return TRUE if operator was active
1487 */
1488 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001489checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 if (oap->op_type == OP_NOP)
1492 return FALSE;
1493 clearopbeep(oap);
1494 return TRUE;
1495}
1496
1497/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001498 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001500 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001505 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001511 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 oap->op_type = OP_NOP;
1515 oap->regname = 0;
1516 oap->motion_force = NUL;
1517 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001518 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519}
1520
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001522clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 clearop(oap);
1525 beep_flush();
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
1529 * Remove the shift modifier from a special key.
1530 */
1531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001532unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 switch (cap->cmdchar)
1535 {
1536 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1537 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1538 case K_S_UP: cap->cmdchar = K_UP; break;
1539 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1540 case K_S_HOME: cap->cmdchar = K_HOME; break;
1541 case K_S_END: cap->cmdchar = K_END; break;
1542 }
1543 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001546/*
1547 * If the mode is currently displayed clear the command line or update the
1548 * command displayed.
1549 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001551may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001552{
1553 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001554 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001555#ifdef FEAT_CMDL_INFO
1556 else
1557 clear_showcmd();
1558#endif
1559}
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1562/*
1563 * Routines for displaying a partly typed command
1564 */
1565
kylo252ae6f1d82022-02-16 19:24:07 +00001566#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569static int showcmd_is_clear = TRUE;
1570static int showcmd_visual = FALSE;
1571
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001572static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001575clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 if (!p_sc)
1578 return;
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (VIsual_active && !char_avail())
1581 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001582 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long lines;
1584 colnr_T leftcol, rightcol;
1585 linenr_T top, bot;
1586
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001587 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001588 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 top = VIsual.lnum;
1591 bot = curwin->w_cursor.lnum;
1592 }
1593 else
1594 {
1595 top = curwin->w_cursor.lnum;
1596 bot = VIsual.lnum;
1597 }
1598# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001599 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001600 (void)hasFolding(top, &top, NULL);
1601 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602# endif
1603 lines = bot - top + 1;
1604
1605 if (VIsual_mode == Ctrl_V)
1606 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001607# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001608 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001609 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001611 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001612 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001613 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001616# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001618 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1621 (long)(rightcol - leftcol + 1));
1622 }
1623 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1624 sprintf((char *)showcmd_buf, "%ld", lines);
1625 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001626 {
1627 char_u *s, *e;
1628 int l;
1629 int bytes = 0;
1630 int chars = 0;
1631
1632 if (cursor_bot)
1633 {
1634 s = ml_get_pos(&VIsual);
1635 e = ml_get_cursor();
1636 }
1637 else
1638 {
1639 s = ml_get_cursor();
1640 e = ml_get_pos(&VIsual);
1641 }
1642 while ((*p_sel != 'e') ? s <= e : s < e)
1643 {
1644 l = (*mb_ptr2len)(s);
1645 if (l == 0)
1646 {
1647 ++bytes;
1648 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001649 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001650 }
1651 bytes += l;
1652 ++chars;
1653 s += l;
1654 }
1655 if (bytes == chars)
1656 sprintf((char *)showcmd_buf, "%d", chars);
1657 else
1658 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1659 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 showcmd_visual = TRUE;
1662 }
1663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665 showcmd_buf[0] = NUL;
1666 showcmd_visual = FALSE;
1667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001668 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (showcmd_is_clear)
1670 return;
1671 }
1672
1673 display_showcmd();
1674}
1675
1676/*
1677 * Add 'c' to string of shown command chars.
1678 * Return TRUE if output has been written (and setcursor() has been called).
1679 */
1680 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 char_u *p;
1684 int old_len;
1685 int extra_len;
1686 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 int i;
1688 static int ignore[] =
1689 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001690#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1692 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001693#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001694 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001695 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1697 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001698 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001700 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 0
1702 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar09df3122006-01-23 22:23:09 +00001704 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 return FALSE;
1706
1707 if (showcmd_visual)
1708 {
1709 showcmd_buf[0] = NUL;
1710 showcmd_visual = FALSE;
1711 }
1712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001713 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (IS_SPECIAL(c))
1715 for (i = 0; ignore[i] != 0; ++i)
1716 if (ignore[i] == c)
1717 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001720 if (*p == ' ')
1721 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 old_len = (int)STRLEN(showcmd_buf);
1723 extra_len = (int)STRLEN(p);
1724 overflow = old_len + extra_len - SHOWCMD_COLS;
1725 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001726 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1727 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 STRCAT(showcmd_buf, p);
1729
1730 if (char_avail())
1731 return FALSE;
1732
1733 display_showcmd();
1734
1735 return TRUE;
1736}
1737
1738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741 if (!add_to_showcmd(c))
1742 setcursor();
1743}
1744
1745/*
1746 * Delete 'len' characters from the end of the shown command.
1747 */
1748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 int old_len;
1752
1753 if (!p_sc)
1754 return;
1755
1756 old_len = (int)STRLEN(showcmd_buf);
1757 if (len > old_len)
1758 len = old_len;
1759 showcmd_buf[old_len - len] = NUL;
1760
1761 if (!char_avail())
1762 display_showcmd();
1763}
1764
1765/*
1766 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1767 * something and there is a partial mapping.
1768 */
1769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001770push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 if (p_sc)
1773 STRCPY(old_showcmd_buf, showcmd_buf);
1774}
1775
1776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001777pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 if (!p_sc)
1780 return;
1781
1782 STRCPY(showcmd_buf, old_showcmd_buf);
1783
1784 display_showcmd();
1785}
1786
1787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 int len;
1791
1792 cursor_off();
1793
1794 len = (int)STRLEN(showcmd_buf);
1795 if (len == 0)
1796 showcmd_is_clear = TRUE;
1797 else
1798 {
1799 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1800 showcmd_is_clear = FALSE;
1801 }
1802
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001803 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1804 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001807 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808}
1809#endif
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
1812 * When "check" is FALSE, prepare for commands that scroll the window.
1813 * When "check" is TRUE, take care of scroll-binding after the window has
1814 * scrolled. Called from normal_cmd() and edit().
1815 */
1816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001817do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 static win_T *old_curwin = NULL;
1820 static linenr_T old_topline = 0;
1821#ifdef FEAT_DIFF
1822 static int old_topfill = 0;
1823#endif
1824 static buf_T *old_buf = NULL;
1825 static colnr_T old_leftcol = 0;
1826
1827 if (check && curwin->w_p_scb)
1828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001829 // If a ":syncbind" command was just used, don't scroll, only reset
1830 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (did_syncbind)
1832 did_syncbind = FALSE;
1833 else if (curwin == old_curwin)
1834 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001835 // Synchronize other windows, as necessary according to
1836 // 'scrollbind'. Don't do this after an ":edit" command, except
1837 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if ((curwin->w_buffer == old_buf
1839#ifdef FEAT_DIFF
1840 || curwin->w_p_diff
1841#endif
1842 )
1843 && (curwin->w_topline != old_topline
1844#ifdef FEAT_DIFF
1845 || curwin->w_topfill != old_topfill
1846#endif
1847 || curwin->w_leftcol != old_leftcol))
1848 {
1849 check_scrollbind(curwin->w_topline - old_topline,
1850 (long)(curwin->w_leftcol - old_leftcol));
1851 }
1852 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001853 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001855 // When switching between windows, make sure that the relative
1856 // vertical offset is valid for the new window. The relative
1857 // offset is invalid whenever another 'scrollbind' window has
1858 // scrolled to a point that would force the current window to
1859 // scroll past the beginning or end of its buffer. When the
1860 // resync is performed, some of the other 'scrollbind' windows may
1861 // need to jump so that the current window's relative position is
1862 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1864 }
1865 curwin->w_scbind_pos = curwin->w_topline;
1866 }
1867
1868 old_curwin = curwin;
1869 old_topline = curwin->w_topline;
1870#ifdef FEAT_DIFF
1871 old_topfill = curwin->w_topfill;
1872#endif
1873 old_buf = curwin->w_buffer;
1874 old_leftcol = curwin->w_leftcol;
1875}
1876
1877/*
1878 * Synchronize any windows that have "scrollbind" set, based on the
1879 * number of rows by which the current window has changed
1880 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1881 */
1882 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001883check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
1885 int want_ver;
1886 int want_hor;
1887 win_T *old_curwin = curwin;
1888 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 int old_VIsual_select = VIsual_select;
1890 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 colnr_T tgt_leftcol = curwin->w_leftcol;
1892 long topline;
1893 long y;
1894
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001895 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1897#ifdef FEAT_DIFF
1898 want_ver |= old_curwin->w_p_diff;
1899#endif
1900 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1901
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001902 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001904 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001907 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (curwin != old_curwin && curwin->w_p_scb)
1909 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001910 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (want_ver)
1912 {
1913#ifdef FEAT_DIFF
1914 if (old_curwin->w_p_diff && curwin->w_p_diff)
1915 {
1916 diff_set_topline(old_curwin, curwin);
1917 }
1918 else
1919#endif
1920 {
1921 curwin->w_scbind_pos += topline_diff;
1922 topline = curwin->w_scbind_pos;
1923 if (topline > curbuf->b_ml.ml_line_count)
1924 topline = curbuf->b_ml.ml_line_count;
1925 if (topline < 1)
1926 topline = 1;
1927
1928 y = topline - curwin->w_topline;
1929 if (y > 0)
1930 scrollup(y, FALSE);
1931 else
1932 scrolldown(-y, FALSE);
1933 }
1934
1935 redraw_later(VALID);
1936 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001940 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1942 {
1943 curwin->w_leftcol = tgt_leftcol;
1944 leftcol_changed();
1945 }
1946 }
1947 }
1948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001949 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 VIsual_select = old_VIsual_select;
1951 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 curwin = old_curwin;
1953 curbuf = old_curbuf;
1954}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956/*
1957 * Command character that's ignored.
1958 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001959 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001962nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965}
1966
1967/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001968 * Command character that doesn't do anything, but unlike nv_ignore() does
1969 * start edit(). Used for "startinsert" executed while starting up.
1970 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001972nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001973{
1974}
1975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Command character doesn't exist.
1978 */
1979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 clearopbeep(cap->oap);
1983}
1984
1985/*
1986 * <Help> and <F1> commands.
1987 */
1988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001989nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 if (!checkclearopq(cap->oap))
1992 ex_help(NULL);
1993}
1994
1995/*
1996 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
1997 */
1998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002001#ifdef FEAT_JOB_CHANNEL
2002 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2003 clearopbeep(cap->oap);
2004 else
2005#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002006 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002007 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002008 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002009 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2010 op_addsub(cap->oap, cap->count1, cap->arg);
2011 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002012 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002013 else if (VIsual_active)
2014 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002015 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002016 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017}
2018
2019/*
2020 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2021 */
2022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002023nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002026 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002027 if (mod_mask & MOD_MASK_CTRL)
2028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002030 if (cap->arg == BACKWARD)
2031 goto_tabpage(-(int)cap->count1);
2032 else
2033 goto_tabpage((int)cap->count0);
2034 }
2035 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002036 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038}
2039
2040/*
2041 * Implementation of "gd" and "gD" command.
2042 */
2043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044nv_gd(
2045 oparg_T *oap,
2046 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002047 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 char_u *ptr;
2051
Bram Moolenaard9d30582005-05-18 22:10:28 +00002052 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002053 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002054 == FAIL)
2055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002057 }
2058 else
2059 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002060#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002061 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2062 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002063#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002064 // clear any search statistics
2065 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2066 clear_cmdline = TRUE;
2067 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002068}
2069
2070/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002071 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2072 * otherwise.
2073 */
2074 static int
2075is_ident(char_u *line, int offset)
2076{
2077 int i;
2078 int incomment = FALSE;
2079 int instring = 0;
2080 int prev = 0;
2081
2082 for (i = 0; i < offset && line[i] != NUL; i++)
2083 {
2084 if (instring != 0)
2085 {
2086 if (prev != '\\' && line[i] == instring)
2087 instring = 0;
2088 }
2089 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2090 {
2091 instring = line[i];
2092 }
2093 else
2094 {
2095 if (incomment)
2096 {
2097 if (prev == '*' && line[i] == '/')
2098 incomment = FALSE;
2099 }
2100 else if (prev == '/' && line[i] == '*')
2101 {
2102 incomment = TRUE;
2103 }
2104 else if (prev == '/' && line[i] == '/')
2105 {
2106 return FALSE;
2107 }
2108 }
2109
2110 prev = line[i];
2111 }
2112
2113 return incomment == FALSE && instring == 0;
2114}
2115
2116/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002117 * Search for variable declaration of "ptr[len]".
2118 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2119 * current file ("gD").
2120 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002121 * Return FAIL when not found.
2122 */
2123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124find_decl(
2125 char_u *ptr,
2126 int len,
2127 int locally,
2128 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002129 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002130{
2131 char_u *pat;
2132 pos_T old_pos;
2133 pos_T par_pos;
2134 pos_T found_pos;
2135 int t;
2136 int save_p_ws;
2137 int save_p_scs;
2138 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002139 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002140 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002141 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002142
2143 if ((pat = alloc(len + 7)) == NULL)
2144 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002146 // Put "\V" before the pattern to avoid that the special meaning of "."
2147 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002148 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2149 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 old_pos = curwin->w_cursor;
2151 save_p_ws = p_ws;
2152 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002153 p_ws = FALSE; // don't wrap around end of file now
2154 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002156 // With "gD" go to line 1.
2157 // With "gd" Search back for the start of the current function, then go
2158 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002159 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002161 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002163 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 else
2166 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002167 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2169 --curwin->w_cursor.lnum;
2170 }
2171 curwin->w_cursor.col = 0;
2172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002173 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002174 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002175 for (;;)
2176 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002177 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002178 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002179 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002181
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002182 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002183 {
2184 pos_T *pos;
2185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002186 // Check that the block the match is in doesn't end before the
2187 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002188 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2189 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2190 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 // There can't be a useful match before the end of this block.
2193 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002194 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002196 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002197 }
2198
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002199 if (t == FAIL)
2200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002202 if (found_pos.lnum != 0)
2203 {
2204 curwin->w_cursor = found_pos;
2205 t = OK;
2206 }
2207 break;
2208 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002209 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002211 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002212 ++curwin->w_cursor.lnum;
2213 curwin->w_cursor.col = 0;
2214 continue;
2215 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002216 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // If the current position is not a valid identifier and a previous
2219 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002220 if (!valid && found_pos.lnum != 0)
2221 {
2222 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002224 }
2225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002226 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 if (valid && !locally)
2228 break;
2229 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002232 if (found_pos.lnum != 0)
2233 curwin->w_cursor = found_pos;
2234 break;
2235 }
2236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // For finding a local variable and the match is before the "{" or
2238 // inside a comment, continue searching. For K&R style function
2239 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002240 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002241 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002242 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002243 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // Remove SEARCH_START from flags to avoid getting stuck at one
2245 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002246 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002248
2249 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002251 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 curwin->w_cursor = old_pos;
2253 }
2254 else
2255 {
2256 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002257 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 reset_search_dir();
2259 }
2260
2261 vim_free(pat);
2262 p_ws = save_p_ws;
2263 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002264
2265 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266}
2267
2268/*
2269 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2270 * lines rather than lines in the file.
2271 * 'dist' must be positive.
2272 *
2273 * Return OK if able to move cursor, FAIL otherwise.
2274 */
2275 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002276nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277{
2278 int linelen = linetabsize(ml_get_curline());
2279 int retval = OK;
2280 int atend = FALSE;
2281 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002282 int col_off1; // margin offset for first screen line
2283 int col_off2; // margin offset for wrapped screen line
2284 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002285 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002288 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 col_off1 = curwin_col_off();
2291 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002292 width1 = curwin->w_width - col_off1;
2293 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002294 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002295 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002298 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002299 // Instead of sticking at the last character of the buffer line we
2300 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (curwin->w_curswant == MAXCOL)
2302 {
2303 atend = TRUE;
2304 validate_virtcol();
2305 if (width1 <= 0)
2306 curwin->w_curswant = 0;
2307 else
2308 {
2309 curwin->w_curswant = width1 - 1;
2310 if (curwin->w_virtcol > curwin->w_curswant)
2311 curwin->w_curswant += ((curwin->w_virtcol
2312 - curwin->w_curswant - 1) / width2 + 1) * width2;
2313 }
2314 }
2315 else
2316 {
2317 if (linelen > width1)
2318 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2319 else
2320 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002321 if (curwin->w_curswant >= (colnr_T)n)
2322 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
2325 while (dist--)
2326 {
2327 if (dir == BACKWARD)
2328 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002329 if ((long)curwin->w_curswant >= width1
2330#ifdef FEAT_FOLDING
2331 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2332#endif
2333 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002334 // Move back within the line. This can give a negative value
2335 // for w_curswant if width1 < width2 (with cpoptions+=n),
2336 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 curwin->w_curswant -= width2;
2338 else
2339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 // Move to the start of a closed fold. Don't do that when
2343 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (!(fdo_flags & FDO_ALL))
2345 (void)hasFolding(curwin->w_cursor.lnum,
2346 &curwin->w_cursor.lnum, NULL);
2347#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002348 if (curwin->w_cursor.lnum == 1)
2349 {
2350 retval = FAIL;
2351 break;
2352 }
2353 --curwin->w_cursor.lnum;
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 linelen = linetabsize(ml_get_curline());
2356 if (linelen > width1)
2357 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2358 + 1) * width2;
2359 }
2360 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002361 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 if (linelen > width1)
2364 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2365 else
2366 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002367 if (curwin->w_curswant + width2 < (colnr_T)n
2368#ifdef FEAT_FOLDING
2369 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2370#endif
2371 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 curwin->w_curswant += width2;
2374 else
2375 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002376 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002378 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2380 &curwin->w_cursor.lnum);
2381#endif
2382 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2383 {
2384 retval = FAIL;
2385 break;
2386 }
2387 curwin->w_cursor.lnum++;
2388 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002389 // Check if the cursor has moved below the number display
2390 // when width1 < width2 (with cpoptions+=n). Subtract width2
2391 // to get a negative value for w_curswant, which will get
2392 // clipped to column 0.
2393 if (curwin->w_curswant >= width1)
2394 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002395 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 }
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002401 if (virtual_active() && atend)
2402 coladvance(MAXCOL);
2403 else
2404 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2407 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002408 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002409 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002410
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002411 // Check for landing on a character that got split at the end of the
2412 // last line. We want to advance a screenline, not end up in the same
2413 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002415 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002416#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002417 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2418 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002419#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002420
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002421 c = (*mb_ptr2char)(ml_get_cursor());
2422 if (dir == FORWARD && virtcol < curwin->w_curswant
2423 && (curwin->w_curswant <= (colnr_T)width1)
2424 && !vim_isprintc(c) && c > 255)
2425 oneright();
2426
Bram Moolenaar773b1582014-08-29 14:20:51 +02002427 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 && (curwin->w_curswant < (colnr_T)width1
2429 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2430 : ((curwin->w_curswant - width1) % width2
2431 > (colnr_T)width2 / 2)))
2432 --curwin->w_cursor.col;
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 return retval;
2439}
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441/*
2442 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2443 * cap->arg must be TRUE for CTRL-E.
2444 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002446nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 if (!checkclearop(cap->oap))
2449 scroll_redraw(cap->arg, cap->count1);
2450}
2451
2452/*
2453 * Scroll "count" lines up or down, and redraw.
2454 */
2455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 linenr_T prev_topline = curwin->w_topline;
2459#ifdef FEAT_DIFF
2460 int prev_topfill = curwin->w_topfill;
2461#endif
2462 linenr_T prev_lnum = curwin->w_cursor.lnum;
2463
2464 if (up)
2465 scrollup(count, TRUE);
2466 else
2467 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002468 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002470 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2471 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 cursor_correct();
2473 check_cursor_moved(curwin);
2474 curwin->w_valid |= VALID_TOPLINE;
2475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002476 // If moved back to where we were, at least move the cursor, otherwise
2477 // we get stuck at one position. Don't move the cursor up if the
2478 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 while (curwin->w_topline == prev_topline
2480#ifdef FEAT_DIFF
2481 && curwin->w_topfill == prev_topfill
2482#endif
2483 )
2484 {
2485 if (up)
2486 {
2487 if (curwin->w_cursor.lnum > prev_lnum
2488 || cursor_down(1L, FALSE) == FAIL)
2489 break;
2490 }
2491 else
2492 {
2493 if (curwin->w_cursor.lnum < prev_lnum
2494 || prev_topline == 1L
2495 || cursor_up(1L, FALSE) == FAIL)
2496 break;
2497 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002498 // Mark w_topline as valid, otherwise the screen jumps back at the
2499 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 check_cursor_moved(curwin);
2501 curwin->w_valid |= VALID_TOPLINE;
2502 }
2503 }
2504 if (curwin->w_cursor.lnum != prev_lnum)
2505 coladvance(curwin->w_curswant);
2506 redraw_later(VALID);
2507}
2508
2509/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002510 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2511 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2512 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002513 */
2514 static int
2515nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2516{
2517 int nchar = *nchar_arg;
2518 long n;
2519
2520 // "z123{nchar}": edit the count before obtaining {nchar}
2521 if (checkclearop(cap->oap))
2522 return FALSE;
2523 n = nchar - '0';
2524
2525 for (;;)
2526 {
2527#ifdef USE_ON_FLY_SCROLL
2528 dont_scroll = TRUE; // disallow scrolling here
2529#endif
2530 ++no_mapping;
2531 ++allow_keys; // no mapping for nchar, but allow key codes
2532 nchar = plain_vgetc();
2533 LANGMAP_ADJUST(nchar, TRUE);
2534 --no_mapping;
2535 --allow_keys;
2536#ifdef FEAT_CMDL_INFO
2537 (void)add_to_showcmd(nchar);
2538#endif
2539 if (nchar == K_DEL || nchar == K_KDEL)
2540 n /= 10;
2541 else if (VIM_ISDIGIT(nchar))
2542 n = n * 10 + (nchar - '0');
2543 else if (nchar == CAR)
2544 {
2545#ifdef FEAT_GUI
2546 need_mouse_correct = TRUE;
2547#endif
2548 win_setheight((int)n);
2549 break;
2550 }
2551 else if (nchar == 'l'
2552 || nchar == 'h'
2553 || nchar == K_LEFT
2554 || nchar == K_RIGHT)
2555 {
2556 cap->count1 = n ? n * cap->count1 : cap->count1;
2557 *nchar_arg = nchar;
2558 return TRUE;
2559 }
2560 else
2561 {
2562 clearopbeep(cap->oap);
2563 break;
2564 }
2565 }
2566 cap->oap->op_type = OP_NOP;
2567 return FALSE;
2568}
2569
2570#ifdef FEAT_SPELL
2571/*
2572 * "zug" and "zuw": undo "zg" and "zw"
2573 * "zg": add good word to word list
2574 * "zw": add wrong word to word list
2575 * "zG": add good word to temp word list
2576 * "zW": add wrong word to temp word list
2577 */
2578 static int
2579nv_zg_zw(cmdarg_T *cap, int nchar)
2580{
2581 char_u *ptr = NULL;
2582 int len;
2583 int undo = FALSE;
2584
2585 if (nchar == 'u')
2586 {
2587 ++no_mapping;
2588 ++allow_keys; // no mapping for nchar, but allow key codes
2589 nchar = plain_vgetc();
2590 LANGMAP_ADJUST(nchar, TRUE);
2591 --no_mapping;
2592 --allow_keys;
2593#ifdef FEAT_CMDL_INFO
2594 (void)add_to_showcmd(nchar);
2595#endif
2596 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2597 {
2598 clearopbeep(cap->oap);
2599 return OK;
2600 }
2601 undo = TRUE;
2602 }
2603
2604 if (checkclearop(cap->oap))
2605 return OK;
2606 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2607 return FAIL;
2608 if (ptr == NULL)
2609 {
2610 pos_T pos = curwin->w_cursor;
2611
2612 // Find bad word under the cursor. When 'spell' is
2613 // off this fails and find_ident_under_cursor() is
2614 // used below.
2615 emsg_off++;
2616 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2617 emsg_off--;
2618 if (len != 0 && curwin->w_cursor.col <= pos.col)
2619 ptr = ml_get_pos(&curwin->w_cursor);
2620 curwin->w_cursor = pos;
2621 }
2622
2623 if (ptr == NULL
2624 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2625 return FAIL;
2626 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2627 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2628 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2629
2630 return OK;
2631}
2632#endif
2633
2634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * Commands that start with "z".
2636 */
2637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002638nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 long n;
2641 colnr_T col;
2642 int nchar = cap->nchar;
2643#ifdef FEAT_FOLDING
2644 long old_fdl = curwin->w_p_fdl;
2645 int old_fen = curwin->w_p_fen;
2646#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002647 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002649 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (
2653#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002654 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2655 // and "zC" only in Visual mode. "zj" and "zk" are motion
2656 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 cap->nchar != 'f' && cap->nchar != 'F'
2658 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2659 && cap->nchar != 'j' && cap->nchar != 'k'
2660 &&
2661#endif
2662 checkclearop(cap->oap))
2663 return;
2664
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002665 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2666 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2668 && cap->count0
2669 && cap->count0 != curwin->w_cursor.lnum)
2670 {
2671 setpcmark();
2672 if (cap->count0 > curbuf->b_ml.ml_line_count)
2673 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2674 else
2675 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002676 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 switch (nchar)
2680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002681 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 case '+':
2683 if (cap->count0 == 0)
2684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002685 // No count given: put cursor at the line below screen
2686 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2688 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2689 else
2690 curwin->w_cursor.lnum = curwin->w_botline;
2691 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 case NL:
2694 case CAR:
2695 case K_KENTER:
2696 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 case 't': scroll_cursor_top(0, TRUE);
2700 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002701 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 break;
2703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 case 'z': scroll_cursor_halfway(TRUE);
2709 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002710 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // "z^", "z-" and "zb": put cursor at bottom of screen
2714 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2715 // when <count> is at bottom of window, and puts that one at
2716 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (cap->count0 != 0)
2718 {
2719 scroll_cursor_bot(0, TRUE);
2720 curwin->w_cursor.lnum = curwin->w_topline;
2721 }
2722 else if (curwin->w_topline == 1)
2723 curwin->w_cursor.lnum = 1;
2724 else
2725 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 case '-':
2728 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 case 'b': scroll_cursor_bot(0, TRUE);
2732 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002733 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 break;
2735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002738 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 case 'h':
2743 case K_LEFT:
2744 if (!curwin->w_p_wrap)
2745 {
2746 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2747 curwin->w_leftcol = 0;
2748 else
2749 curwin->w_leftcol -= (colnr_T)cap->count1;
2750 leftcol_changed();
2751 }
2752 break;
2753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002755 case 'L': 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 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 case 'l':
2760 case K_RIGHT:
2761 if (!curwin->w_p_wrap)
2762 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 curwin->w_leftcol += (colnr_T)cap->count1;
2765 leftcol_changed();
2766 }
2767 break;
2768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case 's': if (!curwin->w_p_wrap)
2771 {
2772#ifdef FEAT_FOLDING
2773 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
2776#endif
2777 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002778 if ((long)col > siso)
2779 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781 col = 0;
2782 if (curwin->w_leftcol != col)
2783 {
2784 curwin->w_leftcol = col;
2785 redraw_later(NOT_VALID);
2786 }
2787 }
2788 break;
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 case 'e': if (!curwin->w_p_wrap)
2792 {
2793#ifdef FEAT_FOLDING
2794 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else
2797#endif
2798 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002800 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 col = 0;
2802 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002803 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (curwin->w_leftcol != col)
2805 {
2806 curwin->w_leftcol = col;
2807 redraw_later(NOT_VALID);
2808 }
2809 }
2810 break;
2811
Christian Brabandt2fa93842021-05-30 22:17:25 +02002812 // "zp", "zP" in block mode put without addind trailing spaces
2813 case 'P':
2814 case 'p': nv_put(cap);
2815 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002816 // "zy" Yank without trailing spaces
2817 case 'y': nv_operator(cap);
2818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // "zF": create fold command
2821 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case 'F':
2823 case 'f': if (foldManualAllowed(TRUE))
2824 {
2825 cap->nchar = 'f';
2826 nv_operator(cap);
2827 curwin->w_p_fen = TRUE;
2828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2831 {
2832 nv_operator(cap);
2833 finish_op = TRUE;
2834 }
2835 }
2836 else
2837 clearopbeep(cap->oap);
2838 break;
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // "zd": delete fold at cursor
2841 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case 'd':
2843 case 'D': if (foldManualAllowed(FALSE))
2844 {
2845 if (VIsual_active)
2846 nv_operator(cap);
2847 else
2848 deleteFold(curwin->w_cursor.lnum,
2849 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2850 }
2851 break;
2852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case 'E': if (foldmethodIsManual(curwin))
2855 {
2856 clearFolding(curwin);
2857 changed_window_setting();
2858 }
2859 else if (foldmethodIsMarker(curwin))
2860 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2861 TRUE, FALSE);
2862 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002863 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case 'n': curwin->w_p_fen = FALSE;
2868 break;
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case 'N': curwin->w_p_fen = TRUE;
2872 break;
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2876 break;
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2880 openFold(curwin->w_cursor.lnum, cap->count1);
2881 else
2882 {
2883 closeFold(curwin->w_cursor.lnum, cap->count1);
2884 curwin->w_p_fen = TRUE;
2885 }
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2890 openFoldRecurse(curwin->w_cursor.lnum);
2891 else
2892 {
2893 closeFoldRecurse(curwin->w_cursor.lnum);
2894 curwin->w_p_fen = TRUE;
2895 }
2896 break;
2897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 case 'o': if (VIsual_active)
2900 nv_operator(cap);
2901 else
2902 openFold(curwin->w_cursor.lnum, cap->count1);
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'O': if (VIsual_active)
2907 nv_operator(cap);
2908 else
2909 openFoldRecurse(curwin->w_cursor.lnum);
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'c': if (VIsual_active)
2914 nv_operator(cap);
2915 else
2916 closeFold(curwin->w_cursor.lnum, cap->count1);
2917 curwin->w_p_fen = TRUE;
2918 break;
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 case 'C': if (VIsual_active)
2922 nv_operator(cap);
2923 else
2924 closeFoldRecurse(curwin->w_cursor.lnum);
2925 curwin->w_p_fen = TRUE;
2926 break;
2927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 case 'v': foldOpenCursor();
2930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 curwin->w_foldinvalid = TRUE; // recompute folds
2935 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 foldOpenCursor();
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 curwin->w_foldinvalid = TRUE; // recompute folds
2942 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002947 {
2948 curwin->w_p_fdl -= cap->count1;
2949 if (curwin->w_p_fdl < 0)
2950 curwin->w_p_fdl = 0;
2951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 curwin->w_p_fen = TRUE;
2954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 curwin->w_p_fen = TRUE;
2960 break;
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002963 case 'r': curwin->w_p_fdl += cap->count1;
2964 {
2965 int d = getDeepestNesting();
2966
2967 if (curwin->w_p_fdl >= d)
2968 curwin->w_p_fdl = d;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 break;
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 case 'j': // "zj" move to next fold downwards
2978 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2980 cap->count1) == FAIL)
2981 clearopbeep(cap->oap);
2982 break;
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002986#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case 'g': // "zg": add good word to word list
2989 case 'w': // "zw": add wrong word to word list
2990 case 'G': // "zG": add good word to temp word list
2991 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002992 if (nv_zg_zw(cap, nchar) == FAIL)
2993 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002994 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002997 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002998 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 default: clearopbeep(cap->oap);
3003 }
3004
3005#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (old_fen != curwin->w_p_fen)
3008 {
3009# ifdef FEAT_DIFF
3010 win_T *wp;
3011
3012 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003014 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 FOR_ALL_WINDOWS(wp)
3016 {
3017 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3018 {
3019 wp->w_p_fen = curwin->w_p_fen;
3020 changed_window_setting_win(wp);
3021 }
3022 }
3023 }
3024# endif
3025 changed_window_setting();
3026 }
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (old_fdl != curwin->w_p_fdl)
3030 newFoldLevel();
3031#endif
3032}
3033
3034#ifdef FEAT_GUI
3035/*
3036 * Vertical scrollbar movement.
3037 */
3038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003039nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 if (cap->oap->op_type != OP_NOP)
3042 clearopbeep(cap->oap);
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 gui_do_scroll();
3046}
3047
3048/*
3049 * Horizontal scrollbar movement.
3050 */
3051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 if (cap->oap->op_type != OP_NOP)
3055 clearopbeep(cap->oap);
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003058 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059}
3060#endif
3061
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003062#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003063/*
3064 * Click in GUI tab.
3065 */
3066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003068{
3069 if (cap->oap->op_type != OP_NOP)
3070 clearopbeep(cap->oap);
3071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073 goto_tabpage(current_tab);
3074}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003075
3076/*
3077 * Selected item in tab line menu.
3078 */
3079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003081{
3082 if (cap->oap->op_type != OP_NOP)
3083 clearopbeep(cap->oap);
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003086 handle_tabmenu();
3087}
3088
3089/*
3090 * Handle selecting an item of the GUI tab line menu.
3091 * Used in Normal and Insert mode.
3092 */
3093 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003094handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003095{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003096 switch (current_tabmenu)
3097 {
3098 case TABLINE_MENU_CLOSE:
3099 if (current_tab == 0)
3100 do_cmdline_cmd((char_u *)"tabclose");
3101 else
3102 {
3103 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3104 current_tab);
3105 do_cmdline_cmd(IObuff);
3106 }
3107 break;
3108
3109 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003110 if (current_tab == 0)
3111 do_cmdline_cmd((char_u *)"$tabnew");
3112 else
3113 {
3114 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3115 current_tab - 1);
3116 do_cmdline_cmd(IObuff);
3117 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003118 break;
3119
3120 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003121 if (current_tab == 0)
3122 do_cmdline_cmd((char_u *)"browse $tabnew");
3123 else
3124 {
3125 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3126 current_tab - 1);
3127 do_cmdline_cmd(IObuff);
3128 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003129 break;
3130 }
3131}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003132#endif
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * "Q" command.
3136 */
3137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003138nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003140 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003142 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003143 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 do_exmode(FALSE);
3145}
3146
3147/*
3148 * Handle a ":" command.
3149 */
3150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003151nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003153 int old_p_im;
3154 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003155 int is_cmdkey = cap->cmdchar == K_COMMAND
3156 || cap->cmdchar == K_SCRIPT_COMMAND;
3157 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar957cf672020-11-12 14:21:06 +01003159 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 nv_operator(cap);
3161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 if (cap->oap->op_type != OP_NOP)
3164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 cap->oap->motion_type = MCHAR;
3167 cap->oap->inclusive = FALSE;
3168 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003169 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 stuffcharReadbuff('.');
3173 if (cap->count0 > 1)
3174 {
3175 stuffReadbuff((char_u *)",.+");
3176 stuffnumReadbuff((long)cap->count0 - 1L);
3177 }
3178 }
3179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (KeyTyped)
3182 compute_cmdrow();
3183
3184 old_p_im = p_im;
3185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003186 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003187 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3188 if (is_cmdkey)
3189 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3190 else
3191 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (p_im != old_p_im)
3195 {
3196 if (p_im)
3197 restart_edit = 'i';
3198 else
3199 restart_edit = 0;
3200 }
3201
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003202 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003204 clearop(cap->oap);
3205 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3207 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003208 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3209 || did_emsg
3210 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 clearopbeep(cap->oap);
3213 }
3214}
3215
3216/*
3217 * Handle CTRL-G command.
3218 */
3219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003220nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003225 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 showmode();
3227 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003228 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 fileinfo((int)cap->count0, FALSE, TRUE);
3231}
3232
3233/*
3234 * Handle CTRL-H <Backspace> command.
3235 */
3236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003237nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (VIsual_active && VIsual_select)
3240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003241 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 v_visop(cap);
3243 }
3244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 nv_left(cap);
3246}
3247
3248/*
3249 * CTRL-L: clear screen and redraw.
3250 */
3251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003252nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 if (!checkclearop(cap->oap))
3255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003258 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003259# ifdef FEAT_RELTIME
3260 {
3261 win_T *wp;
3262
3263 FOR_ALL_WINDOWS(wp)
3264 wp->w_s->b_syn_slow = FALSE;
3265 }
3266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
3268 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003269#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3270# ifdef VIMDLL
3271 if (!gui.in_use)
3272# endif
3273 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276}
3277
3278/*
3279 * CTRL-O: In Select mode: switch to Visual mode for one command.
3280 * Otherwise: Go to older pcmark.
3281 */
3282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (VIsual_active && VIsual_select)
3286 {
3287 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003288 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 cap->count1 = -cap->count1;
3295 nv_pcmark(cap);
3296 }
3297}
3298
3299/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003300 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3301 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
3303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 if (!checkclearopq(cap->oap))
3307 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3308 GETF_SETMARK|GETF_ALT, FALSE);
3309}
3310
3311/*
3312 * "Z" commands.
3313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (!checkclearopq(cap->oap))
3318 {
3319 switch (cap->nchar)
3320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 case 'Z': do_cmdline_cmd((char_u *)"x");
3323 break;
3324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 case 'Q': do_cmdline_cmd((char_u *)"q!");
3327 break;
3328
3329 default: clearopbeep(cap->oap);
3330 }
3331 }
3332}
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3336 */
3337 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003338do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 oparg_T oa;
3341 cmdarg_T ca;
3342
3343 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003344 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 ca.oap = &oa;
3346 ca.cmdchar = c1;
3347 ca.nchar = c2;
3348 nv_ident(&ca);
3349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003352 * 'K' normal-mode command. Get the command to lookup the keyword under the
3353 * cursor.
3354 */
3355 static int
3356nv_K_getcmd(
3357 cmdarg_T *cap,
3358 char_u *kp,
3359 int kp_help,
3360 int kp_ex,
3361 char_u **ptr_arg,
3362 int n,
3363 char_u *buf,
3364 unsigned buflen)
3365{
3366 char_u *ptr = *ptr_arg;
3367 int isman;
3368 int isman_s;
3369
3370 if (kp_help)
3371 {
3372 // in the help buffer
3373 STRCPY(buf, "he! ");
3374 return n;
3375 }
3376
3377 if (kp_ex)
3378 {
3379 // 'keywordprog' is an ex command
3380 if (cap->count0 != 0)
3381 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3382 else
3383 STRCPY(buf, kp);
3384 STRCAT(buf, " ");
3385 return n;
3386 }
3387
3388 // An external command will probably use an argument starting
3389 // with "-" as an option. To avoid trouble we skip the "-".
3390 while (*ptr == '-' && n > 0)
3391 {
3392 ++ptr;
3393 --n;
3394 }
3395 if (n == 0)
3396 {
3397 // found dashes only
3398 emsg(_(e_no_identifier_under_cursor));
3399 vim_free(buf);
3400 *ptr_arg = ptr;
3401 return 0;
3402 }
3403
3404 // When a count is given, turn it into a range. Is this
3405 // really what we want?
3406 isman = (STRCMP(kp, "man") == 0);
3407 isman_s = (STRCMP(kp, "man -s") == 0);
3408 if (cap->count0 != 0 && !(isman || isman_s))
3409 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3410
3411 STRCAT(buf, "! ");
3412 if (cap->count0 == 0 && isman_s)
3413 STRCAT(buf, "man");
3414 else
3415 STRCAT(buf, kp);
3416 STRCAT(buf, " ");
3417 if (cap->count0 != 0 && (isman || isman_s))
3418 {
3419 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3420 STRCAT(buf, " ");
3421 }
3422
3423 *ptr_arg = ptr;
3424 return n;
3425}
3426
3427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 * Handle the commands that use the word under the cursor.
3429 * [g] CTRL-] :ta to current identifier
3430 * [g] 'K' run program for current identifier
3431 * [g] '*' / to current identifier or string
3432 * [g] '#' ? to current identifier or string
3433 * g ']' :tselect for current identifier
3434 */
3435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003436nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 char_u *ptr = NULL;
3439 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003440 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003441 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 char_u *kp; // value of 'keywordprg'
3444 int kp_help; // 'keywordprg' is ":he"
3445 int kp_ex; // 'keywordprg' starts with ":"
3446 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003449 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 cmdchar = cap->nchar;
3455 g_cmd = TRUE;
3456 }
3457 else
3458 {
3459 cmdchar = cap->cmdchar;
3460 g_cmd = FALSE;
3461 }
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 cmdchar = '#';
3465
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003466 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (checkclearopq(cap->oap))
3472 return;
3473 }
3474
3475 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3476 (cmdchar == '*' || cmdchar == '#')
3477 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3478 {
3479 clearop(cap->oap);
3480 return;
3481 }
3482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // Allocate buffer to put the command in. Inserting backslashes can
3484 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3485 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3487 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3488 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003489 if (kp_help && *skipwhite(ptr) == NUL)
3490 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003491 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003492 return;
3493 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003494 kp_ex = (*kp == ':');
3495 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3496 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (buf == NULL)
3498 return;
3499 buf[0] = NUL;
3500
3501 switch (cmdchar)
3502 {
3503 case '*':
3504 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003505 // Put cursor at start of word, makes search skip the word
3506 // under the cursor.
3507 // Call setpcmark() first, so "*``" puts the cursor back where
3508 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 setpcmark();
3510 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3511
3512 if (!g_cmd && vim_iswordp(ptr))
3513 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
3516
3517 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003518 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3519 if (n == 0)
3520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 break;
3522
3523 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003524 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#ifdef FEAT_CSCOPE
3526 if (p_cst)
3527 STRCPY(buf, "cstag ");
3528 else
3529#endif
3530 STRCPY(buf, "ts ");
3531 break;
3532
3533 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003534 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (curbuf->b_help)
3536 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003539 if (g_cmd)
3540 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003541 else if (cap->count0 == 0)
3542 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003543 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003544 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003548 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003549 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003551 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003552 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003554 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003555 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003556 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003557 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003559 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 vim_free(buf);
3562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003564 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003565 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003566 {
3567 vim_free(buf);
3568 vim_free(p);
3569 return;
3570 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003571 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003572 STRCAT(buf, p);
3573 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 else
3576 {
3577 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003578 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003579 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003580 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003581 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003582 {
3583 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003585 aux_ptr = (char_u *)"";
3586 else
3587 aux_ptr = (char_u *)"\\|\"\n[";
3588 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003589 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003590 aux_ptr = (char_u *)"\\|\"\n*?[";
3591
3592 p = buf + STRLEN(buf);
3593 while (n-- > 0)
3594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003595 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596 if (vim_strchr(aux_ptr, *ptr) != NULL)
3597 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 // When current byte is a part of multibyte character, copy all
3599 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 if (has_mbyte)
3601 {
3602 int i;
3603 int len = (*mb_ptr2len)(ptr) - 1;
3604
3605 for (i = 0; i < len && n >= 1; ++i, --n)
3606 *p++ = *ptr++;
3607 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608 *p++ = *ptr++;
3609 }
3610 *p = NUL;
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003613 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (cmdchar == '*' || cmdchar == '#')
3615 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003616 if (!g_cmd && (has_mbyte
3617 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3618 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003620
3621 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003622 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003624
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003625 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003628 {
3629 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003631 g_tag_at_cursor = FALSE;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 vim_free(buf);
3635}
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637/*
3638 * Get visually selected text, within one line only.
3639 * Returns FAIL if more than one line selected.
3640 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003641 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003642get_visual_text(
3643 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 char_u **pp, // return: start of selected text
3645 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 if (VIsual_mode != 'V')
3648 unadjust_for_sel();
3649 if (VIsual.lnum != curwin->w_cursor.lnum)
3650 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003651 if (cap != NULL)
3652 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return FAIL;
3654 }
3655 if (VIsual_mode == 'V')
3656 {
3657 *pp = ml_get_curline();
3658 *lenp = (int)STRLEN(*pp);
3659 }
3660 else
3661 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003662 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 *pp = ml_get_pos(&curwin->w_cursor);
3665 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3666 }
3667 else
3668 {
3669 *pp = ml_get_pos(&VIsual);
3670 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3671 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003672 if (**pp == NUL)
3673 *lenp = 0;
3674 if (has_mbyte && *lenp > 0)
3675 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003676 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 reset_VIsual_and_resel();
3679 return OK;
3680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682/*
3683 * CTRL-T: backwards in tag stack
3684 */
3685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003686nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 if (!checkclearopq(cap->oap))
3689 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3690}
3691
3692/*
3693 * Handle scrolling command 'H', 'L' and 'M'.
3694 */
3695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003696nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 int used = 0;
3699 long n;
3700#ifdef FEAT_FOLDING
3701 linenr_T lnum;
3702#endif
3703 int half;
3704
3705 cap->oap->motion_type = MLINE;
3706 setpcmark();
3707
3708 if (cap->cmdchar == 'L')
3709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003710 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 curwin->w_cursor.lnum = curwin->w_botline - 1;
3712 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3713 curwin->w_cursor.lnum = 1;
3714 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003715 {
3716#ifdef FEAT_FOLDING
3717 if (hasAnyFolding(curwin))
3718 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003719 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003720 for (n = cap->count1 - 1; n > 0
3721 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3722 {
3723 (void)hasFolding(curwin->w_cursor.lnum,
3724 &curwin->w_cursor.lnum, NULL);
3725 --curwin->w_cursor.lnum;
3726 }
3727 }
3728 else
3729#endif
3730 curwin->w_cursor.lnum -= cap->count1 - 1;
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 else
3734 {
3735 if (cap->cmdchar == 'M')
3736 {
3737#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 used -= diff_check_fill(curwin, curwin->w_topline)
3740 - curwin->w_topfill;
3741#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3744 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3745 {
3746#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 // Count half he number of filler lines to be "below this
3748 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3750 + n) / 2 >= half)
3751 {
3752 --n;
3753 break;
3754 }
3755#endif
3756 used += plines(curwin->w_topline + n);
3757 if (used >= half)
3758 break;
3759#ifdef FEAT_FOLDING
3760 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3761 n = lnum - curwin->w_topline;
3762#endif
3763 }
3764 if (n > 0 && used > curwin->w_height)
3765 --n;
3766 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003767 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003770#ifdef FEAT_FOLDING
3771 if (hasAnyFolding(curwin))
3772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003774 lnum = curwin->w_topline;
3775 while (n-- > 0 && lnum < curwin->w_botline - 1)
3776 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003777 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003778 ++lnum;
3779 }
3780 n = lnum - curwin->w_topline;
3781 }
3782#endif
3783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 curwin->w_cursor.lnum = curwin->w_topline + n;
3785 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3786 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3787 }
3788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003789 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003790 if (cap->oap->op_type == OP_NOP)
3791 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 beginline(BL_SOL | BL_FIX);
3793}
3794
3795/*
3796 * Cursor right commands.
3797 */
3798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003799nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
3801 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003802 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003804 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003806 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003807 if (mod_mask & MOD_MASK_CTRL)
3808 cap->arg = TRUE;
3809 nv_wordcmd(cap);
3810 return;
3811 }
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 cap->oap->motion_type = MCHAR;
3814 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003815 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003817 // In virtual edit mode, there's no such thing as "past_line", as lines
3818 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003820 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
3822 for (n = cap->count1; n > 0; --n)
3823 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003824 if ((!past_line && oneright() == FAIL)
3825 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003826 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003828 // <Space> wraps to next line if 'whichwrap' has 's'.
3829 // 'l' wraps to next line if 'whichwrap' has 'l'.
3830 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if ( ((cap->cmdchar == ' '
3832 && vim_strchr(p_ww, 's') != NULL)
3833 || (cap->cmdchar == 'l'
3834 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003835 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 && vim_strchr(p_ww, '>') != NULL))
3837 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 // When deleting we also count the NL as a character.
3840 // Set cap->oap->inclusive when last char in the line is
3841 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003842 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003844 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 cap->oap->inclusive = TRUE;
3846 else
3847 {
3848 ++curwin->w_cursor.lnum;
3849 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 curwin->w_set_curswant = TRUE;
3852 cap->oap->inclusive = FALSE;
3853 }
3854 continue;
3855 }
3856 if (cap->oap->op_type == OP_NOP)
3857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003858 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (n == cap->count1)
3860 beep_flush();
3861 }
3862 else
3863 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003864 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 cap->oap->inclusive = TRUE;
3866 }
3867 break;
3868 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003869 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (virtual_active())
3873 oneright();
3874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003877 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 ++curwin->w_cursor.col;
3880 }
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883#ifdef FEAT_FOLDING
3884 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3885 && cap->oap->op_type == OP_NOP)
3886 foldOpenCursor();
3887#endif
3888}
3889
3890/*
3891 * Cursor left commands.
3892 *
3893 * Returns TRUE when operator end should not be adjusted.
3894 */
3895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003896nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 long n;
3899
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003900 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003903 if (mod_mask & MOD_MASK_CTRL)
3904 cap->arg = 1;
3905 nv_bck_word(cap);
3906 return;
3907 }
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 cap->oap->motion_type = MCHAR;
3910 cap->oap->inclusive = FALSE;
3911 for (n = cap->count1; n > 0; --n)
3912 {
3913 if (oneleft() == FAIL)
3914 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003915 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3916 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3917 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if ( (((cap->cmdchar == K_BS
3919 || cap->cmdchar == Ctrl_H)
3920 && vim_strchr(p_ww, 'b') != NULL)
3921 || (cap->cmdchar == 'h'
3922 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003923 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 && vim_strchr(p_ww, '<') != NULL))
3925 && curwin->w_cursor.lnum > 1)
3926 {
3927 --(curwin->w_cursor.lnum);
3928 coladvance((colnr_T)MAXCOL);
3929 curwin->w_set_curswant = TRUE;
3930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 // When the NL before the first char has to be deleted we
3932 // put the cursor on the NUL after the previous line.
3933 // This is a very special case, be careful!
3934 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if ( (cap->oap->op_type == OP_DELETE
3936 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003937 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003939 char_u *cp = ml_get_cursor();
3940
3941 if (*cp != NUL)
3942 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003943 if (has_mbyte)
3944 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3945 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003946 ++curwin->w_cursor.col;
3947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 cap->retval |= CA_NO_ADJ_OP_END;
3949 }
3950 continue;
3951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003952 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3954 beep_flush();
3955 break;
3956 }
3957 }
3958#ifdef FEAT_FOLDING
3959 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3960 && cap->oap->op_type == OP_NOP)
3961 foldOpenCursor();
3962#endif
3963}
3964
3965/*
3966 * Cursor up commands.
3967 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3968 */
3969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003970nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 if (mod_mask & MOD_MASK_SHIFT)
3973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003974 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 cap->arg = BACKWARD;
3976 nv_page(cap);
3977 }
3978 else
3979 {
3980 cap->oap->motion_type = MLINE;
3981 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3982 clearopbeep(cap->oap);
3983 else if (cap->arg)
3984 beginline(BL_WHITE | BL_FIX);
3985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986}
3987
3988/*
3989 * Cursor down commands.
3990 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3991 */
3992 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003993nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 if (mod_mask & MOD_MASK_SHIFT)
3996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003997 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 cap->arg = FORWARD;
3999 nv_page(cap);
4000 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004001#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004002 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004003 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4004 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004010 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 cmdwin_result = CAR;
4012 else
4013#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004014#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004015 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004016 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4017 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4018 {
4019 invoke_prompt_callback();
4020 if (restart_edit == 0)
4021 restart_edit = 'a';
4022 }
4023 else
4024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026 cap->oap->motion_type = MLINE;
4027 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4028 clearopbeep(cap->oap);
4029 else if (cap->arg)
4030 beginline(BL_WHITE | BL_FIX);
4031 }
4032 }
4033}
4034
4035#ifdef FEAT_SEARCHPATH
4036/*
4037 * Grab the file name under the cursor and edit it.
4038 */
4039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004040nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
4042 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004043 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004045 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004048 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return;
4050 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 if (curbuf_locked())
4052 {
4053 clearop(cap->oap);
4054 return;
4055 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004056#ifdef FEAT_PROP_POPUP
4057 if (ERROR_IF_TERM_POPUP_WINDOW)
4058 return;
4059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004061 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 if (ptr != NULL)
4064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004065 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004066 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004067 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004069 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004070 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004071 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004072 {
4073 curwin->w_cursor.lnum = lnum;
4074 check_cursor_lnum();
4075 beginline(BL_SOL | BL_FIX);
4076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 vim_free(ptr);
4078 }
4079 else
4080 clearop(cap->oap);
4081}
4082#endif
4083
4084/*
4085 * <End> command: to end of current line or last line.
4086 */
4087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004088nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004094 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 nv_dollar(cap);
4097}
4098
4099/*
4100 * Handle the "$" command.
4101 */
4102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 cap->oap->motion_type = MCHAR;
4106 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004107 // In virtual mode when off the edge of a line and an operator
4108 // is pending (whew!) keep the cursor where it is.
4109 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (!virtual_active() || gchar_cursor() != NUL
4111 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004112 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (cursor_down((long)(cap->count1 - 1),
4114 cap->oap->op_type == OP_NOP) == FAIL)
4115 clearopbeep(cap->oap);
4116#ifdef FEAT_FOLDING
4117 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4118 foldOpenCursor();
4119#endif
4120}
4121
4122/*
4123 * Implementation of '?' and '/' commands.
4124 * If cap->arg is TRUE don't set PC mark.
4125 */
4126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004127nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004130 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4133 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004134 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 cap->cmdchar = 'g';
4136 cap->nchar = '?';
4137 nv_operator(cap);
4138 return;
4139 }
4140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004141 // When using 'incsearch' the cursor may be moved to set a different search
4142 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004143 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 if (cap->searchbuf == NULL)
4146 {
4147 clearop(oap);
4148 return;
4149 }
4150
Bram Moolenaar46539112015-02-17 15:43:57 +01004151 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004152 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004153 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
4155
LemonBoya4399382022-04-09 21:04:08 +01004156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157/*
4158 * Handle "N" and "n" commands.
4159 * cap->arg is SEARCH_REV for "N", 0 for "n".
4160 */
4161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004162nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004164 pos_T old = curwin->w_cursor;
4165 int wrapped = FALSE;
4166 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004167
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004168 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004170 // Avoid getting stuck on the current cursor position, which can
4171 // happen when an offset is given and the cursor is on the last char
4172 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004173 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004174 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004175 cap->count1 -= 1;
4176 }
LemonBoya4399382022-04-09 21:04:08 +01004177
4178#ifdef FEAT_SEARCH_EXTRA
4179 // Redraw the window to refresh the highlighted matches.
4180 if (i > 0 && p_hls && !no_hlsearch)
4181 redraw_later(SOME_VALID);
4182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183}
4184
4185/*
4186 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4187 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004188 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004190 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004191normal_search(
4192 cmdarg_T *cap,
4193 int dir,
4194 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004195 int opt, // extra flags for do_search()
4196 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004199 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004200#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004201 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 cap->oap->motion_type = MCHAR;
4205 cap->oap->inclusive = FALSE;
4206 cap->oap->use_reg_one = TRUE;
4207 curwin->w_set_curswant = TRUE;
4208
Bram Moolenaara80faa82020-04-12 19:37:17 +02004209 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004210 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004211 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4212 if (wrapped != NULL)
4213 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (i == 0)
4215 clearop(cap->oap);
4216 else
4217 {
4218 if (i == 2)
4219 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifdef FEAT_FOLDING
4222 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4223 foldOpenCursor();
4224#endif
4225 }
LemonBoya4399382022-04-09 21:04:08 +01004226#ifdef FEAT_SEARCH_EXTRA
4227 // Redraw the window to refresh the highlighted matches.
4228 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
4229 redraw_later(SOME_VALID);
4230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004232 // "/$" will put the cursor after the end of the line, may need to
4233 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004235 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236}
4237
4238/*
4239 * Character search commands.
4240 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4241 * ',' and FALSE for ';'.
4242 * cap->nchar is NUL for ',' and ';' (repeat the search)
4243 */
4244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004245nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 int t_cmd;
4248
4249 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4250 t_cmd = TRUE;
4251 else
4252 t_cmd = FALSE;
4253
4254 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4256 clearopbeep(cap->oap);
4257 else
4258 {
4259 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004260 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4262 && (t_cmd || cap->oap->op_type != OP_NOP))
4263 {
4264 colnr_T scol, ecol;
4265
4266 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4267 curwin->w_cursor.coladd = ecol - scol;
4268 }
4269 else
4270 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#ifdef FEAT_FOLDING
4273 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4274 foldOpenCursor();
4275#endif
4276 }
4277}
4278
4279/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004280 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4281 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4282 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4283 * "[m" or "]m" search for prev/next start of (Java) method.
4284 * "[M" or "]M" search for prev/next end of (Java) method.
4285 */
4286 static void
4287nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4288{
4289 pos_T new_pos = {0, 0, 0};
4290 pos_T *pos = NULL; // init for GCC
4291 pos_T prev_pos;
4292 long n;
4293 int findc;
4294 int c;
4295
4296 if (cap->nchar == '*')
4297 cap->nchar = '/';
4298 prev_pos.lnum = 0;
4299 if (cap->nchar == 'm' || cap->nchar == 'M')
4300 {
4301 if (cap->cmdchar == '[')
4302 findc = '{';
4303 else
4304 findc = '}';
4305 n = 9999;
4306 }
4307 else
4308 {
4309 findc = cap->nchar;
4310 n = cap->count1;
4311 }
4312 for ( ; n > 0; --n)
4313 {
4314 if ((pos = findmatchlimit(cap->oap, findc,
4315 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4316 {
4317 if (new_pos.lnum == 0) // nothing found
4318 {
4319 if (cap->nchar != 'm' && cap->nchar != 'M')
4320 clearopbeep(cap->oap);
4321 }
4322 else
4323 pos = &new_pos; // use last one found
4324 break;
4325 }
4326 prev_pos = new_pos;
4327 curwin->w_cursor = *pos;
4328 new_pos = *pos;
4329 }
4330 curwin->w_cursor = *old_pos;
4331
4332 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4333 // brought us to the match for "[m" and "]M" when inside a method.
4334 // Try finding the '{' or '}' we want to be at.
4335 // Also repeat for the given count.
4336 if (cap->nchar == 'm' || cap->nchar == 'M')
4337 {
4338 // norm is TRUE for "]M" and "[m"
4339 int norm = ((findc == '{') == (cap->nchar == 'm'));
4340
4341 n = cap->count1;
4342 // found a match: we were inside a method
4343 if (prev_pos.lnum != 0)
4344 {
4345 pos = &prev_pos;
4346 curwin->w_cursor = prev_pos;
4347 if (norm)
4348 --n;
4349 }
4350 else
4351 pos = NULL;
4352 while (n > 0)
4353 {
4354 for (;;)
4355 {
4356 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4357 {
4358 // if not found anything, that's an error
4359 if (pos == NULL)
4360 clearopbeep(cap->oap);
4361 n = 0;
4362 break;
4363 }
4364 c = gchar_cursor();
4365 if (c == '{' || c == '}')
4366 {
4367 // Must have found end/start of class: use it.
4368 // Or found the place to be at.
4369 if ((c == findc && norm) || (n == 1 && !norm))
4370 {
4371 new_pos = curwin->w_cursor;
4372 pos = &new_pos;
4373 n = 0;
4374 }
4375 // if no match found at all, we started outside of the
4376 // class and we're inside now. Just go on.
4377 else if (new_pos.lnum == 0)
4378 {
4379 new_pos = curwin->w_cursor;
4380 pos = &new_pos;
4381 }
4382 // found start/end of other method: go to match
4383 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004384 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4385 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004386 n = 0;
4387 else
4388 curwin->w_cursor = *pos;
4389 break;
4390 }
4391 }
4392 --n;
4393 }
4394 curwin->w_cursor = *old_pos;
4395 if (pos == NULL && new_pos.lnum != 0)
4396 clearopbeep(cap->oap);
4397 }
4398 if (pos != NULL)
4399 {
4400 setpcmark();
4401 curwin->w_cursor = *pos;
4402 curwin->w_set_curswant = TRUE;
4403#ifdef FEAT_FOLDING
4404 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4405 && cap->oap->op_type == OP_NOP)
4406 foldOpenCursor();
4407#endif
4408 }
4409}
4410
4411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 * "[" and "]" commands.
4413 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4414 */
4415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004416nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004419 pos_T *pos = NULL; // init for GCC
4420 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 int flag;
4422 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 cap->oap->motion_type = MCHAR;
4425 cap->oap->inclusive = FALSE;
4426 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004427 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004430 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (cap->nchar == 'f')
4432 nv_gotofile(cap);
4433 else
4434#endif
4435
4436#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004437 // Find the occurrence(s) of the identifier or define under cursor
4438 // in current and included files or jump to the first occurrence.
4439 //
4440 // search list jump
4441 // fwd bwd fwd bwd fwd bwd
4442 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4443 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004444 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
4446 char_u *ptr;
4447 int len;
4448
4449 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4450 clearop(cap->oap);
4451 else
4452 {
4453 find_pattern_in_path(ptr, 0, len, TRUE,
4454 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4455 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4456 cap->count1,
4457 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4458 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4459 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4460 (linenr_T)MAXLNUM);
4461 curwin->w_set_curswant = TRUE;
4462 }
4463 }
4464 else
4465#endif
4466
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004467 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4468 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4469 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4470 // "[m" or "]m" search for prev/next start of (Java) method.
4471 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if ( (cap->cmdchar == '['
4473 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4474 || (cap->cmdchar == ']'
4475 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004476 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004478 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 else if (cap->nchar == '[' || cap->nchar == ']')
4480 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004481 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 flag = '{';
4483 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004484 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004487 // Imitate strange Vi behaviour: When using "]]" with an operator
4488 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004489 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 (cap->oap->op_type != OP_NOP
4491 && cap->arg == FORWARD && flag == '{')))
4492 clearopbeep(cap->oap);
4493 else
4494 {
4495 if (cap->oap->op_type == OP_NOP)
4496 beginline(BL_WHITE | BL_FIX);
4497#ifdef FEAT_FOLDING
4498 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4499 foldOpenCursor();
4500#endif
4501 }
4502 }
4503
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004504 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 else if (cap->nchar == 'p' || cap->nchar == 'P')
4506 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004507 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004510 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 else if (cap->nchar == '\'' || cap->nchar == '`')
4512 {
4513 pos = &curwin->w_cursor;
4514 for (n = cap->count1; n > 0; --n)
4515 {
4516 prev_pos = *pos;
4517 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4518 cap->nchar == '\'');
4519 if (pos == NULL)
4520 break;
4521 }
4522 if (pos == NULL)
4523 pos = &prev_pos;
4524 nv_cursormark(cap, cap->nchar == '\'', pos);
4525 }
4526
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004527 // [ or ] followed by a middle mouse click: put selected text with
4528 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004529 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 (void)do_mouse(cap->oap, cap->nchar,
4532 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4533 cap->count1, PUT_FIXINDENT);
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004537 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else if (cap->nchar == 'z')
4539 {
4540 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4541 cap->count1) == FAIL)
4542 clearopbeep(cap->oap);
4543 }
4544#endif
4545
4546#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004547 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 else if (cap->nchar == 'c')
4549 {
4550 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4551 cap->count1) == FAIL)
4552 clearopbeep(cap->oap);
4553 }
4554#endif
4555
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004556#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004557 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004558 else if (cap->nchar == 's' || cap->nchar == 'S')
4559 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004560 setpcmark();
4561 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004562 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4563 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004564 {
4565 clearopbeep(cap->oap);
4566 break;
4567 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004568 else
4569 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004570# ifdef FEAT_FOLDING
4571 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4572 foldOpenCursor();
4573# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004574 }
4575#endif
4576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004577 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 else
4579 clearopbeep(cap->oap);
4580}
4581
4582/*
4583 * Handle Normal mode "%" command.
4584 */
4585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004586nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587{
4588 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004589#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 linenr_T lnum = curwin->w_cursor.lnum;
4591#endif
4592
4593 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004594 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 if (cap->count0 > 100)
4597 clearopbeep(cap->oap);
4598 else
4599 {
4600 cap->oap->motion_type = MLINE;
4601 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004602 // Round up, so 'normal 100%' always jumps at the line line.
4603 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4604 // overflow on 32-bits, so use a formula with less accuracy
4605 // to avoid overflows.
4606 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4608 / 100L * cap->count0;
4609 else
4610 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4611 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004612 if (curwin->w_cursor.lnum < 1)
4613 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4615 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4616 beginline(BL_SOL | BL_FIX);
4617 }
4618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004619 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 cap->oap->motion_type = MCHAR;
4622 cap->oap->use_reg_one = TRUE;
4623 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4624 clearopbeep(cap->oap);
4625 else
4626 {
4627 setpcmark();
4628 curwin->w_cursor = *pos;
4629 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 }
4634#ifdef FEAT_FOLDING
4635 if (cap->oap->op_type == OP_NOP
4636 && lnum != curwin->w_cursor.lnum
4637 && (fdo_flags & FDO_PERCENT)
4638 && KeyTyped)
4639 foldOpenCursor();
4640#endif
4641}
4642
4643/*
4644 * Handle "(" and ")" commands.
4645 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4646 */
4647 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004648nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
4650 cap->oap->motion_type = MCHAR;
4651 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004652 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004653 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 curwin->w_set_curswant = TRUE;
4655
4656 if (findsent(cap->arg, cap->count1) == FAIL)
4657 clearopbeep(cap->oap);
4658 else
4659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004661 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#ifdef FEAT_FOLDING
4664 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4665 foldOpenCursor();
4666#endif
4667 }
4668}
4669
4670/*
4671 * "m" command: Mark a position.
4672 */
4673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004674nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 if (!checkclearop(cap->oap))
4677 {
4678 if (setmark(cap->nchar) == FAIL)
4679 clearopbeep(cap->oap);
4680 }
4681}
4682
4683/*
4684 * "{" and "}" commands.
4685 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4686 */
4687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 cap->oap->motion_type = MCHAR;
4691 cap->oap->inclusive = FALSE;
4692 cap->oap->use_reg_one = TRUE;
4693 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004694 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 clearopbeep(cap->oap);
4696 else
4697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#ifdef FEAT_FOLDING
4700 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4701 foldOpenCursor();
4702#endif
4703 }
4704}
4705
4706/*
4707 * "u" command: Undo or make lower case.
4708 */
4709 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004710nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004712 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 cap->cmdchar = 'g';
4716 cap->nchar = 'u';
4717 nv_operator(cap);
4718 }
4719 else
4720 nv_kundo(cap);
4721}
4722
4723/*
4724 * <Undo> command.
4725 */
4726 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004727nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
4729 if (!checkclearopq(cap->oap))
4730 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004731#ifdef FEAT_JOB_CHANNEL
4732 if (bt_prompt(curbuf))
4733 {
4734 clearopbeep(cap->oap);
4735 return;
4736 }
4737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 u_undo((int)cap->count1);
4739 curwin->w_set_curswant = TRUE;
4740 }
4741}
4742
4743/*
4744 * Handle the "r" command.
4745 */
4746 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004747nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 char_u *ptr;
4750 int had_ctrl_v;
4751 long n;
4752
4753 if (checkclearop(cap->oap))
4754 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004755#ifdef FEAT_JOB_CHANNEL
4756 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4757 {
4758 clearopbeep(cap->oap);
4759 return;
4760 }
4761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004763 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (cap->nchar == Ctrl_V)
4765 {
4766 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004767 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004768 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (cap->nchar > DEL)
4770 had_ctrl_v = NUL;
4771 }
4772 else
4773 had_ctrl_v = NUL;
4774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004776 if (IS_SPECIAL(cap->nchar))
4777 {
4778 clearopbeep(cap->oap);
4779 return;
4780 }
4781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (VIsual_active)
4784 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004785 if (got_int)
4786 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004787 if (had_ctrl_v)
4788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Use a special (negative) number to make a difference between a
4790 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004791 if (cap->nchar == CAR)
4792 cap->nchar = REPLACE_CR_NCHAR;
4793 else if (cap->nchar == NL)
4794 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 nv_operator(cap);
4797 return;
4798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004800 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (virtual_active())
4802 {
4803 if (u_save_cursor() == FAIL)
4804 return;
4805 if (gchar_cursor() == NUL)
4806 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004807 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 coladvance_force((colnr_T)(getviscol() + cap->count1));
4809 curwin->w_cursor.col -= cap->count1;
4810 }
4811 else if (gchar_cursor() == TAB)
4812 coladvance_force(getviscol());
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004815 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004817 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004818 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 clearopbeep(cap->oap);
4821 return;
4822 }
4823
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004824 // Replacing with a TAB is done by edit() when it is complicated because
4825 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4826 // Other characters are done below to avoid problems with things like
4827 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4829 {
4830 stuffnumReadbuff(cap->count1);
4831 stuffcharReadbuff('R');
4832 stuffcharReadbuff('\t');
4833 stuffcharReadbuff(ESC);
4834 return;
4835 }
4836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004837 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (u_save_cursor() == FAIL)
4839 return;
4840
4841 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4842 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004843 // Replace character(s) by a single newline.
4844 // Strange vi behaviour: Only one newline is inserted.
4845 // Delete the characters here.
4846 // Insert the newline with an insert command, takes care of
4847 // autoindent. The insert command depends on being on the last
4848 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004849 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 stuffcharReadbuff('\r');
4851 stuffcharReadbuff(ESC);
4852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004853 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 invoke_edit(cap, TRUE, 'r', FALSE);
4855 }
4856 else
4857 {
4858 prep_redo(cap->oap->regname, cap->count1,
4859 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4860
4861 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 if (has_mbyte)
4863 {
4864 int old_State = State;
4865
4866 if (cap->ncharC1 != 0)
4867 AppendCharToRedobuff(cap->ncharC1);
4868 if (cap->ncharC2 != 0)
4869 AppendCharToRedobuff(cap->ncharC2);
4870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // This is slow, but it handles replacing a single-byte with a
4872 // multi-byte and the other way around. Also handles adding
4873 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 for (n = cap->count1; n > 0; --n)
4875 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004876 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004877 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4878 {
4879 int c = ins_copychar(curwin->w_cursor.lnum
4880 + (cap->nchar == Ctrl_Y ? -1 : 1));
4881 if (c != NUL)
4882 ins_char(c);
4883 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004884 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004885 ++curwin->w_cursor.col;
4886 }
4887 else
4888 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 State = old_State;
4890 if (cap->ncharC1 != 0)
4891 ins_char(cap->ncharC1);
4892 if (cap->ncharC2 != 0)
4893 ins_char(cap->ncharC2);
4894 }
4895 }
4896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004898 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 for (n = cap->count1; n > 0; --n)
4900 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004901 // Get ptr again, because u_save and/or showmatch() will have
4902 // released the line. This may also happen in ins_copychar().
4903 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004904 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4905 {
4906 int c = ins_copychar(curwin->w_cursor.lnum
4907 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004908
4909 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004910 if (c != NUL)
4911 ptr[curwin->w_cursor.col] = c;
4912 }
4913 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004914 {
4915 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004916 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 if (p_sm && msg_silent == 0)
4919 showmatch(cap->nchar);
4920 ++curwin->w_cursor.col;
4921 }
4922#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004923 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004925 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar009b2592004-10-24 19:18:58 +00004927 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004928 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004930 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932#endif
4933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004934 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 changed_bytes(curwin->w_cursor.lnum,
4936 (colnr_T)(curwin->w_cursor.col - cap->count1));
4937 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 --curwin->w_cursor.col; // cursor on the last replaced char
4939 // if the character on the left of the current cursor is a multi-byte
4940 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (has_mbyte)
4942 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 curbuf->b_op_end = curwin->w_cursor;
4944 curwin->w_set_curswant = TRUE;
4945 set_last_insert(cap->nchar);
4946 }
4947}
4948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949/*
4950 * 'o': Exchange start and end of Visual area.
4951 * 'O': same, but in block mode exchange left and right corners.
4952 */
4953 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004954v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
4956 pos_T old_cursor;
4957 colnr_T left, right;
4958
4959 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4960 {
4961 old_cursor = curwin->w_cursor;
4962 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4963 curwin->w_cursor.lnum = VIsual.lnum;
4964 coladvance(left);
4965 VIsual = curwin->w_cursor;
4966
4967 curwin->w_cursor.lnum = old_cursor.lnum;
4968 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004969 // 'selection "exclusive" and cursor at right-bottom corner: move it
4970 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4972 ++curwin->w_curswant;
4973 coladvance(curwin->w_curswant);
4974 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004976 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 curwin->w_cursor.lnum = VIsual.lnum;
4979 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4980 ++right;
4981 coladvance(right);
4982 VIsual = curwin->w_cursor;
4983
4984 curwin->w_cursor.lnum = old_cursor.lnum;
4985 coladvance(left);
4986 curwin->w_curswant = left;
4987 }
4988 }
4989 else
4990 {
4991 old_cursor = curwin->w_cursor;
4992 curwin->w_cursor = VIsual;
4993 VIsual = old_cursor;
4994 curwin->w_set_curswant = TRUE;
4995 }
4996}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
4998/*
4999 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5000 */
5001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005002nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005004 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 cap->cmdchar = 'c';
5007 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005008 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 VIsual_mode = 'V';
5010 nv_operator(cap);
5011 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005012 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005015 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 else
5017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (virtual_active())
5019 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5021 }
5022 }
5023}
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025/*
5026 * "gr".
5027 */
5028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005029nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (VIsual_active)
5032 {
5033 cap->cmdchar = 'r';
5034 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005035 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005037 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005040 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 else
5042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005044 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 stuffcharReadbuff(cap->extra_char);
5046 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (virtual_active())
5048 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 invoke_edit(cap, TRUE, 'v', FALSE);
5050 }
5051 }
5052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054/*
5055 * Swap case for "~" command, when it does not work like an operator.
5056 */
5057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005058n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 long n;
5061 pos_T startpos;
5062 int did_change = 0;
5063#ifdef FEAT_NETBEANS_INTG
5064 pos_T pos;
5065 char_u *ptr;
5066 int count;
5067#endif
5068
5069 if (checkclearopq(cap->oap))
5070 return;
5071
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005072 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 clearopbeep(cap->oap);
5075 return;
5076 }
5077
5078 prep_redo_cmd(cap);
5079
5080 if (u_save_cursor() == FAIL)
5081 return;
5082
5083 startpos = curwin->w_cursor;
5084#ifdef FEAT_NETBEANS_INTG
5085 pos = startpos;
5086#endif
5087 for (n = cap->count1; n > 0; --n)
5088 {
5089 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5090 inc_cursor();
5091 if (gchar_cursor() == NUL)
5092 {
5093 if (vim_strchr(p_ww, '~') != NULL
5094 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5095 {
5096#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005097 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099 if (did_change)
5100 {
5101 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005102 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005103 netbeans_removed(curbuf, pos.lnum, pos.col,
5104 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005106 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 pos.col = 0;
5109 pos.lnum++;
5110 }
5111#endif
5112 ++curwin->w_cursor.lnum;
5113 curwin->w_cursor.col = 0;
5114 if (n > 1)
5115 {
5116 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5117 break;
5118 u_clearline();
5119 }
5120 }
5121 else
5122 break;
5123 }
5124 }
5125#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005126 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 ptr = ml_get(pos.lnum);
5129 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005130 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5131 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133#endif
5134
5135
5136 check_cursor();
5137 curwin->w_set_curswant = TRUE;
5138 if (did_change)
5139 {
5140 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5141 0L);
5142 curbuf->b_op_start = startpos;
5143 curbuf->b_op_end = curwin->w_cursor;
5144 if (curbuf->b_op_end.col > 0)
5145 --curbuf->b_op_end.col;
5146 }
5147}
5148
5149/*
5150 * Move cursor to mark.
5151 */
5152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154{
5155 if (check_mark(pos) == FAIL)
5156 clearop(cap->oap);
5157 else
5158 {
5159 if (cap->cmdchar == '\''
5160 || cap->cmdchar == '`'
5161 || cap->cmdchar == '['
5162 || cap->cmdchar == ']')
5163 setpcmark();
5164 curwin->w_cursor = *pos;
5165 if (flag)
5166 beginline(BL_WHITE | BL_FIX);
5167 else
5168 check_cursor();
5169 }
5170 cap->oap->motion_type = flag ? MLINE : MCHAR;
5171 if (cap->cmdchar == '`')
5172 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 curwin->w_set_curswant = TRUE;
5175}
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177/*
5178 * Handle commands that are operators in Visual mode.
5179 */
5180 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005185 // Uppercase means linewise, except in block mode, then "D" deletes till
5186 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (isupper(cap->cmdchar))
5188 {
5189 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005190 {
5191 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5195 curwin->w_curswant = MAXCOL;
5196 }
5197 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5198 nv_operator(cap);
5199}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
5201/*
5202 * "s" and "S" commands.
5203 */
5204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005205nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005207#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005208 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005209 if (term_swap_diff() == OK)
5210 return;
5211#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005212#ifdef FEAT_JOB_CHANNEL
5213 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5214 {
5215 clearopbeep(cap->oap);
5216 return;
5217 }
5218#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005219 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005222 {
5223 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 cap->cmdchar = 'c';
5227 nv_operator(cap);
5228 }
5229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 nv_optrans(cap);
5231}
5232
5233/*
5234 * Abbreviated commands.
5235 */
5236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005237nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005242 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (VIsual_active)
5244 v_visop(cap);
5245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 nv_optrans(cap);
5247}
5248
5249/*
5250 * Translate a command into another command.
5251 */
5252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005253nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5256 (char_u *)"d$", (char_u *)"c$",
5257 (char_u *)"cl", (char_u *)"cc",
5258 (char_u *)"yy", (char_u *)":s\r"};
5259 static char_u *str = (char_u *)"xXDCsSY&";
5260
5261 if (!checkclearopq(cap->oap))
5262 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005263 // In Vi "2D" doesn't delete the next line. Can't translate it
5264 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005265 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5266 {
5267 cap->oap->start = curwin->w_cursor;
5268 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005269#ifdef FEAT_EVAL
5270 set_op_var(OP_DELETE);
5271#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005272 cap->count1 = 1;
5273 nv_dollar(cap);
5274 finish_op = TRUE;
5275 ResetRedobuff();
5276 AppendCharToRedobuff('D');
5277 }
5278 else
5279 {
5280 if (cap->count0)
5281 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005282 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285 cap->opcount = 0;
5286}
5287
5288/*
5289 * "'" and "`" commands. Also for "g'" and "g`".
5290 * cap->arg is TRUE for "'" and "g'".
5291 */
5292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005293nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
5295 pos_T *pos;
5296 int c;
5297#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005298 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005299 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300#endif
5301
5302 if (cap->cmdchar == 'g')
5303 c = cap->extra_char;
5304 else
5305 c = cap->nchar;
5306 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
5309 if (cap->arg)
5310 {
5311 check_cursor_lnum();
5312 beginline(BL_WHITE | BL_FIX);
5313 }
5314 else
5315 check_cursor();
5316 }
5317 else
5318 nv_cursormark(cap, cap->arg, pos);
5319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (!virtual_active())
5322 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005323 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#ifdef FEAT_FOLDING
5325 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005326 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005327 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 && (fdo_flags & FDO_MARK)
5329 && old_KeyTyped)
5330 foldOpenCursor();
5331#endif
5332}
5333
5334/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005335 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 */
5337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005338nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005341#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 if (!checkclearopq(cap->oap))
5347 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005348 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5349 {
5350 if (goto_tabpage_lastused() == FAIL)
5351 clearopbeep(cap->oap);
5352 return;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (cap->cmdchar == 'g')
5355 pos = movechangelist((int)cap->count1);
5356 else
5357 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005358 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 curwin->w_set_curswant = TRUE;
5361 check_cursor();
5362 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005363 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 nv_cursormark(cap, FALSE, pos);
5365 else if (cap->cmdchar == 'g')
5366 {
5367 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005368 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005370 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005372 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 else
5375 clearopbeep(cap->oap);
5376# ifdef FEAT_FOLDING
5377 if (cap->oap->op_type == OP_NOP
5378 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5379 && (fdo_flags & FDO_MARK)
5380 && old_KeyTyped)
5381 foldOpenCursor();
5382# endif
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384}
5385
5386/*
5387 * Handle '"' command.
5388 */
5389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005390nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 if (checkclearop(cap->oap))
5393 return;
5394#ifdef FEAT_EVAL
5395 if (cap->nchar == '=')
5396 cap->nchar = get_expr_register();
5397#endif
5398 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5399 {
5400 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005401 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#ifdef FEAT_EVAL
5403 set_reg_var(cap->oap->regname);
5404#endif
5405 }
5406 else
5407 clearopbeep(cap->oap);
5408}
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
5411 * Handle "v", "V" and "CTRL-V" commands.
5412 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5413 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005414 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 */
5416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005417nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005419 if (cap->cmdchar == Ctrl_Q)
5420 cap->cmdchar = Ctrl_V;
5421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005422 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5423 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (cap->oap->op_type != OP_NOP)
5425 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005426 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005427 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return;
5429 }
5430
5431 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005432 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 else // toggle char/block mode
5437 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 VIsual_mode = cap->cmdchar;
5439 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005440 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
5446 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005447 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005449 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 VIsual = curwin->w_cursor;
5451
5452 VIsual_active = TRUE;
5453 VIsual_reselect = TRUE;
5454 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005458 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005460 // For V and ^V, we multiply the number of lines even if there
5461 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5463 {
5464 curwin->w_cursor.lnum +=
5465 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005466 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 VIsual_mode = resel_VIsual_mode;
5469 if (VIsual_mode == 'v')
5470 {
5471 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005472 {
5473 validate_virtcol();
5474 curwin->w_curswant = curwin->w_virtcol
5475 + resel_VIsual_vcol * cap->count0 - 1;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005478 curwin->w_curswant = resel_VIsual_vcol;
5479 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005481 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 curwin->w_curswant = MAXCOL;
5484 coladvance((colnr_T)MAXCOL);
5485 }
5486 else if (VIsual_mode == Ctrl_V)
5487 {
5488 validate_virtcol();
5489 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005490 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 coladvance(curwin->w_curswant);
5492 }
5493 else
5494 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 else
5498 {
5499 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005500 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 may_start_select('c');
5502 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005503 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005504 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005505 if (cap->count0 > 0 && --cap->count1 > 0)
5506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005507 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005508 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5509 nv_right(cap);
5510 else if (VIsual_mode == 'V')
5511 nv_down(cap);
5512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 }
5514 }
5515}
5516
5517/*
5518 * Start selection for Shift-movement keys.
5519 */
5520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005521start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005523 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 may_start_select('k');
5525 n_start_visual_mode('v');
5526}
5527
5528/*
5529 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005530 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 */
5532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005533may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005535 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5536 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537}
5538
5539/*
5540 * Start Visual mode "c".
5541 * Should set VIsual_select before calling this.
5542 */
5543 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005544n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005546#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005547 int cursor_line_was_concealed = curwin->w_p_cole > 0
5548 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005549#endif
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 VIsual_mode = c;
5552 VIsual_active = TRUE;
5553 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005554
5555 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005556 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005557 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005558 {
5559 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 VIsual = curwin->w_cursor;
5563
5564#ifdef FEAT_FOLDING
5565 foldAdjustVisual();
5566#endif
5567
LemonBoy2bf52dd2022-04-09 18:17:34 +01005568 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005570#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005571 // Check if redraw is needed after changing the state.
5572 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005573#endif
5574
Bram Moolenaar09df3122006-01-23 22:23:09 +00005575 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005576 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 // Make sure the clipboard gets updated. Needed because start and
5579 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 clip_star.vmode = NUL;
5581#endif
5582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005583 // Only need to redraw this line, unless still need to redraw an old
5584 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 if (curwin->w_redr_type < INVERTED)
5586 {
5587 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5588 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5589 }
5590}
5591
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
5593/*
5594 * CTRL-W: Window commands
5595 */
5596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005597nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005599 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005601 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005602 cap->cmdchar = ':';
5603 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005604 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005605 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005606 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005607 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610/*
5611 * CTRL-Z: Suspend
5612 */
5613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005614nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005618 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005619 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
5621
5622/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005623 * "gv": Reselect the previous Visual area. If Visual already active,
5624 * exchange previous and current Visual area.
5625 */
5626 static void
5627nv_gv_cmd(cmdarg_T *cap)
5628{
5629 pos_T tpos;
5630 int i;
5631
5632 if (checkclearop(cap->oap))
5633 return;
5634
5635 if (curbuf->b_visual.vi_start.lnum == 0
5636 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5637 || curbuf->b_visual.vi_end.lnum == 0)
5638 {
5639 beep_flush();
5640 return;
5641 }
5642
5643 // set w_cursor to the start of the Visual area, tpos to the end
5644 if (VIsual_active)
5645 {
5646 i = VIsual_mode;
5647 VIsual_mode = curbuf->b_visual.vi_mode;
5648 curbuf->b_visual.vi_mode = i;
5649# ifdef FEAT_EVAL
5650 curbuf->b_visual_mode_eval = i;
5651# endif
5652 i = curwin->w_curswant;
5653 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5654 curbuf->b_visual.vi_curswant = i;
5655
5656 tpos = curbuf->b_visual.vi_end;
5657 curbuf->b_visual.vi_end = curwin->w_cursor;
5658 curwin->w_cursor = curbuf->b_visual.vi_start;
5659 curbuf->b_visual.vi_start = VIsual;
5660 }
5661 else
5662 {
5663 VIsual_mode = curbuf->b_visual.vi_mode;
5664 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5665 tpos = curbuf->b_visual.vi_end;
5666 curwin->w_cursor = curbuf->b_visual.vi_start;
5667 }
5668
5669 VIsual_active = TRUE;
5670 VIsual_reselect = TRUE;
5671
5672 // Set Visual to the start and w_cursor to the end of the Visual
5673 // area. Make sure they are on an existing character.
5674 check_cursor();
5675 VIsual = curwin->w_cursor;
5676 curwin->w_cursor = tpos;
5677 check_cursor();
5678 update_topline();
5679
5680 // When called from normal "g" command: start Select mode when
5681 // 'selectmode' contains "cmd". When called for K_SELECT, always
5682 // start Select mode.
5683 if (cap->arg)
5684 {
5685 VIsual_select = TRUE;
5686 VIsual_select_reg = 0;
5687 }
5688 else
5689 may_start_select('c');
5690 setmouse();
5691#ifdef FEAT_CLIPBOARD
5692 // Make sure the clipboard gets updated. Needed because start and
5693 // end are still the same, and the selection needs to be owned
5694 clip_star.vmode = NUL;
5695#endif
5696 redraw_curbuf_later(INVERTED);
5697 showmode();
5698}
5699
5700/*
5701 * "g0", "g^" : Like "0" and "^" but for screen lines.
5702 * "gm": middle of "g0" and "g$".
5703 */
5704 static void
5705nv_g_home_m_cmd(cmdarg_T *cap)
5706{
5707 int i;
5708 int flag = FALSE;
5709
5710 if (cap->nchar == '^')
5711 flag = TRUE;
5712
5713 cap->oap->motion_type = MCHAR;
5714 cap->oap->inclusive = FALSE;
5715 if (curwin->w_p_wrap && curwin->w_width != 0)
5716 {
5717 int width1 = curwin->w_width - curwin_col_off();
5718 int width2 = width1 + curwin_col_off2();
5719
5720 validate_virtcol();
5721 i = 0;
5722 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5723 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5724 }
5725 else
5726 i = curwin->w_leftcol;
5727 // Go to the middle of the screen line. When 'number' or
5728 // 'relativenumber' is on and lines are wrapping the middle can be more
5729 // to the left.
5730 if (cap->nchar == 'm')
5731 i += (curwin->w_width - curwin_col_off()
5732 + ((curwin->w_p_wrap && i > 0)
5733 ? curwin_col_off2() : 0)) / 2;
5734 coladvance((colnr_T)i);
5735 if (flag)
5736 {
5737 do
5738 i = gchar_cursor();
5739 while (VIM_ISWHITE(i) && oneright() == OK);
5740 curwin->w_valid &= ~VALID_WCOL;
5741 }
5742 curwin->w_set_curswant = TRUE;
5743}
5744
5745/*
5746 * "g_": to the last non-blank character in the line or <count> lines
5747 * downward.
5748 */
5749 static void
5750nv_g_underscore_cmd(cmdarg_T *cap)
5751{
5752 char_u *ptr;
5753
5754 cap->oap->motion_type = MCHAR;
5755 cap->oap->inclusive = TRUE;
5756 curwin->w_curswant = MAXCOL;
5757 if (cursor_down((long)(cap->count1 - 1),
5758 cap->oap->op_type == OP_NOP) == FAIL)
5759 {
5760 clearopbeep(cap->oap);
5761 return;
5762 }
5763
5764 ptr = ml_get_curline();
5765
5766 // In Visual mode we may end up after the line.
5767 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5768 --curwin->w_cursor.col;
5769
5770 // Decrease the cursor column until it's on a non-blank.
5771 while (curwin->w_cursor.col > 0
5772 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5773 --curwin->w_cursor.col;
5774 curwin->w_set_curswant = TRUE;
5775 adjust_for_sel(cap);
5776}
5777
5778/*
5779 * "g$" : Like "$" but for screen lines.
5780 */
5781 static void
5782nv_g_dollar_cmd(cmdarg_T *cap)
5783{
5784 oparg_T *oap = cap->oap;
5785 int i;
5786 int col_off = curwin_col_off();
5787
5788 oap->motion_type = MCHAR;
5789 oap->inclusive = TRUE;
5790 if (curwin->w_p_wrap && curwin->w_width != 0)
5791 {
5792 curwin->w_curswant = MAXCOL; // so we stay at the end
5793 if (cap->count1 == 1)
5794 {
5795 int width1 = curwin->w_width - col_off;
5796 int width2 = width1 + curwin_col_off2();
5797
5798 validate_virtcol();
5799 i = width1 - 1;
5800 if (curwin->w_virtcol >= (colnr_T)width1)
5801 i += ((curwin->w_virtcol - width1) / width2 + 1)
5802 * width2;
5803 coladvance((colnr_T)i);
5804
5805 // Make sure we stick in this column.
5806 validate_virtcol();
5807 curwin->w_curswant = curwin->w_virtcol;
5808 curwin->w_set_curswant = FALSE;
5809 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5810 {
5811 // Check for landing on a character that got split at
5812 // the end of the line. We do not want to advance to
5813 // the next screen line.
5814 if (curwin->w_virtcol > (colnr_T)i)
5815 --curwin->w_cursor.col;
5816 }
5817 }
5818 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5819 clearopbeep(oap);
5820 }
5821 else
5822 {
5823 if (cap->count1 > 1)
5824 // if it fails, let the cursor still move to the last char
5825 (void)cursor_down(cap->count1 - 1, FALSE);
5826
5827 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5828 coladvance((colnr_T)i);
5829
5830 // if the character doesn't fit move one back
5831 if (curwin->w_cursor.col > 0
5832 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5833 {
5834 colnr_T vcol;
5835
5836 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5837 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5838 --curwin->w_cursor.col;
5839 }
5840
5841 // Make sure we stick in this column.
5842 validate_virtcol();
5843 curwin->w_curswant = curwin->w_virtcol;
5844 curwin->w_set_curswant = FALSE;
5845 }
5846}
5847
5848/*
5849 * "gi": start Insert at the last position.
5850 */
5851 static void
5852nv_gi_cmd(cmdarg_T *cap)
5853{
5854 int i;
5855
5856 if (curbuf->b_last_insert.lnum != 0)
5857 {
5858 curwin->w_cursor = curbuf->b_last_insert;
5859 check_cursor_lnum();
5860 i = (int)STRLEN(ml_get_curline());
5861 if (curwin->w_cursor.col > (colnr_T)i)
5862 {
5863 if (virtual_active())
5864 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5865 curwin->w_cursor.col = i;
5866 }
5867 }
5868 cap->cmdchar = 'i';
5869 nv_edit(cap);
5870}
5871
5872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 * Commands starting with "g".
5874 */
5875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005876nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 switch (cap->nchar)
5882 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005883 case Ctrl_A:
5884 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005886 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005887 if (!VIsual_active && cap->nchar == Ctrl_A)
5888 vim_mem_profile_dump();
5889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005891 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005892 if (VIsual_active)
5893 {
5894 cap->arg = TRUE;
5895 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005896 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005897 nv_addsub(cap);
5898 }
5899 else
5900 clearopbeep(oap);
5901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005903 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 case 'R':
5905 cap->arg = TRUE;
5906 nv_Replace(cap);
5907 break;
5908
5909 case 'r':
5910 nv_vreplace(cap);
5911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 case '&':
5914 do_cmdline_cmd((char_u *)"%s//~/&");
5915 break;
5916
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005917 // "gv": Reselect the previous Visual area. If Visual already active,
5918 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005920 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005922
5923 // "gV": Don't reselect the previous Visual area after a Select mode
5924 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 case 'V':
5926 VIsual_reselect = FALSE;
5927 break;
5928
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005929 // "gh": start Select mode.
5930 // "gH": start Select line mode.
5931 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 case K_BS:
5933 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005934 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 case 'h':
5936 case 'H':
5937 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 cap->cmdchar = cap->nchar + ('v' - 'h');
5939 cap->arg = TRUE;
5940 nv_visual(cap);
5941 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005943 // "gn", "gN" visually select next/previous search match
5944 // "gn" selects next match
5945 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005946 case 'N':
5947 case 'n':
5948 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005949 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005952 // "gj" and "gk" two new funny movement keys -- up and down
5953 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 case 'j':
5955 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005956 // with 'nowrap' it works just like the normal "j" command.
5957 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {
5959 oap->motion_type = MLINE;
5960 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5961 }
5962 else
5963 i = nv_screengo(oap, FORWARD, cap->count1);
5964 if (i == FAIL)
5965 clearopbeep(oap);
5966 break;
5967
5968 case 'k':
5969 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005970 // with 'nowrap' it works just like the normal "k" command.
5971 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 oap->motion_type = MLINE;
5974 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5975 }
5976 else
5977 i = nv_screengo(oap, BACKWARD, cap->count1);
5978 if (i == FAIL)
5979 clearopbeep(oap);
5980 break;
5981
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005982 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 case 'J':
5984 nv_join(cap);
5985 break;
5986
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005987 // "g0", "g^" : Like "0" and "^" but for screen lines.
5988 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 case '0':
5991 case 'm':
5992 case K_HOME:
5993 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005994 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 break;
5996
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005997 case 'M':
5998 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005999 oap->motion_type = MCHAR;
6000 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006001 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006002 if (cap->count0 > 0 && cap->count0 <= 100)
6003 coladvance((colnr_T)(i * cap->count0 / 100));
6004 else
6005 coladvance((colnr_T)(i / 2));
6006 curwin->w_set_curswant = TRUE;
6007 }
6008 break;
6009
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006010 // "g_": to the last non-blank character in the line or <count> lines
6011 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006013 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 break;
6015
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006016 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 case '$':
6018 case K_END:
6019 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006020 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 break;
6022
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006023 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 case '*':
6025 case '#':
6026#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006027 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006029 case Ctrl_RSB: // :tag or :tselect for current identifier
6030 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 nv_ident(cap);
6032 break;
6033
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 case 'e':
6036 case 'E':
6037 oap->motion_type = MCHAR;
6038 curwin->w_set_curswant = TRUE;
6039 oap->inclusive = TRUE;
6040 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6041 clearopbeep(oap);
6042 break;
6043
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006044 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006046 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 break;
6048
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006049 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006051 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 break;
6053
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006054 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 case 'I':
6056 beginline(0);
6057 if (!checkclearopq(oap))
6058 invoke_edit(cap, FALSE, 'g', FALSE);
6059 break;
6060
6061#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006062 // "gf": goto file, edit file under cursor
6063 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006065 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 nv_gotofile(cap);
6067 break;
6068#endif
6069
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006070 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 case '\'':
6072 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006073 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 case '`':
6075 nv_gomark(cap);
6076 break;
6077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006080 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 break;
6082
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "ga": Display the ascii value of the character under the
6084 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case 'a':
6086 do_ascii(NULL);
6087 break;
6088
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006089 // "g8": Display the bytes used for the UTF-8 character under the
6090 // cursor. It is displayed in hex.
6091 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006093 if (cap->count0 == 8)
6094 utf_find_illegal();
6095 else
6096 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006099 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006100 case '<':
6101 show_sb_text();
6102 break;
6103
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006104 // "gg": Goto the first line in file. With a count it goes to
6105 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 case 'g':
6107 cap->arg = FALSE;
6108 nv_goto(cap);
6109 break;
6110
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006111 // Two-character operators:
6112 // "gq" Format text
6113 // "gw" Format text and keep cursor position
6114 // "g~" Toggle the case of the text.
6115 // "gu" Change text to lower case.
6116 // "gU" Change text to upper case.
6117 // "g?" rot13 encoding
6118 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 case 'q':
6120 case 'w':
6121 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006122 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 case '~':
6124 case 'u':
6125 case 'U':
6126 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006127 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 nv_operator(cap);
6129 break;
6130
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006131 // "gd": Find first occurrence of pattern under the cursor in the
6132 // current function
6133 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 case 'd':
6135 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006136 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 break;
6138
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006139 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 case K_MIDDLEMOUSE:
6141 case K_MIDDLEDRAG:
6142 case K_MIDDLERELEASE:
6143 case K_LEFTMOUSE:
6144 case K_LEFTDRAG:
6145 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006146 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 case K_RIGHTMOUSE:
6148 case K_RIGHTDRAG:
6149 case K_RIGHTRELEASE:
6150 case K_X1MOUSE:
6151 case K_X1DRAG:
6152 case K_X1RELEASE:
6153 case K_X2MOUSE:
6154 case K_X2DRAG:
6155 case K_X2RELEASE:
6156 mod_mask = MOD_MASK_CTRL;
6157 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 case K_IGNORE:
6161 break;
6162
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006163 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 case 'p':
6165 case 'P':
6166 nv_put(cap);
6167 break;
6168
6169#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006170 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 case 'o':
6172 goto_byte(cap->count0);
6173 break;
6174#endif
6175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006176 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006178 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {
6180 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006181 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 break;
6183 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (!checkclearopq(oap))
6186 do_exmode(TRUE);
6187 break;
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case ',':
6190 nv_pcmark(cap);
6191 break;
6192
6193 case ';':
6194 cap->count1 = -cap->count1;
6195 nv_pcmark(cap);
6196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006199 if (!checkclearop(oap))
6200 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006202 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006203 if (!checkclearop(oap))
6204 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006205 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206
Bram Moolenaar62a23252020-08-09 14:04:42 +02006207 case TAB:
6208 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6209 clearopbeep(oap);
6210 break;
6211
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006212 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006213 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006214 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006215 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006216 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006217 break;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 default:
6220 clearopbeep(oap);
6221 break;
6222 }
6223}
6224
6225/*
6226 * Handle "o" and "O" commands.
6227 */
6228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006229n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006231#ifdef FEAT_CONCEAL
6232 linenr_T oldline = curwin->w_cursor.lnum;
6233#endif
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (!checkclearopq(cap->oap))
6236 {
6237#ifdef FEAT_FOLDING
6238 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006239 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 (void)hasFolding(curwin->w_cursor.lnum,
6241 &curwin->w_cursor.lnum, NULL);
6242 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006243 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 (void)hasFolding(curwin->w_cursor.lnum,
6245 NULL, &curwin->w_cursor.lnum);
6246#endif
6247 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6248 (cap->cmdchar == 'O' ? 1 : 0)),
6249 (linenr_T)(curwin->w_cursor.lnum +
6250 (cap->cmdchar == 'o' ? 1 : 0))
6251 ) == OK
6252 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006253 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006254 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006256#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006257 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006258 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006259#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006260#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006261 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006262 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006263 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006264#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006265 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006266 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6267 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6269 }
6270 }
6271}
6272
6273/*
6274 * "." command: redo last change.
6275 */
6276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006277nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 if (!checkclearopq(cap->oap))
6280 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006281 // If "restart_edit" is TRUE, the last but one command is repeated
6282 // instead of the last command (inserting text). This is used for
6283 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6285 clearopbeep(cap->oap);
6286 }
6287}
6288
6289/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006290 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 */
6292 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006293nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006295 if (VIsual_select && VIsual_active)
6296 {
6297 int reg;
6298 // Get register name
6299 ++no_mapping;
6300 ++allow_keys;
6301 reg = plain_vgetc();
6302 LANGMAP_ADJUST(reg, TRUE);
6303 --no_mapping;
6304 --allow_keys;
6305
6306 if (reg == '"')
6307 // the unnamed register is 0
6308 reg = 0;
6309
6310 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6311 return;
6312 }
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (!checkclearopq(cap->oap))
6315 {
6316 u_redo((int)cap->count1);
6317 curwin->w_set_curswant = TRUE;
6318 }
6319}
6320
6321/*
6322 * Handle "U" command.
6323 */
6324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006325nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006327 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006328 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006330 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 cap->cmdchar = 'g';
6332 cap->nchar = 'U';
6333 nv_operator(cap);
6334 }
6335 else if (!checkclearopq(cap->oap))
6336 {
6337 u_undoline();
6338 curwin->w_set_curswant = TRUE;
6339 }
6340}
6341
6342/*
6343 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6344 * single character.
6345 */
6346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006347nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006349 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006350 {
6351#ifdef FEAT_JOB_CHANNEL
6352 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6353 {
6354 clearopbeep(cap->oap);
6355 return;
6356 }
6357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 else
6361 nv_operator(cap);
6362}
6363
6364/*
6365 * Handle an operator command.
6366 * The actual work is done by do_pending_operator().
6367 */
6368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006369nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 int op_type;
6372
6373 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006374#ifdef FEAT_JOB_CHANNEL
6375 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6376 {
6377 clearopbeep(cap->oap);
6378 return;
6379 }
6380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006382 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 nv_lineop(cap);
6384 else if (!checkclearop(cap->oap))
6385 {
6386 cap->oap->start = curwin->w_cursor;
6387 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006388#ifdef FEAT_EVAL
6389 set_op_var(op_type);
6390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392}
6393
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006394#ifdef FEAT_EVAL
6395/*
6396 * Set v:operator to the characters for "optype".
6397 */
6398 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006399set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006400{
6401 char_u opchars[3];
6402
6403 if (optype == OP_NOP)
6404 set_vim_var_string(VV_OP, NULL, 0);
6405 else
6406 {
6407 opchars[0] = get_op_char(optype);
6408 opchars[1] = get_extra_op_char(optype);
6409 opchars[2] = NUL;
6410 set_vim_var_string(VV_OP, opchars, -1);
6411 }
6412}
6413#endif
6414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415/*
6416 * Handle linewise operator "dd", "yy", etc.
6417 *
6418 * "_" is is a strange motion command that helps make operators more logical.
6419 * It is actually implemented, but not documented in the real Vi. This motion
6420 * command actually refers to "the current line". Commands like "dd" and "yy"
6421 * are really an alternate form of "d_" and "y_". It does accept a count, so
6422 * "d3_" works to delete 3 lines.
6423 */
6424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006425nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 cap->oap->motion_type = MLINE;
6428 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6429 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006430 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006431 && cap->oap->motion_force != 'v'
6432 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 || cap->oap->op_type == OP_LSHIFT
6434 || cap->oap->op_type == OP_RSHIFT)
6435 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006436 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 beginline(BL_WHITE | BL_FIX);
6438}
6439
6440/*
6441 * <Home> command.
6442 */
6443 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006444nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006446 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006447 if (mod_mask & MOD_MASK_CTRL)
6448 nv_goto(cap);
6449 else
6450 {
6451 cap->count0 = 1;
6452 nv_pipe(cap);
6453 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006454 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6455 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456}
6457
6458/*
6459 * "|" command.
6460 */
6461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006462nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 cap->oap->motion_type = MCHAR;
6465 cap->oap->inclusive = FALSE;
6466 beginline(0);
6467 if (cap->count0 > 0)
6468 {
6469 coladvance((colnr_T)(cap->count0 - 1));
6470 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6471 }
6472 else
6473 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006474 // keep curswant at the column where we wanted to go, not where
6475 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 curwin->w_set_curswant = FALSE;
6477}
6478
6479/*
6480 * Handle back-word command "b" and "B".
6481 * cap->arg is 1 for "B"
6482 */
6483 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006484nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
6486 cap->oap->motion_type = MCHAR;
6487 cap->oap->inclusive = FALSE;
6488 curwin->w_set_curswant = TRUE;
6489 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6490 clearopbeep(cap->oap);
6491#ifdef FEAT_FOLDING
6492 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6493 foldOpenCursor();
6494#endif
6495}
6496
6497/*
6498 * Handle word motion commands "e", "E", "w" and "W".
6499 * cap->arg is TRUE for "E" and "W".
6500 */
6501 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006502nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 int n;
6505 int word_end;
6506 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006507 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006509 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6511 word_end = TRUE;
6512 else
6513 word_end = FALSE;
6514 cap->oap->inclusive = word_end;
6515
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006516 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 if (!word_end && cap->oap->op_type == OP_CHANGE)
6518 {
6519 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006520 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006522 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006524 // Reproduce a funny Vi behaviour: "cw" on a blank only
6525 // changes one character, not all blanks until the start of
6526 // the next word. Only do this when the 'w' flag is included
6527 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6529 {
6530 cap->oap->inclusive = TRUE;
6531 cap->oap->motion_type = MCHAR;
6532 return;
6533 }
6534 }
6535 else
6536 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006537 // This is a little strange. To match what the real Vi does,
6538 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6539 // that we are not on a space or a TAB. This seems impolite
6540 // at first, but it's really more what we mean when we say
6541 // 'cw'.
6542 // Another strangeness: When standing on the end of a word
6543 // "ce" will change until the end of the next word, but "cw"
6544 // will change only one character! This is done by setting
6545 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 cap->oap->inclusive = TRUE;
6547 word_end = TRUE;
6548 flag = TRUE;
6549 }
6550 }
6551 }
6552
6553 cap->oap->motion_type = MCHAR;
6554 curwin->w_set_curswant = TRUE;
6555 if (word_end)
6556 n = end_word(cap->count1, cap->arg, flag, FALSE);
6557 else
6558 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6559
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006560 // Don't leave the cursor on the NUL past the end of line. Unless we
6561 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006562 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006563 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
6565 if (n == FAIL && cap->oap->op_type == OP_NOP)
6566 clearopbeep(cap->oap);
6567 else
6568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570#ifdef FEAT_FOLDING
6571 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6572 foldOpenCursor();
6573#endif
6574 }
6575}
6576
6577/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006578 * Used after a movement command: If the cursor ends up on the NUL after the
6579 * end of the line, may move it back to the last character and make the motion
6580 * inclusive.
6581 */
6582 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006584{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006585 // The cursor cannot remain on the NUL when:
6586 // - the column is > 0
6587 // - not in Visual mode or 'selection' is "o"
6588 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006589 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006590 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006591 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006592 {
6593 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006594 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006595 if (has_mbyte)
6596 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 oap->inclusive = TRUE;
6598 }
6599}
6600
6601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * "0" and "^" commands.
6603 * cap->arg is the argument for beginline().
6604 */
6605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 cap->oap->motion_type = MCHAR;
6609 cap->oap->inclusive = FALSE;
6610 beginline(cap->arg);
6611#ifdef FEAT_FOLDING
6612 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6613 foldOpenCursor();
6614#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006615 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6616 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
6618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619/*
6620 * In exclusive Visual mode, may include the last character.
6621 */
6622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006623adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006626 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 if (has_mbyte)
6629 inc_cursor();
6630 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 ++curwin->w_cursor.col;
6632 cap->oap->inclusive = FALSE;
6633 }
6634}
6635
6636/*
6637 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6638 * Should check VIsual_mode before calling this.
6639 * Returns TRUE when backed up to the previous line.
6640 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006641 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006642unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 pos_T *pp;
6645
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006646 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006648 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 pp = &curwin->w_cursor;
6650 else
6651 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (pp->coladd > 0)
6653 --pp->coladd;
6654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (pp->col > 0)
6656 {
6657 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006658 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 }
6660 else if (pp->lnum > 1)
6661 {
6662 --pp->lnum;
6663 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6664 return TRUE;
6665 }
6666 }
6667 return FALSE;
6668}
6669
6670/*
6671 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6672 */
6673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006674nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006679 VIsual_select_reg = 0;
6680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 else if (VIsual_reselect)
6682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006683 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 cap->arg = TRUE;
6685 nv_g_cmd(cap);
6686 }
6687}
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
6690/*
6691 * "G", "gg", CTRL-END, CTRL-HOME.
6692 * cap->arg is TRUE for "G".
6693 */
6694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006695nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 linenr_T lnum;
6698
6699 if (cap->arg)
6700 lnum = curbuf->b_ml.ml_line_count;
6701 else
6702 lnum = 1L;
6703 cap->oap->motion_type = MLINE;
6704 setpcmark();
6705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006706 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (cap->count0 != 0)
6708 lnum = cap->count0;
6709 if (lnum < 1L)
6710 lnum = 1L;
6711 else if (lnum > curbuf->b_ml.ml_line_count)
6712 lnum = curbuf->b_ml.ml_line_count;
6713 curwin->w_cursor.lnum = lnum;
6714 beginline(BL_SOL | BL_FIX);
6715#ifdef FEAT_FOLDING
6716 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6717 foldOpenCursor();
6718#endif
6719}
6720
6721/*
6722 * CTRL-\ in Normal mode.
6723 */
6724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6728 {
6729 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006730 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006731 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 restart_edit = 0;
6733#ifdef FEAT_CMDWIN
6734 if (cmdwin_type != 0)
6735 cmdwin_result = Ctrl_C;
6736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (VIsual_active)
6738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 redraw_curbuf_later(INVERTED);
6741 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006742 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (cap->nchar == Ctrl_G && p_im)
6744 restart_edit = 'a';
6745 }
6746 else
6747 clearopbeep(cap->oap);
6748}
6749
6750/*
6751 * ESC in Normal mode: beep, but don't flush buffers.
6752 * Don't even beep if we are canceling a command.
6753 */
6754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006755nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 int no_reason;
6758
6759 no_reason = (cap->oap->op_type == OP_NOP
6760 && cap->opcount == 0
6761 && cap->count0 == 0
6762 && cap->oap->regname == 0
6763 && !p_im);
6764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006765 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
6767 if (restart_edit == 0
6768#ifdef FEAT_CMDWIN
6769 && cmdwin_type == 0
6770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006773 {
6774 if (anyBufIsChanged())
6775 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6776 else
6777 msg(_("Type :qa and press <Enter> to exit Vim"));
6778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006780 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6781 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (!p_im)
6783 restart_edit = 0;
6784#ifdef FEAT_CMDWIN
6785 if (cmdwin_type != 0)
6786 {
6787 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006788 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 return;
6790 }
6791#endif
6792 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006793#ifdef FEAT_CMDWIN
6794 else if (cmdwin_type != 0 && ex_normal_busy)
6795 {
6796 // When :normal runs out of characters while in the command line window
6797 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6798 // loop.
6799 cmdwin_result = K_IGNORE;
6800 return;
6801 }
6802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (VIsual_active)
6805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006806 end_visual_mode(); // stop Visual
6807 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 curwin->w_set_curswant = TRUE;
6809 redraw_curbuf_later(INVERTED);
6810 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006811 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006812 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 clearop(cap->oap);
6814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006815 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6816 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006817 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 restart_edit = 'a';
6819}
6820
6821/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006822 * Move the cursor for the "A" command.
6823 */
6824 void
6825set_cursor_for_append_to_line(void)
6826{
6827 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006828 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006829 {
6830 int save_State = State;
6831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006832 // Pretend Insert mode here to allow the cursor on the
6833 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006834 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006835 coladvance((colnr_T)MAXCOL);
6836 State = save_State;
6837 }
6838 else
6839 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6840}
6841
6842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006844 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 */
6846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006847nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006849 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6851 cap->cmdchar = 'i';
6852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006853 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006855 {
6856#ifdef FEAT_TERMINAL
6857 if (term_in_normal_mode())
6858 {
6859 end_visual_mode();
6860 clearop(cap->oap);
6861 term_enter_job_mode();
6862 return;
6863 }
6864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006869 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6870 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
6872#ifdef FEAT_TEXTOBJ
6873 nv_object(cap);
6874#else
6875 clearopbeep(cap->oap);
6876#endif
6877 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006878#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006879 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006880 {
6881 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006882 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006883 return;
6884 }
6885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 else if (!curbuf->b_p_ma && !p_im)
6887 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006888 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006889 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006891 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006892 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006893 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006895 else if (cap->cmdchar == K_PS && VIsual_active)
6896 {
6897 pos_T old_pos = curwin->w_cursor;
6898 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006899 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006902 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6903 {
6904 shift_delete_registers();
6905 cap->oap->regname = '1';
6906 }
6907 else
6908 cap->oap->regname = '-';
6909 cap->cmdchar = 'd';
6910 cap->nchar = NUL;
6911 nv_operator(cap);
6912 do_pending_operator(cap, 0, FALSE);
6913 cap->cmdchar = K_PS;
6914
zeertzjq7a732522022-03-14 20:46:41 +00006915 if (*ml_get_cursor() != NUL)
6916 {
6917 if (old_visual_mode == 'V')
6918 {
6919 // In linewise Visual mode insert before the beginning of the
6920 // next line.
6921 // When the last line in the buffer was deleted then create a
6922 // new line, otherwise there is not need to move cursor.
6923 // Detect this by checking if cursor moved above Visual area.
6924 if (curwin->w_cursor.lnum < old_pos.lnum
6925 && curwin->w_cursor.lnum < old_visual.lnum)
6926 {
6927 if (u_save_cursor() == OK)
6928 {
6929 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6930 FALSE);
6931 appended_lines(curwin->w_cursor.lnum++, 1L);
6932 }
6933 }
6934 }
6935 // When the last char in the line was deleted then append.
6936 // Detect this by checking if cursor moved before Visual area.
6937 else if (curwin->w_cursor.col < old_pos.col
6938 && curwin->w_cursor.col < old_visual.col)
6939 inc_cursor();
6940 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006942 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006943 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 else if (!checkclearopq(cap->oap))
6946 {
6947 switch (cap->cmdchar)
6948 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006949 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006950 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 break;
6952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006954 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6955 beginline(BL_WHITE);
6956 else
6957 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 break;
6959
Bram Moolenaara1891842017-02-04 21:34:31 +01006960 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006961 // Bracketed paste works like "a"ppend, unless the cursor is in
6962 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006963 if (curwin->w_cursor.col == 0)
6964 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006965 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 case 'a': // "a"ppend is like "i"nsert on the next character.
6968 // increment coladd when in virtual space, increment the
6969 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (virtual_active()
6971 && (curwin->w_cursor.coladd > 0
6972 || *ml_get_cursor() == NUL
6973 || *ml_get_cursor() == TAB))
6974 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006975 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 inc_cursor();
6977 break;
6978 }
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6981 {
6982 int save_State = State;
6983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006984 // Pretend Insert mode here to allow the cursor on the
6985 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006986 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 coladvance(getviscol());
6988 State = save_State;
6989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6992 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01006993 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006995 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996}
6997
6998/*
6999 * Invoke edit() and take care of "restart_edit" and the return value.
7000 */
7001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007002invoke_edit(
7003 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007004 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007005 int cmd,
7006 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 int restart_edit_save = 0;
7009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007010 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7011 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7012 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (repl || !stuff_empty())
7014 restart_edit_save = restart_edit;
7015 else
7016 restart_edit_save = 0;
7017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007018 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 restart_edit = 0;
7020
7021 if (edit(cmd, startln, cap->count1))
7022 cap->retval |= CA_COMMAND_BUSY;
7023
7024 if (restart_edit == 0)
7025 restart_edit = restart_edit_save;
7026}
7027
7028#ifdef FEAT_TEXTOBJ
7029/*
7030 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7031 */
7032 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007033nv_object(
7034 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 int flag;
7037 int include;
7038 char_u *mps_save;
7039
7040 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007041 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007043 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007045 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 mps_save = curbuf->b_p_mps;
7047 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7048
7049 switch (cap->nchar)
7050 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 flag = current_word(cap->oap, cap->count1, include, FALSE);
7053 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 flag = current_word(cap->oap, cap->count1, include, TRUE);
7056 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 case '(':
7059 case ')':
7060 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7061 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 case '{':
7064 case '}':
7065 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7066 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 case ']':
7069 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7070 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007071 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 case '>':
7073 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7074 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 case 't': // "at" = a tag block (xml and html)
7076 // Do not adjust oap->end in do_pending_operator()
7077 // otherwise there are different results for 'dit'
7078 // (note leading whitespace in last line):
7079 // 1) <b> 2) <b>
7080 // foobar foobar
7081 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007082 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007083 flag = current_tagblock(cap->oap, cap->count1, include);
7084 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007085 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 flag = current_par(cap->oap, cap->count1, include, 'p');
7087 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 flag = current_sent(cap->oap, cap->count1, include);
7090 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007091 case '"': // "a"" = a double quoted string
7092 case '\'': // "a'" = a single quoted string
7093 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007094 flag = current_quote(cap->oap, cap->count1, include,
7095 cap->nchar);
7096 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097#if 0 // TODO
7098 case 'S': // "aS" = a section
7099 case 'f': // "af" = a filename
7100 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101#endif
7102 default:
7103 flag = FAIL;
7104 break;
7105 }
7106
7107 curbuf->b_p_mps = mps_save;
7108 if (flag == FAIL)
7109 clearopbeep(cap->oap);
7110 adjust_cursor_col();
7111 curwin->w_set_curswant = TRUE;
7112}
7113#endif
7114
7115/*
7116 * "q" command: Start/stop recording.
7117 * "q:", "q/", "q?": edit command-line in command-line window.
7118 */
7119 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007120nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 if (cap->oap->op_type == OP_FORMAT)
7123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 cap->cmdchar = 'g';
7126 cap->nchar = 'q';
7127 nv_operator(cap);
7128 }
7129 else if (!checkclearop(cap->oap))
7130 {
7131#ifdef FEAT_CMDWIN
7132 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7133 {
7134 stuffcharReadbuff(cap->nchar);
7135 stuffcharReadbuff(K_CMDWIN);
7136 }
7137 else
7138#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007139 // (stop) recording into a named register, unless executing a
7140 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007141 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 clearopbeep(cap->oap);
7143 }
7144}
7145
7146/*
7147 * Handle the "@r" command.
7148 */
7149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007150nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 if (checkclearop(cap->oap))
7153 return;
7154#ifdef FEAT_EVAL
7155 if (cap->nchar == '=')
7156 {
7157 if (get_expr_register() == NUL)
7158 return;
7159 }
7160#endif
7161 while (cap->count1-- && !got_int)
7162 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007163 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
7165 clearopbeep(cap->oap);
7166 break;
7167 }
7168 line_breakcheck();
7169 }
7170}
7171
7172/*
7173 * Handle the CTRL-U and CTRL-D commands.
7174 */
7175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007176nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7179 || (cap->cmdchar == Ctrl_D
7180 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7181 clearopbeep(cap->oap);
7182 else if (!checkclearop(cap->oap))
7183 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7184}
7185
7186/*
7187 * Handle "J" or "gJ" command.
7188 */
7189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007190nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007192 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007194 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
7196 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7199 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007201 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007202 if (cap->count0 <= 2)
7203 {
7204 clearopbeep(cap->oap);
7205 return;
7206 }
7207 cap->count0 = curbuf->b_ml.ml_line_count
7208 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007210
7211 prep_redo(cap->oap->regname, cap->count0,
7212 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7213 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215}
7216
7217/*
7218 * "P", "gP", "p" and "gp" commands.
7219 */
7220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007221nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007223 nv_put_opt(cap, FALSE);
7224}
7225
7226/*
7227 * "P", "gP", "p" and "gp" commands.
7228 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7229 */
7230 static void
7231nv_put_opt(cmdarg_T *cap, int fix_indent)
7232{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 int regname = 0;
7234 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007235 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007236 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 int dir;
7238 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007239 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241 if (cap->oap->op_type != OP_NOP)
7242 {
7243#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007244 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7246 {
7247 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007248 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 }
7250 else
7251#endif
7252 clearopbeep(cap->oap);
7253 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007254#ifdef FEAT_JOB_CHANNEL
7255 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7256 {
7257 clearopbeep(cap->oap);
7258 }
7259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 else
7261 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007262 if (fix_indent)
7263 {
7264 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7265 ? FORWARD : BACKWARD;
7266 flags |= PUT_FIXINDENT;
7267 }
7268 else
7269 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007270 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7271 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 prep_redo_cmd(cap);
7273 if (cap->cmdchar == 'g')
7274 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007275 else if (cap->cmdchar == 'z')
7276 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 if (VIsual_active)
7279 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007280 // Putting in Visual mode: The put text replaces the selected
7281 // text. First delete the selected text, then put the new text.
7282 // Need to save and restore the registers that the delete
7283 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007284 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007286 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007287#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007289#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007290 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007291 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007292#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
7296 )
7297 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007298 // The delete is going to overwrite the register we want to
7299 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 reg1 = get_register(regname, TRUE);
7301 }
7302
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007303 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 cap->cmdchar = 'd';
7305 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007306 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007307 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 nv_operator(cap);
7309 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007310 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007311 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007313 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 cap->oap->regname = regname;
7315
7316 if (reg1 != NULL)
7317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007318 // Delete probably changed the register we want to put, save
7319 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 reg2 = get_register(regname, FALSE);
7321 put_register(regname, reg1);
7322 }
7323
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007324 // When deleted a linewise Visual area, put the register as
7325 // lines to avoid it joined with the next line. When deletion was
7326 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 if (VIsual_mode == 'V')
7328 flags |= PUT_LINE;
7329 else if (VIsual_mode == 'v')
7330 flags |= PUT_LINE_SPLIT;
7331 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7332 flags |= PUT_LINE_FORWARD;
7333 dir = BACKWARD;
7334 if ((VIsual_mode != 'V'
7335 && curwin->w_cursor.col < curbuf->b_op_start.col)
7336 || (VIsual_mode == 'V'
7337 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 // cursor is at the end of the line or end of file, put
7339 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007341 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007342 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007344 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007346 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (reg2 != NULL)
7348 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007350 // What to reselect with "gv"? Selecting the just put text seems to
7351 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007352 if (was_visual)
7353 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007354 curbuf->b_visual.vi_start = curbuf->b_op_start;
7355 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007356 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007357 if (*p_sel == 'e')
7358 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007359 }
7360
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // When all lines were selected and deleted do_put() leaves an empty
7362 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007363 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007364 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007365 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007366 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // If the cursor was in that line, move it to the end of the last
7369 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007370 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7371 {
7372 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7373 coladvance((colnr_T)MAXCOL);
7374 }
7375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 auto_format(FALSE, TRUE);
7377 }
7378}
7379
7380/*
7381 * "o" and "O" commands.
7382 */
7383 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007384nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007387 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7389 {
7390 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007391 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 }
7393 else
7394#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007395 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007397#ifdef FEAT_JOB_CHANNEL
7398 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007399 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 n_opencmd(cap);
7403}
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405#ifdef FEAT_NETBEANS_INTG
7406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007407nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409 netbeans_keycommand(cap->nchar);
7410}
7411#endif
7412
7413#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007415nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007417 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007420
Bram Moolenaar3918c952005-03-15 22:34:55 +00007421/*
7422 * Trigger CursorHold event.
7423 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7424 * input buffer. "did_cursorhold" is set to avoid retriggering.
7425 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007427nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007428{
7429 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7430 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007431 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007432}