blob: 53c50dc8b368c54d2ee15694201ca64b184dc2d2 [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/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100187 * If currently editing a cmdline or text is locked: beep and give an error
188 * message, return TRUE.
189 */
190 static int
191check_text_locked(oparg_T *oap)
192{
193 if (text_locked())
194 {
195 clearopbeep(oap);
196 text_locked_msg();
197 return TRUE;
198 }
199 return FALSE;
200}
201
202/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000203 * Handle the count before a normal command and set cap->count0.
204 */
205 static int
206normal_cmd_get_count(
207 cmdarg_T *cap,
208 int c,
209 int toplevel UNUSED,
210 int set_prevcount UNUSED,
211 int *ctrl_w,
212 int *need_flushbuf UNUSED)
213{
214getcount:
215 if (!(VIsual_active && VIsual_select))
216 {
217 // Handle a count before a command and compute ca.count0.
218 // Note that '0' is a command and not the start of a count, but it's
219 // part of a count after other digits.
220 while ((c >= '1' && c <= '9')
221 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
222 || c == '0')))
223 {
224 if (c == K_DEL || c == K_KDEL)
225 {
226 cap->count0 /= 10;
227#ifdef FEAT_CMDL_INFO
228 del_from_showcmd(4); // delete the digit and ~@%
229#endif
230 }
231 else if (cap->count0 > 99999999L)
232 {
233 cap->count0 = 999999999L;
234 }
235 else
236 {
237 cap->count0 = cap->count0 * 10 + (c - '0');
238 }
239#ifdef FEAT_EVAL
240 // Set v:count here, when called from main() and not a stuffed
241 // command, so that v:count can be used in an expression mapping
242 // right after the count. Do set it for redo.
243 if (toplevel && readbuf1_empty())
244 set_vcount_ca(cap, &set_prevcount);
245#endif
246 if (*ctrl_w)
247 {
248 ++no_mapping;
249 ++allow_keys; // no mapping for nchar, but keys
250 }
251 ++no_zero_mapping; // don't map zero here
252 c = plain_vgetc();
253 LANGMAP_ADJUST(c, TRUE);
254 --no_zero_mapping;
255 if (*ctrl_w)
256 {
257 --no_mapping;
258 --allow_keys;
259 }
260#ifdef FEAT_CMDL_INFO
261 *need_flushbuf |= add_to_showcmd(c);
262#endif
263 }
264
265 // If we got CTRL-W there may be a/another count
266 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
267 {
268 *ctrl_w = TRUE;
269 cap->opcount = cap->count0; // remember first count
270 cap->count0 = 0;
271 ++no_mapping;
272 ++allow_keys; // no mapping for nchar, but keys
273 c = plain_vgetc(); // get next character
274 LANGMAP_ADJUST(c, TRUE);
275 --no_mapping;
276 --allow_keys;
277#ifdef FEAT_CMDL_INFO
278 *need_flushbuf |= add_to_showcmd(c);
279#endif
280 goto getcount; // jump back
281 }
282 }
283
284 if (c == K_CURSORHOLD)
285 {
286 // Save the count values so that ca.opcount and ca.count0 are exactly
287 // the same when coming back here after handling K_CURSORHOLD.
288 cap->oap->prev_opcount = cap->opcount;
289 cap->oap->prev_count0 = cap->count0;
290 }
291 else if (cap->opcount != 0)
292 {
293 // If we're in the middle of an operator (including after entering a
294 // yank buffer with '"') AND we had a count before the operator, then
295 // that count overrides the current value of ca.count0.
296 // What this means effectively, is that commands like "3dw" get turned
297 // into "d3w" which makes things fall into place pretty neatly.
298 // If you give a count before AND after the operator, they are
299 // multiplied.
300 if (cap->count0)
301 {
302 if (cap->opcount >= 999999999L / cap->count0)
303 cap->count0 = 999999999L;
304 else
305 cap->count0 *= cap->opcount;
306 }
307 else
308 cap->count0 = cap->opcount;
309 }
310
311 // Always remember the count. It will be set to zero (on the next call,
312 // above) when there is no pending operator.
313 // When called from main(), save the count for use by the "count" built-in
314 // variable.
315 cap->opcount = cap->count0;
316 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
317
318#ifdef FEAT_EVAL
319 // Only set v:count when called from main() and not a stuffed command.
320 // Do set it for redo.
321 if (toplevel && readbuf1_empty())
322 set_vcount(cap->count0, cap->count1, set_prevcount);
323#endif
324
325 return c;
326}
327
328/*
329 * Returns TRUE if the normal command (cap) needs a second character.
330 */
331 static int
332normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
333{
334 return ((cmd_flags & NV_NCH)
335 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
336 && cap->oap->op_type == OP_NOP)
337 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
338 || (cap->cmdchar == 'q'
339 && cap->oap->op_type == OP_NOP
340 && reg_recording == 0
341 && reg_executing == 0)
342 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
343 && (cap->oap->op_type != OP_NOP || VIsual_active))));
344}
345
346/*
347 * Get one or more additional characters for a normal command.
348 * Return the updated command index (if changed).
349 */
350 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000351normal_cmd_get_more_chars(
352 int idx_arg,
353 cmdarg_T *cap,
354 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000355{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000356 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000357 int c;
358 int *cp;
359 int repl = FALSE; // get character for replace mode
360 int lit = FALSE; // get extra character literally
361 int langmap_active = FALSE; // using :lmap mappings
362 int lang; // getting a text character
363#ifdef HAVE_INPUT_METHOD
364 int save_smd; // saved value of p_smd
365#endif
366
367 ++no_mapping;
368 ++allow_keys; // no mapping for nchar, but allow key codes
369 // Don't generate a CursorHold event here, most commands can't handle
370 // it, e.g., nv_replace(), nv_csearch().
371 did_cursorhold = TRUE;
372 if (cap->cmdchar == 'g')
373 {
374 /*
375 * For 'g' get the next character now, so that we can check for
376 * "gr", "g'" and "g`".
377 */
378 cap->nchar = plain_vgetc();
379 LANGMAP_ADJUST(cap->nchar, TRUE);
380#ifdef FEAT_CMDL_INFO
381 *need_flushbuf |= add_to_showcmd(cap->nchar);
382#endif
383 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
384 || cap->nchar == Ctrl_BSL)
385 {
386 cp = &cap->extra_char; // need to get a third character
387 if (cap->nchar != 'r')
388 lit = TRUE; // get it literally
389 else
390 repl = TRUE; // get it in replace mode
391 }
392 else
393 cp = NULL; // no third character needed
394 }
395 else
396 {
397 if (cap->cmdchar == 'r') // get it in replace mode
398 repl = TRUE;
399 cp = &cap->nchar;
400 }
401 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
402
403 /*
404 * Get a second or third character.
405 */
406 if (cp != NULL)
407 {
408 if (repl)
409 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100410 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000411#ifdef CURSOR_SHAPE
412 ui_cursor_shape(); // show different cursor shape
413#endif
414 }
415 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
416 {
417 // Allow mappings defined with ":lmap".
418 --no_mapping;
419 --allow_keys;
420 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100421 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000422 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100423 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000424 langmap_active = TRUE;
425 }
426#ifdef HAVE_INPUT_METHOD
427 save_smd = p_smd;
428 p_smd = FALSE; // Don't let the IM code show the mode here
429 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
430 im_set_active(TRUE);
431#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100432 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000433 {
434#ifdef FEAT_JOB_CHANNEL
435 ch_log_output = TRUE;
436#endif
437 // Disable bracketed paste and modifyOtherKeys here, we won't
438 // recognize the escape sequences with 'esckeys' off.
439 out_str(T_BD);
440 out_str(T_CTE);
441 }
442
443 *cp = plain_vgetc();
444
Bram Moolenaar24959102022-05-07 20:01:16 +0100445 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000446 {
447#ifdef FEAT_JOB_CHANNEL
448 ch_log_output = TRUE;
449#endif
450 // Re-enable bracketed paste mode and modifyOtherKeys
451 out_str(T_BE);
452 out_str(T_CTI);
453 }
454
455 if (langmap_active)
456 {
457 // Undo the decrement done above
458 ++no_mapping;
459 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100460 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000461 }
462#ifdef HAVE_INPUT_METHOD
463 if (lang)
464 {
465 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
466 im_save_status(&curbuf->b_p_iminsert);
467 im_set_active(FALSE);
468 }
469 p_smd = save_smd;
470#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100471 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000472#ifdef FEAT_CMDL_INFO
473 *need_flushbuf |= add_to_showcmd(*cp);
474#endif
475
476 if (!lit)
477 {
478#ifdef FEAT_DIGRAPHS
479 // Typing CTRL-K gets a digraph.
480 if (*cp == Ctrl_K
481 && ((nv_cmds[idx].cmd_flags & NV_LANG)
482 || cp == &cap->extra_char)
483 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
484 {
485 c = get_digraph(FALSE);
486 if (c > 0)
487 {
488 *cp = c;
489# ifdef FEAT_CMDL_INFO
490 // Guessing how to update showcmd here...
491 del_from_showcmd(3);
492 *need_flushbuf |= add_to_showcmd(*cp);
493# endif
494 }
495 }
496#endif
497
498 // adjust chars > 127, except after "tTfFr" commands
499 LANGMAP_ADJUST(*cp, !lang);
500#ifdef FEAT_RIGHTLEFT
501 // adjust Hebrew mapped char
502 if (p_hkmap && lang && KeyTyped)
503 *cp = hkmap(*cp);
504#endif
505 }
506
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000507 // When the next character is CTRL-\ a following CTRL-N means the
508 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000509 if (cp == &cap->extra_char
510 && cap->nchar == Ctrl_BSL
511 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
512 {
513 cap->cmdchar = Ctrl_BSL;
514 cap->nchar = cap->extra_char;
515 idx = find_command(cap->cmdchar);
516 }
517 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
518 cap->oap->op_type = get_op_type(*cp, NUL);
519 else if (*cp == Ctrl_BSL)
520 {
521 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
522
523 // There is a busy wait here when typing "f<C-\>" and then
524 // something different from CTRL-N. Can't be avoided.
525 while ((c = vpeekc()) <= 0 && towait > 0L)
526 {
527 do_sleep(towait > 50L ? 50L : towait, FALSE);
528 towait -= 50L;
529 }
530 if (c > 0)
531 {
532 c = plain_vgetc();
533 if (c != Ctrl_N && c != Ctrl_G)
534 vungetc(c);
535 else
536 {
537 cap->cmdchar = Ctrl_BSL;
538 cap->nchar = c;
539 idx = find_command(cap->cmdchar);
540 }
541 }
542 }
543
544 // When getting a text character and the next character is a
545 // multi-byte character, it could be a composing character.
546 // However, don't wait for it to arrive. Also, do enable mapping,
547 // because if it's put back with vungetc() it's too late to apply
548 // mapping.
549 --no_mapping;
550 while (enc_utf8 && lang && (c = vpeekc()) > 0
551 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
552 {
553 c = plain_vgetc();
554 if (!utf_iscomposing(c))
555 {
556 vungetc(c); // it wasn't, put it back
557 break;
558 }
559 else if (cap->ncharC1 == 0)
560 cap->ncharC1 = c;
561 else
562 cap->ncharC2 = c;
563 }
564 ++no_mapping;
565 }
566 --no_mapping;
567 --allow_keys;
568
569 return idx;
570}
571
572/*
573 * Returns TRUE if after processing a normal mode command, need to wait for a
574 * moment when a message is displayed that will be overwritten by the mode
575 * message.
576 */
577 static int
578normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
579{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000580 // In Visual mode and with "^O" in Insert mode, a short message will be
581 // overwritten by the mode message. Wait a bit, until a key is hit.
582 // In Visual mode, it's more important to keep the Visual area updated
583 // than keeping a message (e.g. from a /pat search).
584 // Only do this if the command was typed, not from a mapping.
585 // Don't wait when emsg_silent is non-zero.
586 // Also wait a bit after an error message, e.g. for "^O:".
587 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000588 return ( ((p_smd
589 && msg_silent == 0
590 && (restart_edit != 0
591 || (VIsual_active
592 && old_pos->lnum == curwin->w_cursor.lnum
593 && old_pos->col == curwin->w_cursor.col)
594 )
595 && (clear_cmdline
596 || redraw_cmdline)
597 && (msg_didout || (msg_didany && msg_scroll))
598 && !msg_nowait
599 && KeyTyped)
600 || (restart_edit != 0
601 && !VIsual_active
602 && (msg_scroll
603 || emsg_on_display)))
604 && cap->oap->regname == 0
605 && !(cap->retval & CA_COMMAND_BUSY)
606 && stuff_empty()
607 && typebuf_typed()
608 && emsg_silent == 0
609 && !in_assert_fails
610 && !did_wait_return
611 && cap->oap->op_type == OP_NOP);
612}
613
614/*
615 * After processing a normal mode command, wait for a moment when a message is
616 * displayed that will be overwritten by the mode message.
617 */
618 static void
619normal_cmd_wait_for_msg(void)
620{
621 int save_State = State;
622
623 // Draw the cursor with the right shape here
624 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100625 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000626
627 // If need to redraw, and there is a "keep_msg", redraw before the
628 // delay
629 if (must_redraw && keep_msg != NULL && !emsg_on_display)
630 {
631 char_u *kmsg;
632
633 kmsg = keep_msg;
634 keep_msg = NULL;
635 // Showmode() will clear keep_msg, but we want to use it anyway.
636 // First update w_topline.
637 setcursor();
638 update_screen(0);
639 // now reset it, otherwise it's put in the history again
640 keep_msg = kmsg;
641
642 kmsg = vim_strsave(keep_msg);
643 if (kmsg != NULL)
644 {
645 msg_attr((char *)kmsg, keep_msg_attr);
646 vim_free(kmsg);
647 }
648 }
649 setcursor();
650#ifdef CURSOR_SHAPE
651 ui_cursor_shape(); // may show different cursor shape
652#endif
653 cursor_on();
654 out_flush();
655 if (msg_scroll || emsg_on_display)
656 ui_delay(1003L, TRUE); // wait at least one second
657 ui_delay(3003L, FALSE); // wait up to three seconds
658 State = save_State;
659
660 msg_scroll = FALSE;
661 emsg_on_display = FALSE;
662}
663
664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 * Execute a command in Normal mode.
666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668normal_cmd(
669 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000676 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 static int old_mapped_len = 0;
680 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000681 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200682 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaara80faa82020-04-12 19:37:17 +0200684 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000686
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 // Use a count remembered from before entering an operator. After typing
688 // "3d" we return from normal_cmd() and come back here, the "3" is
689 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ca.opcount = opcount;
691
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000692 // If there is an operator pending, then the command we take this time
693 // will terminate it. Finish_op tells us to finish the operation before
694 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef CURSOR_SHAPE
696 c = finish_op;
697#endif
698 finish_op = (oap->op_type != OP_NOP);
699#ifdef CURSOR_SHAPE
700 if (finish_op != c)
701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703# ifdef FEAT_MOUSESHAPE
704 update_mouseshape(-1);
705# endif
706 }
707#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100708 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100710 // When not finishing an operator and no register name typed, reset the
711 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000715#ifdef FEAT_EVAL
716 set_prevcount = TRUE;
717#endif
718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // Restore counts from before receiving K_CURSORHOLD. This means after
721 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
722 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000723 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
724 {
725 ca.opcount = oap->prev_opcount;
726 ca.count0 = oap->prev_count0;
727 oap->prev_opcount = 0;
728 oap->prev_count0 = 0;
729 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar24959102022-05-07 20:01:16 +0100733 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
737
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100738#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 // Set v:count here, when called from main() and not a stuffed
740 // command, so that v:count can be used in an expression mapping
741 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100742 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100743 set_vcount_ca(&ca, &set_prevcount);
744#endif
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /*
747 * Get the command character from the user.
748 */
749 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100750 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000752 // If a mapping was started in Visual or Select mode, remember the length
753 // of the mapping. This is used below to not return to Insert mode for as
754 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (restart_edit == 0)
756 old_mapped_len = 0;
757 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000758 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 old_mapped_len = typebuf_maplen();
760
761 if (c == NUL)
762 c = K_ZERO;
763
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000764 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (VIsual_active
766 && VIsual_select
767 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
768 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000769 int len;
770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
772 // 'insertmode' is set) fake a "d"elete command, Insert mode will
773 // restart automatically.
774 // Insert the typed character in the typeahead buffer, so that it can
775 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000776 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
777
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000778 // When recording and gotchars() was called the character will be
779 // recorded again, remove the previous recording.
780 if (KeyTyped)
781 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000782
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000783 if (restart_edit != 0)
784 c = 'd';
785 else
786 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100787 msg_nowait = TRUE; // don't delay going to insert mode
788 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000791 // If the window was made so small that nothing shows, make it at least one
792 // line and one column when typing a command.
793 if (KeyTyped && !KeyStuffed)
794 win_ensure_size();
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#ifdef FEAT_CMDL_INFO
797 need_flushbuf = add_to_showcmd(c);
798#endif
799
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000800 // Get the command count
801 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
802 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000804 // Find the command character in the table of commands.
805 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (ctrl_w)
807 {
808 ca.nchar = c;
809 ca.cmdchar = Ctrl_W;
810 }
811 else
812 ca.cmdchar = c;
813 idx = find_command(ca.cmdchar);
814 if (idx < 0)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
818 goto normal_end;
819 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000820
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100821 if ((nv_cmds[idx].cmd_flags & NV_NCW)
822 && (check_text_locked(oap) || curbuf_locked()))
823 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000824 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000826 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (VIsual_active)
828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (km_stopsel
831 && (nv_cmds[idx].cmd_flags & NV_STS)
832 && !(mod_mask & MOD_MASK_SHIFT))
833 {
834 end_visual_mode();
835 redraw_curbuf_later(INVERTED);
836 }
837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (km_startsel)
840 {
841 if (nv_cmds[idx].cmd_flags & NV_SS)
842 {
843 unshift_special(&ca);
844 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000845 if (idx < 0)
846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000848 clearopbeep(oap);
849 goto normal_end;
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
853 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858#ifdef FEAT_RIGHTLEFT
859 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
860 && (nv_cmds[idx].cmd_flags & NV_RL))
861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 // Invert horizontal movements and operations. Only when typed by the
863 // user directly, not when the result of a mapping or "x" translated
864 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 switch (ca.cmdchar)
866 {
867 case 'l': ca.cmdchar = 'h'; break;
868 case K_RIGHT: ca.cmdchar = K_LEFT; break;
869 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
870 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
871 case 'h': ca.cmdchar = 'l'; break;
872 case K_LEFT: ca.cmdchar = K_RIGHT; break;
873 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
874 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
875 case '>': ca.cmdchar = '<'; break;
876 case '<': ca.cmdchar = '>'; break;
877 }
878 idx = find_command(ca.cmdchar);
879 }
880#endif
881
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000882 // Get additional characters if we need them.
883 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
884 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000887 // Flush the showcmd characters onto the screen so we can see them while
888 // the command is being executed. Only do this when the shown command was
889 // actually displayed, otherwise this will slow down a lot when executing
890 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (need_flushbuf)
892 out_flush();
893#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000894 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200895 {
896 if (ex_normal_busy)
897 did_cursorhold = save_did_cursorhold;
898 else
899 did_cursorhold = FALSE;
900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaar24959102022-05-07 20:01:16 +0100902 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904 if (ca.nchar == ESC)
905 {
906 clearop(oap);
907 if (restart_edit == 0 && goto_im())
908 restart_edit = 'a';
909 goto normal_end;
910 }
911
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000912 if (ca.cmdchar != K_IGNORE)
913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100914 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000915 msg_col = 0;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 // When 'keymodel' contains "startsel" some keys start Select/Visual
921 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (!VIsual_active && km_startsel)
923 {
924 if (nv_cmds[idx].cmd_flags & NV_SS)
925 {
926 start_selection();
927 unshift_special(&ca);
928 idx = find_command(ca.cmdchar);
929 }
930 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
931 && (mod_mask & MOD_MASK_SHIFT))
932 {
933 start_selection();
934 mod_mask &= ~MOD_MASK_SHIFT;
935 }
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000938 // Execute the command!
939 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 ca.arg = nv_cmds[idx].cmd_arg;
941 (nv_cmds[idx].cmd_func)(&ca);
942
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000943 // If we didn't start or finish an operator, reset oap->regname, unless we
944 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (!finish_op
946 && !oap->op_type
947 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
948 {
949 clearop(oap);
950#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200951 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#endif
953 }
954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100955 // Get the length of mapped chars again after typing a count, second
956 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000957 if (old_mapped_len > 0)
958 old_mapped_len = typebuf_maplen();
959
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000960 // If an operation is pending, handle it. But not for K_IGNORE or
961 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200962 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100963 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000965 // Wait for a moment when a message is displayed that will be overwritten
966 // by the mode message.
967 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
968 normal_cmd_wait_for_msg();
969
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000970 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971normal_end:
972
973 msg_nowait = FALSE;
974
Bram Moolenaarcc613032020-06-07 21:31:18 +0200975#ifdef FEAT_EVAL
976 if (finish_op)
977 reset_reg_var();
978#endif
979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100980 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#ifdef CURSOR_SHAPE
982 c = finish_op;
983#endif
984 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100985 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100987 // Redraw the cursor with another shape, if we were in Operator-pending
988 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (c || ca.cmdchar == 'r')
990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100991 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992# ifdef FEAT_MOUSESHAPE
993 update_mouseshape(-1);
994# endif
995 }
996#endif
997
998#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000999 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001000 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 clear_showcmd();
1002#endif
1003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001004 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 vim_free(ca.searchbuf);
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (has_mbyte)
1008 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (curwin->w_p_scb && toplevel)
1011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 do_check_scrollbind(TRUE);
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar860cae12010-06-05 23:22:07 +02001016 if (curwin->w_p_crb && toplevel)
1017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001019 do_check_cursorbind();
1020 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001021
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001022#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001023 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001024 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001025 restart_edit = 0;
1026#endif
1027
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001028 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1029 // if still inside a mapping that started in Visual mode).
1030 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1033 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && !(ca.retval & CA_COMMAND_BUSY)
1035 && stuff_empty()
1036 && oap->regname == 0)
1037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (restart_VIsual_select == 1)
1039 {
1040 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001041 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 showmode();
1043 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001044 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001046 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 (void)edit(restart_edit, FALSE, 1L);
1048 }
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (restart_VIsual_select == 2)
1051 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 opcount = ca.opcount;
1055}
1056
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001057#ifdef FEAT_EVAL
1058/*
1059 * Set v:count and v:count1 according to "cap".
1060 * Set v:prevcount only when "set_prevcount" is TRUE.
1061 */
1062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064{
1065 long count = cap->count0;
1066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068 if (cap->opcount != 0)
1069 count = cap->opcount * (count == 0 ? 1 : count);
1070 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001072}
1073#endif
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001076 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * if not.
1078 */
1079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 static int did_check = FALSE;
1083
1084 if (full_screen)
1085 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001086 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001087 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 did_check = TRUE;
1089 }
1090}
1091
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001092#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1093/*
1094 * Call yank_do_autocmd() for "regname".
1095 */
1096 static void
1097call_yank_do_autocmd(int regname)
1098{
1099 oparg_T oa;
1100 yankreg_T *reg;
1101
1102 clear_oparg(&oa);
1103 oa.regname = regname;
1104 oa.op_type = OP_YANK;
1105 oa.is_VIsual = TRUE;
1106 reg = get_register(regname, TRUE);
1107 yank_do_autocmd(&oa, reg);
1108 free_register(reg);
1109}
1110#endif
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001113 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114 * This function or the next should ALWAYS be called to end Visual mode, except
1115 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
1117 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001118end_visual_mode()
1119{
1120 end_visual_mode_keep_button();
1121 reset_held_button();
1122}
1123
1124 void
1125end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001128 // If we are using the clipboard, then remember what was selected in case
1129 // we need to paste it somewhere while we still own the selection.
1130 // Only do this when the clipboard is already owned. Don't want to grab
1131 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (clip_star.available && clip_star.owned)
1133 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001134
1135# if defined(FEAT_EVAL)
1136 // Emit a TextYankPost for the automatic copy of the selection into the
1137 // star and/or plus register.
1138 if (has_textyankpost())
1139 {
1140 if (clip_isautosel_star())
1141 call_yank_do_autocmd('*');
1142 if (clip_isautosel_plus())
1143 call_yank_do_autocmd('+');
1144 }
1145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#endif
1147
1148 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 setmouse();
1150 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001152 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001153 curbuf->b_visual.vi_mode = VIsual_mode;
1154 curbuf->b_visual.vi_start = VIsual;
1155 curbuf->b_visual.vi_end = curwin->w_cursor;
1156 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#ifdef FEAT_EVAL
1158 curbuf->b_visual_mode_eval = VIsual_mode;
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (!virtual_active())
1161 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001162 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001164 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001165 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166}
1167
1168/*
1169 * Reset VIsual_active and VIsual_reselect.
1170 */
1171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001172reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
1174 if (VIsual_active)
1175 {
1176 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001177 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 VIsual_reselect = FALSE;
1180}
1181
1182/*
1183 * Reset VIsual_active and VIsual_reselect if it's set.
1184 */
1185 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001186reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 if (VIsual_active)
1189 {
1190 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 VIsual_reselect = FALSE;
1193 }
1194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001196 void
1197restore_visual_mode(void)
1198{
1199 if (VIsual_mode_orig != NUL)
1200 {
1201 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1202 VIsual_mode_orig = NUL;
1203 }
1204}
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206/*
1207 * Check for a balloon-eval special item to include when searching for an
1208 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1209 * Returns TRUE if the character at "*ptr" should be included.
1210 * "dir" is FORWARD or BACKWARD, the direction of searching.
1211 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1212 * "bnp" points to a counter for square brackets.
1213 */
1214 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001215find_is_eval_item(
1216 char_u *ptr,
1217 int *colp,
1218 int *bnp,
1219 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001221 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1223 ++*bnp;
1224 if (*bnp > 0)
1225 {
1226 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1227 --*bnp;
1228 return TRUE;
1229 }
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (*ptr == '.')
1233 return TRUE;
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1237 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1238 {
1239 *colp += dir;
1240 return TRUE;
1241 }
1242 return FALSE;
1243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245/*
1246 * Find the identifier under or to the right of the cursor.
1247 * "find_type" can have one of three values:
1248 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001249 * FIND_STRING: find any non-white text
1250 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001251 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 *
1253 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001254 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * This doesn't match the real Vi but I like it a little better and it
1258 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * When FIND_IDENT isn't defined, we backup until a blank.
1261 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001262 * Returns the length of the text, or zero if no text is found.
1263 * If text is found, a pointer to the text is put in "*text". This
1264 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 */
1266 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001270 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273/*
1274 * Like find_ident_under_cursor(), but for any window and any position.
1275 * However: Uses 'iskeyword' from the current window!.
1276 */
1277 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001278find_ident_at_pos(
1279 win_T *wp,
1280 linenr_T lnum,
1281 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001282 char_u **text,
1283 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001284 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001287 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 int this_class = 0;
1290 int prev_class;
1291 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001292 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001294 // if i == 0: try to find an identifier
1295 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1297 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1298 {
1299 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001300 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 */
1302 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (has_mbyte)
1304 {
1305 while (ptr[col] != NUL)
1306 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001307 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 this_class = mb_get_class(ptr + col);
1311 if (this_class != 0 && (i == 1 || this_class != 1))
1312 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001313 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001318 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 )
1321 ++col;
1322
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
1326 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001327 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (has_mbyte)
1330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1333 this_class = mb_get_class((char_u *)"a");
1334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001336 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1339 prev_class = mb_get_class(ptr + prevcol);
1340 if (this_class != prev_class
1341 && (i == 0
1342 || prev_class == 0
1343 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 && (!(find_type & FIND_EVAL)
1345 || prevcol == 0
1346 || !find_is_eval_item(ptr + prevcol, &prevcol,
1347 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 )
1349 break;
1350 col = prevcol;
1351 }
1352
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001353 // If we don't want just any old text, or we've found an
1354 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (this_class > 2)
1356 this_class = 2;
1357 if (!(find_type & FIND_STRING) || this_class == 2)
1358 break;
1359 }
1360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 while (col > 0
1363 && ((i == 0
1364 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001365 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 && (!(find_type & FIND_IDENT)
1367 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 || ((find_type & FIND_EVAL)
1369 && col > 1
1370 && find_is_eval_item(ptr + col - 1, &col,
1371 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ))
1373 --col;
1374
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001375 // If we don't want just any old text, or we've found an
1376 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1378 break;
1379 }
1380 }
1381
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001382 if (ptr[col] == NUL || (i == 0
1383 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 if ((find_type & FIND_NOERROR) == 0)
1387 {
1388 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001389 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001390 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001391 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 return 0;
1394 }
1395 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001396 *text = ptr;
1397 if (textcol != NULL)
1398 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001401 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 bn = 0;
1404 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (has_mbyte)
1407 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001408 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 this_class = mb_get_class(ptr);
1410 while (ptr[col] != NUL
1411 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1412 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 || ((find_type & FIND_EVAL)
1414 && col <= (int)startcol
1415 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001417 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001421 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 || ((find_type & FIND_EVAL)
1423 && col <= (int)startcol
1424 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 return col;
1429}
1430
1431/*
1432 * Prepare for redo of a normal command.
1433 */
1434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001435prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 prep_redo(cap->oap->regname, cap->count0,
1438 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1439}
1440
1441/*
1442 * Prepare for redo of any command.
1443 * Note that only the last argument can be a multi-byte char.
1444 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001446prep_redo(
1447 int regname,
1448 long num,
1449 int cmd1,
1450 int cmd2,
1451 int cmd3,
1452 int cmd4,
1453 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001455 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1456}
1457
1458/*
1459 * Prepare for redo of any command with extra count after "cmd2".
1460 */
1461 void
1462prep_redo_num2(
1463 int regname,
1464 long num1,
1465 int cmd1,
1466 int cmd2,
1467 long num2,
1468 int cmd3,
1469 int cmd4,
1470 int cmd5)
1471{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001473 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 AppendCharToRedobuff('"');
1476 AppendCharToRedobuff(regname);
1477 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001478 if (num1 != 0)
1479 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (cmd1 != NUL)
1481 AppendCharToRedobuff(cmd1);
1482 if (cmd2 != NUL)
1483 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001484 if (num2 != 0)
1485 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if (cmd3 != NUL)
1487 AppendCharToRedobuff(cmd3);
1488 if (cmd4 != NUL)
1489 AppendCharToRedobuff(cmd4);
1490 if (cmd5 != NUL)
1491 AppendCharToRedobuff(cmd5);
1492}
1493
1494/*
1495 * check for operator active and clear it
1496 *
1497 * return TRUE if operator was active
1498 */
1499 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001500checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 if (oap->op_type == OP_NOP)
1503 return FALSE;
1504 clearopbeep(oap);
1505 return TRUE;
1506}
1507
1508/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001509 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001511 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 */
1513 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001514checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001516 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 return FALSE;
1518 clearopbeep(oap);
1519 return TRUE;
1520}
1521
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001523clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 oap->op_type = OP_NOP;
1526 oap->regname = 0;
1527 oap->motion_force = NUL;
1528 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001529 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
1531
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001533clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535 clearop(oap);
1536 beep_flush();
1537}
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
1540 * Remove the shift modifier from a special key.
1541 */
1542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001543unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 switch (cap->cmdchar)
1546 {
1547 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1548 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1549 case K_S_UP: cap->cmdchar = K_UP; break;
1550 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1551 case K_S_HOME: cap->cmdchar = K_HOME; break;
1552 case K_S_END: cap->cmdchar = K_END; break;
1553 }
1554 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001557/*
1558 * If the mode is currently displayed clear the command line or update the
1559 * command displayed.
1560 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001561 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001562may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001563{
1564 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566#ifdef FEAT_CMDL_INFO
1567 else
1568 clear_showcmd();
1569#endif
1570}
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1573/*
1574 * Routines for displaying a partly typed command
1575 */
1576
kylo252ae6f1d82022-02-16 19:24:07 +00001577#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001579static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580static int showcmd_is_clear = TRUE;
1581static int showcmd_visual = FALSE;
1582
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001583static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001586clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 if (!p_sc)
1589 return;
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (VIsual_active && !char_avail())
1592 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001593 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 long lines;
1595 colnr_T leftcol, rightcol;
1596 linenr_T top, bot;
1597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001598 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001599 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 top = VIsual.lnum;
1602 bot = curwin->w_cursor.lnum;
1603 }
1604 else
1605 {
1606 top = curwin->w_cursor.lnum;
1607 bot = VIsual.lnum;
1608 }
1609# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001611 (void)hasFolding(top, &top, NULL);
1612 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613# endif
1614 lines = bot - top + 1;
1615
1616 if (VIsual_mode == Ctrl_V)
1617 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001618# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001620 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001623 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001624 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001627# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001628 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001629 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1632 (long)(rightcol - leftcol + 1));
1633 }
1634 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1635 sprintf((char *)showcmd_buf, "%ld", lines);
1636 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001637 {
1638 char_u *s, *e;
1639 int l;
1640 int bytes = 0;
1641 int chars = 0;
1642
1643 if (cursor_bot)
1644 {
1645 s = ml_get_pos(&VIsual);
1646 e = ml_get_cursor();
1647 }
1648 else
1649 {
1650 s = ml_get_cursor();
1651 e = ml_get_pos(&VIsual);
1652 }
1653 while ((*p_sel != 'e') ? s <= e : s < e)
1654 {
1655 l = (*mb_ptr2len)(s);
1656 if (l == 0)
1657 {
1658 ++bytes;
1659 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001661 }
1662 bytes += l;
1663 ++chars;
1664 s += l;
1665 }
1666 if (bytes == chars)
1667 sprintf((char *)showcmd_buf, "%d", chars);
1668 else
1669 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1670 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001671 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 showcmd_visual = TRUE;
1673 }
1674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 showcmd_buf[0] = NUL;
1677 showcmd_visual = FALSE;
1678
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001679 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (showcmd_is_clear)
1681 return;
1682 }
1683
1684 display_showcmd();
1685}
1686
1687/*
1688 * Add 'c' to string of shown command chars.
1689 * Return TRUE if output has been written (and setcursor() has been called).
1690 */
1691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001692add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 char_u *p;
1695 int old_len;
1696 int extra_len;
1697 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 int i;
1699 static int ignore[] =
1700 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001701#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1703 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001704#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001705 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001706 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1708 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001709 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001711 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 0
1713 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar09df3122006-01-23 22:23:09 +00001715 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return FALSE;
1717
1718 if (showcmd_visual)
1719 {
1720 showcmd_buf[0] = NUL;
1721 showcmd_visual = FALSE;
1722 }
1723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001724 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (IS_SPECIAL(c))
1726 for (i = 0; ignore[i] != 0; ++i)
1727 if (ignore[i] == c)
1728 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001731 if (*p == ' ')
1732 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 old_len = (int)STRLEN(showcmd_buf);
1734 extra_len = (int)STRLEN(p);
1735 overflow = old_len + extra_len - SHOWCMD_COLS;
1736 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001737 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1738 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 STRCAT(showcmd_buf, p);
1740
1741 if (char_avail())
1742 return FALSE;
1743
1744 display_showcmd();
1745
1746 return TRUE;
1747}
1748
1749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 if (!add_to_showcmd(c))
1753 setcursor();
1754}
1755
1756/*
1757 * Delete 'len' characters from the end of the shown command.
1758 */
1759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 int old_len;
1763
1764 if (!p_sc)
1765 return;
1766
1767 old_len = (int)STRLEN(showcmd_buf);
1768 if (len > old_len)
1769 len = old_len;
1770 showcmd_buf[old_len - len] = NUL;
1771
1772 if (!char_avail())
1773 display_showcmd();
1774}
1775
1776/*
1777 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1778 * something and there is a partial mapping.
1779 */
1780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001781push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 if (p_sc)
1784 STRCPY(old_showcmd_buf, showcmd_buf);
1785}
1786
1787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 if (!p_sc)
1791 return;
1792
1793 STRCPY(showcmd_buf, old_showcmd_buf);
1794
1795 display_showcmd();
1796}
1797
1798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int len;
1802
1803 cursor_off();
1804
1805 len = (int)STRLEN(showcmd_buf);
1806 if (len == 0)
1807 showcmd_is_clear = TRUE;
1808 else
1809 {
1810 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1811 showcmd_is_clear = FALSE;
1812 }
1813
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001814 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1815 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001818 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * When "check" is FALSE, prepare for commands that scroll the window.
1824 * When "check" is TRUE, take care of scroll-binding after the window has
1825 * scrolled. Called from normal_cmd() and edit().
1826 */
1827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001828do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 static win_T *old_curwin = NULL;
1831 static linenr_T old_topline = 0;
1832#ifdef FEAT_DIFF
1833 static int old_topfill = 0;
1834#endif
1835 static buf_T *old_buf = NULL;
1836 static colnr_T old_leftcol = 0;
1837
1838 if (check && curwin->w_p_scb)
1839 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001840 // If a ":syncbind" command was just used, don't scroll, only reset
1841 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (did_syncbind)
1843 did_syncbind = FALSE;
1844 else if (curwin == old_curwin)
1845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001846 // Synchronize other windows, as necessary according to
1847 // 'scrollbind'. Don't do this after an ":edit" command, except
1848 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if ((curwin->w_buffer == old_buf
1850#ifdef FEAT_DIFF
1851 || curwin->w_p_diff
1852#endif
1853 )
1854 && (curwin->w_topline != old_topline
1855#ifdef FEAT_DIFF
1856 || curwin->w_topfill != old_topfill
1857#endif
1858 || curwin->w_leftcol != old_leftcol))
1859 {
1860 check_scrollbind(curwin->w_topline - old_topline,
1861 (long)(curwin->w_leftcol - old_leftcol));
1862 }
1863 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001866 // When switching between windows, make sure that the relative
1867 // vertical offset is valid for the new window. The relative
1868 // offset is invalid whenever another 'scrollbind' window has
1869 // scrolled to a point that would force the current window to
1870 // scroll past the beginning or end of its buffer. When the
1871 // resync is performed, some of the other 'scrollbind' windows may
1872 // need to jump so that the current window's relative position is
1873 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1875 }
1876 curwin->w_scbind_pos = curwin->w_topline;
1877 }
1878
1879 old_curwin = curwin;
1880 old_topline = curwin->w_topline;
1881#ifdef FEAT_DIFF
1882 old_topfill = curwin->w_topfill;
1883#endif
1884 old_buf = curwin->w_buffer;
1885 old_leftcol = curwin->w_leftcol;
1886}
1887
1888/*
1889 * Synchronize any windows that have "scrollbind" set, based on the
1890 * number of rows by which the current window has changed
1891 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1892 */
1893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001894check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 int want_ver;
1897 int want_hor;
1898 win_T *old_curwin = curwin;
1899 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int old_VIsual_select = VIsual_select;
1901 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 colnr_T tgt_leftcol = curwin->w_leftcol;
1903 long topline;
1904 long y;
1905
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001906 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1908#ifdef FEAT_DIFF
1909 want_ver |= old_curwin->w_p_diff;
1910#endif
1911 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1912
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001913 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001915 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (curwin != old_curwin && curwin->w_p_scb)
1920 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001921 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (want_ver)
1923 {
1924#ifdef FEAT_DIFF
1925 if (old_curwin->w_p_diff && curwin->w_p_diff)
1926 {
1927 diff_set_topline(old_curwin, curwin);
1928 }
1929 else
1930#endif
1931 {
1932 curwin->w_scbind_pos += topline_diff;
1933 topline = curwin->w_scbind_pos;
1934 if (topline > curbuf->b_ml.ml_line_count)
1935 topline = curbuf->b_ml.ml_line_count;
1936 if (topline < 1)
1937 topline = 1;
1938
1939 y = topline - curwin->w_topline;
1940 if (y > 0)
1941 scrollup(y, FALSE);
1942 else
1943 scrolldown(-y, FALSE);
1944 }
1945
1946 redraw_later(VALID);
1947 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001951 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1953 {
1954 curwin->w_leftcol = tgt_leftcol;
1955 leftcol_changed();
1956 }
1957 }
1958 }
1959
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001960 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 VIsual_select = old_VIsual_select;
1962 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 curwin = old_curwin;
1964 curbuf = old_curbuf;
1965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967/*
1968 * Command character that's ignored.
1969 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001970 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001975 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976}
1977
1978/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001979 * Command character that doesn't do anything, but unlike nv_ignore() does
1980 * start edit(). Used for "startinsert" executed while starting up.
1981 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001983nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001984{
1985}
1986
1987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * Command character doesn't exist.
1989 */
1990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001991nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 clearopbeep(cap->oap);
1994}
1995
1996/*
1997 * <Help> and <F1> commands.
1998 */
1999 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002000nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 if (!checkclearopq(cap->oap))
2003 ex_help(NULL);
2004}
2005
2006/*
2007 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2008 */
2009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002012#ifdef FEAT_JOB_CHANNEL
2013 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2014 clearopbeep(cap->oap);
2015 else
2016#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002017 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002018 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002019 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002020 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2021 op_addsub(cap->oap, cap->count1, cap->arg);
2022 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002023 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002024 else if (VIsual_active)
2025 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002026 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002027 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028}
2029
2030/*
2031 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2032 */
2033 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002038 if (mod_mask & MOD_MASK_CTRL)
2039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 if (cap->arg == BACKWARD)
2042 goto_tabpage(-(int)cap->count1);
2043 else
2044 goto_tabpage((int)cap->count0);
2045 }
2046 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002047 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049}
2050
2051/*
2052 * Implementation of "gd" and "gD" command.
2053 */
2054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002055nv_gd(
2056 oparg_T *oap,
2057 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 char_u *ptr;
2062
Bram Moolenaard9d30582005-05-18 22:10:28 +00002063 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002064 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002065 == FAIL)
2066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002068 }
2069 else
2070 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002071#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002072 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2073 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002074#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002075 // clear any search statistics
2076 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2077 clear_cmdline = TRUE;
2078 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002079}
2080
2081/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002082 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2083 * otherwise.
2084 */
2085 static int
2086is_ident(char_u *line, int offset)
2087{
2088 int i;
2089 int incomment = FALSE;
2090 int instring = 0;
2091 int prev = 0;
2092
2093 for (i = 0; i < offset && line[i] != NUL; i++)
2094 {
2095 if (instring != 0)
2096 {
2097 if (prev != '\\' && line[i] == instring)
2098 instring = 0;
2099 }
2100 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2101 {
2102 instring = line[i];
2103 }
2104 else
2105 {
2106 if (incomment)
2107 {
2108 if (prev == '*' && line[i] == '/')
2109 incomment = FALSE;
2110 }
2111 else if (prev == '/' && line[i] == '*')
2112 {
2113 incomment = TRUE;
2114 }
2115 else if (prev == '/' && line[i] == '/')
2116 {
2117 return FALSE;
2118 }
2119 }
2120
2121 prev = line[i];
2122 }
2123
2124 return incomment == FALSE && instring == 0;
2125}
2126
2127/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002128 * Search for variable declaration of "ptr[len]".
2129 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2130 * current file ("gD").
2131 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002132 * Return FAIL when not found.
2133 */
2134 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002135find_decl(
2136 char_u *ptr,
2137 int len,
2138 int locally,
2139 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002140 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002141{
2142 char_u *pat;
2143 pos_T old_pos;
2144 pos_T par_pos;
2145 pos_T found_pos;
2146 int t;
2147 int save_p_ws;
2148 int save_p_scs;
2149 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002150 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002151 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002152 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002153
2154 if ((pat = alloc(len + 7)) == NULL)
2155 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002157 // Put "\V" before the pattern to avoid that the special meaning of "."
2158 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002159 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2160 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 old_pos = curwin->w_cursor;
2162 save_p_ws = p_ws;
2163 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002164 p_ws = FALSE; // don't wrap around end of file now
2165 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002167 // With "gD" go to line 1.
2168 // With "gd" Search back for the start of the current function, then go
2169 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002170 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002172 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002174 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 else
2177 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002178 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2180 --curwin->w_cursor.lnum;
2181 }
2182 curwin->w_cursor.col = 0;
2183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002184 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002185 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002186 for (;;)
2187 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002188 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002189 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002190 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002192
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002193 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002194 {
2195 pos_T *pos;
2196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002197 // Check that the block the match is in doesn't end before the
2198 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002199 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2200 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2201 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002202 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002203 // There can't be a useful match before the end of this block.
2204 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002205 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002206 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002207 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002208 }
2209
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 if (t == FAIL)
2211 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002213 if (found_pos.lnum != 0)
2214 {
2215 curwin->w_cursor = found_pos;
2216 t = OK;
2217 }
2218 break;
2219 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002220 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 ++curwin->w_cursor.lnum;
2224 curwin->w_cursor.col = 0;
2225 continue;
2226 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002229 // If the current position is not a valid identifier and a previous
2230 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 if (!valid && found_pos.lnum != 0)
2232 {
2233 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002234 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002235 }
2236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002238 if (valid && !locally)
2239 break;
2240 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002242 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002243 if (found_pos.lnum != 0)
2244 curwin->w_cursor = found_pos;
2245 break;
2246 }
2247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 // For finding a local variable and the match is before the "{" or
2249 // inside a comment, continue searching. For K&R style function
2250 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002251 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002252 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002253 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002254 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 // Remove SEARCH_START from flags to avoid getting stuck at one
2256 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002257 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002259
2260 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002262 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 curwin->w_cursor = old_pos;
2264 }
2265 else
2266 {
2267 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002268 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 reset_search_dir();
2270 }
2271
2272 vim_free(pat);
2273 p_ws = save_p_ws;
2274 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002275
2276 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277}
2278
2279/*
2280 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2281 * lines rather than lines in the file.
2282 * 'dist' must be positive.
2283 *
2284 * Return OK if able to move cursor, FAIL otherwise.
2285 */
2286 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int linelen = linetabsize(ml_get_curline());
2290 int retval = OK;
2291 int atend = FALSE;
2292 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 int col_off1; // margin offset for first screen line
2294 int col_off2; // margin offset for wrapped screen line
2295 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002296 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002299 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 col_off1 = curwin_col_off();
2302 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002303 width1 = curwin->w_width - col_off1;
2304 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002305 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002309 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002310 // Instead of sticking at the last character of the buffer line we
2311 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (curwin->w_curswant == MAXCOL)
2313 {
2314 atend = TRUE;
2315 validate_virtcol();
2316 if (width1 <= 0)
2317 curwin->w_curswant = 0;
2318 else
2319 {
2320 curwin->w_curswant = width1 - 1;
2321 if (curwin->w_virtcol > curwin->w_curswant)
2322 curwin->w_curswant += ((curwin->w_virtcol
2323 - curwin->w_curswant - 1) / width2 + 1) * width2;
2324 }
2325 }
2326 else
2327 {
2328 if (linelen > width1)
2329 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2330 else
2331 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002332 if (curwin->w_curswant >= (colnr_T)n)
2333 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335
2336 while (dist--)
2337 {
2338 if (dir == BACKWARD)
2339 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002340 if ((long)curwin->w_curswant >= width1
2341#ifdef FEAT_FOLDING
2342 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2343#endif
2344 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002345 // Move back within the line. This can give a negative value
2346 // for w_curswant if width1 < width2 (with cpoptions+=n),
2347 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 curwin->w_curswant -= width2;
2349 else
2350 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002351 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 // Move to the start of a closed fold. Don't do that when
2354 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (!(fdo_flags & FDO_ALL))
2356 (void)hasFolding(curwin->w_cursor.lnum,
2357 &curwin->w_cursor.lnum, NULL);
2358#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002359 if (curwin->w_cursor.lnum == 1)
2360 {
2361 retval = FAIL;
2362 break;
2363 }
2364 --curwin->w_cursor.lnum;
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 linelen = linetabsize(ml_get_curline());
2367 if (linelen > width1)
2368 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2369 + 1) * width2;
2370 }
2371 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 if (linelen > width1)
2375 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2376 else
2377 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002378 if (curwin->w_curswant + width2 < (colnr_T)n
2379#ifdef FEAT_FOLDING
2380 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2381#endif
2382 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 curwin->w_curswant += width2;
2385 else
2386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2391 &curwin->w_cursor.lnum);
2392#endif
2393 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2394 {
2395 retval = FAIL;
2396 break;
2397 }
2398 curwin->w_cursor.lnum++;
2399 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002400 // Check if the cursor has moved below the number display
2401 // when width1 < width2 (with cpoptions+=n). Subtract width2
2402 // to get a negative value for w_curswant, which will get
2403 // clipped to column 0.
2404 if (curwin->w_curswant >= width1)
2405 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002406 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 }
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002412 if (virtual_active() && atend)
2413 coladvance(MAXCOL);
2414 else
2415 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2418 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002419 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002420 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002421
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002422 // Check for landing on a character that got split at the end of the
2423 // last line. We want to advance a screenline, not end up in the same
2424 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002426 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002427#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002428 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2429 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002430#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002431
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002432 c = (*mb_ptr2char)(ml_get_cursor());
2433 if (dir == FORWARD && virtcol < curwin->w_curswant
2434 && (curwin->w_curswant <= (colnr_T)width1)
2435 && !vim_isprintc(c) && c > 255)
2436 oneright();
2437
Bram Moolenaar773b1582014-08-29 14:20:51 +02002438 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 && (curwin->w_curswant < (colnr_T)width1
2440 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2441 : ((curwin->w_curswant - width1) % width2
2442 > (colnr_T)width2 / 2)))
2443 --curwin->w_cursor.col;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 return retval;
2450}
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452/*
2453 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2454 * cap->arg must be TRUE for CTRL-E.
2455 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 if (!checkclearop(cap->oap))
2460 scroll_redraw(cap->arg, cap->count1);
2461}
2462
2463/*
2464 * Scroll "count" lines up or down, and redraw.
2465 */
2466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002467scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 linenr_T prev_topline = curwin->w_topline;
2470#ifdef FEAT_DIFF
2471 int prev_topfill = curwin->w_topfill;
2472#endif
2473 linenr_T prev_lnum = curwin->w_cursor.lnum;
2474
2475 if (up)
2476 scrollup(count, TRUE);
2477 else
2478 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002479 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2482 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 cursor_correct();
2484 check_cursor_moved(curwin);
2485 curwin->w_valid |= VALID_TOPLINE;
2486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 // If moved back to where we were, at least move the cursor, otherwise
2488 // we get stuck at one position. Don't move the cursor up if the
2489 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 while (curwin->w_topline == prev_topline
2491#ifdef FEAT_DIFF
2492 && curwin->w_topfill == prev_topfill
2493#endif
2494 )
2495 {
2496 if (up)
2497 {
2498 if (curwin->w_cursor.lnum > prev_lnum
2499 || cursor_down(1L, FALSE) == FAIL)
2500 break;
2501 }
2502 else
2503 {
2504 if (curwin->w_cursor.lnum < prev_lnum
2505 || prev_topline == 1L
2506 || cursor_up(1L, FALSE) == FAIL)
2507 break;
2508 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002509 // Mark w_topline as valid, otherwise the screen jumps back at the
2510 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 check_cursor_moved(curwin);
2512 curwin->w_valid |= VALID_TOPLINE;
2513 }
2514 }
2515 if (curwin->w_cursor.lnum != prev_lnum)
2516 coladvance(curwin->w_curswant);
2517 redraw_later(VALID);
2518}
2519
2520/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002521 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2522 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2523 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002524 */
2525 static int
2526nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2527{
2528 int nchar = *nchar_arg;
2529 long n;
2530
2531 // "z123{nchar}": edit the count before obtaining {nchar}
2532 if (checkclearop(cap->oap))
2533 return FALSE;
2534 n = nchar - '0';
2535
2536 for (;;)
2537 {
2538#ifdef USE_ON_FLY_SCROLL
2539 dont_scroll = TRUE; // disallow scrolling here
2540#endif
2541 ++no_mapping;
2542 ++allow_keys; // no mapping for nchar, but allow key codes
2543 nchar = plain_vgetc();
2544 LANGMAP_ADJUST(nchar, TRUE);
2545 --no_mapping;
2546 --allow_keys;
2547#ifdef FEAT_CMDL_INFO
2548 (void)add_to_showcmd(nchar);
2549#endif
2550 if (nchar == K_DEL || nchar == K_KDEL)
2551 n /= 10;
2552 else if (VIM_ISDIGIT(nchar))
2553 n = n * 10 + (nchar - '0');
2554 else if (nchar == CAR)
2555 {
2556#ifdef FEAT_GUI
2557 need_mouse_correct = TRUE;
2558#endif
2559 win_setheight((int)n);
2560 break;
2561 }
2562 else if (nchar == 'l'
2563 || nchar == 'h'
2564 || nchar == K_LEFT
2565 || nchar == K_RIGHT)
2566 {
2567 cap->count1 = n ? n * cap->count1 : cap->count1;
2568 *nchar_arg = nchar;
2569 return TRUE;
2570 }
2571 else
2572 {
2573 clearopbeep(cap->oap);
2574 break;
2575 }
2576 }
2577 cap->oap->op_type = OP_NOP;
2578 return FALSE;
2579}
2580
2581#ifdef FEAT_SPELL
2582/*
2583 * "zug" and "zuw": undo "zg" and "zw"
2584 * "zg": add good word to word list
2585 * "zw": add wrong word to word list
2586 * "zG": add good word to temp word list
2587 * "zW": add wrong word to temp word list
2588 */
2589 static int
2590nv_zg_zw(cmdarg_T *cap, int nchar)
2591{
2592 char_u *ptr = NULL;
2593 int len;
2594 int undo = FALSE;
2595
2596 if (nchar == 'u')
2597 {
2598 ++no_mapping;
2599 ++allow_keys; // no mapping for nchar, but allow key codes
2600 nchar = plain_vgetc();
2601 LANGMAP_ADJUST(nchar, TRUE);
2602 --no_mapping;
2603 --allow_keys;
2604#ifdef FEAT_CMDL_INFO
2605 (void)add_to_showcmd(nchar);
2606#endif
2607 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2608 {
2609 clearopbeep(cap->oap);
2610 return OK;
2611 }
2612 undo = TRUE;
2613 }
2614
2615 if (checkclearop(cap->oap))
2616 return OK;
2617 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2618 return FAIL;
2619 if (ptr == NULL)
2620 {
2621 pos_T pos = curwin->w_cursor;
2622
2623 // Find bad word under the cursor. When 'spell' is
2624 // off this fails and find_ident_under_cursor() is
2625 // used below.
2626 emsg_off++;
2627 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2628 emsg_off--;
2629 if (len != 0 && curwin->w_cursor.col <= pos.col)
2630 ptr = ml_get_pos(&curwin->w_cursor);
2631 curwin->w_cursor = pos;
2632 }
2633
2634 if (ptr == NULL
2635 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2636 return FAIL;
2637 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2638 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2639 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2640
2641 return OK;
2642}
2643#endif
2644
2645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 * Commands that start with "z".
2647 */
2648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002649nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 long n;
2652 colnr_T col;
2653 int nchar = cap->nchar;
2654#ifdef FEAT_FOLDING
2655 long old_fdl = curwin->w_p_fdl;
2656 int old_fen = curwin->w_p_fen;
2657#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002658 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002660 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (
2664#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002665 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2666 // and "zC" only in Visual mode. "zj" and "zk" are motion
2667 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 cap->nchar != 'f' && cap->nchar != 'F'
2669 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2670 && cap->nchar != 'j' && cap->nchar != 'k'
2671 &&
2672#endif
2673 checkclearop(cap->oap))
2674 return;
2675
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002676 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2677 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2679 && cap->count0
2680 && cap->count0 != curwin->w_cursor.lnum)
2681 {
2682 setpcmark();
2683 if (cap->count0 > curbuf->b_ml.ml_line_count)
2684 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2685 else
2686 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002687 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 switch (nchar)
2691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 case '+':
2694 if (cap->count0 == 0)
2695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // No count given: put cursor at the line below screen
2697 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2699 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2700 else
2701 curwin->w_cursor.lnum = curwin->w_botline;
2702 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 case NL:
2705 case CAR:
2706 case K_KENTER:
2707 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 case 't': scroll_cursor_top(0, TRUE);
2711 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002712 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 break;
2714
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 case 'z': scroll_cursor_halfway(TRUE);
2720 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002721 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 break;
2723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 // "z^", "z-" and "zb": put cursor at bottom of screen
2725 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2726 // when <count> is at bottom of window, and puts that one at
2727 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (cap->count0 != 0)
2729 {
2730 scroll_cursor_bot(0, TRUE);
2731 curwin->w_cursor.lnum = curwin->w_topline;
2732 }
2733 else if (curwin->w_topline == 1)
2734 curwin->w_cursor.lnum = 1;
2735 else
2736 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 case '-':
2739 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 case 'b': scroll_cursor_bot(0, TRUE);
2743 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002744 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 break;
2746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002749 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 case 'h':
2754 case K_LEFT:
2755 if (!curwin->w_p_wrap)
2756 {
2757 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2758 curwin->w_leftcol = 0;
2759 else
2760 curwin->w_leftcol -= (colnr_T)cap->count1;
2761 leftcol_changed();
2762 }
2763 break;
2764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002766 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case 'l':
2771 case K_RIGHT:
2772 if (!curwin->w_p_wrap)
2773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 curwin->w_leftcol += (colnr_T)cap->count1;
2776 leftcol_changed();
2777 }
2778 break;
2779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case 's': if (!curwin->w_p_wrap)
2782 {
2783#ifdef FEAT_FOLDING
2784 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
2787#endif
2788 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002789 if ((long)col > siso)
2790 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792 col = 0;
2793 if (curwin->w_leftcol != col)
2794 {
2795 curwin->w_leftcol = col;
2796 redraw_later(NOT_VALID);
2797 }
2798 }
2799 break;
2800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case 'e': if (!curwin->w_p_wrap)
2803 {
2804#ifdef FEAT_FOLDING
2805 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
2808#endif
2809 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002810 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002811 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 col = 0;
2813 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002814 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (curwin->w_leftcol != col)
2816 {
2817 curwin->w_leftcol = col;
2818 redraw_later(NOT_VALID);
2819 }
2820 }
2821 break;
2822
Christian Brabandt2fa93842021-05-30 22:17:25 +02002823 // "zp", "zP" in block mode put without addind trailing spaces
2824 case 'P':
2825 case 'p': nv_put(cap);
2826 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002827 // "zy" Yank without trailing spaces
2828 case 'y': nv_operator(cap);
2829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 // "zF": create fold command
2832 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 case 'F':
2834 case 'f': if (foldManualAllowed(TRUE))
2835 {
2836 cap->nchar = 'f';
2837 nv_operator(cap);
2838 curwin->w_p_fen = TRUE;
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2842 {
2843 nv_operator(cap);
2844 finish_op = TRUE;
2845 }
2846 }
2847 else
2848 clearopbeep(cap->oap);
2849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zd": delete fold at cursor
2852 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 case 'd':
2854 case 'D': if (foldManualAllowed(FALSE))
2855 {
2856 if (VIsual_active)
2857 nv_operator(cap);
2858 else
2859 deleteFold(curwin->w_cursor.lnum,
2860 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2861 }
2862 break;
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case 'E': if (foldmethodIsManual(curwin))
2866 {
2867 clearFolding(curwin);
2868 changed_window_setting();
2869 }
2870 else if (foldmethodIsMarker(curwin))
2871 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2872 TRUE, FALSE);
2873 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002874 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'n': curwin->w_p_fen = FALSE;
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'N': curwin->w_p_fen = TRUE;
2883 break;
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2891 openFold(curwin->w_cursor.lnum, cap->count1);
2892 else
2893 {
2894 closeFold(curwin->w_cursor.lnum, cap->count1);
2895 curwin->w_p_fen = TRUE;
2896 }
2897 break;
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2901 openFoldRecurse(curwin->w_cursor.lnum);
2902 else
2903 {
2904 closeFoldRecurse(curwin->w_cursor.lnum);
2905 curwin->w_p_fen = TRUE;
2906 }
2907 break;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case 'o': if (VIsual_active)
2911 nv_operator(cap);
2912 else
2913 openFold(curwin->w_cursor.lnum, cap->count1);
2914 break;
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case 'O': if (VIsual_active)
2918 nv_operator(cap);
2919 else
2920 openFoldRecurse(curwin->w_cursor.lnum);
2921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'c': if (VIsual_active)
2925 nv_operator(cap);
2926 else
2927 closeFold(curwin->w_cursor.lnum, cap->count1);
2928 curwin->w_p_fen = TRUE;
2929 break;
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case 'C': if (VIsual_active)
2933 nv_operator(cap);
2934 else
2935 closeFoldRecurse(curwin->w_cursor.lnum);
2936 curwin->w_p_fen = TRUE;
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'v': foldOpenCursor();
2941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 curwin->w_foldinvalid = TRUE; // recompute folds
2946 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 foldOpenCursor();
2948 break;
2949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 curwin->w_foldinvalid = TRUE; // recompute folds
2953 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002958 {
2959 curwin->w_p_fdl -= cap->count1;
2960 if (curwin->w_p_fdl < 0)
2961 curwin->w_p_fdl = 0;
2962 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 curwin->w_p_fen = TRUE;
2965 break;
2966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 curwin->w_p_fen = TRUE;
2971 break;
2972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002974 case 'r': curwin->w_p_fdl += cap->count1;
2975 {
2976 int d = getDeepestNesting();
2977
2978 if (curwin->w_p_fdl >= d)
2979 curwin->w_p_fdl = d;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 break;
2987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case 'j': // "zj" move to next fold downwards
2989 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2991 cap->count1) == FAIL)
2992 clearopbeep(cap->oap);
2993 break;
2994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002997#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 case 'g': // "zg": add good word to word list
3000 case 'w': // "zw": add wrong word to word list
3001 case 'G': // "zG": add good word to temp word list
3002 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003003 if (nv_zg_zw(cap, nchar) == FAIL)
3004 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003005 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003007 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003008 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003009 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003010 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003011#endif
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 default: clearopbeep(cap->oap);
3014 }
3015
3016#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (old_fen != curwin->w_p_fen)
3019 {
3020# ifdef FEAT_DIFF
3021 win_T *wp;
3022
3023 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 FOR_ALL_WINDOWS(wp)
3027 {
3028 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3029 {
3030 wp->w_p_fen = curwin->w_p_fen;
3031 changed_window_setting_win(wp);
3032 }
3033 }
3034 }
3035# endif
3036 changed_window_setting();
3037 }
3038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (old_fdl != curwin->w_p_fdl)
3041 newFoldLevel();
3042#endif
3043}
3044
3045#ifdef FEAT_GUI
3046/*
3047 * Vertical scrollbar movement.
3048 */
3049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (cap->oap->op_type != OP_NOP)
3053 clearopbeep(cap->oap);
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 gui_do_scroll();
3057}
3058
3059/*
3060 * Horizontal scrollbar movement.
3061 */
3062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003063nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 if (cap->oap->op_type != OP_NOP)
3066 clearopbeep(cap->oap);
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003069 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070}
3071#endif
3072
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003073#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003074/*
3075 * Click in GUI tab.
3076 */
3077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003079{
3080 if (cap->oap->op_type != OP_NOP)
3081 clearopbeep(cap->oap);
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003084 goto_tabpage(current_tab);
3085}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003086
3087/*
3088 * Selected item in tab line menu.
3089 */
3090 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003091nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003092{
3093 if (cap->oap->op_type != OP_NOP)
3094 clearopbeep(cap->oap);
3095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003097 handle_tabmenu();
3098}
3099
3100/*
3101 * Handle selecting an item of the GUI tab line menu.
3102 * Used in Normal and Insert mode.
3103 */
3104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003105handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003106{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003107 switch (current_tabmenu)
3108 {
3109 case TABLINE_MENU_CLOSE:
3110 if (current_tab == 0)
3111 do_cmdline_cmd((char_u *)"tabclose");
3112 else
3113 {
3114 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3115 current_tab);
3116 do_cmdline_cmd(IObuff);
3117 }
3118 break;
3119
3120 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003121 if (current_tab == 0)
3122 do_cmdline_cmd((char_u *)"$tabnew");
3123 else
3124 {
3125 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3126 current_tab - 1);
3127 do_cmdline_cmd(IObuff);
3128 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003129 break;
3130
3131 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003132 if (current_tab == 0)
3133 do_cmdline_cmd((char_u *)"browse $tabnew");
3134 else
3135 {
3136 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3137 current_tab - 1);
3138 do_cmdline_cmd(IObuff);
3139 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003140 break;
3141 }
3142}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003143#endif
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * "Q" command.
3147 */
3148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003151 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003153 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003154 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 do_exmode(FALSE);
3156}
3157
3158/*
3159 * Handle a ":" command.
3160 */
3161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003162nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003164 int old_p_im;
3165 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003166 int is_cmdkey = cap->cmdchar == K_COMMAND
3167 || cap->cmdchar == K_SCRIPT_COMMAND;
3168 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaar957cf672020-11-12 14:21:06 +01003170 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 nv_operator(cap);
3172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 if (cap->oap->op_type != OP_NOP)
3175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003176 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 cap->oap->motion_type = MCHAR;
3178 cap->oap->inclusive = FALSE;
3179 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003180 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 stuffcharReadbuff('.');
3184 if (cap->count0 > 1)
3185 {
3186 stuffReadbuff((char_u *)",.+");
3187 stuffnumReadbuff((long)cap->count0 - 1L);
3188 }
3189 }
3190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003191 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (KeyTyped)
3193 compute_cmdrow();
3194
3195 old_p_im = p_im;
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003198 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3199 if (is_cmdkey)
3200 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3201 else
3202 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003204 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (p_im != old_p_im)
3206 {
3207 if (p_im)
3208 restart_edit = 'i';
3209 else
3210 restart_edit = 0;
3211 }
3212
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003213 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003215 clearop(cap->oap);
3216 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3218 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003219 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3220 || did_emsg
3221 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 clearopbeep(cap->oap);
3224 }
3225}
3226
3227/*
3228 * Handle CTRL-G command.
3229 */
3230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003231nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003236 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 showmode();
3238 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003239 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 fileinfo((int)cap->count0, FALSE, TRUE);
3242}
3243
3244/*
3245 * Handle CTRL-H <Backspace> command.
3246 */
3247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003248nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (VIsual_active && VIsual_select)
3251 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 v_visop(cap);
3254 }
3255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 nv_left(cap);
3257}
3258
3259/*
3260 * CTRL-L: clear screen and redraw.
3261 */
3262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003263nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 if (!checkclearop(cap->oap))
3266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003268 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003269 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003270# ifdef FEAT_RELTIME
3271 {
3272 win_T *wp;
3273
3274 FOR_ALL_WINDOWS(wp)
3275 wp->w_s->b_syn_slow = FALSE;
3276 }
3277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#endif
3279 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003280#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3281# ifdef VIMDLL
3282 if (!gui.in_use)
3283# endif
3284 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287}
3288
3289/*
3290 * CTRL-O: In Select mode: switch to Visual mode for one command.
3291 * Otherwise: Go to older pcmark.
3292 */
3293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003294nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (VIsual_active && VIsual_select)
3297 {
3298 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003299 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003301 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 cap->count1 = -cap->count1;
3306 nv_pcmark(cap);
3307 }
3308}
3309
3310/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003311 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3312 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (!checkclearopq(cap->oap))
3318 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3319 GETF_SETMARK|GETF_ALT, FALSE);
3320}
3321
3322/*
3323 * "Z" commands.
3324 */
3325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003326nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 if (!checkclearopq(cap->oap))
3329 {
3330 switch (cap->nchar)
3331 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 case 'Z': do_cmdline_cmd((char_u *)"x");
3334 break;
3335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003336 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 case 'Q': do_cmdline_cmd((char_u *)"q!");
3338 break;
3339
3340 default: clearopbeep(cap->oap);
3341 }
3342 }
3343}
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345/*
3346 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3347 */
3348 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003349do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 oparg_T oa;
3352 cmdarg_T ca;
3353
3354 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003355 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 ca.oap = &oa;
3357 ca.cmdchar = c1;
3358 ca.nchar = c2;
3359 nv_ident(&ca);
3360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
3362/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003363 * 'K' normal-mode command. Get the command to lookup the keyword under the
3364 * cursor.
3365 */
3366 static int
3367nv_K_getcmd(
3368 cmdarg_T *cap,
3369 char_u *kp,
3370 int kp_help,
3371 int kp_ex,
3372 char_u **ptr_arg,
3373 int n,
3374 char_u *buf,
3375 unsigned buflen)
3376{
3377 char_u *ptr = *ptr_arg;
3378 int isman;
3379 int isman_s;
3380
3381 if (kp_help)
3382 {
3383 // in the help buffer
3384 STRCPY(buf, "he! ");
3385 return n;
3386 }
3387
3388 if (kp_ex)
3389 {
3390 // 'keywordprog' is an ex command
3391 if (cap->count0 != 0)
3392 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3393 else
3394 STRCPY(buf, kp);
3395 STRCAT(buf, " ");
3396 return n;
3397 }
3398
3399 // An external command will probably use an argument starting
3400 // with "-" as an option. To avoid trouble we skip the "-".
3401 while (*ptr == '-' && n > 0)
3402 {
3403 ++ptr;
3404 --n;
3405 }
3406 if (n == 0)
3407 {
3408 // found dashes only
3409 emsg(_(e_no_identifier_under_cursor));
3410 vim_free(buf);
3411 *ptr_arg = ptr;
3412 return 0;
3413 }
3414
3415 // When a count is given, turn it into a range. Is this
3416 // really what we want?
3417 isman = (STRCMP(kp, "man") == 0);
3418 isman_s = (STRCMP(kp, "man -s") == 0);
3419 if (cap->count0 != 0 && !(isman || isman_s))
3420 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3421
3422 STRCAT(buf, "! ");
3423 if (cap->count0 == 0 && isman_s)
3424 STRCAT(buf, "man");
3425 else
3426 STRCAT(buf, kp);
3427 STRCAT(buf, " ");
3428 if (cap->count0 != 0 && (isman || isman_s))
3429 {
3430 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3431 STRCAT(buf, " ");
3432 }
3433
3434 *ptr_arg = ptr;
3435 return n;
3436}
3437
3438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Handle the commands that use the word under the cursor.
3440 * [g] CTRL-] :ta to current identifier
3441 * [g] 'K' run program for current identifier
3442 * [g] '*' / to current identifier or string
3443 * [g] '#' ? to current identifier or string
3444 * g ']' :tselect for current identifier
3445 */
3446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003447nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 char_u *ptr = NULL;
3450 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003451 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003452 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 char_u *kp; // value of 'keywordprg'
3455 int kp_help; // 'keywordprg' is ":he"
3456 int kp_ex; // 'keywordprg' starts with ":"
3457 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003460 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 cmdchar = cap->nchar;
3466 g_cmd = TRUE;
3467 }
3468 else
3469 {
3470 cmdchar = cap->cmdchar;
3471 g_cmd = FALSE;
3472 }
3473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003474 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 cmdchar = '#';
3476
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003477 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3481 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (checkclearopq(cap->oap))
3483 return;
3484 }
3485
3486 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3487 (cmdchar == '*' || cmdchar == '#')
3488 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3489 {
3490 clearop(cap->oap);
3491 return;
3492 }
3493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // Allocate buffer to put the command in. Inserting backslashes can
3495 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3496 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3498 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3499 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003500 if (kp_help && *skipwhite(ptr) == NUL)
3501 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003502 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003503 return;
3504 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003505 kp_ex = (*kp == ':');
3506 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3507 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (buf == NULL)
3509 return;
3510 buf[0] = NUL;
3511
3512 switch (cmdchar)
3513 {
3514 case '*':
3515 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003516 // Put cursor at start of word, makes search skip the word
3517 // under the cursor.
3518 // Call setpcmark() first, so "*``" puts the cursor back where
3519 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 setpcmark();
3521 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3522
3523 if (!g_cmd && vim_iswordp(ptr))
3524 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003525 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 break;
3527
3528 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003529 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3530 if (n == 0)
3531 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533
3534 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003535 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#ifdef FEAT_CSCOPE
3537 if (p_cst)
3538 STRCPY(buf, "cstag ");
3539 else
3540#endif
3541 STRCPY(buf, "ts ");
3542 break;
3543
3544 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003545 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (curbuf->b_help)
3547 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003549 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003550 if (g_cmd)
3551 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003552 else if (cap->count0 == 0)
3553 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003554 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003555 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003559 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003560 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003562 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003563 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003564 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003565 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003566 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003568 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003569 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003570 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003572 vim_free(buf);
3573 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003575 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003576 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003577 {
3578 vim_free(buf);
3579 vim_free(p);
3580 return;
3581 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003582 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003583 STRCAT(buf, p);
3584 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003586 else
3587 {
3588 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003589 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003590 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003591 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003592 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003593 {
3594 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003595 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003596 aux_ptr = (char_u *)"";
3597 else
3598 aux_ptr = (char_u *)"\\|\"\n[";
3599 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003600 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003601 aux_ptr = (char_u *)"\\|\"\n*?[";
3602
3603 p = buf + STRLEN(buf);
3604 while (n-- > 0)
3605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003606 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 if (vim_strchr(aux_ptr, *ptr) != NULL)
3608 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003609 // When current byte is a part of multibyte character, copy all
3610 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003611 if (has_mbyte)
3612 {
3613 int i;
3614 int len = (*mb_ptr2len)(ptr) - 1;
3615
3616 for (i = 0; i < len && n >= 1; ++i, --n)
3617 *p++ = *ptr++;
3618 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003619 *p++ = *ptr++;
3620 }
3621 *p = NUL;
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003624 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (cmdchar == '*' || cmdchar == '#')
3626 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003627 if (!g_cmd && (has_mbyte
3628 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3629 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003631
3632 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003633 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003635
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003636 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003639 {
3640 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003642 g_tag_at_cursor = FALSE;
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 vim_free(buf);
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648/*
3649 * Get visually selected text, within one line only.
3650 * Returns FAIL if more than one line selected.
3651 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003652 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003653get_visual_text(
3654 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003655 char_u **pp, // return: start of selected text
3656 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (VIsual_mode != 'V')
3659 unadjust_for_sel();
3660 if (VIsual.lnum != curwin->w_cursor.lnum)
3661 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003662 if (cap != NULL)
3663 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 return FAIL;
3665 }
3666 if (VIsual_mode == 'V')
3667 {
3668 *pp = ml_get_curline();
3669 *lenp = (int)STRLEN(*pp);
3670 }
3671 else
3672 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003673 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 *pp = ml_get_pos(&curwin->w_cursor);
3676 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3677 }
3678 else
3679 {
3680 *pp = ml_get_pos(&VIsual);
3681 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3682 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003683 if (**pp == NUL)
3684 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003685 if (*lenp > 0)
3686 {
3687 if (has_mbyte)
3688 // Correct the length to include all bytes of the last
3689 // character.
3690 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3691 else if ((*pp)[*lenp - 1] == NUL)
3692 // Do not include a trailing NUL.
3693 *lenp -= 1;
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 reset_VIsual_and_resel();
3697 return OK;
3698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700/*
3701 * CTRL-T: backwards in tag stack
3702 */
3703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003704nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 if (!checkclearopq(cap->oap))
3707 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3708}
3709
3710/*
3711 * Handle scrolling command 'H', 'L' and 'M'.
3712 */
3713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003714nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 int used = 0;
3717 long n;
3718#ifdef FEAT_FOLDING
3719 linenr_T lnum;
3720#endif
3721 int half;
3722
3723 cap->oap->motion_type = MLINE;
3724 setpcmark();
3725
3726 if (cap->cmdchar == 'L')
3727 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003728 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 curwin->w_cursor.lnum = curwin->w_botline - 1;
3730 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3731 curwin->w_cursor.lnum = 1;
3732 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003733 {
3734#ifdef FEAT_FOLDING
3735 if (hasAnyFolding(curwin))
3736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003737 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003738 for (n = cap->count1 - 1; n > 0
3739 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3740 {
3741 (void)hasFolding(curwin->w_cursor.lnum,
3742 &curwin->w_cursor.lnum, NULL);
3743 --curwin->w_cursor.lnum;
3744 }
3745 }
3746 else
3747#endif
3748 curwin->w_cursor.lnum -= cap->count1 - 1;
3749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 else
3752 {
3753 if (cap->cmdchar == 'M')
3754 {
3755#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003756 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 used -= diff_check_fill(curwin, curwin->w_topline)
3758 - curwin->w_topfill;
3759#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003760 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3762 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3763 {
3764#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003765 // Count half he number of filler lines to be "below this
3766 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3768 + n) / 2 >= half)
3769 {
3770 --n;
3771 break;
3772 }
3773#endif
3774 used += plines(curwin->w_topline + n);
3775 if (used >= half)
3776 break;
3777#ifdef FEAT_FOLDING
3778 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3779 n = lnum - curwin->w_topline;
3780#endif
3781 }
3782 if (n > 0 && used > curwin->w_height)
3783 --n;
3784 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003788#ifdef FEAT_FOLDING
3789 if (hasAnyFolding(curwin))
3790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003791 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003792 lnum = curwin->w_topline;
3793 while (n-- > 0 && lnum < curwin->w_botline - 1)
3794 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003795 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003796 ++lnum;
3797 }
3798 n = lnum - curwin->w_topline;
3799 }
3800#endif
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 curwin->w_cursor.lnum = curwin->w_topline + n;
3803 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3804 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3805 }
3806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003807 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003808 if (cap->oap->op_type == OP_NOP)
3809 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 beginline(BL_SOL | BL_FIX);
3811}
3812
3813/*
3814 * Cursor right commands.
3815 */
3816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003817nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003820 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003822 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003824 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003825 if (mod_mask & MOD_MASK_CTRL)
3826 cap->arg = TRUE;
3827 nv_wordcmd(cap);
3828 return;
3829 }
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 cap->oap->motion_type = MCHAR;
3832 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003833 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003835 // In virtual edit mode, there's no such thing as "past_line", as lines
3836 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003838 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840 for (n = cap->count1; n > 0; --n)
3841 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003842 if ((!past_line && oneright() == FAIL)
3843 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003844 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003846 // <Space> wraps to next line if 'whichwrap' has 's'.
3847 // 'l' wraps to next line if 'whichwrap' has 'l'.
3848 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if ( ((cap->cmdchar == ' '
3850 && vim_strchr(p_ww, 's') != NULL)
3851 || (cap->cmdchar == 'l'
3852 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003853 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 && vim_strchr(p_ww, '>') != NULL))
3855 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003857 // When deleting we also count the NL as a character.
3858 // Set cap->oap->inclusive when last char in the line is
3859 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003860 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003862 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 cap->oap->inclusive = TRUE;
3864 else
3865 {
3866 ++curwin->w_cursor.lnum;
3867 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 curwin->w_set_curswant = TRUE;
3870 cap->oap->inclusive = FALSE;
3871 }
3872 continue;
3873 }
3874 if (cap->oap->op_type == OP_NOP)
3875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003876 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (n == cap->count1)
3878 beep_flush();
3879 }
3880 else
3881 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003882 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 cap->oap->inclusive = TRUE;
3884 }
3885 break;
3886 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003887 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
3889 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (virtual_active())
3891 oneright();
3892 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003895 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ++curwin->w_cursor.col;
3898 }
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901#ifdef FEAT_FOLDING
3902 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3903 && cap->oap->op_type == OP_NOP)
3904 foldOpenCursor();
3905#endif
3906}
3907
3908/*
3909 * Cursor left commands.
3910 *
3911 * Returns TRUE when operator end should not be adjusted.
3912 */
3913 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003914nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 long n;
3917
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003920 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003921 if (mod_mask & MOD_MASK_CTRL)
3922 cap->arg = 1;
3923 nv_bck_word(cap);
3924 return;
3925 }
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 cap->oap->motion_type = MCHAR;
3928 cap->oap->inclusive = FALSE;
3929 for (n = cap->count1; n > 0; --n)
3930 {
3931 if (oneleft() == FAIL)
3932 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003933 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3934 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3935 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if ( (((cap->cmdchar == K_BS
3937 || cap->cmdchar == Ctrl_H)
3938 && vim_strchr(p_ww, 'b') != NULL)
3939 || (cap->cmdchar == 'h'
3940 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003941 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 && vim_strchr(p_ww, '<') != NULL))
3943 && curwin->w_cursor.lnum > 1)
3944 {
3945 --(curwin->w_cursor.lnum);
3946 coladvance((colnr_T)MAXCOL);
3947 curwin->w_set_curswant = TRUE;
3948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003949 // When the NL before the first char has to be deleted we
3950 // put the cursor on the NUL after the previous line.
3951 // This is a very special case, be careful!
3952 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if ( (cap->oap->op_type == OP_DELETE
3954 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003955 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003957 char_u *cp = ml_get_cursor();
3958
3959 if (*cp != NUL)
3960 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003961 if (has_mbyte)
3962 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3963 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003964 ++curwin->w_cursor.col;
3965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 cap->retval |= CA_NO_ADJ_OP_END;
3967 }
3968 continue;
3969 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003970 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3972 beep_flush();
3973 break;
3974 }
3975 }
3976#ifdef FEAT_FOLDING
3977 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3978 && cap->oap->op_type == OP_NOP)
3979 foldOpenCursor();
3980#endif
3981}
3982
3983/*
3984 * Cursor up commands.
3985 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3986 */
3987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003988nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003990 if (mod_mask & MOD_MASK_SHIFT)
3991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003992 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993 cap->arg = BACKWARD;
3994 nv_page(cap);
3995 }
3996 else
3997 {
3998 cap->oap->motion_type = MLINE;
3999 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4000 clearopbeep(cap->oap);
4001 else if (cap->arg)
4002 beginline(BL_WHITE | BL_FIX);
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004}
4005
4006/*
4007 * Cursor down commands.
4008 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4009 */
4010 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004011nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004013 if (mod_mask & MOD_MASK_SHIFT)
4014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004015 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 cap->arg = FORWARD;
4017 nv_page(cap);
4018 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004019#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004021 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4022 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004028 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 cmdwin_result = CAR;
4030 else
4031#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004032#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004033 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004034 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4035 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4036 {
4037 invoke_prompt_callback();
4038 if (restart_edit == 0)
4039 restart_edit = 'a';
4040 }
4041 else
4042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
4044 cap->oap->motion_type = MLINE;
4045 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4046 clearopbeep(cap->oap);
4047 else if (cap->arg)
4048 beginline(BL_WHITE | BL_FIX);
4049 }
4050 }
4051}
4052
4053#ifdef FEAT_SEARCHPATH
4054/*
4055 * Grab the file name under the cursor and edit it.
4056 */
4057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004061 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004063 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 if (curbuf_locked())
4066 {
4067 clearop(cap->oap);
4068 return;
4069 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004070#ifdef FEAT_PROP_POPUP
4071 if (ERROR_IF_TERM_POPUP_WINDOW)
4072 return;
4073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004075 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 if (ptr != NULL)
4078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004079 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004080 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004081 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004083 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004084 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004085 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004086 {
4087 curwin->w_cursor.lnum = lnum;
4088 check_cursor_lnum();
4089 beginline(BL_SOL | BL_FIX);
4090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 vim_free(ptr);
4092 }
4093 else
4094 clearop(cap->oap);
4095}
4096#endif
4097
4098/*
4099 * <End> command: to end of current line or last line.
4100 */
4101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004102nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004104 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004108 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 nv_dollar(cap);
4111}
4112
4113/*
4114 * Handle the "$" command.
4115 */
4116 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004117nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 cap->oap->motion_type = MCHAR;
4120 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004121 // In virtual mode when off the edge of a line and an operator
4122 // is pending (whew!) keep the cursor where it is.
4123 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (!virtual_active() || gchar_cursor() != NUL
4125 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (cursor_down((long)(cap->count1 - 1),
4128 cap->oap->op_type == OP_NOP) == FAIL)
4129 clearopbeep(cap->oap);
4130#ifdef FEAT_FOLDING
4131 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4132 foldOpenCursor();
4133#endif
4134}
4135
4136/*
4137 * Implementation of '?' and '/' commands.
4138 * If cap->arg is TRUE don't set PC mark.
4139 */
4140 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004141nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004144 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004148 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 cap->cmdchar = 'g';
4150 cap->nchar = '?';
4151 nv_operator(cap);
4152 return;
4153 }
4154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004155 // When using 'incsearch' the cursor may be moved to set a different search
4156 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004157 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 if (cap->searchbuf == NULL)
4160 {
4161 clearop(oap);
4162 return;
4163 }
4164
Bram Moolenaar46539112015-02-17 15:43:57 +01004165 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004166 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004167 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168}
4169
LemonBoya4399382022-04-09 21:04:08 +01004170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171/*
4172 * Handle "N" and "n" commands.
4173 * cap->arg is SEARCH_REV for "N", 0 for "n".
4174 */
4175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004176nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004178 pos_T old = curwin->w_cursor;
4179 int wrapped = FALSE;
4180 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004181
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004182 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004183 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004184 // Avoid getting stuck on the current cursor position, which can
4185 // happen when an offset is given and the cursor is on the last char
4186 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004187 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004188 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004189 cap->count1 -= 1;
4190 }
LemonBoya4399382022-04-09 21:04:08 +01004191
4192#ifdef FEAT_SEARCH_EXTRA
4193 // Redraw the window to refresh the highlighted matches.
4194 if (i > 0 && p_hls && !no_hlsearch)
4195 redraw_later(SOME_VALID);
4196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197}
4198
4199/*
4200 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4201 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004202 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004204 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004205normal_search(
4206 cmdarg_T *cap,
4207 int dir,
4208 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004209 int opt, // extra flags for do_search()
4210 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004213 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004214#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004215 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 cap->oap->motion_type = MCHAR;
4219 cap->oap->inclusive = FALSE;
4220 cap->oap->use_reg_one = TRUE;
4221 curwin->w_set_curswant = TRUE;
4222
Bram Moolenaara80faa82020-04-12 19:37:17 +02004223 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004224 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004225 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4226 if (wrapped != NULL)
4227 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (i == 0)
4229 clearop(cap->oap);
4230 else
4231 {
4232 if (i == 2)
4233 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#ifdef FEAT_FOLDING
4236 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4237 foldOpenCursor();
4238#endif
4239 }
LemonBoya4399382022-04-09 21:04:08 +01004240#ifdef FEAT_SEARCH_EXTRA
4241 // Redraw the window to refresh the highlighted matches.
4242 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
4243 redraw_later(SOME_VALID);
4244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004246 // "/$" will put the cursor after the end of the line, may need to
4247 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004249 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250}
4251
4252/*
4253 * Character search commands.
4254 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4255 * ',' and FALSE for ';'.
4256 * cap->nchar is NUL for ',' and ';' (repeat the search)
4257 */
4258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004259nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260{
4261 int t_cmd;
4262
4263 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4264 t_cmd = TRUE;
4265 else
4266 t_cmd = FALSE;
4267
4268 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4270 clearopbeep(cap->oap);
4271 else
4272 {
4273 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004274 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4276 && (t_cmd || cap->oap->op_type != OP_NOP))
4277 {
4278 colnr_T scol, ecol;
4279
4280 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4281 curwin->w_cursor.coladd = ecol - scol;
4282 }
4283 else
4284 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef FEAT_FOLDING
4287 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4288 foldOpenCursor();
4289#endif
4290 }
4291}
4292
4293/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004294 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4295 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4296 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4297 * "[m" or "]m" search for prev/next start of (Java) method.
4298 * "[M" or "]M" search for prev/next end of (Java) method.
4299 */
4300 static void
4301nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4302{
4303 pos_T new_pos = {0, 0, 0};
4304 pos_T *pos = NULL; // init for GCC
4305 pos_T prev_pos;
4306 long n;
4307 int findc;
4308 int c;
4309
4310 if (cap->nchar == '*')
4311 cap->nchar = '/';
4312 prev_pos.lnum = 0;
4313 if (cap->nchar == 'm' || cap->nchar == 'M')
4314 {
4315 if (cap->cmdchar == '[')
4316 findc = '{';
4317 else
4318 findc = '}';
4319 n = 9999;
4320 }
4321 else
4322 {
4323 findc = cap->nchar;
4324 n = cap->count1;
4325 }
4326 for ( ; n > 0; --n)
4327 {
4328 if ((pos = findmatchlimit(cap->oap, findc,
4329 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4330 {
4331 if (new_pos.lnum == 0) // nothing found
4332 {
4333 if (cap->nchar != 'm' && cap->nchar != 'M')
4334 clearopbeep(cap->oap);
4335 }
4336 else
4337 pos = &new_pos; // use last one found
4338 break;
4339 }
4340 prev_pos = new_pos;
4341 curwin->w_cursor = *pos;
4342 new_pos = *pos;
4343 }
4344 curwin->w_cursor = *old_pos;
4345
4346 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4347 // brought us to the match for "[m" and "]M" when inside a method.
4348 // Try finding the '{' or '}' we want to be at.
4349 // Also repeat for the given count.
4350 if (cap->nchar == 'm' || cap->nchar == 'M')
4351 {
4352 // norm is TRUE for "]M" and "[m"
4353 int norm = ((findc == '{') == (cap->nchar == 'm'));
4354
4355 n = cap->count1;
4356 // found a match: we were inside a method
4357 if (prev_pos.lnum != 0)
4358 {
4359 pos = &prev_pos;
4360 curwin->w_cursor = prev_pos;
4361 if (norm)
4362 --n;
4363 }
4364 else
4365 pos = NULL;
4366 while (n > 0)
4367 {
4368 for (;;)
4369 {
4370 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4371 {
4372 // if not found anything, that's an error
4373 if (pos == NULL)
4374 clearopbeep(cap->oap);
4375 n = 0;
4376 break;
4377 }
4378 c = gchar_cursor();
4379 if (c == '{' || c == '}')
4380 {
4381 // Must have found end/start of class: use it.
4382 // Or found the place to be at.
4383 if ((c == findc && norm) || (n == 1 && !norm))
4384 {
4385 new_pos = curwin->w_cursor;
4386 pos = &new_pos;
4387 n = 0;
4388 }
4389 // if no match found at all, we started outside of the
4390 // class and we're inside now. Just go on.
4391 else if (new_pos.lnum == 0)
4392 {
4393 new_pos = curwin->w_cursor;
4394 pos = &new_pos;
4395 }
4396 // found start/end of other method: go to match
4397 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004398 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4399 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004400 n = 0;
4401 else
4402 curwin->w_cursor = *pos;
4403 break;
4404 }
4405 }
4406 --n;
4407 }
4408 curwin->w_cursor = *old_pos;
4409 if (pos == NULL && new_pos.lnum != 0)
4410 clearopbeep(cap->oap);
4411 }
4412 if (pos != NULL)
4413 {
4414 setpcmark();
4415 curwin->w_cursor = *pos;
4416 curwin->w_set_curswant = TRUE;
4417#ifdef FEAT_FOLDING
4418 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4419 && cap->oap->op_type == OP_NOP)
4420 foldOpenCursor();
4421#endif
4422 }
4423}
4424
4425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 * "[" and "]" commands.
4427 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4428 */
4429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004430nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004433 pos_T *pos = NULL; // init for GCC
4434 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 int flag;
4436 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 cap->oap->motion_type = MCHAR;
4439 cap->oap->inclusive = FALSE;
4440 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004441 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
4443#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004444 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (cap->nchar == 'f')
4446 nv_gotofile(cap);
4447 else
4448#endif
4449
4450#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004451 // Find the occurrence(s) of the identifier or define under cursor
4452 // in current and included files or jump to the first occurrence.
4453 //
4454 // search list jump
4455 // fwd bwd fwd bwd fwd bwd
4456 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4457 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004458 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
4460 char_u *ptr;
4461 int len;
4462
4463 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4464 clearop(cap->oap);
4465 else
4466 {
4467 find_pattern_in_path(ptr, 0, len, TRUE,
4468 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4469 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4470 cap->count1,
4471 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4472 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4473 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4474 (linenr_T)MAXLNUM);
4475 curwin->w_set_curswant = TRUE;
4476 }
4477 }
4478 else
4479#endif
4480
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004481 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4482 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4483 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4484 // "[m" or "]m" search for prev/next start of (Java) method.
4485 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if ( (cap->cmdchar == '['
4487 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4488 || (cap->cmdchar == ']'
4489 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004490 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004492 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 else if (cap->nchar == '[' || cap->nchar == ']')
4494 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004495 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 flag = '{';
4497 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004498 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004501 // Imitate strange Vi behaviour: When using "]]" with an operator
4502 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004503 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 (cap->oap->op_type != OP_NOP
4505 && cap->arg == FORWARD && flag == '{')))
4506 clearopbeep(cap->oap);
4507 else
4508 {
4509 if (cap->oap->op_type == OP_NOP)
4510 beginline(BL_WHITE | BL_FIX);
4511#ifdef FEAT_FOLDING
4512 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4513 foldOpenCursor();
4514#endif
4515 }
4516 }
4517
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004518 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 else if (cap->nchar == 'p' || cap->nchar == 'P')
4520 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004521 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004524 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 else if (cap->nchar == '\'' || cap->nchar == '`')
4526 {
4527 pos = &curwin->w_cursor;
4528 for (n = cap->count1; n > 0; --n)
4529 {
4530 prev_pos = *pos;
4531 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4532 cap->nchar == '\'');
4533 if (pos == NULL)
4534 break;
4535 }
4536 if (pos == NULL)
4537 pos = &prev_pos;
4538 nv_cursormark(cap, cap->nchar == '\'', pos);
4539 }
4540
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004541 // [ or ] followed by a middle mouse click: put selected text with
4542 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004543 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 (void)do_mouse(cap->oap, cap->nchar,
4546 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4547 cap->count1, PUT_FIXINDENT);
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004551 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else if (cap->nchar == 'z')
4553 {
4554 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4555 cap->count1) == FAIL)
4556 clearopbeep(cap->oap);
4557 }
4558#endif
4559
4560#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004561 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else if (cap->nchar == 'c')
4563 {
4564 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4565 cap->count1) == FAIL)
4566 clearopbeep(cap->oap);
4567 }
4568#endif
4569
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004570#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004571 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004572 else if (cap->nchar == 's' || cap->nchar == 'S')
4573 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004574 setpcmark();
4575 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004576 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4577 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004578 {
4579 clearopbeep(cap->oap);
4580 break;
4581 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004582 else
4583 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004584# ifdef FEAT_FOLDING
4585 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4586 foldOpenCursor();
4587# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004588 }
4589#endif
4590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004591 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 else
4593 clearopbeep(cap->oap);
4594}
4595
4596/*
4597 * Handle Normal mode "%" command.
4598 */
4599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004600nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
4602 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004603#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 linenr_T lnum = curwin->w_cursor.lnum;
4605#endif
4606
4607 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004608 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 if (cap->count0 > 100)
4611 clearopbeep(cap->oap);
4612 else
4613 {
4614 cap->oap->motion_type = MLINE;
4615 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004616 // Round up, so 'normal 100%' always jumps at the line line.
4617 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4618 // overflow on 32-bits, so use a formula with less accuracy
4619 // to avoid overflows.
4620 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4622 / 100L * cap->count0;
4623 else
4624 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4625 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004626 if (curwin->w_cursor.lnum < 1)
4627 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4629 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4630 beginline(BL_SOL | BL_FIX);
4631 }
4632 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004633 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 cap->oap->motion_type = MCHAR;
4636 cap->oap->use_reg_one = TRUE;
4637 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4638 clearopbeep(cap->oap);
4639 else
4640 {
4641 setpcmark();
4642 curwin->w_cursor = *pos;
4643 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647 }
4648#ifdef FEAT_FOLDING
4649 if (cap->oap->op_type == OP_NOP
4650 && lnum != curwin->w_cursor.lnum
4651 && (fdo_flags & FDO_PERCENT)
4652 && KeyTyped)
4653 foldOpenCursor();
4654#endif
4655}
4656
4657/*
4658 * Handle "(" and ")" commands.
4659 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4660 */
4661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004662nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 cap->oap->motion_type = MCHAR;
4665 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004666 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004667 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 curwin->w_set_curswant = TRUE;
4669
4670 if (findsent(cap->arg, cap->count1) == FAIL)
4671 clearopbeep(cap->oap);
4672 else
4673 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004674 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004675 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#ifdef FEAT_FOLDING
4678 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4679 foldOpenCursor();
4680#endif
4681 }
4682}
4683
4684/*
4685 * "m" command: Mark a position.
4686 */
4687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 if (!checkclearop(cap->oap))
4691 {
4692 if (setmark(cap->nchar) == FAIL)
4693 clearopbeep(cap->oap);
4694 }
4695}
4696
4697/*
4698 * "{" and "}" commands.
4699 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4700 */
4701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004702nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 cap->oap->motion_type = MCHAR;
4705 cap->oap->inclusive = FALSE;
4706 cap->oap->use_reg_one = TRUE;
4707 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004708 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 clearopbeep(cap->oap);
4710 else
4711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#ifdef FEAT_FOLDING
4714 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4715 foldOpenCursor();
4716#endif
4717 }
4718}
4719
4720/*
4721 * "u" command: Undo or make lower case.
4722 */
4723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004724nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004726 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004728 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 cap->cmdchar = 'g';
4730 cap->nchar = 'u';
4731 nv_operator(cap);
4732 }
4733 else
4734 nv_kundo(cap);
4735}
4736
4737/*
4738 * <Undo> command.
4739 */
4740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004741nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 if (!checkclearopq(cap->oap))
4744 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004745#ifdef FEAT_JOB_CHANNEL
4746 if (bt_prompt(curbuf))
4747 {
4748 clearopbeep(cap->oap);
4749 return;
4750 }
4751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 u_undo((int)cap->count1);
4753 curwin->w_set_curswant = TRUE;
4754 }
4755}
4756
4757/*
4758 * Handle the "r" command.
4759 */
4760 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004761nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 char_u *ptr;
4764 int had_ctrl_v;
4765 long n;
4766
4767 if (checkclearop(cap->oap))
4768 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004769#ifdef FEAT_JOB_CHANNEL
4770 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4771 {
4772 clearopbeep(cap->oap);
4773 return;
4774 }
4775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004777 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (cap->nchar == Ctrl_V)
4779 {
4780 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004781 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (cap->nchar > DEL)
4784 had_ctrl_v = NUL;
4785 }
4786 else
4787 had_ctrl_v = NUL;
4788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004790 if (IS_SPECIAL(cap->nchar))
4791 {
4792 clearopbeep(cap->oap);
4793 return;
4794 }
4795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004796 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (VIsual_active)
4798 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004799 if (got_int)
4800 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004801 if (had_ctrl_v)
4802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Use a special (negative) number to make a difference between a
4804 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004805 if (cap->nchar == CAR)
4806 cap->nchar = REPLACE_CR_NCHAR;
4807 else if (cap->nchar == NL)
4808 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 nv_operator(cap);
4811 return;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004814 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (virtual_active())
4816 {
4817 if (u_save_cursor() == FAIL)
4818 return;
4819 if (gchar_cursor() == NUL)
4820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004821 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 coladvance_force((colnr_T)(getviscol() + cap->count1));
4823 curwin->w_cursor.col -= cap->count1;
4824 }
4825 else if (gchar_cursor() == TAB)
4826 coladvance_force(getviscol());
4827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004829 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004831 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004832 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 clearopbeep(cap->oap);
4835 return;
4836 }
4837
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004838 // Replacing with a TAB is done by edit() when it is complicated because
4839 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4840 // Other characters are done below to avoid problems with things like
4841 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4843 {
4844 stuffnumReadbuff(cap->count1);
4845 stuffcharReadbuff('R');
4846 stuffcharReadbuff('\t');
4847 stuffcharReadbuff(ESC);
4848 return;
4849 }
4850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (u_save_cursor() == FAIL)
4853 return;
4854
4855 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4856 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004857 // Replace character(s) by a single newline.
4858 // Strange vi behaviour: Only one newline is inserted.
4859 // Delete the characters here.
4860 // Insert the newline with an insert command, takes care of
4861 // autoindent. The insert command depends on being on the last
4862 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004863 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 stuffcharReadbuff('\r');
4865 stuffcharReadbuff(ESC);
4866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004867 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 invoke_edit(cap, TRUE, 'r', FALSE);
4869 }
4870 else
4871 {
4872 prep_redo(cap->oap->regname, cap->count1,
4873 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4874
4875 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (has_mbyte)
4877 {
4878 int old_State = State;
4879
4880 if (cap->ncharC1 != 0)
4881 AppendCharToRedobuff(cap->ncharC1);
4882 if (cap->ncharC2 != 0)
4883 AppendCharToRedobuff(cap->ncharC2);
4884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 // This is slow, but it handles replacing a single-byte with a
4886 // multi-byte and the other way around. Also handles adding
4887 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 for (n = cap->count1; n > 0; --n)
4889 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004890 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004891 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4892 {
4893 int c = ins_copychar(curwin->w_cursor.lnum
4894 + (cap->nchar == Ctrl_Y ? -1 : 1));
4895 if (c != NUL)
4896 ins_char(c);
4897 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004898 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004899 ++curwin->w_cursor.col;
4900 }
4901 else
4902 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 State = old_State;
4904 if (cap->ncharC1 != 0)
4905 ins_char(cap->ncharC1);
4906 if (cap->ncharC2 != 0)
4907 ins_char(cap->ncharC2);
4908 }
4909 }
4910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004912 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 for (n = cap->count1; n > 0; --n)
4914 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004915 // Get ptr again, because u_save and/or showmatch() will have
4916 // released the line. This may also happen in ins_copychar().
4917 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004918 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4919 {
4920 int c = ins_copychar(curwin->w_cursor.lnum
4921 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004922
4923 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004924 if (c != NUL)
4925 ptr[curwin->w_cursor.col] = c;
4926 }
4927 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004928 {
4929 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004930 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (p_sm && msg_silent == 0)
4933 showmatch(cap->nchar);
4934 ++curwin->w_cursor.col;
4935 }
4936#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004937 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004939 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar009b2592004-10-24 19:18:58 +00004941 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004942 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004944 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946#endif
4947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 changed_bytes(curwin->w_cursor.lnum,
4950 (colnr_T)(curwin->w_cursor.col - cap->count1));
4951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 --curwin->w_cursor.col; // cursor on the last replaced char
4953 // if the character on the left of the current cursor is a multi-byte
4954 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (has_mbyte)
4956 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 curbuf->b_op_end = curwin->w_cursor;
4958 curwin->w_set_curswant = TRUE;
4959 set_last_insert(cap->nchar);
4960 }
4961}
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * 'o': Exchange start and end of Visual area.
4965 * 'O': same, but in block mode exchange left and right corners.
4966 */
4967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004968v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 pos_T old_cursor;
4971 colnr_T left, right;
4972
4973 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4974 {
4975 old_cursor = curwin->w_cursor;
4976 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4977 curwin->w_cursor.lnum = VIsual.lnum;
4978 coladvance(left);
4979 VIsual = curwin->w_cursor;
4980
4981 curwin->w_cursor.lnum = old_cursor.lnum;
4982 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 // 'selection "exclusive" and cursor at right-bottom corner: move it
4984 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4986 ++curwin->w_curswant;
4987 coladvance(curwin->w_curswant);
4988 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004990 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 curwin->w_cursor.lnum = VIsual.lnum;
4993 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4994 ++right;
4995 coladvance(right);
4996 VIsual = curwin->w_cursor;
4997
4998 curwin->w_cursor.lnum = old_cursor.lnum;
4999 coladvance(left);
5000 curwin->w_curswant = left;
5001 }
5002 }
5003 else
5004 {
5005 old_cursor = curwin->w_cursor;
5006 curwin->w_cursor = VIsual;
5007 VIsual = old_cursor;
5008 curwin->w_set_curswant = TRUE;
5009 }
5010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
5012/*
5013 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5014 */
5015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005016nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 cap->cmdchar = 'c';
5021 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 VIsual_mode = 'V';
5024 nv_operator(cap);
5025 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005026 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005029 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 else
5031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (virtual_active())
5033 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5035 }
5036 }
5037}
5038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039/*
5040 * "gr".
5041 */
5042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005043nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (VIsual_active)
5046 {
5047 cap->cmdchar = 'r';
5048 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005049 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005051 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005054 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 else
5056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005057 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005058 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 stuffcharReadbuff(cap->extra_char);
5060 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (virtual_active())
5062 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 invoke_edit(cap, TRUE, 'v', FALSE);
5064 }
5065 }
5066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068/*
5069 * Swap case for "~" command, when it does not work like an operator.
5070 */
5071 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005072n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074 long n;
5075 pos_T startpos;
5076 int did_change = 0;
5077#ifdef FEAT_NETBEANS_INTG
5078 pos_T pos;
5079 char_u *ptr;
5080 int count;
5081#endif
5082
5083 if (checkclearopq(cap->oap))
5084 return;
5085
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005086 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 clearopbeep(cap->oap);
5089 return;
5090 }
5091
5092 prep_redo_cmd(cap);
5093
5094 if (u_save_cursor() == FAIL)
5095 return;
5096
5097 startpos = curwin->w_cursor;
5098#ifdef FEAT_NETBEANS_INTG
5099 pos = startpos;
5100#endif
5101 for (n = cap->count1; n > 0; --n)
5102 {
5103 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5104 inc_cursor();
5105 if (gchar_cursor() == NUL)
5106 {
5107 if (vim_strchr(p_ww, '~') != NULL
5108 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5109 {
5110#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005111 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 if (did_change)
5114 {
5115 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005116 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005117 netbeans_removed(curbuf, pos.lnum, pos.col,
5118 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005120 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122 pos.col = 0;
5123 pos.lnum++;
5124 }
5125#endif
5126 ++curwin->w_cursor.lnum;
5127 curwin->w_cursor.col = 0;
5128 if (n > 1)
5129 {
5130 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5131 break;
5132 u_clearline();
5133 }
5134 }
5135 else
5136 break;
5137 }
5138 }
5139#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005140 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 ptr = ml_get(pos.lnum);
5143 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005144 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5145 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147#endif
5148
5149
5150 check_cursor();
5151 curwin->w_set_curswant = TRUE;
5152 if (did_change)
5153 {
5154 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5155 0L);
5156 curbuf->b_op_start = startpos;
5157 curbuf->b_op_end = curwin->w_cursor;
5158 if (curbuf->b_op_end.col > 0)
5159 --curbuf->b_op_end.col;
5160 }
5161}
5162
5163/*
5164 * Move cursor to mark.
5165 */
5166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005167nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 if (check_mark(pos) == FAIL)
5170 clearop(cap->oap);
5171 else
5172 {
5173 if (cap->cmdchar == '\''
5174 || cap->cmdchar == '`'
5175 || cap->cmdchar == '['
5176 || cap->cmdchar == ']')
5177 setpcmark();
5178 curwin->w_cursor = *pos;
5179 if (flag)
5180 beginline(BL_WHITE | BL_FIX);
5181 else
5182 check_cursor();
5183 }
5184 cap->oap->motion_type = flag ? MLINE : MCHAR;
5185 if (cap->cmdchar == '`')
5186 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005187 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 curwin->w_set_curswant = TRUE;
5189}
5190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191/*
5192 * Handle commands that are operators in Visual mode.
5193 */
5194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005195v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005199 // Uppercase means linewise, except in block mode, then "D" deletes till
5200 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (isupper(cap->cmdchar))
5202 {
5203 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005204 {
5205 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5209 curwin->w_curswant = MAXCOL;
5210 }
5211 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5212 nv_operator(cap);
5213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215/*
5216 * "s" and "S" commands.
5217 */
5218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005219nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005221#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005222 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005223 if (term_swap_diff() == OK)
5224 return;
5225#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005226#ifdef FEAT_JOB_CHANNEL
5227 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5228 {
5229 clearopbeep(cap->oap);
5230 return;
5231 }
5232#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005233 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
5235 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005236 {
5237 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 cap->cmdchar = 'c';
5241 nv_operator(cap);
5242 }
5243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 nv_optrans(cap);
5245}
5246
5247/*
5248 * Abbreviated commands.
5249 */
5250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005254 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005256 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 if (VIsual_active)
5258 v_visop(cap);
5259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 nv_optrans(cap);
5261}
5262
5263/*
5264 * Translate a command into another command.
5265 */
5266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005267nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5270 (char_u *)"d$", (char_u *)"c$",
5271 (char_u *)"cl", (char_u *)"cc",
5272 (char_u *)"yy", (char_u *)":s\r"};
5273 static char_u *str = (char_u *)"xXDCsSY&";
5274
5275 if (!checkclearopq(cap->oap))
5276 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005277 // In Vi "2D" doesn't delete the next line. Can't translate it
5278 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005279 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5280 {
5281 cap->oap->start = curwin->w_cursor;
5282 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005283#ifdef FEAT_EVAL
5284 set_op_var(OP_DELETE);
5285#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005286 cap->count1 = 1;
5287 nv_dollar(cap);
5288 finish_op = TRUE;
5289 ResetRedobuff();
5290 AppendCharToRedobuff('D');
5291 }
5292 else
5293 {
5294 if (cap->count0)
5295 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005296 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299 cap->opcount = 0;
5300}
5301
5302/*
5303 * "'" and "`" commands. Also for "g'" and "g`".
5304 * cap->arg is TRUE for "'" and "g'".
5305 */
5306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 pos_T *pos;
5310 int c;
5311#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005312 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#endif
5315
5316 if (cap->cmdchar == 'g')
5317 c = cap->extra_char;
5318 else
5319 c = cap->nchar;
5320 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 if (cap->arg)
5324 {
5325 check_cursor_lnum();
5326 beginline(BL_WHITE | BL_FIX);
5327 }
5328 else
5329 check_cursor();
5330 }
5331 else
5332 nv_cursormark(cap, cap->arg, pos);
5333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005334 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (!virtual_active())
5336 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005337 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#ifdef FEAT_FOLDING
5339 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005340 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005341 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 && (fdo_flags & FDO_MARK)
5343 && old_KeyTyped)
5344 foldOpenCursor();
5345#endif
5346}
5347
5348/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005349 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
5351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005355#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005357 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 if (!checkclearopq(cap->oap))
5361 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005362 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5363 {
5364 if (goto_tabpage_lastused() == FAIL)
5365 clearopbeep(cap->oap);
5366 return;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (cap->cmdchar == 'g')
5369 pos = movechangelist((int)cap->count1);
5370 else
5371 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005372 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 curwin->w_set_curswant = TRUE;
5375 check_cursor();
5376 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005377 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 nv_cursormark(cap, FALSE, pos);
5379 else if (cap->cmdchar == 'g')
5380 {
5381 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005382 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005384 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005386 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 else
5389 clearopbeep(cap->oap);
5390# ifdef FEAT_FOLDING
5391 if (cap->oap->op_type == OP_NOP
5392 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5393 && (fdo_flags & FDO_MARK)
5394 && old_KeyTyped)
5395 foldOpenCursor();
5396# endif
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398}
5399
5400/*
5401 * Handle '"' command.
5402 */
5403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005404nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 if (checkclearop(cap->oap))
5407 return;
5408#ifdef FEAT_EVAL
5409 if (cap->nchar == '=')
5410 cap->nchar = get_expr_register();
5411#endif
5412 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5413 {
5414 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005415 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#ifdef FEAT_EVAL
5417 set_reg_var(cap->oap->regname);
5418#endif
5419 }
5420 else
5421 clearopbeep(cap->oap);
5422}
5423
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424/*
5425 * Handle "v", "V" and "CTRL-V" commands.
5426 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5427 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005428 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 */
5430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005431nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005433 if (cap->cmdchar == Ctrl_Q)
5434 cap->cmdchar = Ctrl_V;
5435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5437 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (cap->oap->op_type != OP_NOP)
5439 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005440 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return;
5443 }
5444
5445 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005448 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 else // toggle char/block mode
5451 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 VIsual_mode = cap->cmdchar;
5453 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005454 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005458 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
5460 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005461 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005463 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 VIsual = curwin->w_cursor;
5465
5466 VIsual_active = TRUE;
5467 VIsual_reselect = TRUE;
5468 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005472 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005473 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005474 // For V and ^V, we multiply the number of lines even if there
5475 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5477 {
5478 curwin->w_cursor.lnum +=
5479 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005480 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 VIsual_mode = resel_VIsual_mode;
5483 if (VIsual_mode == 'v')
5484 {
5485 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005486 {
5487 validate_virtcol();
5488 curwin->w_curswant = curwin->w_virtcol
5489 + resel_VIsual_vcol * cap->count0 - 1;
5490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005492 curwin->w_curswant = resel_VIsual_vcol;
5493 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005495 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 curwin->w_curswant = MAXCOL;
5498 coladvance((colnr_T)MAXCOL);
5499 }
5500 else if (VIsual_mode == Ctrl_V)
5501 {
5502 validate_virtcol();
5503 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005504 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 coladvance(curwin->w_curswant);
5506 }
5507 else
5508 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005509 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 else
5512 {
5513 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005514 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 may_start_select('c');
5516 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005517 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005518 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005519 if (cap->count0 > 0 && --cap->count1 > 0)
5520 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005522 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5523 nv_right(cap);
5524 else if (VIsual_mode == 'V')
5525 nv_down(cap);
5526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 }
5528 }
5529}
5530
5531/*
5532 * Start selection for Shift-movement keys.
5533 */
5534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005535start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005537 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 may_start_select('k');
5539 n_start_visual_mode('v');
5540}
5541
5542/*
5543 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005544 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
5546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005547may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005549 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5550 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
5552
5553/*
5554 * Start Visual mode "c".
5555 * Should set VIsual_select before calling this.
5556 */
5557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005560#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005561 int cursor_line_was_concealed = curwin->w_p_cole > 0
5562 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005563#endif
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 VIsual_mode = c;
5566 VIsual_active = TRUE;
5567 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005568
5569 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005570 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005571 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005572 {
5573 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 VIsual = curwin->w_cursor;
5577
5578#ifdef FEAT_FOLDING
5579 foldAdjustVisual();
5580#endif
5581
LemonBoy2bf52dd2022-04-09 18:17:34 +01005582 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005584#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005585 // Check if redraw is needed after changing the state.
5586 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005587#endif
5588
Bram Moolenaar09df3122006-01-23 22:23:09 +00005589 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005590 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 // Make sure the clipboard gets updated. Needed because start and
5593 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 clip_star.vmode = NUL;
5595#endif
5596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 // Only need to redraw this line, unless still need to redraw an old
5598 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (curwin->w_redr_type < INVERTED)
5600 {
5601 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5602 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5603 }
5604}
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
5607/*
5608 * CTRL-W: Window commands
5609 */
5610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005611nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005613 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005616 cap->cmdchar = ':';
5617 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005618 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005619 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005620 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623
5624/*
5625 * CTRL-Z: Suspend
5626 */
5627 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005628nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005633 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635
5636/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005637 * "gv": Reselect the previous Visual area. If Visual already active,
5638 * exchange previous and current Visual area.
5639 */
5640 static void
5641nv_gv_cmd(cmdarg_T *cap)
5642{
5643 pos_T tpos;
5644 int i;
5645
5646 if (checkclearop(cap->oap))
5647 return;
5648
5649 if (curbuf->b_visual.vi_start.lnum == 0
5650 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5651 || curbuf->b_visual.vi_end.lnum == 0)
5652 {
5653 beep_flush();
5654 return;
5655 }
5656
5657 // set w_cursor to the start of the Visual area, tpos to the end
5658 if (VIsual_active)
5659 {
5660 i = VIsual_mode;
5661 VIsual_mode = curbuf->b_visual.vi_mode;
5662 curbuf->b_visual.vi_mode = i;
5663# ifdef FEAT_EVAL
5664 curbuf->b_visual_mode_eval = i;
5665# endif
5666 i = curwin->w_curswant;
5667 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5668 curbuf->b_visual.vi_curswant = i;
5669
5670 tpos = curbuf->b_visual.vi_end;
5671 curbuf->b_visual.vi_end = curwin->w_cursor;
5672 curwin->w_cursor = curbuf->b_visual.vi_start;
5673 curbuf->b_visual.vi_start = VIsual;
5674 }
5675 else
5676 {
5677 VIsual_mode = curbuf->b_visual.vi_mode;
5678 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5679 tpos = curbuf->b_visual.vi_end;
5680 curwin->w_cursor = curbuf->b_visual.vi_start;
5681 }
5682
5683 VIsual_active = TRUE;
5684 VIsual_reselect = TRUE;
5685
5686 // Set Visual to the start and w_cursor to the end of the Visual
5687 // area. Make sure they are on an existing character.
5688 check_cursor();
5689 VIsual = curwin->w_cursor;
5690 curwin->w_cursor = tpos;
5691 check_cursor();
5692 update_topline();
5693
5694 // When called from normal "g" command: start Select mode when
5695 // 'selectmode' contains "cmd". When called for K_SELECT, always
5696 // start Select mode.
5697 if (cap->arg)
5698 {
5699 VIsual_select = TRUE;
5700 VIsual_select_reg = 0;
5701 }
5702 else
5703 may_start_select('c');
5704 setmouse();
5705#ifdef FEAT_CLIPBOARD
5706 // Make sure the clipboard gets updated. Needed because start and
5707 // end are still the same, and the selection needs to be owned
5708 clip_star.vmode = NUL;
5709#endif
5710 redraw_curbuf_later(INVERTED);
5711 showmode();
5712}
5713
5714/*
5715 * "g0", "g^" : Like "0" and "^" but for screen lines.
5716 * "gm": middle of "g0" and "g$".
5717 */
5718 static void
5719nv_g_home_m_cmd(cmdarg_T *cap)
5720{
5721 int i;
5722 int flag = FALSE;
5723
5724 if (cap->nchar == '^')
5725 flag = TRUE;
5726
5727 cap->oap->motion_type = MCHAR;
5728 cap->oap->inclusive = FALSE;
5729 if (curwin->w_p_wrap && curwin->w_width != 0)
5730 {
5731 int width1 = curwin->w_width - curwin_col_off();
5732 int width2 = width1 + curwin_col_off2();
5733
5734 validate_virtcol();
5735 i = 0;
5736 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5737 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5738 }
5739 else
5740 i = curwin->w_leftcol;
5741 // Go to the middle of the screen line. When 'number' or
5742 // 'relativenumber' is on and lines are wrapping the middle can be more
5743 // to the left.
5744 if (cap->nchar == 'm')
5745 i += (curwin->w_width - curwin_col_off()
5746 + ((curwin->w_p_wrap && i > 0)
5747 ? curwin_col_off2() : 0)) / 2;
5748 coladvance((colnr_T)i);
5749 if (flag)
5750 {
5751 do
5752 i = gchar_cursor();
5753 while (VIM_ISWHITE(i) && oneright() == OK);
5754 curwin->w_valid &= ~VALID_WCOL;
5755 }
5756 curwin->w_set_curswant = TRUE;
5757}
5758
5759/*
5760 * "g_": to the last non-blank character in the line or <count> lines
5761 * downward.
5762 */
5763 static void
5764nv_g_underscore_cmd(cmdarg_T *cap)
5765{
5766 char_u *ptr;
5767
5768 cap->oap->motion_type = MCHAR;
5769 cap->oap->inclusive = TRUE;
5770 curwin->w_curswant = MAXCOL;
5771 if (cursor_down((long)(cap->count1 - 1),
5772 cap->oap->op_type == OP_NOP) == FAIL)
5773 {
5774 clearopbeep(cap->oap);
5775 return;
5776 }
5777
5778 ptr = ml_get_curline();
5779
5780 // In Visual mode we may end up after the line.
5781 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5782 --curwin->w_cursor.col;
5783
5784 // Decrease the cursor column until it's on a non-blank.
5785 while (curwin->w_cursor.col > 0
5786 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5787 --curwin->w_cursor.col;
5788 curwin->w_set_curswant = TRUE;
5789 adjust_for_sel(cap);
5790}
5791
5792/*
5793 * "g$" : Like "$" but for screen lines.
5794 */
5795 static void
5796nv_g_dollar_cmd(cmdarg_T *cap)
5797{
5798 oparg_T *oap = cap->oap;
5799 int i;
5800 int col_off = curwin_col_off();
5801
5802 oap->motion_type = MCHAR;
5803 oap->inclusive = TRUE;
5804 if (curwin->w_p_wrap && curwin->w_width != 0)
5805 {
5806 curwin->w_curswant = MAXCOL; // so we stay at the end
5807 if (cap->count1 == 1)
5808 {
5809 int width1 = curwin->w_width - col_off;
5810 int width2 = width1 + curwin_col_off2();
5811
5812 validate_virtcol();
5813 i = width1 - 1;
5814 if (curwin->w_virtcol >= (colnr_T)width1)
5815 i += ((curwin->w_virtcol - width1) / width2 + 1)
5816 * width2;
5817 coladvance((colnr_T)i);
5818
5819 // Make sure we stick in this column.
5820 validate_virtcol();
5821 curwin->w_curswant = curwin->w_virtcol;
5822 curwin->w_set_curswant = FALSE;
5823 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5824 {
5825 // Check for landing on a character that got split at
5826 // the end of the line. We do not want to advance to
5827 // the next screen line.
5828 if (curwin->w_virtcol > (colnr_T)i)
5829 --curwin->w_cursor.col;
5830 }
5831 }
5832 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5833 clearopbeep(oap);
5834 }
5835 else
5836 {
5837 if (cap->count1 > 1)
5838 // if it fails, let the cursor still move to the last char
5839 (void)cursor_down(cap->count1 - 1, FALSE);
5840
5841 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5842 coladvance((colnr_T)i);
5843
5844 // if the character doesn't fit move one back
5845 if (curwin->w_cursor.col > 0
5846 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5847 {
5848 colnr_T vcol;
5849
5850 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5851 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5852 --curwin->w_cursor.col;
5853 }
5854
5855 // Make sure we stick in this column.
5856 validate_virtcol();
5857 curwin->w_curswant = curwin->w_virtcol;
5858 curwin->w_set_curswant = FALSE;
5859 }
5860}
5861
5862/*
5863 * "gi": start Insert at the last position.
5864 */
5865 static void
5866nv_gi_cmd(cmdarg_T *cap)
5867{
5868 int i;
5869
5870 if (curbuf->b_last_insert.lnum != 0)
5871 {
5872 curwin->w_cursor = curbuf->b_last_insert;
5873 check_cursor_lnum();
5874 i = (int)STRLEN(ml_get_curline());
5875 if (curwin->w_cursor.col > (colnr_T)i)
5876 {
5877 if (virtual_active())
5878 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5879 curwin->w_cursor.col = i;
5880 }
5881 }
5882 cap->cmdchar = 'i';
5883 nv_edit(cap);
5884}
5885
5886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 * Commands starting with "g".
5888 */
5889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005890nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 switch (cap->nchar)
5896 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005897 case Ctrl_A:
5898 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005900 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005901 if (!VIsual_active && cap->nchar == Ctrl_A)
5902 vim_mem_profile_dump();
5903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005905 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005906 if (VIsual_active)
5907 {
5908 cap->arg = TRUE;
5909 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005910 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005911 nv_addsub(cap);
5912 }
5913 else
5914 clearopbeep(oap);
5915 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005917 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 case 'R':
5919 cap->arg = TRUE;
5920 nv_Replace(cap);
5921 break;
5922
5923 case 'r':
5924 nv_vreplace(cap);
5925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 case '&':
5928 do_cmdline_cmd((char_u *)"%s//~/&");
5929 break;
5930
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005931 // "gv": Reselect the previous Visual area. If Visual already active,
5932 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005934 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005936
5937 // "gV": Don't reselect the previous Visual area after a Select mode
5938 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case 'V':
5940 VIsual_reselect = FALSE;
5941 break;
5942
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005943 // "gh": start Select mode.
5944 // "gH": start Select line mode.
5945 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case K_BS:
5947 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005948 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case 'h':
5950 case 'H':
5951 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 cap->cmdchar = cap->nchar + ('v' - 'h');
5953 cap->arg = TRUE;
5954 nv_visual(cap);
5955 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005957 // "gn", "gN" visually select next/previous search match
5958 // "gn" selects next match
5959 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005960 case 'N':
5961 case 'n':
5962 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005963 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005964 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005966 // "gj" and "gk" two new funny movement keys -- up and down
5967 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 case 'j':
5969 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005970 // with 'nowrap' it works just like the normal "j" command.
5971 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 oap->motion_type = MLINE;
5974 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5975 }
5976 else
5977 i = nv_screengo(oap, FORWARD, cap->count1);
5978 if (i == FAIL)
5979 clearopbeep(oap);
5980 break;
5981
5982 case 'k':
5983 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005984 // with 'nowrap' it works just like the normal "k" command.
5985 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 oap->motion_type = MLINE;
5988 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5989 }
5990 else
5991 i = nv_screengo(oap, BACKWARD, cap->count1);
5992 if (i == FAIL)
5993 clearopbeep(oap);
5994 break;
5995
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005996 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 case 'J':
5998 nv_join(cap);
5999 break;
6000
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006001 // "g0", "g^" : Like "0" and "^" but for screen lines.
6002 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 case '0':
6005 case 'm':
6006 case K_HOME:
6007 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 break;
6010
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006011 case 'M':
6012 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006013 oap->motion_type = MCHAR;
6014 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006015 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006016 if (cap->count0 > 0 && cap->count0 <= 100)
6017 coladvance((colnr_T)(i * cap->count0 / 100));
6018 else
6019 coladvance((colnr_T)(i / 2));
6020 curwin->w_set_curswant = TRUE;
6021 }
6022 break;
6023
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006024 // "g_": to the last non-blank character in the line or <count> lines
6025 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006027 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 case '$':
6032 case K_END:
6033 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 break;
6036
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 case '*':
6039 case '#':
6040#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006041 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006043 case Ctrl_RSB: // :tag or :tselect for current identifier
6044 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 nv_ident(cap);
6046 break;
6047
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 case 'e':
6050 case 'E':
6051 oap->motion_type = MCHAR;
6052 curwin->w_set_curswant = TRUE;
6053 oap->inclusive = TRUE;
6054 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6055 clearopbeep(oap);
6056 break;
6057
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006060 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 break;
6062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006065 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 break;
6067
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006068 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 case 'I':
6070 beginline(0);
6071 if (!checkclearopq(oap))
6072 invoke_edit(cap, FALSE, 'g', FALSE);
6073 break;
6074
6075#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006076 // "gf": goto file, edit file under cursor
6077 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006079 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 nv_gotofile(cap);
6081 break;
6082#endif
6083
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006084 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case '\'':
6086 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006087 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 case '`':
6089 nv_gomark(cap);
6090 break;
6091
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006092 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006094 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 break;
6096
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006097 // "ga": Display the ascii value of the character under the
6098 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 case 'a':
6100 do_ascii(NULL);
6101 break;
6102
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006103 // "g8": Display the bytes used for the UTF-8 character under the
6104 // cursor. It is displayed in hex.
6105 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006107 if (cap->count0 == 8)
6108 utf_find_illegal();
6109 else
6110 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006113 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006114 case '<':
6115 show_sb_text();
6116 break;
6117
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006118 // "gg": Goto the first line in file. With a count it goes to
6119 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 case 'g':
6121 cap->arg = FALSE;
6122 nv_goto(cap);
6123 break;
6124
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006125 // Two-character operators:
6126 // "gq" Format text
6127 // "gw" Format text and keep cursor position
6128 // "g~" Toggle the case of the text.
6129 // "gu" Change text to lower case.
6130 // "gU" Change text to upper case.
6131 // "g?" rot13 encoding
6132 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 case 'q':
6134 case 'w':
6135 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006136 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 case '~':
6138 case 'u':
6139 case 'U':
6140 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006141 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 nv_operator(cap);
6143 break;
6144
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006145 // "gd": Find first occurrence of pattern under the cursor in the
6146 // current function
6147 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 case 'd':
6149 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006150 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 break;
6152
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006153 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 case K_MIDDLEMOUSE:
6155 case K_MIDDLEDRAG:
6156 case K_MIDDLERELEASE:
6157 case K_LEFTMOUSE:
6158 case K_LEFTDRAG:
6159 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006160 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 case K_RIGHTMOUSE:
6162 case K_RIGHTDRAG:
6163 case K_RIGHTRELEASE:
6164 case K_X1MOUSE:
6165 case K_X1DRAG:
6166 case K_X1RELEASE:
6167 case K_X2MOUSE:
6168 case K_X2DRAG:
6169 case K_X2RELEASE:
6170 mod_mask = MOD_MASK_CTRL;
6171 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6172 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 case K_IGNORE:
6175 break;
6176
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006177 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 case 'p':
6179 case 'P':
6180 nv_put(cap);
6181 break;
6182
6183#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006184 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 case 'o':
6186 goto_byte(cap->count0);
6187 break;
6188#endif
6189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006190 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006192 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 do_exmode(TRUE);
6194 break;
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 case ',':
6197 nv_pcmark(cap);
6198 break;
6199
6200 case ';':
6201 cap->count1 = -cap->count1;
6202 nv_pcmark(cap);
6203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006206 if (!checkclearop(oap))
6207 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006209 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006210 if (!checkclearop(oap))
6211 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006212 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213
Bram Moolenaar62a23252020-08-09 14:04:42 +02006214 case TAB:
6215 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6216 clearopbeep(oap);
6217 break;
6218
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006219 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006220 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006221 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006222 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006223 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006224 break;
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 default:
6227 clearopbeep(oap);
6228 break;
6229 }
6230}
6231
6232/*
6233 * Handle "o" and "O" commands.
6234 */
6235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006236n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006238#ifdef FEAT_CONCEAL
6239 linenr_T oldline = curwin->w_cursor.lnum;
6240#endif
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 if (!checkclearopq(cap->oap))
6243 {
6244#ifdef FEAT_FOLDING
6245 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006246 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 (void)hasFolding(curwin->w_cursor.lnum,
6248 &curwin->w_cursor.lnum, NULL);
6249 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006250 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 (void)hasFolding(curwin->w_cursor.lnum,
6252 NULL, &curwin->w_cursor.lnum);
6253#endif
6254 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6255 (cap->cmdchar == 'O' ? 1 : 0)),
6256 (linenr_T)(curwin->w_cursor.lnum +
6257 (cap->cmdchar == 'o' ? 1 : 0))
6258 ) == OK
6259 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006260 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006261 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006263#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006264 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006265 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006266#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006267#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006268 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006269 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006270 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006271#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006273 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6274 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6276 }
6277 }
6278}
6279
6280/*
6281 * "." command: redo last change.
6282 */
6283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006284nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 if (!checkclearopq(cap->oap))
6287 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006288 // If "restart_edit" is TRUE, the last but one command is repeated
6289 // instead of the last command (inserting text). This is used for
6290 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6292 clearopbeep(cap->oap);
6293 }
6294}
6295
6296/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006297 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 */
6299 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006300nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006302 if (VIsual_select && VIsual_active)
6303 {
6304 int reg;
6305 // Get register name
6306 ++no_mapping;
6307 ++allow_keys;
6308 reg = plain_vgetc();
6309 LANGMAP_ADJUST(reg, TRUE);
6310 --no_mapping;
6311 --allow_keys;
6312
6313 if (reg == '"')
6314 // the unnamed register is 0
6315 reg = 0;
6316
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006317 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006318 return;
6319 }
6320
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (!checkclearopq(cap->oap))
6322 {
6323 u_redo((int)cap->count1);
6324 curwin->w_set_curswant = TRUE;
6325 }
6326}
6327
6328/*
6329 * Handle "U" command.
6330 */
6331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006332nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006334 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006335 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006337 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 cap->cmdchar = 'g';
6339 cap->nchar = 'U';
6340 nv_operator(cap);
6341 }
6342 else if (!checkclearopq(cap->oap))
6343 {
6344 u_undoline();
6345 curwin->w_set_curswant = TRUE;
6346 }
6347}
6348
6349/*
6350 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6351 * single character.
6352 */
6353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006354nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006356 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006357 {
6358#ifdef FEAT_JOB_CHANNEL
6359 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6360 {
6361 clearopbeep(cap->oap);
6362 return;
6363 }
6364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 else
6368 nv_operator(cap);
6369}
6370
6371/*
6372 * Handle an operator command.
6373 * The actual work is done by do_pending_operator().
6374 */
6375 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006376nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 int op_type;
6379
6380 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006381#ifdef FEAT_JOB_CHANNEL
6382 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6383 {
6384 clearopbeep(cap->oap);
6385 return;
6386 }
6387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006389 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 nv_lineop(cap);
6391 else if (!checkclearop(cap->oap))
6392 {
6393 cap->oap->start = curwin->w_cursor;
6394 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006395#ifdef FEAT_EVAL
6396 set_op_var(op_type);
6397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399}
6400
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006401#ifdef FEAT_EVAL
6402/*
6403 * Set v:operator to the characters for "optype".
6404 */
6405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006406set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006407{
6408 char_u opchars[3];
6409
6410 if (optype == OP_NOP)
6411 set_vim_var_string(VV_OP, NULL, 0);
6412 else
6413 {
6414 opchars[0] = get_op_char(optype);
6415 opchars[1] = get_extra_op_char(optype);
6416 opchars[2] = NUL;
6417 set_vim_var_string(VV_OP, opchars, -1);
6418 }
6419}
6420#endif
6421
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422/*
6423 * Handle linewise operator "dd", "yy", etc.
6424 *
6425 * "_" is is a strange motion command that helps make operators more logical.
6426 * It is actually implemented, but not documented in the real Vi. This motion
6427 * command actually refers to "the current line". Commands like "dd" and "yy"
6428 * are really an alternate form of "d_" and "y_". It does accept a count, so
6429 * "d3_" works to delete 3 lines.
6430 */
6431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 cap->oap->motion_type = MLINE;
6435 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6436 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006437 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006438 && cap->oap->motion_force != 'v'
6439 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 || cap->oap->op_type == OP_LSHIFT
6441 || cap->oap->op_type == OP_RSHIFT)
6442 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 beginline(BL_WHITE | BL_FIX);
6445}
6446
6447/*
6448 * <Home> command.
6449 */
6450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006451nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006453 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006454 if (mod_mask & MOD_MASK_CTRL)
6455 nv_goto(cap);
6456 else
6457 {
6458 cap->count0 = 1;
6459 nv_pipe(cap);
6460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006461 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6462 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
6465/*
6466 * "|" command.
6467 */
6468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006469nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 cap->oap->motion_type = MCHAR;
6472 cap->oap->inclusive = FALSE;
6473 beginline(0);
6474 if (cap->count0 > 0)
6475 {
6476 coladvance((colnr_T)(cap->count0 - 1));
6477 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6478 }
6479 else
6480 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006481 // keep curswant at the column where we wanted to go, not where
6482 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 curwin->w_set_curswant = FALSE;
6484}
6485
6486/*
6487 * Handle back-word command "b" and "B".
6488 * cap->arg is 1 for "B"
6489 */
6490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006491nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 cap->oap->motion_type = MCHAR;
6494 cap->oap->inclusive = FALSE;
6495 curwin->w_set_curswant = TRUE;
6496 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6497 clearopbeep(cap->oap);
6498#ifdef FEAT_FOLDING
6499 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6500 foldOpenCursor();
6501#endif
6502}
6503
6504/*
6505 * Handle word motion commands "e", "E", "w" and "W".
6506 * cap->arg is TRUE for "E" and "W".
6507 */
6508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 int n;
6512 int word_end;
6513 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006514 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006516 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6518 word_end = TRUE;
6519 else
6520 word_end = FALSE;
6521 cap->oap->inclusive = word_end;
6522
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006523 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (!word_end && cap->oap->op_type == OP_CHANGE)
6525 {
6526 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006527 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006529 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006531 // Reproduce a funny Vi behaviour: "cw" on a blank only
6532 // changes one character, not all blanks until the start of
6533 // the next word. Only do this when the 'w' flag is included
6534 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6536 {
6537 cap->oap->inclusive = TRUE;
6538 cap->oap->motion_type = MCHAR;
6539 return;
6540 }
6541 }
6542 else
6543 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006544 // This is a little strange. To match what the real Vi does,
6545 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6546 // that we are not on a space or a TAB. This seems impolite
6547 // at first, but it's really more what we mean when we say
6548 // 'cw'.
6549 // Another strangeness: When standing on the end of a word
6550 // "ce" will change until the end of the next word, but "cw"
6551 // will change only one character! This is done by setting
6552 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 cap->oap->inclusive = TRUE;
6554 word_end = TRUE;
6555 flag = TRUE;
6556 }
6557 }
6558 }
6559
6560 cap->oap->motion_type = MCHAR;
6561 curwin->w_set_curswant = TRUE;
6562 if (word_end)
6563 n = end_word(cap->count1, cap->arg, flag, FALSE);
6564 else
6565 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006567 // Don't leave the cursor on the NUL past the end of line. Unless we
6568 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006569 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006570 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 if (n == FAIL && cap->oap->op_type == OP_NOP)
6573 clearopbeep(cap->oap);
6574 else
6575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#ifdef FEAT_FOLDING
6578 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6579 foldOpenCursor();
6580#endif
6581 }
6582}
6583
6584/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006585 * Used after a movement command: If the cursor ends up on the NUL after the
6586 * end of the line, may move it back to the last character and make the motion
6587 * inclusive.
6588 */
6589 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006590adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006592 // The cursor cannot remain on the NUL when:
6593 // - the column is > 0
6594 // - not in Visual mode or 'selection' is "o"
6595 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006596 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006598 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006599 {
6600 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 if (has_mbyte)
6603 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006604 oap->inclusive = TRUE;
6605 }
6606}
6607
6608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 * "0" and "^" commands.
6610 * cap->arg is the argument for beginline().
6611 */
6612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 cap->oap->motion_type = MCHAR;
6616 cap->oap->inclusive = FALSE;
6617 beginline(cap->arg);
6618#ifdef FEAT_FOLDING
6619 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6620 foldOpenCursor();
6621#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006622 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6623 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624}
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626/*
6627 * In exclusive Visual mode, may include the last character.
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006633 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (has_mbyte)
6636 inc_cursor();
6637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 ++curwin->w_cursor.col;
6639 cap->oap->inclusive = FALSE;
6640 }
6641}
6642
6643/*
6644 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6645 * Should check VIsual_mode before calling this.
6646 * Returns TRUE when backed up to the previous line.
6647 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006649unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 pos_T *pp;
6652
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006653 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006655 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 pp = &curwin->w_cursor;
6657 else
6658 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (pp->coladd > 0)
6660 --pp->coladd;
6661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (pp->col > 0)
6663 {
6664 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006665 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667 else if (pp->lnum > 1)
6668 {
6669 --pp->lnum;
6670 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6671 return TRUE;
6672 }
6673 }
6674 return FALSE;
6675}
6676
6677/*
6678 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6679 */
6680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006681nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006686 VIsual_select_reg = 0;
6687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 else if (VIsual_reselect)
6689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006690 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 cap->arg = TRUE;
6692 nv_g_cmd(cap);
6693 }
6694}
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697/*
6698 * "G", "gg", CTRL-END, CTRL-HOME.
6699 * cap->arg is TRUE for "G".
6700 */
6701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 linenr_T lnum;
6705
6706 if (cap->arg)
6707 lnum = curbuf->b_ml.ml_line_count;
6708 else
6709 lnum = 1L;
6710 cap->oap->motion_type = MLINE;
6711 setpcmark();
6712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (cap->count0 != 0)
6715 lnum = cap->count0;
6716 if (lnum < 1L)
6717 lnum = 1L;
6718 else if (lnum > curbuf->b_ml.ml_line_count)
6719 lnum = curbuf->b_ml.ml_line_count;
6720 curwin->w_cursor.lnum = lnum;
6721 beginline(BL_SOL | BL_FIX);
6722#ifdef FEAT_FOLDING
6723 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6724 foldOpenCursor();
6725#endif
6726}
6727
6728/*
6729 * CTRL-\ in Normal mode.
6730 */
6731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006732nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6735 {
6736 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006737 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006738 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 restart_edit = 0;
6740#ifdef FEAT_CMDWIN
6741 if (cmdwin_type != 0)
6742 cmdwin_result = Ctrl_C;
6743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 if (VIsual_active)
6745 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006746 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 redraw_curbuf_later(INVERTED);
6748 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006749 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 if (cap->nchar == Ctrl_G && p_im)
6751 restart_edit = 'a';
6752 }
6753 else
6754 clearopbeep(cap->oap);
6755}
6756
6757/*
6758 * ESC in Normal mode: beep, but don't flush buffers.
6759 * Don't even beep if we are canceling a command.
6760 */
6761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006762nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 int no_reason;
6765
6766 no_reason = (cap->oap->op_type == OP_NOP
6767 && cap->opcount == 0
6768 && cap->count0 == 0
6769 && cap->oap->regname == 0
6770 && !p_im);
6771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006772 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
6774 if (restart_edit == 0
6775#ifdef FEAT_CMDWIN
6776 && cmdwin_type == 0
6777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006780 {
6781 if (anyBufIsChanged())
6782 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6783 else
6784 msg(_("Type :qa and press <Enter> to exit Vim"));
6785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006787 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6788 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 if (!p_im)
6790 restart_edit = 0;
6791#ifdef FEAT_CMDWIN
6792 if (cmdwin_type != 0)
6793 {
6794 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006795 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 return;
6797 }
6798#endif
6799 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006800#ifdef FEAT_CMDWIN
6801 else if (cmdwin_type != 0 && ex_normal_busy)
6802 {
6803 // When :normal runs out of characters while in the command line window
6804 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6805 // loop.
6806 cmdwin_result = K_IGNORE;
6807 return;
6808 }
6809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (VIsual_active)
6812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006813 end_visual_mode(); // stop Visual
6814 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 curwin->w_set_curswant = TRUE;
6816 redraw_curbuf_later(INVERTED);
6817 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006818 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006819 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 clearop(cap->oap);
6821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006822 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6823 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006824 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 restart_edit = 'a';
6826}
6827
6828/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006829 * Move the cursor for the "A" command.
6830 */
6831 void
6832set_cursor_for_append_to_line(void)
6833{
6834 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006835 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006836 {
6837 int save_State = State;
6838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006839 // Pretend Insert mode here to allow the cursor on the
6840 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006841 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006842 coladvance((colnr_T)MAXCOL);
6843 State = save_State;
6844 }
6845 else
6846 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6847}
6848
6849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006851 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 */
6853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006854nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006856 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6858 cap->cmdchar = 'i';
6859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006860 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006862 {
6863#ifdef FEAT_TERMINAL
6864 if (term_in_normal_mode())
6865 {
6866 end_visual_mode();
6867 clearop(cap->oap);
6868 term_enter_job_mode();
6869 return;
6870 }
6871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006875 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006876 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6877 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879#ifdef FEAT_TEXTOBJ
6880 nv_object(cap);
6881#else
6882 clearopbeep(cap->oap);
6883#endif
6884 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006885#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006886 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006887 {
6888 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006889 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006890 return;
6891 }
6892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 else if (!curbuf->b_p_ma && !p_im)
6894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006895 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006896 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006898 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006899 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006900 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006902 else if (cap->cmdchar == K_PS && VIsual_active)
6903 {
6904 pos_T old_pos = curwin->w_cursor;
6905 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006906 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006908 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006909 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6910 {
6911 shift_delete_registers();
6912 cap->oap->regname = '1';
6913 }
6914 else
6915 cap->oap->regname = '-';
6916 cap->cmdchar = 'd';
6917 cap->nchar = NUL;
6918 nv_operator(cap);
6919 do_pending_operator(cap, 0, FALSE);
6920 cap->cmdchar = K_PS;
6921
zeertzjq7a732522022-03-14 20:46:41 +00006922 if (*ml_get_cursor() != NUL)
6923 {
6924 if (old_visual_mode == 'V')
6925 {
6926 // In linewise Visual mode insert before the beginning of the
6927 // next line.
6928 // When the last line in the buffer was deleted then create a
6929 // new line, otherwise there is not need to move cursor.
6930 // Detect this by checking if cursor moved above Visual area.
6931 if (curwin->w_cursor.lnum < old_pos.lnum
6932 && curwin->w_cursor.lnum < old_visual.lnum)
6933 {
6934 if (u_save_cursor() == OK)
6935 {
6936 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6937 FALSE);
6938 appended_lines(curwin->w_cursor.lnum++, 1L);
6939 }
6940 }
6941 }
6942 // When the last char in the line was deleted then append.
6943 // Detect this by checking if cursor moved before Visual area.
6944 else if (curwin->w_cursor.col < old_pos.col
6945 && curwin->w_cursor.col < old_visual.col)
6946 inc_cursor();
6947 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006949 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006950 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 else if (!checkclearopq(cap->oap))
6953 {
6954 switch (cap->cmdchar)
6955 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006956 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006957 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 break;
6959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006961 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6962 beginline(BL_WHITE);
6963 else
6964 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 break;
6966
Bram Moolenaara1891842017-02-04 21:34:31 +01006967 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // Bracketed paste works like "a"ppend, unless the cursor is in
6969 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006970 if (curwin->w_cursor.col == 0)
6971 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 case 'a': // "a"ppend is like "i"nsert on the next character.
6975 // increment coladd when in virtual space, increment the
6976 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (virtual_active()
6978 && (curwin->w_cursor.coladd > 0
6979 || *ml_get_cursor() == NUL
6980 || *ml_get_cursor() == TAB))
6981 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006982 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 inc_cursor();
6984 break;
6985 }
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6988 {
6989 int save_State = State;
6990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 // Pretend Insert mode here to allow the cursor on the
6992 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006993 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 coladvance(getviscol());
6995 State = save_State;
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6999 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007000 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007002 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003}
7004
7005/*
7006 * Invoke edit() and take care of "restart_edit" and the return value.
7007 */
7008 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007009invoke_edit(
7010 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007012 int cmd,
7013 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 int restart_edit_save = 0;
7016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7018 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7019 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (repl || !stuff_empty())
7021 restart_edit_save = restart_edit;
7022 else
7023 restart_edit_save = 0;
7024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007025 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 restart_edit = 0;
7027
7028 if (edit(cmd, startln, cap->count1))
7029 cap->retval |= CA_COMMAND_BUSY;
7030
7031 if (restart_edit == 0)
7032 restart_edit = restart_edit_save;
7033}
7034
7035#ifdef FEAT_TEXTOBJ
7036/*
7037 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7038 */
7039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007040nv_object(
7041 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 int flag;
7044 int include;
7045 char_u *mps_save;
7046
7047 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007048 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007050 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007052 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 mps_save = curbuf->b_p_mps;
7054 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7055
7056 switch (cap->nchar)
7057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 flag = current_word(cap->oap, cap->count1, include, FALSE);
7060 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007061 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 flag = current_word(cap->oap, cap->count1, include, TRUE);
7063 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 case '(':
7066 case ')':
7067 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7068 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 case '{':
7071 case '}':
7072 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7073 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 case ']':
7076 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7077 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 case '>':
7080 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7081 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007082 case 't': // "at" = a tag block (xml and html)
7083 // Do not adjust oap->end in do_pending_operator()
7084 // otherwise there are different results for 'dit'
7085 // (note leading whitespace in last line):
7086 // 1) <b> 2) <b>
7087 // foobar foobar
7088 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007089 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007090 flag = current_tagblock(cap->oap, cap->count1, include);
7091 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 flag = current_par(cap->oap, cap->count1, include, 'p');
7094 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007095 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 flag = current_sent(cap->oap, cap->count1, include);
7097 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007098 case '"': // "a"" = a double quoted string
7099 case '\'': // "a'" = a single quoted string
7100 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007101 flag = current_quote(cap->oap, cap->count1, include,
7102 cap->nchar);
7103 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104#if 0 // TODO
7105 case 'S': // "aS" = a section
7106 case 'f': // "af" = a filename
7107 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108#endif
7109 default:
7110 flag = FAIL;
7111 break;
7112 }
7113
7114 curbuf->b_p_mps = mps_save;
7115 if (flag == FAIL)
7116 clearopbeep(cap->oap);
7117 adjust_cursor_col();
7118 curwin->w_set_curswant = TRUE;
7119}
7120#endif
7121
7122/*
7123 * "q" command: Start/stop recording.
7124 * "q:", "q/", "q?": edit command-line in command-line window.
7125 */
7126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007127nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 if (cap->oap->op_type == OP_FORMAT)
7130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 cap->cmdchar = 'g';
7133 cap->nchar = 'q';
7134 nv_operator(cap);
7135 }
7136 else if (!checkclearop(cap->oap))
7137 {
7138#ifdef FEAT_CMDWIN
7139 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7140 {
7141 stuffcharReadbuff(cap->nchar);
7142 stuffcharReadbuff(K_CMDWIN);
7143 }
7144 else
7145#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007146 // (stop) recording into a named register, unless executing a
7147 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007148 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 clearopbeep(cap->oap);
7150 }
7151}
7152
7153/*
7154 * Handle the "@r" command.
7155 */
7156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007157nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 if (checkclearop(cap->oap))
7160 return;
7161#ifdef FEAT_EVAL
7162 if (cap->nchar == '=')
7163 {
7164 if (get_expr_register() == NUL)
7165 return;
7166 }
7167#endif
7168 while (cap->count1-- && !got_int)
7169 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007170 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {
7172 clearopbeep(cap->oap);
7173 break;
7174 }
7175 line_breakcheck();
7176 }
7177}
7178
7179/*
7180 * Handle the CTRL-U and CTRL-D commands.
7181 */
7182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007183nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7186 || (cap->cmdchar == Ctrl_D
7187 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7188 clearopbeep(cap->oap);
7189 else if (!checkclearop(cap->oap))
7190 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7191}
7192
7193/*
7194 * Handle "J" or "gJ" command.
7195 */
7196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007197nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007199 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007201 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007204 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7206 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007209 if (cap->count0 <= 2)
7210 {
7211 clearopbeep(cap->oap);
7212 return;
7213 }
7214 cap->count0 = curbuf->b_ml.ml_line_count
7215 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007217
7218 prep_redo(cap->oap->regname, cap->count0,
7219 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7220 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222}
7223
7224/*
7225 * "P", "gP", "p" and "gp" commands.
7226 */
7227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007228nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007230 nv_put_opt(cap, FALSE);
7231}
7232
7233/*
7234 * "P", "gP", "p" and "gp" commands.
7235 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7236 */
7237 static void
7238nv_put_opt(cmdarg_T *cap, int fix_indent)
7239{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 int regname = 0;
7241 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007242 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007243 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 int dir;
7245 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007246 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
7248 if (cap->oap->op_type != OP_NOP)
7249 {
7250#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007251 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7253 {
7254 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007255 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 else
7258#endif
7259 clearopbeep(cap->oap);
7260 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007261#ifdef FEAT_JOB_CHANNEL
7262 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7263 {
7264 clearopbeep(cap->oap);
7265 }
7266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 else
7268 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007269 if (fix_indent)
7270 {
7271 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7272 ? FORWARD : BACKWARD;
7273 flags |= PUT_FIXINDENT;
7274 }
7275 else
7276 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007277 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7278 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 prep_redo_cmd(cap);
7280 if (cap->cmdchar == 'g')
7281 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007282 else if (cap->cmdchar == 'z')
7283 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (VIsual_active)
7286 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007287 // Putting in Visual mode: The put text replaces the selected
7288 // text. First delete the selected text, then put the new text.
7289 // Need to save and restore the registers that the delete
7290 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007291 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007293 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007294#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007296#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007297 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007298 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007299#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
7303 )
7304 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007305 // The delete is going to overwrite the register we want to
7306 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 reg1 = get_register(regname, TRUE);
7308 }
7309
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007310 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 cap->cmdchar = 'd';
7312 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007313 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007314 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 nv_operator(cap);
7316 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007317 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007318 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007320 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 cap->oap->regname = regname;
7322
7323 if (reg1 != NULL)
7324 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007325 // Delete probably changed the register we want to put, save
7326 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 reg2 = get_register(regname, FALSE);
7328 put_register(regname, reg1);
7329 }
7330
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007331 // When deleted a linewise Visual area, put the register as
7332 // lines to avoid it joined with the next line. When deletion was
7333 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 if (VIsual_mode == 'V')
7335 flags |= PUT_LINE;
7336 else if (VIsual_mode == 'v')
7337 flags |= PUT_LINE_SPLIT;
7338 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7339 flags |= PUT_LINE_FORWARD;
7340 dir = BACKWARD;
7341 if ((VIsual_mode != 'V'
7342 && curwin->w_cursor.col < curbuf->b_op_start.col)
7343 || (VIsual_mode == 'V'
7344 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007345 // cursor is at the end of the line or end of file, put
7346 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007348 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007349 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007351 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 if (reg2 != NULL)
7355 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007357 // What to reselect with "gv"? Selecting the just put text seems to
7358 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007359 if (was_visual)
7360 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007361 curbuf->b_visual.vi_start = curbuf->b_op_start;
7362 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007364 if (*p_sel == 'e')
7365 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007366 }
7367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // When all lines were selected and deleted do_put() leaves an empty
7369 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007370 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007371 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007372 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007373 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007375 // If the cursor was in that line, move it to the end of the last
7376 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007377 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7378 {
7379 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7380 coladvance((colnr_T)MAXCOL);
7381 }
7382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 auto_format(FALSE, TRUE);
7384 }
7385}
7386
7387/*
7388 * "o" and "O" commands.
7389 */
7390 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007391nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007394 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7396 {
7397 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007398 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400 else
7401#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007402 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007404#ifdef FEAT_JOB_CHANNEL
7405 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007406 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 n_opencmd(cap);
7410}
7411
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412#ifdef FEAT_NETBEANS_INTG
7413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007414nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 netbeans_keycommand(cap->nchar);
7417}
7418#endif
7419
7420#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007422nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007424 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425}
7426#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007427
Bram Moolenaar3918c952005-03-15 22:34:55 +00007428/*
7429 * Trigger CursorHold event.
7430 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7431 * input buffer. "did_cursorhold" is set to avoid retriggering.
7432 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007434nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007435{
7436 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7437 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007438 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007439}