blob: 79f168617290a3c6112b338612696e1f1933b2d9 [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 Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
189 if (text_locked())
190 {
191 clearopbeep(oap);
192 text_locked_msg();
193 return TRUE;
194 }
195 return FALSE;
196}
197
198/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000199 * Handle the count before a normal command and set cap->count0.
200 */
201 static int
202normal_cmd_get_count(
203 cmdarg_T *cap,
204 int c,
205 int toplevel UNUSED,
206 int set_prevcount UNUSED,
207 int *ctrl_w,
208 int *need_flushbuf UNUSED)
209{
210getcount:
211 if (!(VIsual_active && VIsual_select))
212 {
213 // Handle a count before a command and compute ca.count0.
214 // Note that '0' is a command and not the start of a count, but it's
215 // part of a count after other digits.
216 while ((c >= '1' && c <= '9')
217 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
218 || c == '0')))
219 {
220 if (c == K_DEL || c == K_KDEL)
221 {
222 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000223 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000224 }
225 else if (cap->count0 > 99999999L)
226 {
227 cap->count0 = 999999999L;
228 }
229 else
230 {
231 cap->count0 = cap->count0 * 10 + (c - '0');
232 }
233#ifdef FEAT_EVAL
234 // Set v:count here, when called from main() and not a stuffed
235 // command, so that v:count can be used in an expression mapping
236 // right after the count. Do set it for redo.
237 if (toplevel && readbuf1_empty())
238 set_vcount_ca(cap, &set_prevcount);
239#endif
240 if (*ctrl_w)
241 {
242 ++no_mapping;
243 ++allow_keys; // no mapping for nchar, but keys
244 }
245 ++no_zero_mapping; // don't map zero here
246 c = plain_vgetc();
247 LANGMAP_ADJUST(c, TRUE);
248 --no_zero_mapping;
249 if (*ctrl_w)
250 {
251 --no_mapping;
252 --allow_keys;
253 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000254 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000255 }
256
257 // If we got CTRL-W there may be a/another count
258 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
259 {
260 *ctrl_w = TRUE;
261 cap->opcount = cap->count0; // remember first count
262 cap->count0 = 0;
263 ++no_mapping;
264 ++allow_keys; // no mapping for nchar, but keys
265 c = plain_vgetc(); // get next character
266 LANGMAP_ADJUST(c, TRUE);
267 --no_mapping;
268 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000269 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000270 goto getcount; // jump back
271 }
272 }
273
274 if (c == K_CURSORHOLD)
275 {
276 // Save the count values so that ca.opcount and ca.count0 are exactly
277 // the same when coming back here after handling K_CURSORHOLD.
278 cap->oap->prev_opcount = cap->opcount;
279 cap->oap->prev_count0 = cap->count0;
280 }
281 else if (cap->opcount != 0)
282 {
283 // If we're in the middle of an operator (including after entering a
284 // yank buffer with '"') AND we had a count before the operator, then
285 // that count overrides the current value of ca.count0.
286 // What this means effectively, is that commands like "3dw" get turned
287 // into "d3w" which makes things fall into place pretty neatly.
288 // If you give a count before AND after the operator, they are
289 // multiplied.
290 if (cap->count0)
291 {
292 if (cap->opcount >= 999999999L / cap->count0)
293 cap->count0 = 999999999L;
294 else
295 cap->count0 *= cap->opcount;
296 }
297 else
298 cap->count0 = cap->opcount;
299 }
300
301 // Always remember the count. It will be set to zero (on the next call,
302 // above) when there is no pending operator.
303 // When called from main(), save the count for use by the "count" built-in
304 // variable.
305 cap->opcount = cap->count0;
306 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
307
308#ifdef FEAT_EVAL
309 // Only set v:count when called from main() and not a stuffed command.
310 // Do set it for redo.
311 if (toplevel && readbuf1_empty())
312 set_vcount(cap->count0, cap->count1, set_prevcount);
313#endif
314
315 return c;
316}
317
318/*
319 * Returns TRUE if the normal command (cap) needs a second character.
320 */
321 static int
322normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
323{
324 return ((cmd_flags & NV_NCH)
325 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
326 && cap->oap->op_type == OP_NOP)
327 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
328 || (cap->cmdchar == 'q'
329 && cap->oap->op_type == OP_NOP
330 && reg_recording == 0
331 && reg_executing == 0)
332 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
333 && (cap->oap->op_type != OP_NOP || VIsual_active))));
334}
335
336/*
337 * Get one or more additional characters for a normal command.
338 * Return the updated command index (if changed).
339 */
340 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000341normal_cmd_get_more_chars(
342 int idx_arg,
343 cmdarg_T *cap,
344 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000345{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000346 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000347 int c;
348 int *cp;
349 int repl = FALSE; // get character for replace mode
350 int lit = FALSE; // get extra character literally
351 int langmap_active = FALSE; // using :lmap mappings
352 int lang; // getting a text character
353#ifdef HAVE_INPUT_METHOD
354 int save_smd; // saved value of p_smd
355#endif
356
357 ++no_mapping;
358 ++allow_keys; // no mapping for nchar, but allow key codes
359 // Don't generate a CursorHold event here, most commands can't handle
360 // it, e.g., nv_replace(), nv_csearch().
361 did_cursorhold = TRUE;
362 if (cap->cmdchar == 'g')
363 {
364 /*
365 * For 'g' get the next character now, so that we can check for
366 * "gr", "g'" and "g`".
367 */
368 cap->nchar = plain_vgetc();
369 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000370 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000371 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
372 || cap->nchar == Ctrl_BSL)
373 {
374 cp = &cap->extra_char; // need to get a third character
375 if (cap->nchar != 'r')
376 lit = TRUE; // get it literally
377 else
378 repl = TRUE; // get it in replace mode
379 }
380 else
381 cp = NULL; // no third character needed
382 }
383 else
384 {
385 if (cap->cmdchar == 'r') // get it in replace mode
386 repl = TRUE;
387 cp = &cap->nchar;
388 }
389 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
390
391 /*
392 * Get a second or third character.
393 */
394 if (cp != NULL)
395 {
396 if (repl)
397 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100398 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000399#ifdef CURSOR_SHAPE
400 ui_cursor_shape(); // show different cursor shape
401#endif
402 }
403 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
404 {
405 // Allow mappings defined with ":lmap".
406 --no_mapping;
407 --allow_keys;
408 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100409 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000410 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100411 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000412 langmap_active = TRUE;
413 }
414#ifdef HAVE_INPUT_METHOD
415 save_smd = p_smd;
416 p_smd = FALSE; // Don't let the IM code show the mode here
417 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
418 im_set_active(TRUE);
419#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100420 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000421 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100422 MAY_WANT_TO_LOG_THIS;
423
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000424 // Disable bracketed paste and modifyOtherKeys here, we won't
425 // recognize the escape sequences with 'esckeys' off.
426 out_str(T_BD);
427 out_str(T_CTE);
428 }
429
430 *cp = plain_vgetc();
431
Bram Moolenaar24959102022-05-07 20:01:16 +0100432 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000433 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100434 MAY_WANT_TO_LOG_THIS;
435
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000436 // Re-enable bracketed paste mode and modifyOtherKeys
437 out_str(T_BE);
438 out_str(T_CTI);
439 }
440
441 if (langmap_active)
442 {
443 // Undo the decrement done above
444 ++no_mapping;
445 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100446 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448#ifdef HAVE_INPUT_METHOD
449 if (lang)
450 {
451 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
452 im_save_status(&curbuf->b_p_iminsert);
453 im_set_active(FALSE);
454 }
455 p_smd = save_smd;
456#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100457 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000459
460 if (!lit)
461 {
462#ifdef FEAT_DIGRAPHS
463 // Typing CTRL-K gets a digraph.
464 if (*cp == Ctrl_K
465 && ((nv_cmds[idx].cmd_flags & NV_LANG)
466 || cp == &cap->extra_char)
467 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
468 {
469 c = get_digraph(FALSE);
470 if (c > 0)
471 {
472 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000473 // Guessing how to update showcmd here...
474 del_from_showcmd(3);
475 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000476 }
477 }
478#endif
479
480 // adjust chars > 127, except after "tTfFr" commands
481 LANGMAP_ADJUST(*cp, !lang);
482#ifdef FEAT_RIGHTLEFT
483 // adjust Hebrew mapped char
484 if (p_hkmap && lang && KeyTyped)
485 *cp = hkmap(*cp);
486#endif
487 }
488
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000489 // When the next character is CTRL-\ a following CTRL-N means the
490 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000491 if (cp == &cap->extra_char
492 && cap->nchar == Ctrl_BSL
493 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
494 {
495 cap->cmdchar = Ctrl_BSL;
496 cap->nchar = cap->extra_char;
497 idx = find_command(cap->cmdchar);
498 }
499 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
500 cap->oap->op_type = get_op_type(*cp, NUL);
501 else if (*cp == Ctrl_BSL)
502 {
503 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
504
505 // There is a busy wait here when typing "f<C-\>" and then
506 // something different from CTRL-N. Can't be avoided.
507 while ((c = vpeekc()) <= 0 && towait > 0L)
508 {
509 do_sleep(towait > 50L ? 50L : towait, FALSE);
510 towait -= 50L;
511 }
512 if (c > 0)
513 {
514 c = plain_vgetc();
515 if (c != Ctrl_N && c != Ctrl_G)
516 vungetc(c);
517 else
518 {
519 cap->cmdchar = Ctrl_BSL;
520 cap->nchar = c;
521 idx = find_command(cap->cmdchar);
522 }
523 }
524 }
525
526 // When getting a text character and the next character is a
527 // multi-byte character, it could be a composing character.
528 // However, don't wait for it to arrive. Also, do enable mapping,
529 // because if it's put back with vungetc() it's too late to apply
530 // mapping.
531 --no_mapping;
532 while (enc_utf8 && lang && (c = vpeekc()) > 0
533 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
534 {
535 c = plain_vgetc();
536 if (!utf_iscomposing(c))
537 {
538 vungetc(c); // it wasn't, put it back
539 break;
540 }
541 else if (cap->ncharC1 == 0)
542 cap->ncharC1 = c;
543 else
544 cap->ncharC2 = c;
545 }
546 ++no_mapping;
547 }
548 --no_mapping;
549 --allow_keys;
550
551 return idx;
552}
553
554/*
555 * Returns TRUE if after processing a normal mode command, need to wait for a
556 * moment when a message is displayed that will be overwritten by the mode
557 * message.
558 */
559 static int
560normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
561{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000562 // In Visual mode and with "^O" in Insert mode, a short message will be
563 // overwritten by the mode message. Wait a bit, until a key is hit.
564 // In Visual mode, it's more important to keep the Visual area updated
565 // than keeping a message (e.g. from a /pat search).
566 // Only do this if the command was typed, not from a mapping.
567 // Don't wait when emsg_silent is non-zero.
568 // Also wait a bit after an error message, e.g. for "^O:".
569 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000570 return ( ((p_smd
571 && msg_silent == 0
572 && (restart_edit != 0
573 || (VIsual_active
574 && old_pos->lnum == curwin->w_cursor.lnum
575 && old_pos->col == curwin->w_cursor.col)
576 )
577 && (clear_cmdline
578 || redraw_cmdline)
579 && (msg_didout || (msg_didany && msg_scroll))
580 && !msg_nowait
581 && KeyTyped)
582 || (restart_edit != 0
583 && !VIsual_active
584 && (msg_scroll
585 || emsg_on_display)))
586 && cap->oap->regname == 0
587 && !(cap->retval & CA_COMMAND_BUSY)
588 && stuff_empty()
589 && typebuf_typed()
590 && emsg_silent == 0
591 && !in_assert_fails
592 && !did_wait_return
593 && cap->oap->op_type == OP_NOP);
594}
595
596/*
597 * After processing a normal mode command, wait for a moment when a message is
598 * displayed that will be overwritten by the mode message.
599 */
600 static void
601normal_cmd_wait_for_msg(void)
602{
603 int save_State = State;
604
605 // Draw the cursor with the right shape here
606 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100607 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000608
609 // If need to redraw, and there is a "keep_msg", redraw before the
610 // delay
611 if (must_redraw && keep_msg != NULL && !emsg_on_display)
612 {
613 char_u *kmsg;
614
615 kmsg = keep_msg;
616 keep_msg = NULL;
617 // Showmode() will clear keep_msg, but we want to use it anyway.
618 // First update w_topline.
619 setcursor();
620 update_screen(0);
621 // now reset it, otherwise it's put in the history again
622 keep_msg = kmsg;
623
624 kmsg = vim_strsave(keep_msg);
625 if (kmsg != NULL)
626 {
627 msg_attr((char *)kmsg, keep_msg_attr);
628 vim_free(kmsg);
629 }
630 }
631 setcursor();
632#ifdef CURSOR_SHAPE
633 ui_cursor_shape(); // may show different cursor shape
634#endif
635 cursor_on();
636 out_flush();
637 if (msg_scroll || emsg_on_display)
638 ui_delay(1003L, TRUE); // wait at least one second
639 ui_delay(3003L, FALSE); // wait up to three seconds
640 State = save_State;
641
642 msg_scroll = FALSE;
643 emsg_on_display = FALSE;
644}
645
646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * Execute a command in Normal mode.
648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100650normal_cmd(
651 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100652 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100654 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000658 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100659 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 static int old_mapped_len = 0;
662 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000663 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200664 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
Bram Moolenaara80faa82020-04-12 19:37:17 +0200666 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000668
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 // Use a count remembered from before entering an operator. After typing
670 // "3d" we return from normal_cmd() and come back here, the "3" is
671 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 ca.opcount = opcount;
673
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000674 // If there is an operator pending, then the command we take this time
675 // will terminate it. Finish_op tells us to finish the operation before
676 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#ifdef CURSOR_SHAPE
678 c = finish_op;
679#endif
680 finish_op = (oap->op_type != OP_NOP);
681#ifdef CURSOR_SHAPE
682 if (finish_op != c)
683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685# ifdef FEAT_MOUSESHAPE
686 update_mouseshape(-1);
687# endif
688 }
689#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100690 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100692 // When not finishing an operator and no register name typed, reset the
693 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000697#ifdef FEAT_EVAL
698 set_prevcount = TRUE;
699#endif
700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 // Restore counts from before receiving K_CURSORHOLD. This means after
703 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
704 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000705 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
706 {
707 ca.opcount = oap->prev_opcount;
708 ca.count0 = oap->prev_count0;
709 oap->prev_opcount = 0;
710 oap->prev_count0 = 0;
711 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
Bram Moolenaar24959102022-05-07 20:01:16 +0100715 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100717 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100720#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // Set v:count here, when called from main() and not a stuffed
722 // command, so that v:count can be used in an expression mapping
723 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100724 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100725 set_vcount_ca(&ca, &set_prevcount);
726#endif
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 /*
729 * Get the command character from the user.
730 */
731 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100732 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000734 // If a mapping was started in Visual or Select mode, remember the length
735 // of the mapping. This is used below to not return to Insert mode for as
736 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (restart_edit == 0)
738 old_mapped_len = 0;
739 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000740 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 old_mapped_len = typebuf_maplen();
742
743 if (c == NUL)
744 c = K_ZERO;
745
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000746 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (VIsual_active
748 && VIsual_select
749 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
750 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000751 int len;
752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100753 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
754 // 'insertmode' is set) fake a "d"elete command, Insert mode will
755 // restart automatically.
756 // Insert the typed character in the typeahead buffer, so that it can
757 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000758 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
759
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000760 // When recording and gotchars() was called the character will be
761 // recorded again, remove the previous recording.
762 if (KeyTyped)
763 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000764
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000765 if (restart_edit != 0)
766 c = 'd';
767 else
768 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 msg_nowait = TRUE; // don't delay going to insert mode
770 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000773 // If the window was made so small that nothing shows, make it at least one
774 // line and one column when typing a command.
775 if (KeyTyped && !KeyStuffed)
776 win_ensure_size();
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000780 // Get the command count
781 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
782 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000784 // Find the command character in the table of commands.
785 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 if (ctrl_w)
787 {
788 ca.nchar = c;
789 ca.cmdchar = Ctrl_W;
790 }
791 else
792 ca.cmdchar = c;
793 idx = find_command(ca.cmdchar);
794 if (idx < 0)
795 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100796 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 clearopbeep(oap);
798 goto normal_end;
799 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000800
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100801 if ((nv_cmds[idx].cmd_flags & NV_NCW)
802 && (check_text_locked(oap) || curbuf_locked()))
803 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000804 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000806 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (VIsual_active)
808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (km_stopsel
811 && (nv_cmds[idx].cmd_flags & NV_STS)
812 && !(mod_mask & MOD_MASK_SHIFT))
813 {
814 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100815 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (km_startsel)
820 {
821 if (nv_cmds[idx].cmd_flags & NV_SS)
822 {
823 unshift_special(&ca);
824 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000825 if (idx < 0)
826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000828 clearopbeep(oap);
829 goto normal_end;
830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
833 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838#ifdef FEAT_RIGHTLEFT
839 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
840 && (nv_cmds[idx].cmd_flags & NV_RL))
841 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // Invert horizontal movements and operations. Only when typed by the
843 // user directly, not when the result of a mapping or "x" translated
844 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 switch (ca.cmdchar)
846 {
847 case 'l': ca.cmdchar = 'h'; break;
848 case K_RIGHT: ca.cmdchar = K_LEFT; break;
849 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
850 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
851 case 'h': ca.cmdchar = 'l'; break;
852 case K_LEFT: ca.cmdchar = K_RIGHT; break;
853 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
854 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
855 case '>': ca.cmdchar = '<'; break;
856 case '<': ca.cmdchar = '>'; break;
857 }
858 idx = find_command(ca.cmdchar);
859 }
860#endif
861
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000862 // Get additional characters if we need them.
863 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
864 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000866 // Flush the showcmd characters onto the screen so we can see them while
867 // the command is being executed. Only do this when the shown command was
868 // actually displayed, otherwise this will slow down a lot when executing
869 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (need_flushbuf)
871 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100872
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000873 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200874 {
875 if (ex_normal_busy)
876 did_cursorhold = save_did_cursorhold;
877 else
878 did_cursorhold = FALSE;
879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
Bram Moolenaar24959102022-05-07 20:01:16 +0100881 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883 if (ca.nchar == ESC)
884 {
885 clearop(oap);
886 if (restart_edit == 0 && goto_im())
887 restart_edit = 'a';
888 goto normal_end;
889 }
890
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000891 if (ca.cmdchar != K_IGNORE)
892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000894 msg_col = 0;
895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100897 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100899 // When 'keymodel' contains "startsel" some keys start Select/Visual
900 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (!VIsual_active && km_startsel)
902 {
903 if (nv_cmds[idx].cmd_flags & NV_SS)
904 {
905 start_selection();
906 unshift_special(&ca);
907 idx = find_command(ca.cmdchar);
908 }
909 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
910 && (mod_mask & MOD_MASK_SHIFT))
911 {
912 start_selection();
913 mod_mask &= ~MOD_MASK_SHIFT;
914 }
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000917 // Execute the command!
918 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 ca.arg = nv_cmds[idx].cmd_arg;
920 (nv_cmds[idx].cmd_func)(&ca);
921
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000922 // If we didn't start or finish an operator, reset oap->regname, unless we
923 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (!finish_op
925 && !oap->op_type
926 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
927 {
928 clearop(oap);
929#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200930 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#endif
932 }
933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100934 // Get the length of mapped chars again after typing a count, second
935 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000936 if (old_mapped_len > 0)
937 old_mapped_len = typebuf_maplen();
938
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000939 // If an operation is pending, handle it. But not for K_IGNORE or
940 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200941 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100942 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000944 // Wait for a moment when a message is displayed that will be overwritten
945 // by the mode message.
946 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
947 normal_cmd_wait_for_msg();
948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000949 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950normal_end:
951
952 msg_nowait = FALSE;
953
Bram Moolenaarcc613032020-06-07 21:31:18 +0200954#ifdef FEAT_EVAL
955 if (finish_op)
956 reset_reg_var();
957#endif
958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef CURSOR_SHAPE
961 c = finish_op;
962#endif
963 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100964 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // Redraw the cursor with another shape, if we were in Operator-pending
967 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (c || ca.cmdchar == 'r')
969 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100970 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971# ifdef FEAT_MOUSESHAPE
972 update_mouseshape(-1);
973# endif
974 }
975#endif
976
Bram Moolenaara983fe92008-07-31 20:04:27 +0000977 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100978 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100981 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 vim_free(ca.searchbuf);
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (has_mbyte)
985 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (curwin->w_p_scb && toplevel)
988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 do_check_scrollbind(TRUE);
991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar860cae12010-06-05 23:22:07 +0200993 if (curwin->w_p_crb && toplevel)
994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100995 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +0200996 do_check_cursorbind();
997 }
Bram Moolenaar860cae12010-06-05 23:22:07 +0200998
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +0200999#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001001 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001002 restart_edit = 0;
1003#endif
1004
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001005 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1006 // if still inside a mapping that started in Visual mode).
1007 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1010 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 && !(ca.retval & CA_COMMAND_BUSY)
1012 && stuff_empty()
1013 && oap->regname == 0)
1014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (restart_VIsual_select == 1)
1016 {
1017 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001018 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 showmode();
1020 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001021 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001023 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 (void)edit(restart_edit, FALSE, 1L);
1025 }
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (restart_VIsual_select == 2)
1028 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001030 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 opcount = ca.opcount;
1032}
1033
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001034#ifdef FEAT_EVAL
1035/*
1036 * Set v:count and v:count1 according to "cap".
1037 * Set v:prevcount only when "set_prevcount" is TRUE.
1038 */
1039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001040set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001041{
1042 long count = cap->count0;
1043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001045 if (cap->opcount != 0)
1046 count = cap->opcount * (count == 0 ? 1 : count);
1047 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001048 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001049}
1050#endif
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001053 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 * if not.
1055 */
1056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001057check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
1059 static int did_check = FALSE;
1060
1061 if (full_screen)
1062 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001063 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001064 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 did_check = TRUE;
1066 }
1067}
1068
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001069#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1070/*
1071 * Call yank_do_autocmd() for "regname".
1072 */
1073 static void
1074call_yank_do_autocmd(int regname)
1075{
1076 oparg_T oa;
1077 yankreg_T *reg;
1078
1079 clear_oparg(&oa);
1080 oa.regname = regname;
1081 oa.op_type = OP_YANK;
1082 oa.is_VIsual = TRUE;
1083 reg = get_register(regname, TRUE);
1084 yank_do_autocmd(&oa, reg);
1085 free_register(reg);
1086}
1087#endif
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001090 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001091 * This function or the next should ALWAYS be called to end Visual mode, except
1092 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
1094 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001095end_visual_mode()
1096{
1097 end_visual_mode_keep_button();
1098 reset_held_button();
1099}
1100
1101 void
1102end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001105 // If we are using the clipboard, then remember what was selected in case
1106 // we need to paste it somewhere while we still own the selection.
1107 // Only do this when the clipboard is already owned. Don't want to grab
1108 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (clip_star.available && clip_star.owned)
1110 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001111
1112# if defined(FEAT_EVAL)
1113 // Emit a TextYankPost for the automatic copy of the selection into the
1114 // star and/or plus register.
1115 if (has_textyankpost())
1116 {
1117 if (clip_isautosel_star())
1118 call_yank_do_autocmd('*');
1119 if (clip_isautosel_plus())
1120 call_yank_do_autocmd('+');
1121 }
1122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124
1125 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 setmouse();
1127 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001129 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001130 curbuf->b_visual.vi_mode = VIsual_mode;
1131 curbuf->b_visual.vi_start = VIsual;
1132 curbuf->b_visual.vi_end = curwin->w_cursor;
1133 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_EVAL
1135 curbuf->b_visual_mode_eval = VIsual_mode;
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (!virtual_active())
1138 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001139 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001141 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001142 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143}
1144
1145/*
1146 * Reset VIsual_active and VIsual_reselect.
1147 */
1148 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001149reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150{
1151 if (VIsual_active)
1152 {
1153 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001154 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 VIsual_reselect = FALSE;
1157}
1158
1159/*
1160 * Reset VIsual_active and VIsual_reselect if it's set.
1161 */
1162 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001163reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
1165 if (VIsual_active)
1166 {
1167 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001168 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 VIsual_reselect = FALSE;
1170 }
1171}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001173 void
1174restore_visual_mode(void)
1175{
1176 if (VIsual_mode_orig != NUL)
1177 {
1178 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1179 VIsual_mode_orig = NUL;
1180 }
1181}
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183/*
1184 * Check for a balloon-eval special item to include when searching for an
1185 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1186 * Returns TRUE if the character at "*ptr" should be included.
1187 * "dir" is FORWARD or BACKWARD, the direction of searching.
1188 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1189 * "bnp" points to a counter for square brackets.
1190 */
1191 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001192find_is_eval_item(
1193 char_u *ptr,
1194 int *colp,
1195 int *bnp,
1196 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001198 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1200 ++*bnp;
1201 if (*bnp > 0)
1202 {
1203 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1204 --*bnp;
1205 return TRUE;
1206 }
1207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001208 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (*ptr == '.')
1210 return TRUE;
1211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001212 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1214 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1215 {
1216 *colp += dir;
1217 return TRUE;
1218 }
1219 return FALSE;
1220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
1222/*
1223 * Find the identifier under or to the right of the cursor.
1224 * "find_type" can have one of three values:
1225 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001226 * FIND_STRING: find any non-white text
1227 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001228 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 *
1230 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001231 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001233 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * This doesn't match the real Vi but I like it a little better and it
1235 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001236 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 * When FIND_IDENT isn't defined, we backup until a blank.
1238 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001239 * Returns the length of the text, or zero if no text is found.
1240 * If text is found, a pointer to the text is put in "*text". This
1241 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 */
1243 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001244find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001247 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248}
1249
1250/*
1251 * Like find_ident_under_cursor(), but for any window and any position.
1252 * However: Uses 'iskeyword' from the current window!.
1253 */
1254 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001255find_ident_at_pos(
1256 win_T *wp,
1257 linenr_T lnum,
1258 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 char_u **text,
1260 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001261 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262{
1263 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001264 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 int this_class = 0;
1267 int prev_class;
1268 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001269 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001271 // if i == 0: try to find an identifier
1272 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1274 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1275 {
1276 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001277 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
1279 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (has_mbyte)
1281 {
1282 while (ptr[col] != NUL)
1283 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001284 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1286 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 this_class = mb_get_class(ptr + col);
1288 if (this_class != 0 && (i == 1 || this_class != 1))
1289 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001290 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001295 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 )
1298 ++col;
1299
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001300 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302
1303 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001304 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (has_mbyte)
1307 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001308 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1310 this_class = mb_get_class((char_u *)"a");
1311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001313 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1316 prev_class = mb_get_class(ptr + prevcol);
1317 if (this_class != prev_class
1318 && (i == 0
1319 || prev_class == 0
1320 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && (!(find_type & FIND_EVAL)
1322 || prevcol == 0
1323 || !find_is_eval_item(ptr + prevcol, &prevcol,
1324 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 )
1326 break;
1327 col = prevcol;
1328 }
1329
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001330 // If we don't want just any old text, or we've found an
1331 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (this_class > 2)
1333 this_class = 2;
1334 if (!(find_type & FIND_STRING) || this_class == 2)
1335 break;
1336 }
1337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 while (col > 0
1340 && ((i == 0
1341 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001342 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 && (!(find_type & FIND_IDENT)
1344 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 || ((find_type & FIND_EVAL)
1346 && col > 1
1347 && find_is_eval_item(ptr + col - 1, &col,
1348 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 ))
1350 --col;
1351
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001352 // If we don't want just any old text, or we've found an
1353 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1355 break;
1356 }
1357 }
1358
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001359 if (ptr[col] == NUL || (i == 0
1360 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001362 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001363 if ((find_type & FIND_NOERROR) == 0)
1364 {
1365 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001366 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001367 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001368 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 return 0;
1371 }
1372 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001373 *text = ptr;
1374 if (textcol != NULL)
1375 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001378 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 bn = 0;
1381 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (has_mbyte)
1384 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 this_class = mb_get_class(ptr);
1387 while (ptr[col] != NUL
1388 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1389 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 || ((find_type & FIND_EVAL)
1391 && col <= (int)startcol
1392 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001394 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001398 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 || ((find_type & FIND_EVAL)
1400 && col <= (int)startcol
1401 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 return col;
1406}
1407
1408/*
1409 * Prepare for redo of a normal command.
1410 */
1411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 prep_redo(cap->oap->regname, cap->count0,
1415 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1416}
1417
1418/*
1419 * Prepare for redo of any command.
1420 * Note that only the last argument can be a multi-byte char.
1421 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001422 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001423prep_redo(
1424 int regname,
1425 long num,
1426 int cmd1,
1427 int cmd2,
1428 int cmd3,
1429 int cmd4,
1430 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001432 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1433}
1434
1435/*
1436 * Prepare for redo of any command with extra count after "cmd2".
1437 */
1438 void
1439prep_redo_num2(
1440 int regname,
1441 long num1,
1442 int cmd1,
1443 int cmd2,
1444 long num2,
1445 int cmd3,
1446 int cmd4,
1447 int cmd5)
1448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001450
1451#ifdef FEAT_EVAL
1452 // Put info about a mapping in the redo buffer, so that "." will use the
1453 // same script context.
1454 may_add_last_used_map_to_redobuff();
1455#endif
1456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001457 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 AppendCharToRedobuff('"');
1460 AppendCharToRedobuff(regname);
1461 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001462 if (num1 != 0)
1463 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (cmd1 != NUL)
1465 AppendCharToRedobuff(cmd1);
1466 if (cmd2 != NUL)
1467 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001468 if (num2 != 0)
1469 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (cmd3 != NUL)
1471 AppendCharToRedobuff(cmd3);
1472 if (cmd4 != NUL)
1473 AppendCharToRedobuff(cmd4);
1474 if (cmd5 != NUL)
1475 AppendCharToRedobuff(cmd5);
1476}
1477
1478/*
1479 * check for operator active and clear it
1480 *
1481 * return TRUE if operator was active
1482 */
1483 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001484checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486 if (oap->op_type == OP_NOP)
1487 return FALSE;
1488 clearopbeep(oap);
1489 return TRUE;
1490}
1491
1492/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001493 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001495 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001498checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001500 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 return FALSE;
1502 clearopbeep(oap);
1503 return TRUE;
1504}
1505
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001506 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001507clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 oap->op_type = OP_NOP;
1510 oap->regname = 0;
1511 oap->motion_force = NUL;
1512 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001513 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514}
1515
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 clearop(oap);
1520 beep_flush();
1521}
1522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523/*
1524 * Remove the shift modifier from a special key.
1525 */
1526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001527unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 switch (cap->cmdchar)
1530 {
1531 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1532 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1533 case K_S_UP: cap->cmdchar = K_UP; break;
1534 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1535 case K_S_HOME: cap->cmdchar = K_HOME; break;
1536 case K_S_END: cap->cmdchar = K_END; break;
1537 }
1538 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001541/*
1542 * If the mode is currently displayed clear the command line or update the
1543 * command displayed.
1544 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001547{
1548 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001549 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001550 else
1551 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001552}
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
1555 * Routines for displaying a partly typed command
1556 */
1557
kylo252ae6f1d82022-02-16 19:24:07 +00001558#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001560static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561static int showcmd_is_clear = TRUE;
1562static int showcmd_visual = FALSE;
1563
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001564static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
1566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001567clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 if (!p_sc)
1570 return;
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 if (VIsual_active && !char_avail())
1573 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001574 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 long lines;
1576 colnr_T leftcol, rightcol;
1577 linenr_T top, bot;
1578
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001579 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001580 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 top = VIsual.lnum;
1583 bot = curwin->w_cursor.lnum;
1584 }
1585 else
1586 {
1587 top = curwin->w_cursor.lnum;
1588 bot = VIsual.lnum;
1589 }
1590# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001592 (void)hasFolding(top, &top, NULL);
1593 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594# endif
1595 lines = bot - top + 1;
1596
1597 if (VIsual_mode == Ctrl_V)
1598 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001599# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001600 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001601 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001603 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001604 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001605 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001608# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001609 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001610 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1613 (long)(rightcol - leftcol + 1));
1614 }
1615 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1616 sprintf((char *)showcmd_buf, "%ld", lines);
1617 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001618 {
1619 char_u *s, *e;
1620 int l;
1621 int bytes = 0;
1622 int chars = 0;
1623
1624 if (cursor_bot)
1625 {
1626 s = ml_get_pos(&VIsual);
1627 e = ml_get_cursor();
1628 }
1629 else
1630 {
1631 s = ml_get_cursor();
1632 e = ml_get_pos(&VIsual);
1633 }
1634 while ((*p_sel != 'e') ? s <= e : s < e)
1635 {
1636 l = (*mb_ptr2len)(s);
1637 if (l == 0)
1638 {
1639 ++bytes;
1640 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001641 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001642 }
1643 bytes += l;
1644 ++chars;
1645 s += l;
1646 }
1647 if (bytes == chars)
1648 sprintf((char *)showcmd_buf, "%d", chars);
1649 else
1650 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1651 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001652 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 showcmd_visual = TRUE;
1654 }
1655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 showcmd_buf[0] = NUL;
1658 showcmd_visual = FALSE;
1659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (showcmd_is_clear)
1662 return;
1663 }
1664
1665 display_showcmd();
1666}
1667
1668/*
1669 * Add 'c' to string of shown command chars.
1670 * Return TRUE if output has been written (and setcursor() has been called).
1671 */
1672 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001673add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 char_u *p;
1676 int old_len;
1677 int extra_len;
1678 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 int i;
1680 static int ignore[] =
1681 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001682#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1684 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001685#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001686 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001687 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1689 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001690 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001692 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 0
1694 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaar09df3122006-01-23 22:23:09 +00001696 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 return FALSE;
1698
1699 if (showcmd_visual)
1700 {
1701 showcmd_buf[0] = NUL;
1702 showcmd_visual = FALSE;
1703 }
1704
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001705 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (IS_SPECIAL(c))
1707 for (i = 0; ignore[i] != 0; ++i)
1708 if (ignore[i] == c)
1709 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001712 if (*p == ' ')
1713 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 old_len = (int)STRLEN(showcmd_buf);
1715 extra_len = (int)STRLEN(p);
1716 overflow = old_len + extra_len - SHOWCMD_COLS;
1717 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001718 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1719 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 STRCAT(showcmd_buf, p);
1721
1722 if (char_avail())
1723 return FALSE;
1724
1725 display_showcmd();
1726
1727 return TRUE;
1728}
1729
1730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001731add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 if (!add_to_showcmd(c))
1734 setcursor();
1735}
1736
1737/*
1738 * Delete 'len' characters from the end of the shown command.
1739 */
1740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001741del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 int old_len;
1744
1745 if (!p_sc)
1746 return;
1747
1748 old_len = (int)STRLEN(showcmd_buf);
1749 if (len > old_len)
1750 len = old_len;
1751 showcmd_buf[old_len - len] = NUL;
1752
1753 if (!char_avail())
1754 display_showcmd();
1755}
1756
1757/*
1758 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1759 * something and there is a partial mapping.
1760 */
1761 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001762push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 if (p_sc)
1765 STRCPY(old_showcmd_buf, showcmd_buf);
1766}
1767
1768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001769pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 if (!p_sc)
1772 return;
1773
1774 STRCPY(showcmd_buf, old_showcmd_buf);
1775
1776 display_showcmd();
1777}
1778
1779 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int len;
1783
1784 cursor_off();
1785
1786 len = (int)STRLEN(showcmd_buf);
1787 if (len == 0)
1788 showcmd_is_clear = TRUE;
1789 else
1790 {
1791 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1792 showcmd_is_clear = FALSE;
1793 }
1794
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001795 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1796 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
1803 * When "check" is FALSE, prepare for commands that scroll the window.
1804 * When "check" is TRUE, take care of scroll-binding after the window has
1805 * scrolled. Called from normal_cmd() and edit().
1806 */
1807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001808do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
1810 static win_T *old_curwin = NULL;
1811 static linenr_T old_topline = 0;
1812#ifdef FEAT_DIFF
1813 static int old_topfill = 0;
1814#endif
1815 static buf_T *old_buf = NULL;
1816 static colnr_T old_leftcol = 0;
1817
1818 if (check && curwin->w_p_scb)
1819 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 // If a ":syncbind" command was just used, don't scroll, only reset
1821 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (did_syncbind)
1823 did_syncbind = FALSE;
1824 else if (curwin == old_curwin)
1825 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001826 // Synchronize other windows, as necessary according to
1827 // 'scrollbind'. Don't do this after an ":edit" command, except
1828 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if ((curwin->w_buffer == old_buf
1830#ifdef FEAT_DIFF
1831 || curwin->w_p_diff
1832#endif
1833 )
1834 && (curwin->w_topline != old_topline
1835#ifdef FEAT_DIFF
1836 || curwin->w_topfill != old_topfill
1837#endif
1838 || curwin->w_leftcol != old_leftcol))
1839 {
1840 check_scrollbind(curwin->w_topline - old_topline,
1841 (long)(curwin->w_leftcol - old_leftcol));
1842 }
1843 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001846 // When switching between windows, make sure that the relative
1847 // vertical offset is valid for the new window. The relative
1848 // offset is invalid whenever another 'scrollbind' window has
1849 // scrolled to a point that would force the current window to
1850 // scroll past the beginning or end of its buffer. When the
1851 // resync is performed, some of the other 'scrollbind' windows may
1852 // need to jump so that the current window's relative position is
1853 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1855 }
1856 curwin->w_scbind_pos = curwin->w_topline;
1857 }
1858
1859 old_curwin = curwin;
1860 old_topline = curwin->w_topline;
1861#ifdef FEAT_DIFF
1862 old_topfill = curwin->w_topfill;
1863#endif
1864 old_buf = curwin->w_buffer;
1865 old_leftcol = curwin->w_leftcol;
1866}
1867
1868/*
1869 * Synchronize any windows that have "scrollbind" set, based on the
1870 * number of rows by which the current window has changed
1871 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1872 */
1873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001874check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
1876 int want_ver;
1877 int want_hor;
1878 win_T *old_curwin = curwin;
1879 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 int old_VIsual_select = VIsual_select;
1881 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 colnr_T tgt_leftcol = curwin->w_leftcol;
1883 long topline;
1884 long y;
1885
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001886 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1888#ifdef FEAT_DIFF
1889 want_ver |= old_curwin->w_p_diff;
1890#endif
1891 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1892
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001893 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001895 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001898 // skip original window and windows with 'noscrollbind'
1899 if (curwin == old_curwin || !curwin->w_p_scb)
1900 continue;
1901
1902 // do the vertical scroll
1903 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001906 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
zeertzjq101d57b2022-07-31 18:34:32 +01001908 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
zeertzjq101d57b2022-07-31 18:34:32 +01001910 else
1911#endif
1912 {
1913 curwin->w_scbind_pos += topline_diff;
1914 topline = curwin->w_scbind_pos;
1915 if (topline > curbuf->b_ml.ml_line_count)
1916 topline = curbuf->b_ml.ml_line_count;
1917 if (topline < 1)
1918 topline = 1;
1919
1920 y = topline - curwin->w_topline;
1921 if (y > 0)
1922 scrollup(y, FALSE);
1923 else
1924 scrolldown(-y, FALSE);
1925 }
1926
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001927 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001928 cursor_correct();
1929 curwin->w_redr_status = TRUE;
1930 }
1931
1932 // do the horizontal scroll
1933 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1934 {
1935 curwin->w_leftcol = tgt_leftcol;
1936 leftcol_changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938 }
1939
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001940 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 VIsual_select = old_VIsual_select;
1942 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 curwin = old_curwin;
1944 curbuf = old_curbuf;
1945}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947/*
1948 * Command character that's ignored.
1949 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001950 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001953nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001955 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956}
1957
1958/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001959 * Command character that doesn't do anything, but unlike nv_ignore() does
1960 * start edit(). Used for "startinsert" executed while starting up.
1961 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001962 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001963nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001964{
1965}
1966
1967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 * Command character doesn't exist.
1969 */
1970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973 clearopbeep(cap->oap);
1974}
1975
1976/*
1977 * <Help> and <F1> commands.
1978 */
1979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 if (!checkclearopq(cap->oap))
1983 ex_help(NULL);
1984}
1985
1986/*
1987 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
1988 */
1989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001990nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
Bram Moolenaarf2732452018-06-03 14:47:35 +02001992#ifdef FEAT_JOB_CHANNEL
1993 if (bt_prompt(curbuf) && !prompt_curpos_editable())
1994 clearopbeep(cap->oap);
1995 else
1996#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01001997 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02001998 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01001999 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002000 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2001 op_addsub(cap->oap, cap->count1, cap->arg);
2002 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002003 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002004 else if (VIsual_active)
2005 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002006 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002007 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008}
2009
2010/*
2011 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2012 */
2013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002014nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002017 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002018 if (mod_mask & MOD_MASK_CTRL)
2019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002020 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002021 if (cap->arg == BACKWARD)
2022 goto_tabpage(-(int)cap->count1);
2023 else
2024 goto_tabpage((int)cap->count0);
2025 }
2026 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002027 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029}
2030
2031/*
2032 * Implementation of "gd" and "gD" command.
2033 */
2034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002035nv_gd(
2036 oparg_T *oap,
2037 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002038 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 char_u *ptr;
2042
Bram Moolenaard9d30582005-05-18 22:10:28 +00002043 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002044 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002045 == FAIL)
2046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002048 }
2049 else
2050 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002051#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002052 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2053 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002054#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002055 // clear any search statistics
2056 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2057 clear_cmdline = TRUE;
2058 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002059}
2060
2061/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002062 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2063 * otherwise.
2064 */
2065 static int
2066is_ident(char_u *line, int offset)
2067{
2068 int i;
2069 int incomment = FALSE;
2070 int instring = 0;
2071 int prev = 0;
2072
2073 for (i = 0; i < offset && line[i] != NUL; i++)
2074 {
2075 if (instring != 0)
2076 {
2077 if (prev != '\\' && line[i] == instring)
2078 instring = 0;
2079 }
2080 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2081 {
2082 instring = line[i];
2083 }
2084 else
2085 {
2086 if (incomment)
2087 {
2088 if (prev == '*' && line[i] == '/')
2089 incomment = FALSE;
2090 }
2091 else if (prev == '/' && line[i] == '*')
2092 {
2093 incomment = TRUE;
2094 }
2095 else if (prev == '/' && line[i] == '/')
2096 {
2097 return FALSE;
2098 }
2099 }
2100
2101 prev = line[i];
2102 }
2103
2104 return incomment == FALSE && instring == 0;
2105}
2106
2107/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002108 * Search for variable declaration of "ptr[len]".
2109 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2110 * current file ("gD").
2111 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002112 * Return FAIL when not found.
2113 */
2114 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002115find_decl(
2116 char_u *ptr,
2117 int len,
2118 int locally,
2119 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002121{
2122 char_u *pat;
2123 pos_T old_pos;
2124 pos_T par_pos;
2125 pos_T found_pos;
2126 int t;
2127 int save_p_ws;
2128 int save_p_scs;
2129 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002130 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002131 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002132 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002133
2134 if ((pat = alloc(len + 7)) == NULL)
2135 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002137 // Put "\V" before the pattern to avoid that the special meaning of "."
2138 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002139 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2140 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 old_pos = curwin->w_cursor;
2142 save_p_ws = p_ws;
2143 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002144 p_ws = FALSE; // don't wrap around end of file now
2145 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002147 // With "gD" go to line 1.
2148 // With "gd" Search back for the start of the current function, then go
2149 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002150 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002152 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002154 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else
2157 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002158 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2160 --curwin->w_cursor.lnum;
2161 }
2162 curwin->w_cursor.col = 0;
2163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002164 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002165 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002166 for (;;)
2167 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002168 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002169 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002170 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002171 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002172
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002173 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002174 {
2175 pos_T *pos;
2176
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002177 // Check that the block the match is in doesn't end before the
2178 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002179 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2180 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2181 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002182 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002183 // There can't be a useful match before the end of this block.
2184 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002185 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002186 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002187 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002188 }
2189
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002190 if (t == FAIL)
2191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002193 if (found_pos.lnum != 0)
2194 {
2195 curwin->w_cursor = found_pos;
2196 t = OK;
2197 }
2198 break;
2199 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002200 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002201 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002203 ++curwin->w_cursor.lnum;
2204 curwin->w_cursor.col = 0;
2205 continue;
2206 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002207 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 // If the current position is not a valid identifier and a previous
2210 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002211 if (!valid && found_pos.lnum != 0)
2212 {
2213 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002214 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002215 }
2216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002218 if (valid && !locally)
2219 break;
2220 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002223 if (found_pos.lnum != 0)
2224 curwin->w_cursor = found_pos;
2225 break;
2226 }
2227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002228 // For finding a local variable and the match is before the "{" or
2229 // inside a comment, continue searching. For K&R style function
2230 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002232 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002233 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002235 // Remove SEARCH_START from flags to avoid getting stuck at one
2236 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002237 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239
2240 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002242 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 curwin->w_cursor = old_pos;
2244 }
2245 else
2246 {
2247 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 reset_search_dir();
2250 }
2251
2252 vim_free(pat);
2253 p_ws = save_p_ws;
2254 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002255
2256 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257}
2258
2259/*
2260 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2261 * lines rather than lines in the file.
2262 * 'dist' must be positive.
2263 *
2264 * Return OK if able to move cursor, FAIL otherwise.
2265 */
2266 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002269 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 int retval = OK;
2271 int atend = FALSE;
2272 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 int col_off1; // margin offset for first screen line
2274 int col_off2; // margin offset for wrapped screen line
2275 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002276 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002279 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280
2281 col_off1 = curwin_col_off();
2282 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002283 width1 = curwin->w_width - col_off1;
2284 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002285 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002286 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002289 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002290 // Instead of sticking at the last character of the buffer line we
2291 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (curwin->w_curswant == MAXCOL)
2293 {
2294 atend = TRUE;
2295 validate_virtcol();
2296 if (width1 <= 0)
2297 curwin->w_curswant = 0;
2298 else
2299 {
2300 curwin->w_curswant = width1 - 1;
2301 if (curwin->w_virtcol > curwin->w_curswant)
2302 curwin->w_curswant += ((curwin->w_virtcol
2303 - curwin->w_curswant - 1) / width2 + 1) * width2;
2304 }
2305 }
2306 else
2307 {
2308 if (linelen > width1)
2309 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2310 else
2311 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002312 if (curwin->w_curswant >= (colnr_T)n)
2313 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315
2316 while (dist--)
2317 {
2318 if (dir == BACKWARD)
2319 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002320 if ((long)curwin->w_curswant >= width1
2321#ifdef FEAT_FOLDING
2322 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2323#endif
2324 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002325 // Move back within the line. This can give a negative value
2326 // for w_curswant if width1 < width2 (with cpoptions+=n),
2327 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 curwin->w_curswant -= width2;
2329 else
2330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002331 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 // Move to the start of a closed fold. Don't do that when
2334 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (!(fdo_flags & FDO_ALL))
2336 (void)hasFolding(curwin->w_cursor.lnum,
2337 &curwin->w_cursor.lnum, NULL);
2338#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002339 if (curwin->w_cursor.lnum == 1)
2340 {
2341 retval = FAIL;
2342 break;
2343 }
2344 --curwin->w_cursor.lnum;
2345
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002346 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (linelen > width1)
2348 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2349 + 1) * width2;
2350 }
2351 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002352 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 if (linelen > width1)
2355 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2356 else
2357 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002358 if (curwin->w_curswant + width2 < (colnr_T)n
2359#ifdef FEAT_FOLDING
2360 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2361#endif
2362 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002363 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 curwin->w_curswant += width2;
2365 else
2366 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002367 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002369 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2371 &curwin->w_cursor.lnum);
2372#endif
2373 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2374 {
2375 retval = FAIL;
2376 break;
2377 }
2378 curwin->w_cursor.lnum++;
2379 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002380 // Check if the cursor has moved below the number display
2381 // when width1 < width2 (with cpoptions+=n). Subtract width2
2382 // to get a negative value for w_curswant, which will get
2383 // clipped to column 0.
2384 if (curwin->w_curswant >= width1)
2385 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002386 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388 }
2389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002392 if (virtual_active() && atend)
2393 coladvance(MAXCOL);
2394 else
2395 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2398 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002399 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002400 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002401
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002402 // Check for landing on a character that got split at the end of the
2403 // last line. We want to advance a screenline, not end up in the same
2404 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002406 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002407#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002408 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2409 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002410#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002411
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002412 c = (*mb_ptr2char)(ml_get_cursor());
2413 if (dir == FORWARD && virtcol < curwin->w_curswant
2414 && (curwin->w_curswant <= (colnr_T)width1)
2415 && !vim_isprintc(c) && c > 255)
2416 oneright();
2417
Bram Moolenaar773b1582014-08-29 14:20:51 +02002418 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 && (curwin->w_curswant < (colnr_T)width1
2420 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2421 : ((curwin->w_curswant - width1) % width2
2422 > (colnr_T)width2 / 2)))
2423 --curwin->w_cursor.col;
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002428 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 return retval;
2431}
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2435 * cap->arg must be TRUE for CTRL-E.
2436 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 if (!checkclearop(cap->oap))
2441 scroll_redraw(cap->arg, cap->count1);
2442}
2443
2444/*
2445 * Scroll "count" lines up or down, and redraw.
2446 */
2447 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002448scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002451 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452#ifdef FEAT_DIFF
2453 int prev_topfill = curwin->w_topfill;
2454#endif
2455 linenr_T prev_lnum = curwin->w_cursor.lnum;
2456
2457 if (up)
2458 scrollup(count, TRUE);
2459 else
2460 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002461 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002463 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2464 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 cursor_correct();
2466 check_cursor_moved(curwin);
2467 curwin->w_valid |= VALID_TOPLINE;
2468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002469 // If moved back to where we were, at least move the cursor, otherwise
2470 // we get stuck at one position. Don't move the cursor up if the
2471 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002473 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#ifdef FEAT_DIFF
2475 && curwin->w_topfill == prev_topfill
2476#endif
2477 )
2478 {
2479 if (up)
2480 {
2481 if (curwin->w_cursor.lnum > prev_lnum
2482 || cursor_down(1L, FALSE) == FAIL)
2483 break;
2484 }
2485 else
2486 {
2487 if (curwin->w_cursor.lnum < prev_lnum
2488 || prev_topline == 1L
2489 || cursor_up(1L, FALSE) == FAIL)
2490 break;
2491 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002492 // Mark w_topline as valid, otherwise the screen jumps back at the
2493 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 check_cursor_moved(curwin);
2495 curwin->w_valid |= VALID_TOPLINE;
2496 }
2497 }
2498 if (curwin->w_cursor.lnum != prev_lnum)
2499 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002500 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501}
2502
2503/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002504 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2505 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2506 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002507 */
2508 static int
2509nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2510{
2511 int nchar = *nchar_arg;
2512 long n;
2513
2514 // "z123{nchar}": edit the count before obtaining {nchar}
2515 if (checkclearop(cap->oap))
2516 return FALSE;
2517 n = nchar - '0';
2518
2519 for (;;)
2520 {
2521#ifdef USE_ON_FLY_SCROLL
2522 dont_scroll = TRUE; // disallow scrolling here
2523#endif
2524 ++no_mapping;
2525 ++allow_keys; // no mapping for nchar, but allow key codes
2526 nchar = plain_vgetc();
2527 LANGMAP_ADJUST(nchar, TRUE);
2528 --no_mapping;
2529 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002530 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002531
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002532 if (nchar == K_DEL || nchar == K_KDEL)
2533 n /= 10;
2534 else if (VIM_ISDIGIT(nchar))
2535 n = n * 10 + (nchar - '0');
2536 else if (nchar == CAR)
2537 {
2538#ifdef FEAT_GUI
2539 need_mouse_correct = TRUE;
2540#endif
2541 win_setheight((int)n);
2542 break;
2543 }
2544 else if (nchar == 'l'
2545 || nchar == 'h'
2546 || nchar == K_LEFT
2547 || nchar == K_RIGHT)
2548 {
2549 cap->count1 = n ? n * cap->count1 : cap->count1;
2550 *nchar_arg = nchar;
2551 return TRUE;
2552 }
2553 else
2554 {
2555 clearopbeep(cap->oap);
2556 break;
2557 }
2558 }
2559 cap->oap->op_type = OP_NOP;
2560 return FALSE;
2561}
2562
2563#ifdef FEAT_SPELL
2564/*
2565 * "zug" and "zuw": undo "zg" and "zw"
2566 * "zg": add good word to word list
2567 * "zw": add wrong word to word list
2568 * "zG": add good word to temp word list
2569 * "zW": add wrong word to temp word list
2570 */
2571 static int
2572nv_zg_zw(cmdarg_T *cap, int nchar)
2573{
2574 char_u *ptr = NULL;
2575 int len;
2576 int undo = FALSE;
2577
2578 if (nchar == 'u')
2579 {
2580 ++no_mapping;
2581 ++allow_keys; // no mapping for nchar, but allow key codes
2582 nchar = plain_vgetc();
2583 LANGMAP_ADJUST(nchar, TRUE);
2584 --no_mapping;
2585 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002586 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002587
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002588 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2589 {
2590 clearopbeep(cap->oap);
2591 return OK;
2592 }
2593 undo = TRUE;
2594 }
2595
2596 if (checkclearop(cap->oap))
2597 return OK;
2598 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2599 return FAIL;
2600 if (ptr == NULL)
2601 {
2602 pos_T pos = curwin->w_cursor;
2603
2604 // Find bad word under the cursor. When 'spell' is
2605 // off this fails and find_ident_under_cursor() is
2606 // used below.
2607 emsg_off++;
2608 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2609 emsg_off--;
2610 if (len != 0 && curwin->w_cursor.col <= pos.col)
2611 ptr = ml_get_pos(&curwin->w_cursor);
2612 curwin->w_cursor = pos;
2613 }
2614
2615 if (ptr == NULL
2616 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2617 return FAIL;
2618 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2619 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2620 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2621
2622 return OK;
2623}
2624#endif
2625
2626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * Commands that start with "z".
2628 */
2629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002630nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 long n;
2633 colnr_T col;
2634 int nchar = cap->nchar;
2635#ifdef FEAT_FOLDING
2636 long old_fdl = curwin->w_p_fdl;
2637 int old_fen = curwin->w_p_fen;
2638#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002639 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002641 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (
2645#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002646 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2647 // and "zC" only in Visual mode. "zj" and "zk" are motion
2648 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 cap->nchar != 'f' && cap->nchar != 'F'
2650 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2651 && cap->nchar != 'j' && cap->nchar != 'k'
2652 &&
2653#endif
2654 checkclearop(cap->oap))
2655 return;
2656
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002657 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2658 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2660 && cap->count0
2661 && cap->count0 != curwin->w_cursor.lnum)
2662 {
2663 setpcmark();
2664 if (cap->count0 > curbuf->b_ml.ml_line_count)
2665 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2666 else
2667 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002668 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670
2671 switch (nchar)
2672 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 case '+':
2675 if (cap->count0 == 0)
2676 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 // No count given: put cursor at the line below screen
2678 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2680 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2681 else
2682 curwin->w_cursor.lnum = curwin->w_botline;
2683 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002684 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 case NL:
2686 case CAR:
2687 case K_KENTER:
2688 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002692 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002693 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 break;
2695
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002701 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002702 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 break;
2704
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // "z^", "z-" and "zb": put cursor at bottom of screen
2706 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2707 // when <count> is at bottom of window, and puts that one at
2708 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (cap->count0 != 0)
2710 {
2711 scroll_cursor_bot(0, TRUE);
2712 curwin->w_cursor.lnum = curwin->w_topline;
2713 }
2714 else if (curwin->w_topline == 1)
2715 curwin->w_cursor.lnum = 1;
2716 else
2717 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 case '-':
2720 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002724 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002725 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 break;
2727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002730 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 case 'h':
2735 case K_LEFT:
2736 if (!curwin->w_p_wrap)
2737 {
2738 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2739 curwin->w_leftcol = 0;
2740 else
2741 curwin->w_leftcol -= (colnr_T)cap->count1;
2742 leftcol_changed();
2743 }
2744 break;
2745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 case 'l':
2752 case K_RIGHT:
2753 if (!curwin->w_p_wrap)
2754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 curwin->w_leftcol += (colnr_T)cap->count1;
2757 leftcol_changed();
2758 }
2759 break;
2760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 case 's': if (!curwin->w_p_wrap)
2763 {
2764#ifdef FEAT_FOLDING
2765 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else
2768#endif
2769 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002770 if ((long)col > siso)
2771 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773 col = 0;
2774 if (curwin->w_leftcol != col)
2775 {
2776 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002777 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779 }
2780 break;
2781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002782 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 case 'e': if (!curwin->w_p_wrap)
2784 {
2785#ifdef FEAT_FOLDING
2786 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
2789#endif
2790 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002791 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002792 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 col = 0;
2794 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002795 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (curwin->w_leftcol != col)
2797 {
2798 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002799 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
2802 break;
2803
Christian Brabandt2fa93842021-05-30 22:17:25 +02002804 // "zp", "zP" in block mode put without addind trailing spaces
2805 case 'P':
2806 case 'p': nv_put(cap);
2807 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002808 // "zy" Yank without trailing spaces
2809 case 'y': nv_operator(cap);
2810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002812 // "zF": create fold command
2813 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 case 'F':
2815 case 'f': if (foldManualAllowed(TRUE))
2816 {
2817 cap->nchar = 'f';
2818 nv_operator(cap);
2819 curwin->w_p_fen = TRUE;
2820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002821 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2823 {
2824 nv_operator(cap);
2825 finish_op = TRUE;
2826 }
2827 }
2828 else
2829 clearopbeep(cap->oap);
2830 break;
2831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // "zd": delete fold at cursor
2833 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case 'd':
2835 case 'D': if (foldManualAllowed(FALSE))
2836 {
2837 if (VIsual_active)
2838 nv_operator(cap);
2839 else
2840 deleteFold(curwin->w_cursor.lnum,
2841 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2842 }
2843 break;
2844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case 'E': if (foldmethodIsManual(curwin))
2847 {
2848 clearFolding(curwin);
2849 changed_window_setting();
2850 }
2851 else if (foldmethodIsMarker(curwin))
2852 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2853 TRUE, FALSE);
2854 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002855 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 break;
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case 'n': curwin->w_p_fen = FALSE;
2860 break;
2861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case 'N': curwin->w_p_fen = TRUE;
2864 break;
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2868 break;
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2872 openFold(curwin->w_cursor.lnum, cap->count1);
2873 else
2874 {
2875 closeFold(curwin->w_cursor.lnum, cap->count1);
2876 curwin->w_p_fen = TRUE;
2877 }
2878 break;
2879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002880 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2882 openFoldRecurse(curwin->w_cursor.lnum);
2883 else
2884 {
2885 closeFoldRecurse(curwin->w_cursor.lnum);
2886 curwin->w_p_fen = TRUE;
2887 }
2888 break;
2889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 case 'o': if (VIsual_active)
2892 nv_operator(cap);
2893 else
2894 openFold(curwin->w_cursor.lnum, cap->count1);
2895 break;
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case 'O': if (VIsual_active)
2899 nv_operator(cap);
2900 else
2901 openFoldRecurse(curwin->w_cursor.lnum);
2902 break;
2903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 case 'c': if (VIsual_active)
2906 nv_operator(cap);
2907 else
2908 closeFold(curwin->w_cursor.lnum, cap->count1);
2909 curwin->w_p_fen = TRUE;
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'C': if (VIsual_active)
2914 nv_operator(cap);
2915 else
2916 closeFoldRecurse(curwin->w_cursor.lnum);
2917 curwin->w_p_fen = TRUE;
2918 break;
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 case 'v': foldOpenCursor();
2922 break;
2923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 curwin->w_foldinvalid = TRUE; // recompute folds
2927 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 foldOpenCursor();
2929 break;
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 curwin->w_foldinvalid = TRUE; // recompute folds
2934 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 break;
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002939 {
2940 curwin->w_p_fdl -= cap->count1;
2941 if (curwin->w_p_fdl < 0)
2942 curwin->w_p_fdl = 0;
2943 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 curwin->w_p_fen = TRUE;
2946 break;
2947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 curwin->w_p_fen = TRUE;
2952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002955 case 'r': curwin->w_p_fdl += cap->count1;
2956 {
2957 int d = getDeepestNesting();
2958
2959 if (curwin->w_p_fdl >= d)
2960 curwin->w_p_fdl = d;
2961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 break;
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 break;
2968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 case 'j': // "zj" move to next fold downwards
2970 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2972 cap->count1) == FAIL)
2973 clearopbeep(cap->oap);
2974 break;
2975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002978#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002980 case 'g': // "zg": add good word to word list
2981 case 'w': // "zw": add wrong word to word list
2982 case 'G': // "zG": add good word to temp word list
2983 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002984 if (nv_zg_zw(cap, nchar) == FAIL)
2985 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002986 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002989 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002990 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002991 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002992#endif
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 default: clearopbeep(cap->oap);
2995 }
2996
2997#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (old_fen != curwin->w_p_fen)
3000 {
3001# ifdef FEAT_DIFF
3002 win_T *wp;
3003
3004 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 FOR_ALL_WINDOWS(wp)
3008 {
3009 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3010 {
3011 wp->w_p_fen = curwin->w_p_fen;
3012 changed_window_setting_win(wp);
3013 }
3014 }
3015 }
3016# endif
3017 changed_window_setting();
3018 }
3019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (old_fdl != curwin->w_p_fdl)
3022 newFoldLevel();
3023#endif
3024}
3025
3026#ifdef FEAT_GUI
3027/*
3028 * Vertical scrollbar movement.
3029 */
3030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003031nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 if (cap->oap->op_type != OP_NOP)
3034 clearopbeep(cap->oap);
3035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 gui_do_scroll();
3038}
3039
3040/*
3041 * Horizontal scrollbar movement.
3042 */
3043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003044nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 if (cap->oap->op_type != OP_NOP)
3047 clearopbeep(cap->oap);
3048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003050 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052#endif
3053
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003054#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003055/*
3056 * Click in GUI tab.
3057 */
3058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003059nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003060{
3061 if (cap->oap->op_type != OP_NOP)
3062 clearopbeep(cap->oap);
3063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003064 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003065 goto_tabpage(current_tab);
3066}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003067
3068/*
3069 * Selected item in tab line menu.
3070 */
3071 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003072nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003073{
3074 if (cap->oap->op_type != OP_NOP)
3075 clearopbeep(cap->oap);
3076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003077 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003078 handle_tabmenu();
3079}
3080
3081/*
3082 * Handle selecting an item of the GUI tab line menu.
3083 * Used in Normal and Insert mode.
3084 */
3085 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003086handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003087{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003088 switch (current_tabmenu)
3089 {
3090 case TABLINE_MENU_CLOSE:
3091 if (current_tab == 0)
3092 do_cmdline_cmd((char_u *)"tabclose");
3093 else
3094 {
3095 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3096 current_tab);
3097 do_cmdline_cmd(IObuff);
3098 }
3099 break;
3100
3101 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003102 if (current_tab == 0)
3103 do_cmdline_cmd((char_u *)"$tabnew");
3104 else
3105 {
3106 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3107 current_tab - 1);
3108 do_cmdline_cmd(IObuff);
3109 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003110 break;
3111
3112 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003113 if (current_tab == 0)
3114 do_cmdline_cmd((char_u *)"browse $tabnew");
3115 else
3116 {
3117 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3118 current_tab - 1);
3119 do_cmdline_cmd(IObuff);
3120 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003121 break;
3122 }
3123}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003124#endif
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126/*
3127 * "Q" command.
3128 */
3129 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003130nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003132 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003134 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003135 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 do_exmode(FALSE);
3137}
3138
3139/*
3140 * Handle a ":" command.
3141 */
3142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003143nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003145 int old_p_im;
3146 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003147 int is_cmdkey = cap->cmdchar == K_COMMAND
3148 || cap->cmdchar == K_SCRIPT_COMMAND;
3149 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
Bram Moolenaar957cf672020-11-12 14:21:06 +01003151 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 nv_operator(cap);
3153 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 if (cap->oap->op_type != OP_NOP)
3156 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 cap->oap->motion_type = MCHAR;
3159 cap->oap->inclusive = FALSE;
3160 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003161 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003163 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 stuffcharReadbuff('.');
3165 if (cap->count0 > 1)
3166 {
3167 stuffReadbuff((char_u *)",.+");
3168 stuffnumReadbuff((long)cap->count0 - 1L);
3169 }
3170 }
3171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (KeyTyped)
3174 compute_cmdrow();
3175
3176 old_p_im = p_im;
3177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003179 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3180 if (is_cmdkey)
3181 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3182 else
3183 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (p_im != old_p_im)
3187 {
3188 if (p_im)
3189 restart_edit = 'i';
3190 else
3191 restart_edit = 0;
3192 }
3193
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003194 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003195 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003196 clearop(cap->oap);
3197 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3199 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003200 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3201 || did_emsg
3202 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 clearopbeep(cap->oap);
3205 }
3206}
3207
3208/*
3209 * Handle CTRL-G command.
3210 */
3211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003212nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003217 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 showmode();
3219 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003220 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 fileinfo((int)cap->count0, FALSE, TRUE);
3223}
3224
3225/*
3226 * Handle CTRL-H <Backspace> command.
3227 */
3228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003229nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (VIsual_active && VIsual_select)
3232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 v_visop(cap);
3235 }
3236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 nv_left(cap);
3238}
3239
3240/*
3241 * CTRL-L: clear screen and redraw.
3242 */
3243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003244nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 if (!checkclearop(cap->oap))
3247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003249 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003250 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003251# ifdef FEAT_RELTIME
3252 {
3253 win_T *wp;
3254
3255 FOR_ALL_WINDOWS(wp)
3256 wp->w_s->b_syn_slow = FALSE;
3257 }
3258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003260 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003261#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3262# ifdef VIMDLL
3263 if (!gui.in_use)
3264# endif
3265 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268}
3269
3270/*
3271 * CTRL-O: In Select mode: switch to Visual mode for one command.
3272 * Otherwise: Go to older pcmark.
3273 */
3274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003275nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (VIsual_active && VIsual_select)
3278 {
3279 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003280 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 cap->count1 = -cap->count1;
3287 nv_pcmark(cap);
3288 }
3289}
3290
3291/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003292 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3293 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 */
3295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003296nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 if (!checkclearopq(cap->oap))
3299 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3300 GETF_SETMARK|GETF_ALT, FALSE);
3301}
3302
3303/*
3304 * "Z" commands.
3305 */
3306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003307nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 if (!checkclearopq(cap->oap))
3310 {
3311 switch (cap->nchar)
3312 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003313 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 case 'Z': do_cmdline_cmd((char_u *)"x");
3315 break;
3316
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003317 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 case 'Q': do_cmdline_cmd((char_u *)"q!");
3319 break;
3320
3321 default: clearopbeep(cap->oap);
3322 }
3323 }
3324}
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326/*
3327 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3328 */
3329 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003330do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
3332 oparg_T oa;
3333 cmdarg_T ca;
3334
3335 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003336 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 ca.oap = &oa;
3338 ca.cmdchar = c1;
3339 ca.nchar = c2;
3340 nv_ident(&ca);
3341}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003344 * 'K' normal-mode command. Get the command to lookup the keyword under the
3345 * cursor.
3346 */
3347 static int
3348nv_K_getcmd(
3349 cmdarg_T *cap,
3350 char_u *kp,
3351 int kp_help,
3352 int kp_ex,
3353 char_u **ptr_arg,
3354 int n,
3355 char_u *buf,
3356 unsigned buflen)
3357{
3358 char_u *ptr = *ptr_arg;
3359 int isman;
3360 int isman_s;
3361
3362 if (kp_help)
3363 {
3364 // in the help buffer
3365 STRCPY(buf, "he! ");
3366 return n;
3367 }
3368
3369 if (kp_ex)
3370 {
3371 // 'keywordprog' is an ex command
3372 if (cap->count0 != 0)
3373 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3374 else
3375 STRCPY(buf, kp);
3376 STRCAT(buf, " ");
3377 return n;
3378 }
3379
3380 // An external command will probably use an argument starting
3381 // with "-" as an option. To avoid trouble we skip the "-".
3382 while (*ptr == '-' && n > 0)
3383 {
3384 ++ptr;
3385 --n;
3386 }
3387 if (n == 0)
3388 {
3389 // found dashes only
3390 emsg(_(e_no_identifier_under_cursor));
3391 vim_free(buf);
3392 *ptr_arg = ptr;
3393 return 0;
3394 }
3395
3396 // When a count is given, turn it into a range. Is this
3397 // really what we want?
3398 isman = (STRCMP(kp, "man") == 0);
3399 isman_s = (STRCMP(kp, "man -s") == 0);
3400 if (cap->count0 != 0 && !(isman || isman_s))
3401 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3402
3403 STRCAT(buf, "! ");
3404 if (cap->count0 == 0 && isman_s)
3405 STRCAT(buf, "man");
3406 else
3407 STRCAT(buf, kp);
3408 STRCAT(buf, " ");
3409 if (cap->count0 != 0 && (isman || isman_s))
3410 {
3411 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3412 STRCAT(buf, " ");
3413 }
3414
3415 *ptr_arg = ptr;
3416 return n;
3417}
3418
3419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 * Handle the commands that use the word under the cursor.
3421 * [g] CTRL-] :ta to current identifier
3422 * [g] 'K' run program for current identifier
3423 * [g] '*' / to current identifier or string
3424 * [g] '#' ? to current identifier or string
3425 * g ']' :tselect for current identifier
3426 */
3427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003428nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 char_u *ptr = NULL;
3431 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003432 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003433 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003435 char_u *kp; // value of 'keywordprg'
3436 int kp_help; // 'keywordprg' is ":he"
3437 int kp_ex; // 'keywordprg' starts with ":"
3438 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003440 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003441 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 cmdchar = cap->nchar;
3447 g_cmd = TRUE;
3448 }
3449 else
3450 {
3451 cmdchar = cap->cmdchar;
3452 g_cmd = FALSE;
3453 }
3454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003455 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 cmdchar = '#';
3457
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003458 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3462 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (checkclearopq(cap->oap))
3464 return;
3465 }
3466
3467 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3468 (cmdchar == '*' || cmdchar == '#')
3469 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3470 {
3471 clearop(cap->oap);
3472 return;
3473 }
3474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475 // Allocate buffer to put the command in. Inserting backslashes can
3476 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3477 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3479 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3480 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003481 if (kp_help && *skipwhite(ptr) == NUL)
3482 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003483 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003484 return;
3485 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003486 kp_ex = (*kp == ':');
3487 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3488 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (buf == NULL)
3490 return;
3491 buf[0] = NUL;
3492
3493 switch (cmdchar)
3494 {
3495 case '*':
3496 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003497 // Put cursor at start of word, makes search skip the word
3498 // under the cursor.
3499 // Call setpcmark() first, so "*``" puts the cursor back where
3500 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 setpcmark();
3502 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3503
3504 if (!g_cmd && vim_iswordp(ptr))
3505 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003506 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 break;
3508
3509 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003510 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3511 if (n == 0)
3512 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 break;
3514
3515 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003516 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#ifdef FEAT_CSCOPE
3518 if (p_cst)
3519 STRCPY(buf, "cstag ");
3520 else
3521#endif
3522 STRCPY(buf, "ts ");
3523 break;
3524
3525 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003526 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (curbuf->b_help)
3528 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003530 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003531 if (g_cmd)
3532 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003533 else if (cap->count0 == 0)
3534 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003535 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003536 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003540 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003541 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003543 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003544 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003546 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003547 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003548 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003549 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003550 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003551 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003553 vim_free(buf);
3554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003556 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003557 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 {
3559 vim_free(buf);
3560 vim_free(p);
3561 return;
3562 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003563 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003564 STRCAT(buf, p);
3565 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003567 else
3568 {
3569 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003570 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003572 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003573 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003574 {
3575 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003576 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003577 aux_ptr = (char_u *)"";
3578 else
3579 aux_ptr = (char_u *)"\\|\"\n[";
3580 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003581 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003582 aux_ptr = (char_u *)"\\|\"\n*?[";
3583
3584 p = buf + STRLEN(buf);
3585 while (n-- > 0)
3586 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003587 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003588 if (vim_strchr(aux_ptr, *ptr) != NULL)
3589 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003590 // When current byte is a part of multibyte character, copy all
3591 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003592 if (has_mbyte)
3593 {
3594 int i;
3595 int len = (*mb_ptr2len)(ptr) - 1;
3596
3597 for (i = 0; i < len && n >= 1; ++i, --n)
3598 *p++ = *ptr++;
3599 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 *p++ = *ptr++;
3601 }
3602 *p = NUL;
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003605 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (cmdchar == '*' || cmdchar == '#')
3607 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003608 if (!g_cmd && (has_mbyte
3609 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3610 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003612
3613 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003614 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003616
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003617 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003620 {
3621 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003623 g_tag_at_cursor = FALSE;
3624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 vim_free(buf);
3627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * Get visually selected text, within one line only.
3631 * Returns FAIL if more than one line selected.
3632 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003633 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003634get_visual_text(
3635 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003636 char_u **pp, // return: start of selected text
3637 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 if (VIsual_mode != 'V')
3640 unadjust_for_sel();
3641 if (VIsual.lnum != curwin->w_cursor.lnum)
3642 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003643 if (cap != NULL)
3644 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 return FAIL;
3646 }
3647 if (VIsual_mode == 'V')
3648 {
3649 *pp = ml_get_curline();
3650 *lenp = (int)STRLEN(*pp);
3651 }
3652 else
3653 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003654 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
3656 *pp = ml_get_pos(&curwin->w_cursor);
3657 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3658 }
3659 else
3660 {
3661 *pp = ml_get_pos(&VIsual);
3662 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3663 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003664 if (**pp == NUL)
3665 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003666 if (*lenp > 0)
3667 {
3668 if (has_mbyte)
3669 // Correct the length to include all bytes of the last
3670 // character.
3671 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3672 else if ((*pp)[*lenp - 1] == NUL)
3673 // Do not include a trailing NUL.
3674 *lenp -= 1;
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 reset_VIsual_and_resel();
3678 return OK;
3679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
3681/*
3682 * CTRL-T: backwards in tag stack
3683 */
3684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003685nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 if (!checkclearopq(cap->oap))
3688 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3689}
3690
3691/*
3692 * Handle scrolling command 'H', 'L' and 'M'.
3693 */
3694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003695nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
3697 int used = 0;
3698 long n;
3699#ifdef FEAT_FOLDING
3700 linenr_T lnum;
3701#endif
3702 int half;
3703
3704 cap->oap->motion_type = MLINE;
3705 setpcmark();
3706
3707 if (cap->cmdchar == 'L')
3708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003709 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 curwin->w_cursor.lnum = curwin->w_botline - 1;
3711 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3712 curwin->w_cursor.lnum = 1;
3713 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003714 {
3715#ifdef FEAT_FOLDING
3716 if (hasAnyFolding(curwin))
3717 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003718 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003719 for (n = cap->count1 - 1; n > 0
3720 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3721 {
3722 (void)hasFolding(curwin->w_cursor.lnum,
3723 &curwin->w_cursor.lnum, NULL);
3724 --curwin->w_cursor.lnum;
3725 }
3726 }
3727 else
3728#endif
3729 curwin->w_cursor.lnum -= cap->count1 - 1;
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else
3733 {
3734 if (cap->cmdchar == 'M')
3735 {
3736#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003737 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 used -= diff_check_fill(curwin, curwin->w_topline)
3739 - curwin->w_topfill;
3740#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003741 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3743 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3744 {
3745#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 // Count half he number of filler lines to be "below this
3747 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3749 + n) / 2 >= half)
3750 {
3751 --n;
3752 break;
3753 }
3754#endif
3755 used += plines(curwin->w_topline + n);
3756 if (used >= half)
3757 break;
3758#ifdef FEAT_FOLDING
3759 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3760 n = lnum - curwin->w_topline;
3761#endif
3762 }
3763 if (n > 0 && used > curwin->w_height)
3764 --n;
3765 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003769#ifdef FEAT_FOLDING
3770 if (hasAnyFolding(curwin))
3771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003772 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003773 lnum = curwin->w_topline;
3774 while (n-- > 0 && lnum < curwin->w_botline - 1)
3775 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003776 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003777 ++lnum;
3778 }
3779 n = lnum - curwin->w_topline;
3780 }
3781#endif
3782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 curwin->w_cursor.lnum = curwin->w_topline + n;
3784 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3785 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3786 }
3787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003788 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003789 if (cap->oap->op_type == OP_NOP)
3790 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 beginline(BL_SOL | BL_FIX);
3792}
3793
3794/*
3795 * Cursor right commands.
3796 */
3797 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003798nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
3800 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003801 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003803 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003805 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003806 if (mod_mask & MOD_MASK_CTRL)
3807 cap->arg = TRUE;
3808 nv_wordcmd(cap);
3809 return;
3810 }
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 cap->oap->motion_type = MCHAR;
3813 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003814 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003816 // In virtual edit mode, there's no such thing as "past_line", as lines
3817 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003819 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
3821 for (n = cap->count1; n > 0; --n)
3822 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003823 if ((!past_line && oneright() == FAIL)
3824 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003825 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003827 // <Space> wraps to next line if 'whichwrap' has 's'.
3828 // 'l' wraps to next line if 'whichwrap' has 'l'.
3829 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if ( ((cap->cmdchar == ' '
3831 && vim_strchr(p_ww, 's') != NULL)
3832 || (cap->cmdchar == 'l'
3833 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003834 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 && vim_strchr(p_ww, '>') != NULL))
3836 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003838 // When deleting we also count the NL as a character.
3839 // Set cap->oap->inclusive when last char in the line is
3840 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003841 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003843 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 cap->oap->inclusive = TRUE;
3845 else
3846 {
3847 ++curwin->w_cursor.lnum;
3848 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 curwin->w_set_curswant = TRUE;
3851 cap->oap->inclusive = FALSE;
3852 }
3853 continue;
3854 }
3855 if (cap->oap->op_type == OP_NOP)
3856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003857 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (n == cap->count1)
3859 beep_flush();
3860 }
3861 else
3862 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003863 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 cap->oap->inclusive = TRUE;
3865 }
3866 break;
3867 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003868 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (virtual_active())
3872 oneright();
3873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003876 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 ++curwin->w_cursor.col;
3879 }
3880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882#ifdef FEAT_FOLDING
3883 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3884 && cap->oap->op_type == OP_NOP)
3885 foldOpenCursor();
3886#endif
3887}
3888
3889/*
3890 * Cursor left commands.
3891 *
3892 * Returns TRUE when operator end should not be adjusted.
3893 */
3894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003895nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
3897 long n;
3898
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003899 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003902 if (mod_mask & MOD_MASK_CTRL)
3903 cap->arg = 1;
3904 nv_bck_word(cap);
3905 return;
3906 }
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 cap->oap->motion_type = MCHAR;
3909 cap->oap->inclusive = FALSE;
3910 for (n = cap->count1; n > 0; --n)
3911 {
3912 if (oneleft() == FAIL)
3913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003914 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3915 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3916 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if ( (((cap->cmdchar == K_BS
3918 || cap->cmdchar == Ctrl_H)
3919 && vim_strchr(p_ww, 'b') != NULL)
3920 || (cap->cmdchar == 'h'
3921 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003922 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 && vim_strchr(p_ww, '<') != NULL))
3924 && curwin->w_cursor.lnum > 1)
3925 {
3926 --(curwin->w_cursor.lnum);
3927 coladvance((colnr_T)MAXCOL);
3928 curwin->w_set_curswant = TRUE;
3929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003930 // When the NL before the first char has to be deleted we
3931 // put the cursor on the NUL after the previous line.
3932 // This is a very special case, be careful!
3933 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if ( (cap->oap->op_type == OP_DELETE
3935 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003936 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003938 char_u *cp = ml_get_cursor();
3939
3940 if (*cp != NUL)
3941 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003942 if (has_mbyte)
3943 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3944 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003945 ++curwin->w_cursor.col;
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 cap->retval |= CA_NO_ADJ_OP_END;
3948 }
3949 continue;
3950 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003951 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3953 beep_flush();
3954 break;
3955 }
3956 }
3957#ifdef FEAT_FOLDING
3958 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3959 && cap->oap->op_type == OP_NOP)
3960 foldOpenCursor();
3961#endif
3962}
3963
3964/*
3965 * Cursor up commands.
3966 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3967 */
3968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003969nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003971 if (mod_mask & MOD_MASK_SHIFT)
3972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003973 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 cap->arg = BACKWARD;
3975 nv_page(cap);
3976 }
3977 else
3978 {
3979 cap->oap->motion_type = MLINE;
3980 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3981 clearopbeep(cap->oap);
3982 else if (cap->arg)
3983 beginline(BL_WHITE | BL_FIX);
3984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985}
3986
3987/*
3988 * Cursor down commands.
3989 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3990 */
3991 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003992nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 if (mod_mask & MOD_MASK_SHIFT)
3995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003996 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997 cap->arg = FORWARD;
3998 nv_page(cap);
3999 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004000#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004001 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004002 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4003 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004007 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004008 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 cmdwin_result = CAR;
4010 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004011#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004012 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004013 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4014 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4015 {
4016 invoke_prompt_callback();
4017 if (restart_edit == 0)
4018 restart_edit = 'a';
4019 }
4020 else
4021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
4023 cap->oap->motion_type = MLINE;
4024 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4025 clearopbeep(cap->oap);
4026 else if (cap->arg)
4027 beginline(BL_WHITE | BL_FIX);
4028 }
4029 }
4030}
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032/*
4033 * Grab the file name under the cursor and edit it.
4034 */
4035 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004036nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
4038 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004039 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004041 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004043 if (curbuf_locked())
4044 {
4045 clearop(cap->oap);
4046 return;
4047 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004048#ifdef FEAT_PROP_POPUP
4049 if (ERROR_IF_TERM_POPUP_WINDOW)
4050 return;
4051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004053 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 if (ptr != NULL)
4056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004057 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004058 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004059 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004061 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004062 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004063 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004064 {
4065 curwin->w_cursor.lnum = lnum;
4066 check_cursor_lnum();
4067 beginline(BL_SOL | BL_FIX);
4068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 vim_free(ptr);
4070 }
4071 else
4072 clearop(cap->oap);
4073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075/*
4076 * <End> command: to end of current line or last line.
4077 */
4078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004079nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004083 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004085 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087 nv_dollar(cap);
4088}
4089
4090/*
4091 * Handle the "$" command.
4092 */
4093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004094nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
4096 cap->oap->motion_type = MCHAR;
4097 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004098 // In virtual mode when off the edge of a line and an operator
4099 // is pending (whew!) keep the cursor where it is.
4100 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (!virtual_active() || gchar_cursor() != NUL
4102 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004103 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (cursor_down((long)(cap->count1 - 1),
4105 cap->oap->op_type == OP_NOP) == FAIL)
4106 clearopbeep(cap->oap);
4107#ifdef FEAT_FOLDING
4108 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4109 foldOpenCursor();
4110#endif
4111}
4112
4113/*
4114 * Implementation of '?' and '/' commands.
4115 * If cap->arg is TRUE don't set PC mark.
4116 */
4117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004118nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004121 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004125 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 cap->cmdchar = 'g';
4127 cap->nchar = '?';
4128 nv_operator(cap);
4129 return;
4130 }
4131
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004132 // When using 'incsearch' the cursor may be moved to set a different search
4133 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004134 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 if (cap->searchbuf == NULL)
4137 {
4138 clearop(oap);
4139 return;
4140 }
4141
Bram Moolenaar46539112015-02-17 15:43:57 +01004142 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004143 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004144 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145}
4146
LemonBoya4399382022-04-09 21:04:08 +01004147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148/*
4149 * Handle "N" and "n" commands.
4150 * cap->arg is SEARCH_REV for "N", 0 for "n".
4151 */
4152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004153nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004155 pos_T old = curwin->w_cursor;
4156 int wrapped = FALSE;
4157 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004158
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004159 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004160 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004161 // Avoid getting stuck on the current cursor position, which can
4162 // happen when an offset is given and the cursor is on the last char
4163 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004164 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004165 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004166 cap->count1 -= 1;
4167 }
LemonBoya4399382022-04-09 21:04:08 +01004168
4169#ifdef FEAT_SEARCH_EXTRA
4170 // Redraw the window to refresh the highlighted matches.
4171 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004172 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174}
4175
4176/*
4177 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4178 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004179 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004181 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004182normal_search(
4183 cmdarg_T *cap,
4184 int dir,
4185 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004186 int opt, // extra flags for do_search()
4187 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
4189 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004190 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004191#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004192 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195 cap->oap->motion_type = MCHAR;
4196 cap->oap->inclusive = FALSE;
4197 cap->oap->use_reg_one = TRUE;
4198 curwin->w_set_curswant = TRUE;
4199
Bram Moolenaara80faa82020-04-12 19:37:17 +02004200 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004201 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004202 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4203 if (wrapped != NULL)
4204 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (i == 0)
4206 clearop(cap->oap);
4207 else
4208 {
4209 if (i == 2)
4210 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#ifdef FEAT_FOLDING
4213 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4214 foldOpenCursor();
4215#endif
4216 }
LemonBoya4399382022-04-09 21:04:08 +01004217#ifdef FEAT_SEARCH_EXTRA
4218 // Redraw the window to refresh the highlighted matches.
4219 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004220 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004223 // "/$" will put the cursor after the end of the line, may need to
4224 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004226 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228
4229/*
4230 * Character search commands.
4231 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4232 * ',' and FALSE for ';'.
4233 * cap->nchar is NUL for ',' and ';' (repeat the search)
4234 */
4235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004236nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 int t_cmd;
4239
4240 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4241 t_cmd = TRUE;
4242 else
4243 t_cmd = FALSE;
4244
4245 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4247 clearopbeep(cap->oap);
4248 else
4249 {
4250 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004251 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4253 && (t_cmd || cap->oap->op_type != OP_NOP))
4254 {
4255 colnr_T scol, ecol;
4256
4257 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4258 curwin->w_cursor.coladd = ecol - scol;
4259 }
4260 else
4261 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef FEAT_FOLDING
4264 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4265 foldOpenCursor();
4266#endif
4267 }
4268}
4269
4270/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004271 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4272 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4273 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4274 * "[m" or "]m" search for prev/next start of (Java) method.
4275 * "[M" or "]M" search for prev/next end of (Java) method.
4276 */
4277 static void
4278nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4279{
4280 pos_T new_pos = {0, 0, 0};
4281 pos_T *pos = NULL; // init for GCC
4282 pos_T prev_pos;
4283 long n;
4284 int findc;
4285 int c;
4286
4287 if (cap->nchar == '*')
4288 cap->nchar = '/';
4289 prev_pos.lnum = 0;
4290 if (cap->nchar == 'm' || cap->nchar == 'M')
4291 {
4292 if (cap->cmdchar == '[')
4293 findc = '{';
4294 else
4295 findc = '}';
4296 n = 9999;
4297 }
4298 else
4299 {
4300 findc = cap->nchar;
4301 n = cap->count1;
4302 }
4303 for ( ; n > 0; --n)
4304 {
4305 if ((pos = findmatchlimit(cap->oap, findc,
4306 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4307 {
4308 if (new_pos.lnum == 0) // nothing found
4309 {
4310 if (cap->nchar != 'm' && cap->nchar != 'M')
4311 clearopbeep(cap->oap);
4312 }
4313 else
4314 pos = &new_pos; // use last one found
4315 break;
4316 }
4317 prev_pos = new_pos;
4318 curwin->w_cursor = *pos;
4319 new_pos = *pos;
4320 }
4321 curwin->w_cursor = *old_pos;
4322
4323 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4324 // brought us to the match for "[m" and "]M" when inside a method.
4325 // Try finding the '{' or '}' we want to be at.
4326 // Also repeat for the given count.
4327 if (cap->nchar == 'm' || cap->nchar == 'M')
4328 {
4329 // norm is TRUE for "]M" and "[m"
4330 int norm = ((findc == '{') == (cap->nchar == 'm'));
4331
4332 n = cap->count1;
4333 // found a match: we were inside a method
4334 if (prev_pos.lnum != 0)
4335 {
4336 pos = &prev_pos;
4337 curwin->w_cursor = prev_pos;
4338 if (norm)
4339 --n;
4340 }
4341 else
4342 pos = NULL;
4343 while (n > 0)
4344 {
4345 for (;;)
4346 {
4347 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4348 {
4349 // if not found anything, that's an error
4350 if (pos == NULL)
4351 clearopbeep(cap->oap);
4352 n = 0;
4353 break;
4354 }
4355 c = gchar_cursor();
4356 if (c == '{' || c == '}')
4357 {
4358 // Must have found end/start of class: use it.
4359 // Or found the place to be at.
4360 if ((c == findc && norm) || (n == 1 && !norm))
4361 {
4362 new_pos = curwin->w_cursor;
4363 pos = &new_pos;
4364 n = 0;
4365 }
4366 // if no match found at all, we started outside of the
4367 // class and we're inside now. Just go on.
4368 else if (new_pos.lnum == 0)
4369 {
4370 new_pos = curwin->w_cursor;
4371 pos = &new_pos;
4372 }
4373 // found start/end of other method: go to match
4374 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004375 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4376 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004377 n = 0;
4378 else
4379 curwin->w_cursor = *pos;
4380 break;
4381 }
4382 }
4383 --n;
4384 }
4385 curwin->w_cursor = *old_pos;
4386 if (pos == NULL && new_pos.lnum != 0)
4387 clearopbeep(cap->oap);
4388 }
4389 if (pos != NULL)
4390 {
4391 setpcmark();
4392 curwin->w_cursor = *pos;
4393 curwin->w_set_curswant = TRUE;
4394#ifdef FEAT_FOLDING
4395 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4396 && cap->oap->op_type == OP_NOP)
4397 foldOpenCursor();
4398#endif
4399 }
4400}
4401
4402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 * "[" and "]" commands.
4404 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4405 */
4406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004407nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004410 pos_T *pos = NULL; // init for GCC
4411 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 int flag;
4413 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 cap->oap->motion_type = MCHAR;
4416 cap->oap->inclusive = FALSE;
4417 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004418 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004420 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (cap->nchar == 'f')
4422 nv_gotofile(cap);
4423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004426 // Find the occurrence(s) of the identifier or define under cursor
4427 // in current and included files or jump to the first occurrence.
4428 //
4429 // search list jump
4430 // fwd bwd fwd bwd fwd bwd
4431 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4432 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004433 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
4435 char_u *ptr;
4436 int len;
4437
4438 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4439 clearop(cap->oap);
4440 else
4441 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004442 // Make a copy, if the line was changed it will be freed.
4443 ptr = vim_strnsave(ptr, len);
4444 if (ptr == NULL)
4445 return;
4446
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 find_pattern_in_path(ptr, 0, len, TRUE,
4448 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4449 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4450 cap->count1,
4451 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4452 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4453 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4454 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004455 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 curwin->w_set_curswant = TRUE;
4457 }
4458 }
4459 else
4460#endif
4461
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004462 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4463 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4464 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4465 // "[m" or "]m" search for prev/next start of (Java) method.
4466 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if ( (cap->cmdchar == '['
4468 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4469 || (cap->cmdchar == ']'
4470 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004471 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004473 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 else if (cap->nchar == '[' || cap->nchar == ']')
4475 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004476 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 flag = '{';
4478 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004479 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004482 // Imitate strange Vi behaviour: When using "]]" with an operator
4483 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004484 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 (cap->oap->op_type != OP_NOP
4486 && cap->arg == FORWARD && flag == '{')))
4487 clearopbeep(cap->oap);
4488 else
4489 {
4490 if (cap->oap->op_type == OP_NOP)
4491 beginline(BL_WHITE | BL_FIX);
4492#ifdef FEAT_FOLDING
4493 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4494 foldOpenCursor();
4495#endif
4496 }
4497 }
4498
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004499 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else if (cap->nchar == 'p' || cap->nchar == 'P')
4501 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004502 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004505 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 else if (cap->nchar == '\'' || cap->nchar == '`')
4507 {
4508 pos = &curwin->w_cursor;
4509 for (n = cap->count1; n > 0; --n)
4510 {
4511 prev_pos = *pos;
4512 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4513 cap->nchar == '\'');
4514 if (pos == NULL)
4515 break;
4516 }
4517 if (pos == NULL)
4518 pos = &prev_pos;
4519 nv_cursormark(cap, cap->nchar == '\'', pos);
4520 }
4521
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // [ or ] followed by a middle mouse click: put selected text with
4523 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004524 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
4526 (void)do_mouse(cap->oap, cap->nchar,
4527 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4528 cap->count1, PUT_FIXINDENT);
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004532 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else if (cap->nchar == 'z')
4534 {
4535 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4536 cap->count1) == FAIL)
4537 clearopbeep(cap->oap);
4538 }
4539#endif
4540
4541#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004542 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else if (cap->nchar == 'c')
4544 {
4545 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4546 cap->count1) == FAIL)
4547 clearopbeep(cap->oap);
4548 }
4549#endif
4550
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004551#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004552 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004553 else if (cap->nchar == 's' || cap->nchar == 'S')
4554 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004555 setpcmark();
4556 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004557 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4558 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004559 {
4560 clearopbeep(cap->oap);
4561 break;
4562 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004563 else
4564 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004565# ifdef FEAT_FOLDING
4566 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4567 foldOpenCursor();
4568# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569 }
4570#endif
4571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004572 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 else
4574 clearopbeep(cap->oap);
4575}
4576
4577/*
4578 * Handle Normal mode "%" command.
4579 */
4580 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004581nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
4583 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004584#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 linenr_T lnum = curwin->w_cursor.lnum;
4586#endif
4587
4588 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004589 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
4591 if (cap->count0 > 100)
4592 clearopbeep(cap->oap);
4593 else
4594 {
4595 cap->oap->motion_type = MLINE;
4596 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004597 // Round up, so 'normal 100%' always jumps at the line line.
4598 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4599 // overflow on 32-bits, so use a formula with less accuracy
4600 // to avoid overflows.
4601 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4603 / 100L * cap->count0;
4604 else
4605 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4606 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004607 if (curwin->w_cursor.lnum < 1)
4608 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4610 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4611 beginline(BL_SOL | BL_FIX);
4612 }
4613 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004614 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 cap->oap->motion_type = MCHAR;
4617 cap->oap->use_reg_one = TRUE;
4618 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4619 clearopbeep(cap->oap);
4620 else
4621 {
4622 setpcmark();
4623 curwin->w_cursor = *pos;
4624 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 }
4629#ifdef FEAT_FOLDING
4630 if (cap->oap->op_type == OP_NOP
4631 && lnum != curwin->w_cursor.lnum
4632 && (fdo_flags & FDO_PERCENT)
4633 && KeyTyped)
4634 foldOpenCursor();
4635#endif
4636}
4637
4638/*
4639 * Handle "(" and ")" commands.
4640 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4641 */
4642 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004643nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 cap->oap->motion_type = MCHAR;
4646 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004647 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004648 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 curwin->w_set_curswant = TRUE;
4650
4651 if (findsent(cap->arg, cap->count1) == FAIL)
4652 clearopbeep(cap->oap);
4653 else
4654 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004656 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#ifdef FEAT_FOLDING
4659 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4660 foldOpenCursor();
4661#endif
4662 }
4663}
4664
4665/*
4666 * "m" command: Mark a position.
4667 */
4668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004669nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 if (!checkclearop(cap->oap))
4672 {
4673 if (setmark(cap->nchar) == FAIL)
4674 clearopbeep(cap->oap);
4675 }
4676}
4677
4678/*
4679 * "{" and "}" commands.
4680 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4681 */
4682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004683nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 cap->oap->motion_type = MCHAR;
4686 cap->oap->inclusive = FALSE;
4687 cap->oap->use_reg_one = TRUE;
4688 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004689 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 clearopbeep(cap->oap);
4691 else
4692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#ifdef FEAT_FOLDING
4695 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4696 foldOpenCursor();
4697#endif
4698 }
4699}
4700
4701/*
4702 * "u" command: Undo or make lower case.
4703 */
4704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004705nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004707 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004709 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 cap->cmdchar = 'g';
4711 cap->nchar = 'u';
4712 nv_operator(cap);
4713 }
4714 else
4715 nv_kundo(cap);
4716}
4717
4718/*
4719 * <Undo> command.
4720 */
4721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004722nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 if (!checkclearopq(cap->oap))
4725 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004726#ifdef FEAT_JOB_CHANNEL
4727 if (bt_prompt(curbuf))
4728 {
4729 clearopbeep(cap->oap);
4730 return;
4731 }
4732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 u_undo((int)cap->count1);
4734 curwin->w_set_curswant = TRUE;
4735 }
4736}
4737
4738/*
4739 * Handle the "r" command.
4740 */
4741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004742nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 char_u *ptr;
4745 int had_ctrl_v;
4746 long n;
4747
4748 if (checkclearop(cap->oap))
4749 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004750#ifdef FEAT_JOB_CHANNEL
4751 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4752 {
4753 clearopbeep(cap->oap);
4754 return;
4755 }
4756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004758 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (cap->nchar == Ctrl_V)
4760 {
4761 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004762 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004763 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (cap->nchar > DEL)
4765 had_ctrl_v = NUL;
4766 }
4767 else
4768 had_ctrl_v = NUL;
4769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004771 if (IS_SPECIAL(cap->nchar))
4772 {
4773 clearopbeep(cap->oap);
4774 return;
4775 }
4776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004777 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (VIsual_active)
4779 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004780 if (got_int)
4781 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004782 if (had_ctrl_v)
4783 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004784 // Use a special (negative) number to make a difference between a
4785 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004786 if (cap->nchar == CAR)
4787 cap->nchar = REPLACE_CR_NCHAR;
4788 else if (cap->nchar == NL)
4789 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 nv_operator(cap);
4792 return;
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004795 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (virtual_active())
4797 {
4798 if (u_save_cursor() == FAIL)
4799 return;
4800 if (gchar_cursor() == NUL)
4801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 coladvance_force((colnr_T)(getviscol() + cap->count1));
4804 curwin->w_cursor.col -= cap->count1;
4805 }
4806 else if (gchar_cursor() == TAB)
4807 coladvance_force(getviscol());
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004810 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004812 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004813 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 clearopbeep(cap->oap);
4816 return;
4817 }
4818
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004819 // Replacing with a TAB is done by edit() when it is complicated because
4820 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4821 // Other characters are done below to avoid problems with things like
4822 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4824 {
4825 stuffnumReadbuff(cap->count1);
4826 stuffcharReadbuff('R');
4827 stuffcharReadbuff('\t');
4828 stuffcharReadbuff(ESC);
4829 return;
4830 }
4831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004832 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (u_save_cursor() == FAIL)
4834 return;
4835
4836 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4837 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004838 // Replace character(s) by a single newline.
4839 // Strange vi behaviour: Only one newline is inserted.
4840 // Delete the characters here.
4841 // Insert the newline with an insert command, takes care of
4842 // autoindent. The insert command depends on being on the last
4843 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004844 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 stuffcharReadbuff('\r');
4846 stuffcharReadbuff(ESC);
4847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004848 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 invoke_edit(cap, TRUE, 'r', FALSE);
4850 }
4851 else
4852 {
4853 prep_redo(cap->oap->regname, cap->count1,
4854 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4855
4856 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (has_mbyte)
4858 {
4859 int old_State = State;
4860
4861 if (cap->ncharC1 != 0)
4862 AppendCharToRedobuff(cap->ncharC1);
4863 if (cap->ncharC2 != 0)
4864 AppendCharToRedobuff(cap->ncharC2);
4865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004866 // This is slow, but it handles replacing a single-byte with a
4867 // multi-byte and the other way around. Also handles adding
4868 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 for (n = cap->count1; n > 0; --n)
4870 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004871 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004872 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4873 {
4874 int c = ins_copychar(curwin->w_cursor.lnum
4875 + (cap->nchar == Ctrl_Y ? -1 : 1));
4876 if (c != NUL)
4877 ins_char(c);
4878 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004879 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004880 ++curwin->w_cursor.col;
4881 }
4882 else
4883 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 State = old_State;
4885 if (cap->ncharC1 != 0)
4886 ins_char(cap->ncharC1);
4887 if (cap->ncharC2 != 0)
4888 ins_char(cap->ncharC2);
4889 }
4890 }
4891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004893 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 for (n = cap->count1; n > 0; --n)
4895 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004896 // Get ptr again, because u_save and/or showmatch() will have
4897 // released the line. This may also happen in ins_copychar().
4898 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004899 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4900 {
4901 int c = ins_copychar(curwin->w_cursor.lnum
4902 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004903
4904 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004905 if (c != NUL)
4906 ptr[curwin->w_cursor.col] = c;
4907 }
4908 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004909 {
4910 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004911 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (p_sm && msg_silent == 0)
4914 showmatch(cap->nchar);
4915 ++curwin->w_cursor.col;
4916 }
4917#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004918 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004920 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar009b2592004-10-24 19:18:58 +00004922 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004923 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004925 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927#endif
4928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 changed_bytes(curwin->w_cursor.lnum,
4931 (colnr_T)(curwin->w_cursor.col - cap->count1));
4932 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004933 --curwin->w_cursor.col; // cursor on the last replaced char
4934 // if the character on the left of the current cursor is a multi-byte
4935 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (has_mbyte)
4937 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 curbuf->b_op_end = curwin->w_cursor;
4939 curwin->w_set_curswant = TRUE;
4940 set_last_insert(cap->nchar);
4941 }
4942}
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944/*
4945 * 'o': Exchange start and end of Visual area.
4946 * 'O': same, but in block mode exchange left and right corners.
4947 */
4948 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004949v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 pos_T old_cursor;
4952 colnr_T left, right;
4953
4954 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4955 {
4956 old_cursor = curwin->w_cursor;
4957 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4958 curwin->w_cursor.lnum = VIsual.lnum;
4959 coladvance(left);
4960 VIsual = curwin->w_cursor;
4961
4962 curwin->w_cursor.lnum = old_cursor.lnum;
4963 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004964 // 'selection "exclusive" and cursor at right-bottom corner: move it
4965 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4967 ++curwin->w_curswant;
4968 coladvance(curwin->w_curswant);
4969 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004971 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 curwin->w_cursor.lnum = VIsual.lnum;
4974 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4975 ++right;
4976 coladvance(right);
4977 VIsual = curwin->w_cursor;
4978
4979 curwin->w_cursor.lnum = old_cursor.lnum;
4980 coladvance(left);
4981 curwin->w_curswant = left;
4982 }
4983 }
4984 else
4985 {
4986 old_cursor = curwin->w_cursor;
4987 curwin->w_cursor = VIsual;
4988 VIsual = old_cursor;
4989 curwin->w_set_curswant = TRUE;
4990 }
4991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993/*
4994 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4995 */
4996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004997nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 cap->cmdchar = 'c';
5002 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 VIsual_mode = 'V';
5005 nv_operator(cap);
5006 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005007 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005010 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
5012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (virtual_active())
5014 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5016 }
5017 }
5018}
5019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020/*
5021 * "gr".
5022 */
5023 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005024nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (VIsual_active)
5027 {
5028 cap->cmdchar = 'r';
5029 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005030 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005032 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005035 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
5037 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005038 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005039 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 stuffcharReadbuff(cap->extra_char);
5041 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (virtual_active())
5043 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 invoke_edit(cap, TRUE, 'v', FALSE);
5045 }
5046 }
5047}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049/*
5050 * Swap case for "~" command, when it does not work like an operator.
5051 */
5052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005053n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 long n;
5056 pos_T startpos;
5057 int did_change = 0;
5058#ifdef FEAT_NETBEANS_INTG
5059 pos_T pos;
5060 char_u *ptr;
5061 int count;
5062#endif
5063
5064 if (checkclearopq(cap->oap))
5065 return;
5066
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005067 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 clearopbeep(cap->oap);
5070 return;
5071 }
5072
5073 prep_redo_cmd(cap);
5074
5075 if (u_save_cursor() == FAIL)
5076 return;
5077
5078 startpos = curwin->w_cursor;
5079#ifdef FEAT_NETBEANS_INTG
5080 pos = startpos;
5081#endif
5082 for (n = cap->count1; n > 0; --n)
5083 {
5084 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5085 inc_cursor();
5086 if (gchar_cursor() == NUL)
5087 {
5088 if (vim_strchr(p_ww, '~') != NULL
5089 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5090 {
5091#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005092 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 if (did_change)
5095 {
5096 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005097 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005098 netbeans_removed(curbuf, pos.lnum, pos.col,
5099 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005100 // line may have been flushed, get it again
5101 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005103 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 pos.col = 0;
5106 pos.lnum++;
5107 }
5108#endif
5109 ++curwin->w_cursor.lnum;
5110 curwin->w_cursor.col = 0;
5111 if (n > 1)
5112 {
5113 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5114 break;
5115 u_clearline();
5116 }
5117 }
5118 else
5119 break;
5120 }
5121 }
5122#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005123 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 ptr = ml_get(pos.lnum);
5126 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005127 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5128 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130#endif
5131
5132
5133 check_cursor();
5134 curwin->w_set_curswant = TRUE;
5135 if (did_change)
5136 {
5137 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5138 0L);
5139 curbuf->b_op_start = startpos;
5140 curbuf->b_op_end = curwin->w_cursor;
5141 if (curbuf->b_op_end.col > 0)
5142 --curbuf->b_op_end.col;
5143 }
5144}
5145
5146/*
5147 * Move cursor to mark.
5148 */
5149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005150nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 if (check_mark(pos) == FAIL)
5153 clearop(cap->oap);
5154 else
5155 {
5156 if (cap->cmdchar == '\''
5157 || cap->cmdchar == '`'
5158 || cap->cmdchar == '['
5159 || cap->cmdchar == ']')
5160 setpcmark();
5161 curwin->w_cursor = *pos;
5162 if (flag)
5163 beginline(BL_WHITE | BL_FIX);
5164 else
5165 check_cursor();
5166 }
5167 cap->oap->motion_type = flag ? MLINE : MCHAR;
5168 if (cap->cmdchar == '`')
5169 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 curwin->w_set_curswant = TRUE;
5172}
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Handle commands that are operators in Visual mode.
5176 */
5177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005182 // Uppercase means linewise, except in block mode, then "D" deletes till
5183 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (isupper(cap->cmdchar))
5185 {
5186 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005187 {
5188 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5192 curwin->w_curswant = MAXCOL;
5193 }
5194 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5195 nv_operator(cap);
5196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198/*
5199 * "s" and "S" commands.
5200 */
5201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005204#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005205 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005206 if (term_swap_diff() == OK)
5207 return;
5208#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005209#ifdef FEAT_JOB_CHANNEL
5210 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5211 {
5212 clearopbeep(cap->oap);
5213 return;
5214 }
5215#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005216 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005219 {
5220 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 cap->cmdchar = 'c';
5224 nv_operator(cap);
5225 }
5226 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 nv_optrans(cap);
5228}
5229
5230/*
5231 * Abbreviated commands.
5232 */
5233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005234nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005239 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (VIsual_active)
5241 v_visop(cap);
5242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 nv_optrans(cap);
5244}
5245
5246/*
5247 * Translate a command into another command.
5248 */
5249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5253 (char_u *)"d$", (char_u *)"c$",
5254 (char_u *)"cl", (char_u *)"cc",
5255 (char_u *)"yy", (char_u *)":s\r"};
5256 static char_u *str = (char_u *)"xXDCsSY&";
5257
5258 if (!checkclearopq(cap->oap))
5259 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005260 // In Vi "2D" doesn't delete the next line. Can't translate it
5261 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005262 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5263 {
5264 cap->oap->start = curwin->w_cursor;
5265 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005266#ifdef FEAT_EVAL
5267 set_op_var(OP_DELETE);
5268#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005269 cap->count1 = 1;
5270 nv_dollar(cap);
5271 finish_op = TRUE;
5272 ResetRedobuff();
5273 AppendCharToRedobuff('D');
5274 }
5275 else
5276 {
5277 if (cap->count0)
5278 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005279 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 cap->opcount = 0;
5283}
5284
5285/*
5286 * "'" and "`" commands. Also for "g'" and "g`".
5287 * cap->arg is TRUE for "'" and "g'".
5288 */
5289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005290nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 pos_T *pos;
5293 int c;
5294#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005295 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005296 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#endif
5298
5299 if (cap->cmdchar == 'g')
5300 c = cap->extra_char;
5301 else
5302 c = cap->nchar;
5303 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005304 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
5306 if (cap->arg)
5307 {
5308 check_cursor_lnum();
5309 beginline(BL_WHITE | BL_FIX);
5310 }
5311 else
5312 check_cursor();
5313 }
5314 else
5315 nv_cursormark(cap, cap->arg, pos);
5316
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005317 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (!virtual_active())
5319 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005320 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#ifdef FEAT_FOLDING
5322 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005323 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005324 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 && (fdo_flags & FDO_MARK)
5326 && old_KeyTyped)
5327 foldOpenCursor();
5328#endif
5329}
5330
5331/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005332 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 */
5334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005335nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005338#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 if (!checkclearopq(cap->oap))
5344 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005345 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5346 {
5347 if (goto_tabpage_lastused() == FAIL)
5348 clearopbeep(cap->oap);
5349 return;
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 if (cap->cmdchar == 'g')
5352 pos = movechangelist((int)cap->count1);
5353 else
5354 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005355 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 curwin->w_set_curswant = TRUE;
5358 check_cursor();
5359 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005360 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 nv_cursormark(cap, FALSE, pos);
5362 else if (cap->cmdchar == 'g')
5363 {
5364 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005365 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005367 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005369 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 else
5372 clearopbeep(cap->oap);
5373# ifdef FEAT_FOLDING
5374 if (cap->oap->op_type == OP_NOP
5375 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5376 && (fdo_flags & FDO_MARK)
5377 && old_KeyTyped)
5378 foldOpenCursor();
5379# endif
5380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381}
5382
5383/*
5384 * Handle '"' command.
5385 */
5386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 if (checkclearop(cap->oap))
5390 return;
5391#ifdef FEAT_EVAL
5392 if (cap->nchar == '=')
5393 cap->nchar = get_expr_register();
5394#endif
5395 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5396 {
5397 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005398 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#ifdef FEAT_EVAL
5400 set_reg_var(cap->oap->regname);
5401#endif
5402 }
5403 else
5404 clearopbeep(cap->oap);
5405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Handle "v", "V" and "CTRL-V" commands.
5409 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5410 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005411 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 */
5413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005414nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005416 if (cap->cmdchar == Ctrl_Q)
5417 cap->cmdchar = Ctrl_V;
5418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5420 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (cap->oap->op_type != OP_NOP)
5422 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005423 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005424 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 return;
5426 }
5427
5428 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005431 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 else // toggle char/block mode
5434 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 VIsual_mode = cap->cmdchar;
5436 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005437 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005439 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005444 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 VIsual = curwin->w_cursor;
5448
5449 VIsual_active = TRUE;
5450 VIsual_reselect = TRUE;
5451 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005455 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005457 // For V and ^V, we multiply the number of lines even if there
5458 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5460 {
5461 curwin->w_cursor.lnum +=
5462 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005463 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 VIsual_mode = resel_VIsual_mode;
5466 if (VIsual_mode == 'v')
5467 {
5468 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005469 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005470 update_curswant_force();
5471 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005474 curwin->w_curswant = resel_VIsual_vcol;
5475 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005477 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {
5479 curwin->w_curswant = MAXCOL;
5480 coladvance((colnr_T)MAXCOL);
5481 }
5482 else if (VIsual_mode == Ctrl_V)
5483 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005484 update_curswant_force();
5485 curwin->w_curswant += + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 coladvance(curwin->w_curswant);
5487 }
5488 else
5489 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005490 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 else
5493 {
5494 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 may_start_select('c');
5497 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005498 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005499 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005500 if (cap->count0 > 0 && --cap->count1 > 0)
5501 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005502 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005503 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5504 nv_right(cap);
5505 else if (VIsual_mode == 'V')
5506 nv_down(cap);
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 }
5510}
5511
5512/*
5513 * Start selection for Shift-movement keys.
5514 */
5515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005516start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005518 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 may_start_select('k');
5520 n_start_visual_mode('v');
5521}
5522
5523/*
5524 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005525 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 */
5527 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005528may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005530 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5531 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532}
5533
5534/*
5535 * Start Visual mode "c".
5536 * Should set VIsual_select before calling this.
5537 */
5538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005541#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005542 int cursor_line_was_concealed = curwin->w_p_cole > 0
5543 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005544#endif
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 VIsual_mode = c;
5547 VIsual_active = TRUE;
5548 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005549
5550 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005551 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005552 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005553 {
5554 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 VIsual = curwin->w_cursor;
5558
5559#ifdef FEAT_FOLDING
5560 foldAdjustVisual();
5561#endif
5562
LemonBoy2bf52dd2022-04-09 18:17:34 +01005563 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005565#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005566 // Check if redraw is needed after changing the state.
5567 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005568#endif
5569
Bram Moolenaar09df3122006-01-23 22:23:09 +00005570 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005571 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 // Make sure the clipboard gets updated. Needed because start and
5574 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 clip_star.vmode = NUL;
5576#endif
5577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 // Only need to redraw this line, unless still need to redraw an old
5579 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005580 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5583 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5584 }
5585}
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588/*
5589 * CTRL-W: Window commands
5590 */
5591 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005592nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005594 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005595 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005597 cap->cmdchar = ':';
5598 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005599 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005600 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005601 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005602 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605/*
5606 * CTRL-Z: Suspend
5607 */
5608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005609nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
5611 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005614 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005618 * "gv": Reselect the previous Visual area. If Visual already active,
5619 * exchange previous and current Visual area.
5620 */
5621 static void
5622nv_gv_cmd(cmdarg_T *cap)
5623{
5624 pos_T tpos;
5625 int i;
5626
5627 if (checkclearop(cap->oap))
5628 return;
5629
5630 if (curbuf->b_visual.vi_start.lnum == 0
5631 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5632 || curbuf->b_visual.vi_end.lnum == 0)
5633 {
5634 beep_flush();
5635 return;
5636 }
5637
5638 // set w_cursor to the start of the Visual area, tpos to the end
5639 if (VIsual_active)
5640 {
5641 i = VIsual_mode;
5642 VIsual_mode = curbuf->b_visual.vi_mode;
5643 curbuf->b_visual.vi_mode = i;
5644# ifdef FEAT_EVAL
5645 curbuf->b_visual_mode_eval = i;
5646# endif
5647 i = curwin->w_curswant;
5648 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5649 curbuf->b_visual.vi_curswant = i;
5650
5651 tpos = curbuf->b_visual.vi_end;
5652 curbuf->b_visual.vi_end = curwin->w_cursor;
5653 curwin->w_cursor = curbuf->b_visual.vi_start;
5654 curbuf->b_visual.vi_start = VIsual;
5655 }
5656 else
5657 {
5658 VIsual_mode = curbuf->b_visual.vi_mode;
5659 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5660 tpos = curbuf->b_visual.vi_end;
5661 curwin->w_cursor = curbuf->b_visual.vi_start;
5662 }
5663
5664 VIsual_active = TRUE;
5665 VIsual_reselect = TRUE;
5666
5667 // Set Visual to the start and w_cursor to the end of the Visual
5668 // area. Make sure they are on an existing character.
5669 check_cursor();
5670 VIsual = curwin->w_cursor;
5671 curwin->w_cursor = tpos;
5672 check_cursor();
5673 update_topline();
5674
5675 // When called from normal "g" command: start Select mode when
5676 // 'selectmode' contains "cmd". When called for K_SELECT, always
5677 // start Select mode.
5678 if (cap->arg)
5679 {
5680 VIsual_select = TRUE;
5681 VIsual_select_reg = 0;
5682 }
5683 else
5684 may_start_select('c');
5685 setmouse();
5686#ifdef FEAT_CLIPBOARD
5687 // Make sure the clipboard gets updated. Needed because start and
5688 // end are still the same, and the selection needs to be owned
5689 clip_star.vmode = NUL;
5690#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005691 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005692 showmode();
5693}
5694
5695/*
5696 * "g0", "g^" : Like "0" and "^" but for screen lines.
5697 * "gm": middle of "g0" and "g$".
5698 */
5699 static void
5700nv_g_home_m_cmd(cmdarg_T *cap)
5701{
5702 int i;
5703 int flag = FALSE;
5704
5705 if (cap->nchar == '^')
5706 flag = TRUE;
5707
5708 cap->oap->motion_type = MCHAR;
5709 cap->oap->inclusive = FALSE;
5710 if (curwin->w_p_wrap && curwin->w_width != 0)
5711 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005712 int width1 = curwin->w_width - curwin_col_off();
5713 int width2 = width1 + curwin_col_off2();
5714 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005715
5716 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005717 virtcol = curwin->w_virtcol
5718#ifdef FEAT_PROP_POPUP
5719 - curwin->w_virtcol_first_char
5720#endif
5721 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005722 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005723 if (virtcol >= (colnr_T)width1 && width2 > 0)
5724 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005725 }
5726 else
5727 i = curwin->w_leftcol;
5728 // Go to the middle of the screen line. When 'number' or
5729 // 'relativenumber' is on and lines are wrapping the middle can be more
5730 // to the left.
5731 if (cap->nchar == 'm')
5732 i += (curwin->w_width - curwin_col_off()
5733 + ((curwin->w_p_wrap && i > 0)
5734 ? curwin_col_off2() : 0)) / 2;
5735 coladvance((colnr_T)i);
5736 if (flag)
5737 {
5738 do
5739 i = gchar_cursor();
5740 while (VIM_ISWHITE(i) && oneright() == OK);
5741 curwin->w_valid &= ~VALID_WCOL;
5742 }
5743 curwin->w_set_curswant = TRUE;
5744}
5745
5746/*
5747 * "g_": to the last non-blank character in the line or <count> lines
5748 * downward.
5749 */
5750 static void
5751nv_g_underscore_cmd(cmdarg_T *cap)
5752{
5753 char_u *ptr;
5754
5755 cap->oap->motion_type = MCHAR;
5756 cap->oap->inclusive = TRUE;
5757 curwin->w_curswant = MAXCOL;
5758 if (cursor_down((long)(cap->count1 - 1),
5759 cap->oap->op_type == OP_NOP) == FAIL)
5760 {
5761 clearopbeep(cap->oap);
5762 return;
5763 }
5764
5765 ptr = ml_get_curline();
5766
5767 // In Visual mode we may end up after the line.
5768 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5769 --curwin->w_cursor.col;
5770
5771 // Decrease the cursor column until it's on a non-blank.
5772 while (curwin->w_cursor.col > 0
5773 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5774 --curwin->w_cursor.col;
5775 curwin->w_set_curswant = TRUE;
5776 adjust_for_sel(cap);
5777}
5778
5779/*
5780 * "g$" : Like "$" but for screen lines.
5781 */
5782 static void
5783nv_g_dollar_cmd(cmdarg_T *cap)
5784{
5785 oparg_T *oap = cap->oap;
5786 int i;
5787 int col_off = curwin_col_off();
5788
5789 oap->motion_type = MCHAR;
5790 oap->inclusive = TRUE;
5791 if (curwin->w_p_wrap && curwin->w_width != 0)
5792 {
5793 curwin->w_curswant = MAXCOL; // so we stay at the end
5794 if (cap->count1 == 1)
5795 {
5796 int width1 = curwin->w_width - col_off;
5797 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005798 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005799
5800 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005801 virtcol = curwin->w_virtcol
5802#ifdef FEAT_PROP_POPUP
5803 - curwin->w_virtcol_first_char
5804#endif
5805 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005806 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005807 if (virtcol >= (colnr_T)width1)
5808 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005809 * width2;
5810 coladvance((colnr_T)i);
5811
5812 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005813 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005814 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5815 {
5816 // Check for landing on a character that got split at
5817 // the end of the line. We do not want to advance to
5818 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005819 if (curwin->w_virtcol
5820#ifdef FEAT_PROP_POPUP
5821 - curwin->w_virtcol_first_char
5822#endif
5823 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005824 --curwin->w_cursor.col;
5825 }
5826 }
5827 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5828 clearopbeep(oap);
5829 }
5830 else
5831 {
5832 if (cap->count1 > 1)
5833 // if it fails, let the cursor still move to the last char
5834 (void)cursor_down(cap->count1 - 1, FALSE);
5835
5836 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5837 coladvance((colnr_T)i);
5838
5839 // if the character doesn't fit move one back
5840 if (curwin->w_cursor.col > 0
5841 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5842 {
5843 colnr_T vcol;
5844
5845 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5846 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5847 --curwin->w_cursor.col;
5848 }
5849
5850 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005851 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005852 }
5853}
5854
5855/*
5856 * "gi": start Insert at the last position.
5857 */
5858 static void
5859nv_gi_cmd(cmdarg_T *cap)
5860{
5861 int i;
5862
5863 if (curbuf->b_last_insert.lnum != 0)
5864 {
5865 curwin->w_cursor = curbuf->b_last_insert;
5866 check_cursor_lnum();
5867 i = (int)STRLEN(ml_get_curline());
5868 if (curwin->w_cursor.col > (colnr_T)i)
5869 {
5870 if (virtual_active())
5871 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5872 curwin->w_cursor.col = i;
5873 }
5874 }
5875 cap->cmdchar = 'i';
5876 nv_edit(cap);
5877}
5878
5879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 * Commands starting with "g".
5881 */
5882 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005883nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
5885 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
5888 switch (cap->nchar)
5889 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005890 case Ctrl_A:
5891 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005893 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005894 if (!VIsual_active && cap->nchar == Ctrl_A)
5895 vim_mem_profile_dump();
5896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005898 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005899 if (VIsual_active)
5900 {
5901 cap->arg = TRUE;
5902 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005903 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005904 nv_addsub(cap);
5905 }
5906 else
5907 clearopbeep(oap);
5908 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005910 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 case 'R':
5912 cap->arg = TRUE;
5913 nv_Replace(cap);
5914 break;
5915
5916 case 'r':
5917 nv_vreplace(cap);
5918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 case '&':
5921 do_cmdline_cmd((char_u *)"%s//~/&");
5922 break;
5923
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005924 // "gv": Reselect the previous Visual area. If Visual already active,
5925 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005927 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005929
5930 // "gV": Don't reselect the previous Visual area after a Select mode
5931 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 case 'V':
5933 VIsual_reselect = FALSE;
5934 break;
5935
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005936 // "gh": start Select mode.
5937 // "gH": start Select line mode.
5938 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case K_BS:
5940 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005941 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case 'h':
5943 case 'H':
5944 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 cap->cmdchar = cap->nchar + ('v' - 'h');
5946 cap->arg = TRUE;
5947 nv_visual(cap);
5948 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005950 // "gn", "gN" visually select next/previous search match
5951 // "gn" selects next match
5952 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005953 case 'N':
5954 case 'n':
5955 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005956 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005957 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005959 // "gj" and "gk" two new funny movement keys -- up and down
5960 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 case 'j':
5962 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005963 // with 'nowrap' it works just like the normal "j" command.
5964 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {
5966 oap->motion_type = MLINE;
5967 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5968 }
5969 else
5970 i = nv_screengo(oap, FORWARD, cap->count1);
5971 if (i == FAIL)
5972 clearopbeep(oap);
5973 break;
5974
5975 case 'k':
5976 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005977 // with 'nowrap' it works just like the normal "k" command.
5978 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 oap->motion_type = MLINE;
5981 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5982 }
5983 else
5984 i = nv_screengo(oap, BACKWARD, cap->count1);
5985 if (i == FAIL)
5986 clearopbeep(oap);
5987 break;
5988
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005989 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 case 'J':
5991 nv_join(cap);
5992 break;
5993
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005994 // "g0", "g^" : Like "0" and "^" but for screen lines.
5995 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 case '0':
5998 case 'm':
5999 case K_HOME:
6000 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006001 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 break;
6003
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006004 case 'M':
6005 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006006 oap->motion_type = MCHAR;
6007 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006008 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006009 if (cap->count0 > 0 && cap->count0 <= 100)
6010 coladvance((colnr_T)(i * cap->count0 / 100));
6011 else
6012 coladvance((colnr_T)(i / 2));
6013 curwin->w_set_curswant = TRUE;
6014 }
6015 break;
6016
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006017 // "g_": to the last non-blank character in the line or <count> lines
6018 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006020 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 break;
6022
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006023 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 case '$':
6025 case K_END:
6026 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006027 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 case '*':
6032 case '#':
6033#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006034 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006036 case Ctrl_RSB: // :tag or :tselect for current identifier
6037 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 nv_ident(cap);
6039 break;
6040
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006041 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 case 'e':
6043 case 'E':
6044 oap->motion_type = MCHAR;
6045 curwin->w_set_curswant = TRUE;
6046 oap->inclusive = TRUE;
6047 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6048 clearopbeep(oap);
6049 break;
6050
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006051 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006053 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 break;
6055
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006056 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 break;
6060
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006061 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 case 'I':
6063 beginline(0);
6064 if (!checkclearopq(oap))
6065 invoke_edit(cap, FALSE, 'g', FALSE);
6066 break;
6067
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006068 // "gf": goto file, edit file under cursor
6069 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006071 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 nv_gotofile(cap);
6073 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 case '\'':
6077 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006078 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 case '`':
6080 nv_gomark(cap);
6081 break;
6082
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006085 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 break;
6087
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006088 // "ga": Display the ascii value of the character under the
6089 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 case 'a':
6091 do_ascii(NULL);
6092 break;
6093
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006094 // "g8": Display the bytes used for the UTF-8 character under the
6095 // cursor. It is displayed in hex.
6096 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006098 if (cap->count0 == 8)
6099 utf_find_illegal();
6100 else
6101 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006104 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006105 case '<':
6106 show_sb_text();
6107 break;
6108
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006109 // "gg": Goto the first line in file. With a count it goes to
6110 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 case 'g':
6112 cap->arg = FALSE;
6113 nv_goto(cap);
6114 break;
6115
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006116 // Two-character operators:
6117 // "gq" Format text
6118 // "gw" Format text and keep cursor position
6119 // "g~" Toggle the case of the text.
6120 // "gu" Change text to lower case.
6121 // "gU" Change text to upper case.
6122 // "g?" rot13 encoding
6123 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 case 'q':
6125 case 'w':
6126 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006127 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 case '~':
6129 case 'u':
6130 case 'U':
6131 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006132 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 nv_operator(cap);
6134 break;
6135
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006136 // "gd": Find first occurrence of pattern under the cursor in the
6137 // current function
6138 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 case 'd':
6140 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006141 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 break;
6143
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006144 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 case K_MIDDLEMOUSE:
6146 case K_MIDDLEDRAG:
6147 case K_MIDDLERELEASE:
6148 case K_LEFTMOUSE:
6149 case K_LEFTDRAG:
6150 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006151 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 case K_RIGHTMOUSE:
6153 case K_RIGHTDRAG:
6154 case K_RIGHTRELEASE:
6155 case K_X1MOUSE:
6156 case K_X1DRAG:
6157 case K_X1RELEASE:
6158 case K_X2MOUSE:
6159 case K_X2DRAG:
6160 case K_X2RELEASE:
6161 mod_mask = MOD_MASK_CTRL;
6162 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165 case K_IGNORE:
6166 break;
6167
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006168 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 case 'p':
6170 case 'P':
6171 nv_put(cap);
6172 break;
6173
6174#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006175 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 case 'o':
6177 goto_byte(cap->count0);
6178 break;
6179#endif
6180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006181 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006183 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 do_exmode(TRUE);
6185 break;
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 case ',':
6188 nv_pcmark(cap);
6189 break;
6190
6191 case ';':
6192 cap->count1 = -cap->count1;
6193 nv_pcmark(cap);
6194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006196 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006197 if (!checkclearop(oap))
6198 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006200 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006201 if (!checkclearop(oap))
6202 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006203 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204
Bram Moolenaar62a23252020-08-09 14:04:42 +02006205 case TAB:
6206 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6207 clearopbeep(oap);
6208 break;
6209
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006210 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006211 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006212 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006213 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006214 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006215 break;
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 default:
6218 clearopbeep(oap);
6219 break;
6220 }
6221}
6222
6223/*
6224 * Handle "o" and "O" commands.
6225 */
6226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006227n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006229#ifdef FEAT_CONCEAL
6230 linenr_T oldline = curwin->w_cursor.lnum;
6231#endif
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (!checkclearopq(cap->oap))
6234 {
6235#ifdef FEAT_FOLDING
6236 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 (void)hasFolding(curwin->w_cursor.lnum,
6239 &curwin->w_cursor.lnum, NULL);
6240 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006241 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 (void)hasFolding(curwin->w_cursor.lnum,
6243 NULL, &curwin->w_cursor.lnum);
6244#endif
6245 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6246 (cap->cmdchar == 'O' ? 1 : 0)),
6247 (linenr_T)(curwin->w_cursor.lnum +
6248 (cap->cmdchar == 'o' ? 1 : 0))
6249 ) == OK
6250 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006251 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006252 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006254#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006255 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006256 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006257#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006258#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006259 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006260 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006261 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006262#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006264 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6265 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6267 }
6268 }
6269}
6270
6271/*
6272 * "." command: redo last change.
6273 */
6274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006275nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 if (!checkclearopq(cap->oap))
6278 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006279 // If "restart_edit" is TRUE, the last but one command is repeated
6280 // instead of the last command (inserting text). This is used for
6281 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6283 clearopbeep(cap->oap);
6284 }
6285}
6286
6287/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006288 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 */
6290 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006291nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006293 if (VIsual_select && VIsual_active)
6294 {
6295 int reg;
6296 // Get register name
6297 ++no_mapping;
6298 ++allow_keys;
6299 reg = plain_vgetc();
6300 LANGMAP_ADJUST(reg, TRUE);
6301 --no_mapping;
6302 --allow_keys;
6303
6304 if (reg == '"')
6305 // the unnamed register is 0
6306 reg = 0;
6307
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006308 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006309 return;
6310 }
6311
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (!checkclearopq(cap->oap))
6313 {
6314 u_redo((int)cap->count1);
6315 curwin->w_set_curswant = TRUE;
6316 }
6317}
6318
6319/*
6320 * Handle "U" command.
6321 */
6322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006323nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006325 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006326 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006328 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 cap->cmdchar = 'g';
6330 cap->nchar = 'U';
6331 nv_operator(cap);
6332 }
6333 else if (!checkclearopq(cap->oap))
6334 {
6335 u_undoline();
6336 curwin->w_set_curswant = TRUE;
6337 }
6338}
6339
6340/*
6341 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6342 * single character.
6343 */
6344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006345nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006347 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006348 {
6349#ifdef FEAT_JOB_CHANNEL
6350 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6351 {
6352 clearopbeep(cap->oap);
6353 return;
6354 }
6355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 else
6359 nv_operator(cap);
6360}
6361
6362/*
6363 * Handle an operator command.
6364 * The actual work is done by do_pending_operator().
6365 */
6366 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006367nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 int op_type;
6370
6371 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006372#ifdef FEAT_JOB_CHANNEL
6373 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6374 {
6375 clearopbeep(cap->oap);
6376 return;
6377 }
6378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006380 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 nv_lineop(cap);
6382 else if (!checkclearop(cap->oap))
6383 {
6384 cap->oap->start = curwin->w_cursor;
6385 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006386#ifdef FEAT_EVAL
6387 set_op_var(op_type);
6388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 }
6390}
6391
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006392#ifdef FEAT_EVAL
6393/*
6394 * Set v:operator to the characters for "optype".
6395 */
6396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006397set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006398{
6399 char_u opchars[3];
6400
6401 if (optype == OP_NOP)
6402 set_vim_var_string(VV_OP, NULL, 0);
6403 else
6404 {
6405 opchars[0] = get_op_char(optype);
6406 opchars[1] = get_extra_op_char(optype);
6407 opchars[2] = NUL;
6408 set_vim_var_string(VV_OP, opchars, -1);
6409 }
6410}
6411#endif
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413/*
6414 * Handle linewise operator "dd", "yy", etc.
6415 *
6416 * "_" is is a strange motion command that helps make operators more logical.
6417 * It is actually implemented, but not documented in the real Vi. This motion
6418 * command actually refers to "the current line". Commands like "dd" and "yy"
6419 * are really an alternate form of "d_" and "y_". It does accept a count, so
6420 * "d3_" works to delete 3 lines.
6421 */
6422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 cap->oap->motion_type = MLINE;
6426 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6427 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006428 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006429 && cap->oap->motion_force != 'v'
6430 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 || cap->oap->op_type == OP_LSHIFT
6432 || cap->oap->op_type == OP_RSHIFT)
6433 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006434 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 beginline(BL_WHITE | BL_FIX);
6436}
6437
6438/*
6439 * <Home> command.
6440 */
6441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006442nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006444 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006445 if (mod_mask & MOD_MASK_CTRL)
6446 nv_goto(cap);
6447 else
6448 {
6449 cap->count0 = 1;
6450 nv_pipe(cap);
6451 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006452 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6453 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454}
6455
6456/*
6457 * "|" command.
6458 */
6459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006460nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
6462 cap->oap->motion_type = MCHAR;
6463 cap->oap->inclusive = FALSE;
6464 beginline(0);
6465 if (cap->count0 > 0)
6466 {
6467 coladvance((colnr_T)(cap->count0 - 1));
6468 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6469 }
6470 else
6471 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006472 // keep curswant at the column where we wanted to go, not where
6473 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 curwin->w_set_curswant = FALSE;
6475}
6476
6477/*
6478 * Handle back-word command "b" and "B".
6479 * cap->arg is 1 for "B"
6480 */
6481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006482nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 cap->oap->motion_type = MCHAR;
6485 cap->oap->inclusive = FALSE;
6486 curwin->w_set_curswant = TRUE;
6487 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6488 clearopbeep(cap->oap);
6489#ifdef FEAT_FOLDING
6490 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6491 foldOpenCursor();
6492#endif
6493}
6494
6495/*
6496 * Handle word motion commands "e", "E", "w" and "W".
6497 * cap->arg is TRUE for "E" and "W".
6498 */
6499 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006500nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 int n;
6503 int word_end;
6504 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006505 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006507 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6509 word_end = TRUE;
6510 else
6511 word_end = FALSE;
6512 cap->oap->inclusive = word_end;
6513
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006514 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (!word_end && cap->oap->op_type == OP_CHANGE)
6516 {
6517 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006518 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006520 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006522 // Reproduce a funny Vi behaviour: "cw" on a blank only
6523 // changes one character, not all blanks until the start of
6524 // the next word. Only do this when the 'w' flag is included
6525 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6527 {
6528 cap->oap->inclusive = TRUE;
6529 cap->oap->motion_type = MCHAR;
6530 return;
6531 }
6532 }
6533 else
6534 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006535 // This is a little strange. To match what the real Vi does,
6536 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6537 // that we are not on a space or a TAB. This seems impolite
6538 // at first, but it's really more what we mean when we say
6539 // 'cw'.
6540 // Another strangeness: When standing on the end of a word
6541 // "ce" will change until the end of the next word, but "cw"
6542 // will change only one character! This is done by setting
6543 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 cap->oap->inclusive = TRUE;
6545 word_end = TRUE;
6546 flag = TRUE;
6547 }
6548 }
6549 }
6550
6551 cap->oap->motion_type = MCHAR;
6552 curwin->w_set_curswant = TRUE;
6553 if (word_end)
6554 n = end_word(cap->count1, cap->arg, flag, FALSE);
6555 else
6556 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6557
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006558 // Don't leave the cursor on the NUL past the end of line. Unless we
6559 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006560 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006561 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
6563 if (n == FAIL && cap->oap->op_type == OP_NOP)
6564 clearopbeep(cap->oap);
6565 else
6566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568#ifdef FEAT_FOLDING
6569 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6570 foldOpenCursor();
6571#endif
6572 }
6573}
6574
6575/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006576 * Used after a movement command: If the cursor ends up on the NUL after the
6577 * end of the line, may move it back to the last character and make the motion
6578 * inclusive.
6579 */
6580 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006581adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006582{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006583 // The cursor cannot remain on the NUL when:
6584 // - the column is > 0
6585 // - not in Visual mode or 'selection' is "o"
6586 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006587 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006588 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006589 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006590 {
6591 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006592 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006593 if (has_mbyte)
6594 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006595 oap->inclusive = TRUE;
6596 }
6597}
6598
6599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 * "0" and "^" commands.
6601 * cap->arg is the argument for beginline().
6602 */
6603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006604nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 cap->oap->motion_type = MCHAR;
6607 cap->oap->inclusive = FALSE;
6608 beginline(cap->arg);
6609#ifdef FEAT_FOLDING
6610 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6611 foldOpenCursor();
6612#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6614 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615}
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617/*
6618 * In exclusive Visual mode, may include the last character.
6619 */
6620 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006621adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006624 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (has_mbyte)
6627 inc_cursor();
6628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 ++curwin->w_cursor.col;
6630 cap->oap->inclusive = FALSE;
6631 }
6632}
6633
6634/*
6635 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6636 * Should check VIsual_mode before calling this.
6637 * Returns TRUE when backed up to the previous line.
6638 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006640unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 pos_T *pp;
6643
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006644 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006646 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 pp = &curwin->w_cursor;
6648 else
6649 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (pp->coladd > 0)
6651 --pp->coladd;
6652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (pp->col > 0)
6654 {
6655 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006656 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 }
6658 else if (pp->lnum > 1)
6659 {
6660 --pp->lnum;
6661 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6662 return TRUE;
6663 }
6664 }
6665 return FALSE;
6666}
6667
6668/*
6669 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6670 */
6671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006672nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006677 VIsual_select_reg = 0;
6678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 else if (VIsual_reselect)
6680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006681 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 cap->arg = TRUE;
6683 nv_g_cmd(cap);
6684 }
6685}
6686
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688/*
6689 * "G", "gg", CTRL-END, CTRL-HOME.
6690 * cap->arg is TRUE for "G".
6691 */
6692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006693nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 linenr_T lnum;
6696
6697 if (cap->arg)
6698 lnum = curbuf->b_ml.ml_line_count;
6699 else
6700 lnum = 1L;
6701 cap->oap->motion_type = MLINE;
6702 setpcmark();
6703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (cap->count0 != 0)
6706 lnum = cap->count0;
6707 if (lnum < 1L)
6708 lnum = 1L;
6709 else if (lnum > curbuf->b_ml.ml_line_count)
6710 lnum = curbuf->b_ml.ml_line_count;
6711 curwin->w_cursor.lnum = lnum;
6712 beginline(BL_SOL | BL_FIX);
6713#ifdef FEAT_FOLDING
6714 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6715 foldOpenCursor();
6716#endif
6717}
6718
6719/*
6720 * CTRL-\ in Normal mode.
6721 */
6722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006723nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6726 {
6727 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006728 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006729 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (cmdwin_type != 0)
6732 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (VIsual_active)
6734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006735 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006736 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006738 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (cap->nchar == Ctrl_G && p_im)
6740 restart_edit = 'a';
6741 }
6742 else
6743 clearopbeep(cap->oap);
6744}
6745
6746/*
6747 * ESC in Normal mode: beep, but don't flush buffers.
6748 * Don't even beep if we are canceling a command.
6749 */
6750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int no_reason;
6754
6755 no_reason = (cap->oap->op_type == OP_NOP
6756 && cap->opcount == 0
6757 && cap->count0 == 0
6758 && cap->oap->regname == 0
6759 && !p_im);
6760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006761 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006763 if (restart_edit == 0 && cmdwin_type == 0
6764 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006765 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006766 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6767
Bram Moolenaar5939c352022-09-24 12:50:45 +01006768 // The user may accidentally do "vim file | grep word" and then
6769 // CTRL-C doesn't show anything. With a changed buffer give the
6770 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006771 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006772 {
6773 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6774
6775 if (out_redir)
6776 mch_errmsg(ms);
6777 else
6778 msg(ms);
6779 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006780 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006781 {
6782 if (out_redir)
6783 {
6784 got_int = FALSE;
6785 do_cmdline_cmd((char_u *)"qa");
6786 }
6787 else
6788 msg(_("Type :qa and press <Enter> to exit Vim"));
6789 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006792 if (restart_edit != 0)
6793 redraw_mode = TRUE; // remove "-- (insert) --"
6794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006795 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6796 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (!p_im)
6798 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (cmdwin_type != 0)
6800 {
6801 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 return;
6804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006806 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006807 {
6808 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006809 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6810 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006811 cmdwin_result = K_IGNORE;
6812 return;
6813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (VIsual_active)
6816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006817 end_visual_mode(); // stop Visual
6818 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006820 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006822 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006823 {
6824#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006825 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006826 popup_hide_message_win();
6827 else
6828#endif
6829 vim_beep(BO_ESC);
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 clearop(cap->oap);
6832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006833 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6834 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006835 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 restart_edit = 'a';
6837}
6838
6839/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006840 * Move the cursor for the "A" command.
6841 */
6842 void
6843set_cursor_for_append_to_line(void)
6844{
6845 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006846 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006847 {
6848 int save_State = State;
6849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006850 // Pretend Insert mode here to allow the cursor on the
6851 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006852 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006853 coladvance((colnr_T)MAXCOL);
6854 State = save_State;
6855 }
6856 else
6857 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6858}
6859
6860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006862 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 */
6864 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006865nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006867 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6869 cap->cmdchar = 'i';
6870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006873 {
6874#ifdef FEAT_TERMINAL
6875 if (term_in_normal_mode())
6876 {
6877 end_visual_mode();
6878 clearop(cap->oap);
6879 term_enter_job_mode();
6880 return;
6881 }
6882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006886 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006887 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6888 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006892#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006893 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006894 {
6895 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006896 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006897 return;
6898 }
6899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 else if (!curbuf->b_p_ma && !p_im)
6901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006902 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006903 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006905 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006906 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006907 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006909 else if (cap->cmdchar == K_PS && VIsual_active)
6910 {
6911 pos_T old_pos = curwin->w_cursor;
6912 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006913 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006915 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006916 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6917 {
6918 shift_delete_registers();
6919 cap->oap->regname = '1';
6920 }
6921 else
6922 cap->oap->regname = '-';
6923 cap->cmdchar = 'd';
6924 cap->nchar = NUL;
6925 nv_operator(cap);
6926 do_pending_operator(cap, 0, FALSE);
6927 cap->cmdchar = K_PS;
6928
zeertzjq7a732522022-03-14 20:46:41 +00006929 if (*ml_get_cursor() != NUL)
6930 {
6931 if (old_visual_mode == 'V')
6932 {
6933 // In linewise Visual mode insert before the beginning of the
6934 // next line.
6935 // When the last line in the buffer was deleted then create a
6936 // new line, otherwise there is not need to move cursor.
6937 // Detect this by checking if cursor moved above Visual area.
6938 if (curwin->w_cursor.lnum < old_pos.lnum
6939 && curwin->w_cursor.lnum < old_visual.lnum)
6940 {
6941 if (u_save_cursor() == OK)
6942 {
6943 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6944 FALSE);
6945 appended_lines(curwin->w_cursor.lnum++, 1L);
6946 }
6947 }
6948 }
6949 // When the last char in the line was deleted then append.
6950 // Detect this by checking if cursor moved before Visual area.
6951 else if (curwin->w_cursor.col < old_pos.col
6952 && curwin->w_cursor.col < old_visual.col)
6953 inc_cursor();
6954 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006956 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006957 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 else if (!checkclearopq(cap->oap))
6960 {
6961 switch (cap->cmdchar)
6962 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006963 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006964 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 break;
6966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006968 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6969 beginline(BL_WHITE);
6970 else
6971 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 break;
6973
Bram Moolenaara1891842017-02-04 21:34:31 +01006974 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 // Bracketed paste works like "a"ppend, unless the cursor is in
6976 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006977 if (curwin->w_cursor.col == 0)
6978 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006981 case 'a': // "a"ppend is like "i"nsert on the next character.
6982 // increment coladd when in virtual space, increment the
6983 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (virtual_active()
6985 && (curwin->w_cursor.coladd > 0
6986 || *ml_get_cursor() == NUL
6987 || *ml_get_cursor() == TAB))
6988 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006989 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 inc_cursor();
6991 break;
6992 }
6993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6995 {
6996 int save_State = State;
6997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006998 // Pretend Insert mode here to allow the cursor on the
6999 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007000 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 coladvance(getviscol());
7002 State = save_State;
7003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7006 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007007 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007008 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007009 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010}
7011
7012/*
7013 * Invoke edit() and take care of "restart_edit" and the return value.
7014 */
7015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007016invoke_edit(
7017 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007018 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007019 int cmd,
7020 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 int restart_edit_save = 0;
7023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7025 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7026 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (repl || !stuff_empty())
7028 restart_edit_save = restart_edit;
7029 else
7030 restart_edit_save = 0;
7031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007032 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 restart_edit = 0;
7034
7035 if (edit(cmd, startln, cap->count1))
7036 cap->retval |= CA_COMMAND_BUSY;
7037
7038 if (restart_edit == 0)
7039 restart_edit = restart_edit_save;
7040}
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042/*
7043 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7044 */
7045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007046nv_object(
7047 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 int flag;
7050 int include;
7051 char_u *mps_save;
7052
7053 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 mps_save = curbuf->b_p_mps;
7060 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7061
7062 switch (cap->nchar)
7063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 flag = current_word(cap->oap, cap->count1, include, FALSE);
7066 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 flag = current_word(cap->oap, cap->count1, include, TRUE);
7069 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 case '(':
7072 case ')':
7073 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7074 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 case '{':
7077 case '}':
7078 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7079 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007080 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 case ']':
7082 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7083 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 case '>':
7086 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7087 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007088#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 case 't': // "at" = a tag block (xml and html)
7090 // Do not adjust oap->end in do_pending_operator()
7091 // otherwise there are different results for 'dit'
7092 // (note leading whitespace in last line):
7093 // 1) <b> 2) <b>
7094 // foobar foobar
7095 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007096 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007097 flag = current_tagblock(cap->oap, cap->count1, include);
7098 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007099#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 flag = current_par(cap->oap, cap->count1, include, 'p');
7102 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 flag = current_sent(cap->oap, cap->count1, include);
7105 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 case '"': // "a"" = a double quoted string
7107 case '\'': // "a'" = a single quoted string
7108 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007109 flag = current_quote(cap->oap, cap->count1, include,
7110 cap->nchar);
7111 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112#if 0 // TODO
7113 case 'S': // "aS" = a section
7114 case 'f': // "af" = a filename
7115 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#endif
7117 default:
7118 flag = FAIL;
7119 break;
7120 }
7121
7122 curbuf->b_p_mps = mps_save;
7123 if (flag == FAIL)
7124 clearopbeep(cap->oap);
7125 adjust_cursor_col();
7126 curwin->w_set_curswant = TRUE;
7127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129/*
7130 * "q" command: Start/stop recording.
7131 * "q:", "q/", "q?": edit command-line in command-line window.
7132 */
7133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007134nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 if (cap->oap->op_type == OP_FORMAT)
7137 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 cap->cmdchar = 'g';
7140 cap->nchar = 'q';
7141 nv_operator(cap);
7142 }
7143 else if (!checkclearop(cap->oap))
7144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7146 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007147 if (cmdwin_type != 0)
7148 {
7149 emsg(_(e_cmdline_window_already_open));
7150 return;
7151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 stuffcharReadbuff(cap->nchar);
7153 stuffcharReadbuff(K_CMDWIN);
7154 }
7155 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007156 // (stop) recording into a named register, unless executing a
7157 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007158 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 clearopbeep(cap->oap);
7160 }
7161}
7162
7163/*
7164 * Handle the "@r" command.
7165 */
7166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007167nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 if (checkclearop(cap->oap))
7170 return;
7171#ifdef FEAT_EVAL
7172 if (cap->nchar == '=')
7173 {
7174 if (get_expr_register() == NUL)
7175 return;
7176 }
7177#endif
7178 while (cap->count1-- && !got_int)
7179 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007180 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
7182 clearopbeep(cap->oap);
7183 break;
7184 }
7185 line_breakcheck();
7186 }
7187}
7188
7189/*
7190 * Handle the CTRL-U and CTRL-D commands.
7191 */
7192 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007193nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7196 || (cap->cmdchar == Ctrl_D
7197 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7198 clearopbeep(cap->oap);
7199 else if (!checkclearop(cap->oap))
7200 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7201}
7202
7203/*
7204 * Handle "J" or "gJ" command.
7205 */
7206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007207nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007209 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007211 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
7213 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007214 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7216 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007219 if (cap->count0 <= 2)
7220 {
7221 clearopbeep(cap->oap);
7222 return;
7223 }
7224 cap->count0 = curbuf->b_ml.ml_line_count
7225 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007227
7228 prep_redo(cap->oap->regname, cap->count0,
7229 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7230 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232}
7233
7234/*
7235 * "P", "gP", "p" and "gp" commands.
7236 */
7237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007238nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007240 nv_put_opt(cap, FALSE);
7241}
7242
7243/*
7244 * "P", "gP", "p" and "gp" commands.
7245 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7246 */
7247 static void
7248nv_put_opt(cmdarg_T *cap, int fix_indent)
7249{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 int regname = 0;
7251 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007252 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007253 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 int dir;
7255 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007256 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258 if (cap->oap->op_type != OP_NOP)
7259 {
7260#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007261 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7263 {
7264 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007265 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267 else
7268#endif
7269 clearopbeep(cap->oap);
7270 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007271#ifdef FEAT_JOB_CHANNEL
7272 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7273 {
7274 clearopbeep(cap->oap);
7275 }
7276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
7278 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007279 if (fix_indent)
7280 {
7281 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7282 ? FORWARD : BACKWARD;
7283 flags |= PUT_FIXINDENT;
7284 }
7285 else
7286 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007287 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7288 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 prep_redo_cmd(cap);
7290 if (cap->cmdchar == 'g')
7291 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007292 else if (cap->cmdchar == 'z')
7293 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (VIsual_active)
7296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007297 // Putting in Visual mode: The put text replaces the selected
7298 // text. First delete the selected text, then put the new text.
7299 // Need to save and restore the registers that the delete
7300 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007301 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007303 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007304#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007306#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007307 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007308 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007309#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313 )
7314 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 // The delete is going to overwrite the register we want to
7316 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 reg1 = get_register(regname, TRUE);
7318 }
7319
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007320 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 cap->cmdchar = 'd';
7322 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007323 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007324 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 nv_operator(cap);
7326 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007327 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007328 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007330 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 cap->oap->regname = regname;
7332
7333 if (reg1 != NULL)
7334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007335 // Delete probably changed the register we want to put, save
7336 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 reg2 = get_register(regname, FALSE);
7338 put_register(regname, reg1);
7339 }
7340
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007341 // When deleted a linewise Visual area, put the register as
7342 // lines to avoid it joined with the next line. When deletion was
7343 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 if (VIsual_mode == 'V')
7345 flags |= PUT_LINE;
7346 else if (VIsual_mode == 'v')
7347 flags |= PUT_LINE_SPLIT;
7348 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7349 flags |= PUT_LINE_FORWARD;
7350 dir = BACKWARD;
7351 if ((VIsual_mode != 'V'
7352 && curwin->w_cursor.col < curbuf->b_op_start.col)
7353 || (VIsual_mode == 'V'
7354 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 // cursor is at the end of the line or end of file, put
7356 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007359 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007361 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (reg2 != NULL)
7365 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007366
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007367 // What to reselect with "gv"? Selecting the just put text seems to
7368 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007369 if (was_visual)
7370 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007371 curbuf->b_visual.vi_start = curbuf->b_op_start;
7372 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007374 if (*p_sel == 'e')
7375 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 }
7377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007378 // When all lines were selected and deleted do_put() leaves an empty
7379 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007380 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007381 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007382 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007383 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007385 // If the cursor was in that line, move it to the end of the last
7386 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007387 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7388 {
7389 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7390 coladvance((colnr_T)MAXCOL);
7391 }
7392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 auto_format(FALSE, TRUE);
7394 }
7395}
7396
7397/*
7398 * "o" and "O" commands.
7399 */
7400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007401nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007404 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7406 {
7407 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007408 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410 else
7411#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007412 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007414#ifdef FEAT_JOB_CHANNEL
7415 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007416 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 n_opencmd(cap);
7420}
7421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422#ifdef FEAT_NETBEANS_INTG
7423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007424nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 netbeans_keycommand(cap->nchar);
7427}
7428#endif
7429
7430#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007432nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007434 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435}
7436#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007437
Bram Moolenaar3918c952005-03-15 22:34:55 +00007438/*
7439 * Trigger CursorHold event.
7440 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7441 * input buffer. "did_cursorhold" is set to avoid retriggering.
7442 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007443 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007444nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007445{
7446 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7447 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007448 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007449}