blob: 938ae718efa265a8941b0b01f7e5af202c7bd0fc [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);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000427 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000428 }
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
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001933 if (want_hor)
1934 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
1936
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001937 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 VIsual_select = old_VIsual_select;
1939 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 curwin = old_curwin;
1941 curbuf = old_curbuf;
1942}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944/*
1945 * Command character that's ignored.
1946 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001947 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001950nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001952 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953}
1954
1955/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001956 * Command character that doesn't do anything, but unlike nv_ignore() does
1957 * start edit(). Used for "startinsert" executed while starting up.
1958 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001960nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001961{
1962}
1963
1964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 * Command character doesn't exist.
1966 */
1967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001968nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 clearopbeep(cap->oap);
1971}
1972
1973/*
1974 * <Help> and <F1> commands.
1975 */
1976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001977nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979 if (!checkclearopq(cap->oap))
1980 ex_help(NULL);
1981}
1982
1983/*
1984 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
1985 */
1986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001987nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
Bram Moolenaarf2732452018-06-03 14:47:35 +02001989#ifdef FEAT_JOB_CHANNEL
1990 if (bt_prompt(curbuf) && !prompt_curpos_editable())
1991 clearopbeep(cap->oap);
1992 else
1993#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01001994 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02001995 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01001996 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01001997 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
1998 op_addsub(cap->oap, cap->count1, cap->arg);
1999 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002000 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002001 else if (VIsual_active)
2002 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002003 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002004 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005}
2006
2007/*
2008 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2009 */
2010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002014 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002015 if (mod_mask & MOD_MASK_CTRL)
2016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002017 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002018 if (cap->arg == BACKWARD)
2019 goto_tabpage(-(int)cap->count1);
2020 else
2021 goto_tabpage((int)cap->count0);
2022 }
2023 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002024 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026}
2027
2028/*
2029 * Implementation of "gd" and "gD" command.
2030 */
2031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002032nv_gd(
2033 oparg_T *oap,
2034 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002035 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036{
2037 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 char_u *ptr;
2039
Bram Moolenaard9d30582005-05-18 22:10:28 +00002040 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002041 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002042 == FAIL)
2043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002045 }
2046 else
2047 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002048#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002049 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2050 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002051#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002052 // clear any search statistics
2053 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2054 clear_cmdline = TRUE;
2055 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002056}
2057
2058/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002059 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2060 * otherwise.
2061 */
2062 static int
2063is_ident(char_u *line, int offset)
2064{
2065 int i;
2066 int incomment = FALSE;
2067 int instring = 0;
2068 int prev = 0;
2069
2070 for (i = 0; i < offset && line[i] != NUL; i++)
2071 {
2072 if (instring != 0)
2073 {
2074 if (prev != '\\' && line[i] == instring)
2075 instring = 0;
2076 }
2077 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2078 {
2079 instring = line[i];
2080 }
2081 else
2082 {
2083 if (incomment)
2084 {
2085 if (prev == '*' && line[i] == '/')
2086 incomment = FALSE;
2087 }
2088 else if (prev == '/' && line[i] == '*')
2089 {
2090 incomment = TRUE;
2091 }
2092 else if (prev == '/' && line[i] == '/')
2093 {
2094 return FALSE;
2095 }
2096 }
2097
2098 prev = line[i];
2099 }
2100
2101 return incomment == FALSE && instring == 0;
2102}
2103
2104/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002105 * Search for variable declaration of "ptr[len]".
2106 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2107 * current file ("gD").
2108 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002109 * Return FAIL when not found.
2110 */
2111 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002112find_decl(
2113 char_u *ptr,
2114 int len,
2115 int locally,
2116 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002117 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002118{
2119 char_u *pat;
2120 pos_T old_pos;
2121 pos_T par_pos;
2122 pos_T found_pos;
2123 int t;
2124 int save_p_ws;
2125 int save_p_scs;
2126 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002127 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002128 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002129 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002130
2131 if ((pat = alloc(len + 7)) == NULL)
2132 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 // Put "\V" before the pattern to avoid that the special meaning of "."
2135 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002136 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2137 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 old_pos = curwin->w_cursor;
2139 save_p_ws = p_ws;
2140 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 p_ws = FALSE; // don't wrap around end of file now
2142 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002144 // With "gD" go to line 1.
2145 // With "gd" Search back for the start of the current function, then go
2146 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002147 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002149 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002151 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 else
2154 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002155 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2157 --curwin->w_cursor.lnum;
2158 }
2159 curwin->w_cursor.col = 0;
2160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002161 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002162 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002163 for (;;)
2164 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002165 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002166 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002167 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002168 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002169
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002170 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002171 {
2172 pos_T *pos;
2173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002174 // Check that the block the match is in doesn't end before the
2175 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002176 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2177 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2178 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002179 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 // There can't be a useful match before the end of this block.
2181 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002182 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002183 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002184 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002185 }
2186
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187 if (t == FAIL)
2188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002190 if (found_pos.lnum != 0)
2191 {
2192 curwin->w_cursor = found_pos;
2193 t = OK;
2194 }
2195 break;
2196 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002197 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002198 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002200 ++curwin->w_cursor.lnum;
2201 curwin->w_cursor.col = 0;
2202 continue;
2203 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002204 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 // If the current position is not a valid identifier and a previous
2207 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002208 if (!valid && found_pos.lnum != 0)
2209 {
2210 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002211 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002212 }
2213
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002215 if (valid && !locally)
2216 break;
2217 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 if (found_pos.lnum != 0)
2221 curwin->w_cursor = found_pos;
2222 break;
2223 }
2224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 // For finding a local variable and the match is before the "{" or
2226 // inside a comment, continue searching. For K&R style function
2227 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002228 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002229 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002230 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // Remove SEARCH_START from flags to avoid getting stuck at one
2233 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002234 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002236
2237 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 curwin->w_cursor = old_pos;
2241 }
2242 else
2243 {
2244 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 reset_search_dir();
2247 }
2248
2249 vim_free(pat);
2250 p_ws = save_p_ws;
2251 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002252
2253 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254}
2255
2256/*
2257 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2258 * lines rather than lines in the file.
2259 * 'dist' must be positive.
2260 *
2261 * Return OK if able to move cursor, FAIL otherwise.
2262 */
2263 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002266 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 int retval = OK;
2268 int atend = FALSE;
2269 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002270 int col_off1; // margin offset for first screen line
2271 int col_off2; // margin offset for wrapped screen line
2272 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002273 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002276 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 col_off1 = curwin_col_off();
2279 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002280 width1 = curwin->w_width - col_off1;
2281 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002282 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002283 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002286 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002287 // Instead of sticking at the last character of the buffer line we
2288 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (curwin->w_curswant == MAXCOL)
2290 {
2291 atend = TRUE;
2292 validate_virtcol();
2293 if (width1 <= 0)
2294 curwin->w_curswant = 0;
2295 else
2296 {
2297 curwin->w_curswant = width1 - 1;
2298 if (curwin->w_virtcol > curwin->w_curswant)
2299 curwin->w_curswant += ((curwin->w_virtcol
2300 - curwin->w_curswant - 1) / width2 + 1) * width2;
2301 }
2302 }
2303 else
2304 {
2305 if (linelen > width1)
2306 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2307 else
2308 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002309 if (curwin->w_curswant >= (colnr_T)n)
2310 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312
2313 while (dist--)
2314 {
2315 if (dir == BACKWARD)
2316 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002317 if ((long)curwin->w_curswant >= width1
2318#ifdef FEAT_FOLDING
2319 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2320#endif
2321 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002322 // Move back within the line. This can give a negative value
2323 // for w_curswant if width1 < width2 (with cpoptions+=n),
2324 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 curwin->w_curswant -= width2;
2326 else
2327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002328 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002330 // Move to the start of a closed fold. Don't do that when
2331 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (!(fdo_flags & FDO_ALL))
2333 (void)hasFolding(curwin->w_cursor.lnum,
2334 &curwin->w_cursor.lnum, NULL);
2335#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002336 if (curwin->w_cursor.lnum == 1)
2337 {
2338 retval = FAIL;
2339 break;
2340 }
2341 --curwin->w_cursor.lnum;
2342
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002343 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (linelen > width1)
2345 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2346 + 1) * width2;
2347 }
2348 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 if (linelen > width1)
2352 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2353 else
2354 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002355 if (curwin->w_curswant + width2 < (colnr_T)n
2356#ifdef FEAT_FOLDING
2357 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2358#endif
2359 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002360 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 curwin->w_curswant += width2;
2362 else
2363 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002364 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002366 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2368 &curwin->w_cursor.lnum);
2369#endif
2370 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2371 {
2372 retval = FAIL;
2373 break;
2374 }
2375 curwin->w_cursor.lnum++;
2376 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002377 // Check if the cursor has moved below the number display
2378 // when width1 < width2 (with cpoptions+=n). Subtract width2
2379 // to get a negative value for w_curswant, which will get
2380 // clipped to column 0.
2381 if (curwin->w_curswant >= width1)
2382 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002383 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
2385 }
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002389 if (virtual_active() && atend)
2390 coladvance(MAXCOL);
2391 else
2392 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2395 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002396 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002397 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002398
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002399 // Check for landing on a character that got split at the end of the
2400 // last line. We want to advance a screenline, not end up in the same
2401 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002403 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002404#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002405 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2406 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002407#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002408
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002409 c = (*mb_ptr2char)(ml_get_cursor());
2410 if (dir == FORWARD && virtcol < curwin->w_curswant
2411 && (curwin->w_curswant <= (colnr_T)width1)
2412 && !vim_isprintc(c) && c > 255)
2413 oneright();
2414
Bram Moolenaar773b1582014-08-29 14:20:51 +02002415 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 && (curwin->w_curswant < (colnr_T)width1
2417 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2418 : ((curwin->w_curswant - width1) % width2
2419 > (colnr_T)width2 / 2)))
2420 --curwin->w_cursor.col;
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002425 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 return retval;
2428}
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2432 * cap->arg must be TRUE for CTRL-E.
2433 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002435nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 if (!checkclearop(cap->oap))
2438 scroll_redraw(cap->arg, cap->count1);
2439}
2440
2441/*
2442 * Scroll "count" lines up or down, and redraw.
2443 */
2444 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002445scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002448 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449#ifdef FEAT_DIFF
2450 int prev_topfill = curwin->w_topfill;
2451#endif
2452 linenr_T prev_lnum = curwin->w_cursor.lnum;
2453
2454 if (up)
2455 scrollup(count, TRUE);
2456 else
2457 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002458 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002460 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2461 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 cursor_correct();
2463 check_cursor_moved(curwin);
2464 curwin->w_valid |= VALID_TOPLINE;
2465
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002466 // If moved back to where we were, at least move the cursor, otherwise
2467 // we get stuck at one position. Don't move the cursor up if the
2468 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002470 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#ifdef FEAT_DIFF
2472 && curwin->w_topfill == prev_topfill
2473#endif
2474 )
2475 {
2476 if (up)
2477 {
2478 if (curwin->w_cursor.lnum > prev_lnum
2479 || cursor_down(1L, FALSE) == FAIL)
2480 break;
2481 }
2482 else
2483 {
2484 if (curwin->w_cursor.lnum < prev_lnum
2485 || prev_topline == 1L
2486 || cursor_up(1L, FALSE) == FAIL)
2487 break;
2488 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002489 // Mark w_topline as valid, otherwise the screen jumps back at the
2490 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 check_cursor_moved(curwin);
2492 curwin->w_valid |= VALID_TOPLINE;
2493 }
2494 }
2495 if (curwin->w_cursor.lnum != prev_lnum)
2496 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002497 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498}
2499
2500/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002501 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2502 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2503 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002504 */
2505 static int
2506nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2507{
2508 int nchar = *nchar_arg;
2509 long n;
2510
2511 // "z123{nchar}": edit the count before obtaining {nchar}
2512 if (checkclearop(cap->oap))
2513 return FALSE;
2514 n = nchar - '0';
2515
2516 for (;;)
2517 {
2518#ifdef USE_ON_FLY_SCROLL
2519 dont_scroll = TRUE; // disallow scrolling here
2520#endif
2521 ++no_mapping;
2522 ++allow_keys; // no mapping for nchar, but allow key codes
2523 nchar = plain_vgetc();
2524 LANGMAP_ADJUST(nchar, TRUE);
2525 --no_mapping;
2526 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002527 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002528
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002529 if (nchar == K_DEL || nchar == K_KDEL)
2530 n /= 10;
2531 else if (VIM_ISDIGIT(nchar))
2532 n = n * 10 + (nchar - '0');
2533 else if (nchar == CAR)
2534 {
2535#ifdef FEAT_GUI
2536 need_mouse_correct = TRUE;
2537#endif
2538 win_setheight((int)n);
2539 break;
2540 }
2541 else if (nchar == 'l'
2542 || nchar == 'h'
2543 || nchar == K_LEFT
2544 || nchar == K_RIGHT)
2545 {
2546 cap->count1 = n ? n * cap->count1 : cap->count1;
2547 *nchar_arg = nchar;
2548 return TRUE;
2549 }
2550 else
2551 {
2552 clearopbeep(cap->oap);
2553 break;
2554 }
2555 }
2556 cap->oap->op_type = OP_NOP;
2557 return FALSE;
2558}
2559
2560#ifdef FEAT_SPELL
2561/*
2562 * "zug" and "zuw": undo "zg" and "zw"
2563 * "zg": add good word to word list
2564 * "zw": add wrong word to word list
2565 * "zG": add good word to temp word list
2566 * "zW": add wrong word to temp word list
2567 */
2568 static int
2569nv_zg_zw(cmdarg_T *cap, int nchar)
2570{
2571 char_u *ptr = NULL;
2572 int len;
2573 int undo = FALSE;
2574
2575 if (nchar == 'u')
2576 {
2577 ++no_mapping;
2578 ++allow_keys; // no mapping for nchar, but allow key codes
2579 nchar = plain_vgetc();
2580 LANGMAP_ADJUST(nchar, TRUE);
2581 --no_mapping;
2582 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002583 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002584
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002585 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2586 {
2587 clearopbeep(cap->oap);
2588 return OK;
2589 }
2590 undo = TRUE;
2591 }
2592
2593 if (checkclearop(cap->oap))
2594 return OK;
2595 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2596 return FAIL;
2597 if (ptr == NULL)
2598 {
2599 pos_T pos = curwin->w_cursor;
2600
2601 // Find bad word under the cursor. When 'spell' is
2602 // off this fails and find_ident_under_cursor() is
2603 // used below.
2604 emsg_off++;
2605 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2606 emsg_off--;
2607 if (len != 0 && curwin->w_cursor.col <= pos.col)
2608 ptr = ml_get_pos(&curwin->w_cursor);
2609 curwin->w_cursor = pos;
2610 }
2611
2612 if (ptr == NULL
2613 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2614 return FAIL;
2615 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2616 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2617 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2618
2619 return OK;
2620}
2621#endif
2622
2623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 * Commands that start with "z".
2625 */
2626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002627nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 long n;
2630 colnr_T col;
2631 int nchar = cap->nchar;
2632#ifdef FEAT_FOLDING
2633 long old_fdl = curwin->w_p_fdl;
2634 int old_fen = curwin->w_p_fen;
2635#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002636 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002638 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (
2642#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002643 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2644 // and "zC" only in Visual mode. "zj" and "zk" are motion
2645 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 cap->nchar != 'f' && cap->nchar != 'F'
2647 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2648 && cap->nchar != 'j' && cap->nchar != 'k'
2649 &&
2650#endif
2651 checkclearop(cap->oap))
2652 return;
2653
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002654 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2655 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2657 && cap->count0
2658 && cap->count0 != curwin->w_cursor.lnum)
2659 {
2660 setpcmark();
2661 if (cap->count0 > curbuf->b_ml.ml_line_count)
2662 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2663 else
2664 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002665 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667
2668 switch (nchar)
2669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 case '+':
2672 if (cap->count0 == 0)
2673 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002674 // No count given: put cursor at the line below screen
2675 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2677 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2678 else
2679 curwin->w_cursor.lnum = curwin->w_botline;
2680 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002681 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 case NL:
2683 case CAR:
2684 case K_KENTER:
2685 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002686 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002689 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002690 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 break;
2692
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002698 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002699 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 break;
2701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002702 // "z^", "z-" and "zb": put cursor at bottom of screen
2703 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2704 // when <count> is at bottom of window, and puts that one at
2705 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (cap->count0 != 0)
2707 {
2708 scroll_cursor_bot(0, TRUE);
2709 curwin->w_cursor.lnum = curwin->w_topline;
2710 }
2711 else if (curwin->w_topline == 1)
2712 curwin->w_cursor.lnum = 1;
2713 else
2714 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 case '-':
2717 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002721 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002722 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 break;
2724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 case 'h':
2732 case K_LEFT:
2733 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002734 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2735 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 break;
2737
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002738 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002739 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002742 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 case 'l':
2744 case K_RIGHT:
2745 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002746 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 break;
2748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 case 's': if (!curwin->w_p_wrap)
2751 {
2752#ifdef FEAT_FOLDING
2753 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 else
2756#endif
2757 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002758 if ((long)col > siso)
2759 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 else
2761 col = 0;
2762 if (curwin->w_leftcol != col)
2763 {
2764 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002765 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 }
2768 break;
2769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 case 'e': if (!curwin->w_p_wrap)
2772 {
2773#ifdef FEAT_FOLDING
2774 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else
2777#endif
2778 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002779 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002780 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 col = 0;
2782 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002783 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (curwin->w_leftcol != col)
2785 {
2786 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002787 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 }
2790 break;
2791
Christian Brabandt2fa93842021-05-30 22:17:25 +02002792 // "zp", "zP" in block mode put without addind trailing spaces
2793 case 'P':
2794 case 'p': nv_put(cap);
2795 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002796 // "zy" Yank without trailing spaces
2797 case 'y': nv_operator(cap);
2798 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // "zF": create fold command
2801 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case 'F':
2803 case 'f': if (foldManualAllowed(TRUE))
2804 {
2805 cap->nchar = 'f';
2806 nv_operator(cap);
2807 curwin->w_p_fen = TRUE;
2808
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2811 {
2812 nv_operator(cap);
2813 finish_op = TRUE;
2814 }
2815 }
2816 else
2817 clearopbeep(cap->oap);
2818 break;
2819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // "zd": delete fold at cursor
2821 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case 'd':
2823 case 'D': if (foldManualAllowed(FALSE))
2824 {
2825 if (VIsual_active)
2826 nv_operator(cap);
2827 else
2828 deleteFold(curwin->w_cursor.lnum,
2829 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2830 }
2831 break;
2832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case 'E': if (foldmethodIsManual(curwin))
2835 {
2836 clearFolding(curwin);
2837 changed_window_setting();
2838 }
2839 else if (foldmethodIsMarker(curwin))
2840 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2841 TRUE, FALSE);
2842 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002843 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 break;
2845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case 'n': curwin->w_p_fen = FALSE;
2848 break;
2849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case 'N': curwin->w_p_fen = TRUE;
2852 break;
2853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2856 break;
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2860 openFold(curwin->w_cursor.lnum, cap->count1);
2861 else
2862 {
2863 closeFold(curwin->w_cursor.lnum, cap->count1);
2864 curwin->w_p_fen = TRUE;
2865 }
2866 break;
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2870 openFoldRecurse(curwin->w_cursor.lnum);
2871 else
2872 {
2873 closeFoldRecurse(curwin->w_cursor.lnum);
2874 curwin->w_p_fen = TRUE;
2875 }
2876 break;
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case 'o': if (VIsual_active)
2880 nv_operator(cap);
2881 else
2882 openFold(curwin->w_cursor.lnum, cap->count1);
2883 break;
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 case 'O': if (VIsual_active)
2887 nv_operator(cap);
2888 else
2889 openFoldRecurse(curwin->w_cursor.lnum);
2890 break;
2891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 case 'c': if (VIsual_active)
2894 nv_operator(cap);
2895 else
2896 closeFold(curwin->w_cursor.lnum, cap->count1);
2897 curwin->w_p_fen = TRUE;
2898 break;
2899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 case 'C': if (VIsual_active)
2902 nv_operator(cap);
2903 else
2904 closeFoldRecurse(curwin->w_cursor.lnum);
2905 curwin->w_p_fen = TRUE;
2906 break;
2907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 case 'v': foldOpenCursor();
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 curwin->w_foldinvalid = TRUE; // recompute folds
2915 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 foldOpenCursor();
2917 break;
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 curwin->w_foldinvalid = TRUE; // recompute folds
2922 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 break;
2924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002927 {
2928 curwin->w_p_fdl -= cap->count1;
2929 if (curwin->w_p_fdl < 0)
2930 curwin->w_p_fdl = 0;
2931 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 curwin->w_p_fen = TRUE;
2934 break;
2935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 curwin->w_p_fen = TRUE;
2940 break;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002943 case 'r': curwin->w_p_fdl += cap->count1;
2944 {
2945 int d = getDeepestNesting();
2946
2947 if (curwin->w_p_fdl >= d)
2948 curwin->w_p_fdl = d;
2949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 break;
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 break;
2956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 case 'j': // "zj" move to next fold downwards
2958 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2960 cap->count1) == FAIL)
2961 clearopbeep(cap->oap);
2962 break;
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002966#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 case 'g': // "zg": add good word to word list
2969 case 'w': // "zw": add wrong word to word list
2970 case 'G': // "zG": add good word to temp word list
2971 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002972 if (nv_zg_zw(cap, nchar) == FAIL)
2973 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002974 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002977 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002978 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002979 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002980#endif
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 default: clearopbeep(cap->oap);
2983 }
2984
2985#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (old_fen != curwin->w_p_fen)
2988 {
2989# ifdef FEAT_DIFF
2990 win_T *wp;
2991
2992 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 FOR_ALL_WINDOWS(wp)
2996 {
2997 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
2998 {
2999 wp->w_p_fen = curwin->w_p_fen;
3000 changed_window_setting_win(wp);
3001 }
3002 }
3003 }
3004# endif
3005 changed_window_setting();
3006 }
3007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (old_fdl != curwin->w_p_fdl)
3010 newFoldLevel();
3011#endif
3012}
3013
3014#ifdef FEAT_GUI
3015/*
3016 * Vertical scrollbar movement.
3017 */
3018 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003019nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 if (cap->oap->op_type != OP_NOP)
3022 clearopbeep(cap->oap);
3023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 gui_do_scroll();
3026}
3027
3028/*
3029 * Horizontal scrollbar movement.
3030 */
3031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003032nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 if (cap->oap->op_type != OP_NOP)
3035 clearopbeep(cap->oap);
3036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003037 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003038 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039}
3040#endif
3041
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003042#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003043/*
3044 * Click in GUI tab.
3045 */
3046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003047nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003048{
3049 if (cap->oap->op_type != OP_NOP)
3050 clearopbeep(cap->oap);
3051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003052 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003053 goto_tabpage(current_tab);
3054}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003055
3056/*
3057 * Selected item in tab line menu.
3058 */
3059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003060nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003061{
3062 if (cap->oap->op_type != OP_NOP)
3063 clearopbeep(cap->oap);
3064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003066 handle_tabmenu();
3067}
3068
3069/*
3070 * Handle selecting an item of the GUI tab line menu.
3071 * Used in Normal and Insert mode.
3072 */
3073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003074handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003075{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003076 switch (current_tabmenu)
3077 {
3078 case TABLINE_MENU_CLOSE:
3079 if (current_tab == 0)
3080 do_cmdline_cmd((char_u *)"tabclose");
3081 else
3082 {
3083 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3084 current_tab);
3085 do_cmdline_cmd(IObuff);
3086 }
3087 break;
3088
3089 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003090 if (current_tab == 0)
3091 do_cmdline_cmd((char_u *)"$tabnew");
3092 else
3093 {
3094 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3095 current_tab - 1);
3096 do_cmdline_cmd(IObuff);
3097 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003098 break;
3099
3100 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003101 if (current_tab == 0)
3102 do_cmdline_cmd((char_u *)"browse $tabnew");
3103 else
3104 {
3105 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3106 current_tab - 1);
3107 do_cmdline_cmd(IObuff);
3108 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003109 break;
3110 }
3111}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003112#endif
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114/*
3115 * "Q" command.
3116 */
3117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003118nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003120 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003122 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003123 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 do_exmode(FALSE);
3125}
3126
3127/*
3128 * Handle a ":" command.
3129 */
3130 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003131nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003133 int old_p_im;
3134 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003135 int is_cmdkey = cap->cmdchar == K_COMMAND
3136 || cap->cmdchar == K_SCRIPT_COMMAND;
3137 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaar957cf672020-11-12 14:21:06 +01003139 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 nv_operator(cap);
3141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 if (cap->oap->op_type != OP_NOP)
3144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 cap->oap->motion_type = MCHAR;
3147 cap->oap->inclusive = FALSE;
3148 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003149 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 stuffcharReadbuff('.');
3153 if (cap->count0 > 1)
3154 {
3155 stuffReadbuff((char_u *)",.+");
3156 stuffnumReadbuff((long)cap->count0 - 1L);
3157 }
3158 }
3159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (KeyTyped)
3162 compute_cmdrow();
3163
3164 old_p_im = p_im;
3165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003166 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003167 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3168 if (is_cmdkey)
3169 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3170 else
3171 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (p_im != old_p_im)
3175 {
3176 if (p_im)
3177 restart_edit = 'i';
3178 else
3179 restart_edit = 0;
3180 }
3181
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003182 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003183 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003184 clearop(cap->oap);
3185 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3187 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003188 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3189 || did_emsg
3190 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003191 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 clearopbeep(cap->oap);
3193 }
3194}
3195
3196/*
3197 * Handle CTRL-G command.
3198 */
3199 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003200nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003202 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
3204 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003205 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 showmode();
3207 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003208 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 fileinfo((int)cap->count0, FALSE, TRUE);
3211}
3212
3213/*
3214 * Handle CTRL-H <Backspace> command.
3215 */
3216 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003217nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (VIsual_active && VIsual_select)
3220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 v_visop(cap);
3223 }
3224 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 nv_left(cap);
3226}
3227
3228/*
3229 * CTRL-L: clear screen and redraw.
3230 */
3231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003232nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
3234 if (!checkclearop(cap->oap))
3235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003238 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003239# ifdef FEAT_RELTIME
3240 {
3241 win_T *wp;
3242
3243 FOR_ALL_WINDOWS(wp)
3244 wp->w_s->b_syn_slow = FALSE;
3245 }
3246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003248 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003249#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3250# ifdef VIMDLL
3251 if (!gui.in_use)
3252# endif
3253 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256}
3257
3258/*
3259 * CTRL-O: In Select mode: switch to Visual mode for one command.
3260 * Otherwise: Go to older pcmark.
3261 */
3262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003263nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (VIsual_active && VIsual_select)
3266 {
3267 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003268 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003270 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 cap->count1 = -cap->count1;
3275 nv_pcmark(cap);
3276 }
3277}
3278
3279/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003280 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3281 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
3283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003284nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 if (!checkclearopq(cap->oap))
3287 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3288 GETF_SETMARK|GETF_ALT, FALSE);
3289}
3290
3291/*
3292 * "Z" commands.
3293 */
3294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003295nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 if (!checkclearopq(cap->oap))
3298 {
3299 switch (cap->nchar)
3300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003301 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 case 'Z': do_cmdline_cmd((char_u *)"x");
3303 break;
3304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 case 'Q': do_cmdline_cmd((char_u *)"q!");
3307 break;
3308
3309 default: clearopbeep(cap->oap);
3310 }
3311 }
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314/*
3315 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3316 */
3317 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003318do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 oparg_T oa;
3321 cmdarg_T ca;
3322
3323 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003324 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 ca.oap = &oa;
3326 ca.cmdchar = c1;
3327 ca.nchar = c2;
3328 nv_ident(&ca);
3329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003332 * 'K' normal-mode command. Get the command to lookup the keyword under the
3333 * cursor.
3334 */
3335 static int
3336nv_K_getcmd(
3337 cmdarg_T *cap,
3338 char_u *kp,
3339 int kp_help,
3340 int kp_ex,
3341 char_u **ptr_arg,
3342 int n,
3343 char_u *buf,
3344 unsigned buflen)
3345{
3346 char_u *ptr = *ptr_arg;
3347 int isman;
3348 int isman_s;
3349
3350 if (kp_help)
3351 {
3352 // in the help buffer
3353 STRCPY(buf, "he! ");
3354 return n;
3355 }
3356
3357 if (kp_ex)
3358 {
3359 // 'keywordprog' is an ex command
3360 if (cap->count0 != 0)
3361 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3362 else
3363 STRCPY(buf, kp);
3364 STRCAT(buf, " ");
3365 return n;
3366 }
3367
3368 // An external command will probably use an argument starting
3369 // with "-" as an option. To avoid trouble we skip the "-".
3370 while (*ptr == '-' && n > 0)
3371 {
3372 ++ptr;
3373 --n;
3374 }
3375 if (n == 0)
3376 {
3377 // found dashes only
3378 emsg(_(e_no_identifier_under_cursor));
3379 vim_free(buf);
3380 *ptr_arg = ptr;
3381 return 0;
3382 }
3383
3384 // When a count is given, turn it into a range. Is this
3385 // really what we want?
3386 isman = (STRCMP(kp, "man") == 0);
3387 isman_s = (STRCMP(kp, "man -s") == 0);
3388 if (cap->count0 != 0 && !(isman || isman_s))
3389 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3390
3391 STRCAT(buf, "! ");
3392 if (cap->count0 == 0 && isman_s)
3393 STRCAT(buf, "man");
3394 else
3395 STRCAT(buf, kp);
3396 STRCAT(buf, " ");
3397 if (cap->count0 != 0 && (isman || isman_s))
3398 {
3399 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3400 STRCAT(buf, " ");
3401 }
3402
3403 *ptr_arg = ptr;
3404 return n;
3405}
3406
3407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 * Handle the commands that use the word under the cursor.
3409 * [g] CTRL-] :ta to current identifier
3410 * [g] 'K' run program for current identifier
3411 * [g] '*' / to current identifier or string
3412 * [g] '#' ? to current identifier or string
3413 * g ']' :tselect for current identifier
3414 */
3415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003416nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 char_u *ptr = NULL;
3419 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003420 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003421 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 char_u *kp; // value of 'keywordprg'
3424 int kp_help; // 'keywordprg' is ":he"
3425 int kp_ex; // 'keywordprg' starts with ":"
3426 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003429 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003432 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
3434 cmdchar = cap->nchar;
3435 g_cmd = TRUE;
3436 }
3437 else
3438 {
3439 cmdchar = cap->cmdchar;
3440 g_cmd = FALSE;
3441 }
3442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 cmdchar = '#';
3445
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003446 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3450 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (checkclearopq(cap->oap))
3452 return;
3453 }
3454
3455 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3456 (cmdchar == '*' || cmdchar == '#')
3457 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3458 {
3459 clearop(cap->oap);
3460 return;
3461 }
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 // Allocate buffer to put the command in. Inserting backslashes can
3464 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3465 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3467 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3468 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003469 if (kp_help && *skipwhite(ptr) == NUL)
3470 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003471 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003472 return;
3473 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003474 kp_ex = (*kp == ':');
3475 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3476 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (buf == NULL)
3478 return;
3479 buf[0] = NUL;
3480
3481 switch (cmdchar)
3482 {
3483 case '*':
3484 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003485 // Put cursor at start of word, makes search skip the word
3486 // under the cursor.
3487 // Call setpcmark() first, so "*``" puts the cursor back where
3488 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 setpcmark();
3490 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3491
3492 if (!g_cmd && vim_iswordp(ptr))
3493 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 break;
3496
3497 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003498 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3499 if (n == 0)
3500 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 break;
3502
3503 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003504 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#ifdef FEAT_CSCOPE
3506 if (p_cst)
3507 STRCPY(buf, "cstag ");
3508 else
3509#endif
3510 STRCPY(buf, "ts ");
3511 break;
3512
3513 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003514 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (curbuf->b_help)
3516 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003518 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003519 if (g_cmd)
3520 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003521 else if (cap->count0 == 0)
3522 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003523 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003524 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003528 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003529 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003531 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003532 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003533 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003534 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003535 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003537 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003538 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003539 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003541 vim_free(buf);
3542 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003544 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003545 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003546 {
3547 vim_free(buf);
3548 vim_free(p);
3549 return;
3550 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003551 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003552 STRCAT(buf, p);
3553 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003555 else
3556 {
3557 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003558 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003559 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003560 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003561 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003562 {
3563 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003564 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003565 aux_ptr = (char_u *)"";
3566 else
3567 aux_ptr = (char_u *)"\\|\"\n[";
3568 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003569 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003570 aux_ptr = (char_u *)"\\|\"\n*?[";
3571
3572 p = buf + STRLEN(buf);
3573 while (n-- > 0)
3574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003575 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003576 if (vim_strchr(aux_ptr, *ptr) != NULL)
3577 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 // When current byte is a part of multibyte character, copy all
3579 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003580 if (has_mbyte)
3581 {
3582 int i;
3583 int len = (*mb_ptr2len)(ptr) - 1;
3584
3585 for (i = 0; i < len && n >= 1; ++i, --n)
3586 *p++ = *ptr++;
3587 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003588 *p++ = *ptr++;
3589 }
3590 *p = NUL;
3591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003593 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (cmdchar == '*' || cmdchar == '#')
3595 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003596 if (!g_cmd && (has_mbyte
3597 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3598 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003600
3601 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003602 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003604
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003605 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003608 {
3609 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003611 g_tag_at_cursor = FALSE;
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 vim_free(buf);
3615}
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617/*
3618 * Get visually selected text, within one line only.
3619 * Returns FAIL if more than one line selected.
3620 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003621 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003622get_visual_text(
3623 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 char_u **pp, // return: start of selected text
3625 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 if (VIsual_mode != 'V')
3628 unadjust_for_sel();
3629 if (VIsual.lnum != curwin->w_cursor.lnum)
3630 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003631 if (cap != NULL)
3632 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 return FAIL;
3634 }
3635 if (VIsual_mode == 'V')
3636 {
3637 *pp = ml_get_curline();
3638 *lenp = (int)STRLEN(*pp);
3639 }
3640 else
3641 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003642 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 *pp = ml_get_pos(&curwin->w_cursor);
3645 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3646 }
3647 else
3648 {
3649 *pp = ml_get_pos(&VIsual);
3650 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3651 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003652 if (**pp == NUL)
3653 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003654 if (*lenp > 0)
3655 {
3656 if (has_mbyte)
3657 // Correct the length to include all bytes of the last
3658 // character.
3659 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3660 else if ((*pp)[*lenp - 1] == NUL)
3661 // Do not include a trailing NUL.
3662 *lenp -= 1;
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 reset_VIsual_and_resel();
3666 return OK;
3667}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669/*
3670 * CTRL-T: backwards in tag stack
3671 */
3672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003673nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 if (!checkclearopq(cap->oap))
3676 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3677}
3678
3679/*
3680 * Handle scrolling command 'H', 'L' and 'M'.
3681 */
3682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003683nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
3685 int used = 0;
3686 long n;
3687#ifdef FEAT_FOLDING
3688 linenr_T lnum;
3689#endif
3690 int half;
3691
3692 cap->oap->motion_type = MLINE;
3693 setpcmark();
3694
3695 if (cap->cmdchar == 'L')
3696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003697 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 curwin->w_cursor.lnum = curwin->w_botline - 1;
3699 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3700 curwin->w_cursor.lnum = 1;
3701 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003702 {
3703#ifdef FEAT_FOLDING
3704 if (hasAnyFolding(curwin))
3705 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003706 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003707 for (n = cap->count1 - 1; n > 0
3708 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3709 {
3710 (void)hasFolding(curwin->w_cursor.lnum,
3711 &curwin->w_cursor.lnum, NULL);
3712 --curwin->w_cursor.lnum;
3713 }
3714 }
3715 else
3716#endif
3717 curwin->w_cursor.lnum -= cap->count1 - 1;
3718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else
3721 {
3722 if (cap->cmdchar == 'M')
3723 {
3724#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003725 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 used -= diff_check_fill(curwin, curwin->w_topline)
3727 - curwin->w_topfill;
3728#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3731 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3732 {
3733#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003734 // Count half he number of filler lines to be "below this
3735 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3737 + n) / 2 >= half)
3738 {
3739 --n;
3740 break;
3741 }
3742#endif
3743 used += plines(curwin->w_topline + n);
3744 if (used >= half)
3745 break;
3746#ifdef FEAT_FOLDING
3747 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3748 n = lnum - curwin->w_topline;
3749#endif
3750 }
3751 if (n > 0 && used > curwin->w_height)
3752 --n;
3753 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003757#ifdef FEAT_FOLDING
3758 if (hasAnyFolding(curwin))
3759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003760 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003761 lnum = curwin->w_topline;
3762 while (n-- > 0 && lnum < curwin->w_botline - 1)
3763 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003764 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003765 ++lnum;
3766 }
3767 n = lnum - curwin->w_topline;
3768 }
3769#endif
3770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 curwin->w_cursor.lnum = curwin->w_topline + n;
3772 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3773 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3774 }
3775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003777 if (cap->oap->op_type == OP_NOP)
3778 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 beginline(BL_SOL | BL_FIX);
3780}
3781
3782/*
3783 * Cursor right commands.
3784 */
3785 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003786nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787{
3788 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003789 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003791 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003793 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003794 if (mod_mask & MOD_MASK_CTRL)
3795 cap->arg = TRUE;
3796 nv_wordcmd(cap);
3797 return;
3798 }
3799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 cap->oap->motion_type = MCHAR;
3801 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003802 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003804 // In virtual edit mode, there's no such thing as "past_line", as lines
3805 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003807 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 for (n = cap->count1; n > 0; --n)
3810 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003811 if ((!past_line && oneright() == FAIL)
3812 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003815 // <Space> wraps to next line if 'whichwrap' has 's'.
3816 // 'l' wraps to next line if 'whichwrap' has 'l'.
3817 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if ( ((cap->cmdchar == ' '
3819 && vim_strchr(p_ww, 's') != NULL)
3820 || (cap->cmdchar == 'l'
3821 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003822 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 && vim_strchr(p_ww, '>') != NULL))
3824 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003826 // When deleting we also count the NL as a character.
3827 // Set cap->oap->inclusive when last char in the line is
3828 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003829 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003831 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 cap->oap->inclusive = TRUE;
3833 else
3834 {
3835 ++curwin->w_cursor.lnum;
3836 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 curwin->w_set_curswant = TRUE;
3839 cap->oap->inclusive = FALSE;
3840 }
3841 continue;
3842 }
3843 if (cap->oap->op_type == OP_NOP)
3844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003845 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (n == cap->count1)
3847 beep_flush();
3848 }
3849 else
3850 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003851 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 cap->oap->inclusive = TRUE;
3853 }
3854 break;
3855 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003856 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
3858 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (virtual_active())
3860 oneright();
3861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003864 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 ++curwin->w_cursor.col;
3867 }
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870#ifdef FEAT_FOLDING
3871 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3872 && cap->oap->op_type == OP_NOP)
3873 foldOpenCursor();
3874#endif
3875}
3876
3877/*
3878 * Cursor left commands.
3879 *
3880 * Returns TRUE when operator end should not be adjusted.
3881 */
3882 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003883nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
3885 long n;
3886
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003887 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003890 if (mod_mask & MOD_MASK_CTRL)
3891 cap->arg = 1;
3892 nv_bck_word(cap);
3893 return;
3894 }
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 cap->oap->motion_type = MCHAR;
3897 cap->oap->inclusive = FALSE;
3898 for (n = cap->count1; n > 0; --n)
3899 {
3900 if (oneleft() == FAIL)
3901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3903 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3904 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if ( (((cap->cmdchar == K_BS
3906 || cap->cmdchar == Ctrl_H)
3907 && vim_strchr(p_ww, 'b') != NULL)
3908 || (cap->cmdchar == 'h'
3909 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003910 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 && vim_strchr(p_ww, '<') != NULL))
3912 && curwin->w_cursor.lnum > 1)
3913 {
3914 --(curwin->w_cursor.lnum);
3915 coladvance((colnr_T)MAXCOL);
3916 curwin->w_set_curswant = TRUE;
3917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 // When the NL before the first char has to be deleted we
3919 // put the cursor on the NUL after the previous line.
3920 // This is a very special case, be careful!
3921 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if ( (cap->oap->op_type == OP_DELETE
3923 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003924 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003926 char_u *cp = ml_get_cursor();
3927
3928 if (*cp != NUL)
3929 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003930 if (has_mbyte)
3931 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3932 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003933 ++curwin->w_cursor.col;
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 cap->retval |= CA_NO_ADJ_OP_END;
3936 }
3937 continue;
3938 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003939 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3941 beep_flush();
3942 break;
3943 }
3944 }
3945#ifdef FEAT_FOLDING
3946 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3947 && cap->oap->op_type == OP_NOP)
3948 foldOpenCursor();
3949#endif
3950}
3951
3952/*
3953 * Cursor up commands.
3954 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3955 */
3956 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003957nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003959 if (mod_mask & MOD_MASK_SHIFT)
3960 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003961 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003962 cap->arg = BACKWARD;
3963 nv_page(cap);
3964 }
3965 else
3966 {
3967 cap->oap->motion_type = MLINE;
3968 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3969 clearopbeep(cap->oap);
3970 else if (cap->arg)
3971 beginline(BL_WHITE | BL_FIX);
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
3975/*
3976 * Cursor down commands.
3977 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3978 */
3979 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003980nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 if (mod_mask & MOD_MASK_SHIFT)
3983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003984 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003985 cap->arg = FORWARD;
3986 nv_page(cap);
3987 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003988#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003989 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003990 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
3991 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003995 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003996 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 cmdwin_result = CAR;
3998 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02003999#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004000 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004001 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4002 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4003 {
4004 invoke_prompt_callback();
4005 if (restart_edit == 0)
4006 restart_edit = 'a';
4007 }
4008 else
4009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
4011 cap->oap->motion_type = MLINE;
4012 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4013 clearopbeep(cap->oap);
4014 else if (cap->arg)
4015 beginline(BL_WHITE | BL_FIX);
4016 }
4017 }
4018}
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020/*
4021 * Grab the file name under the cursor and edit it.
4022 */
4023 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004024nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004027 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004029 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004031 if (curbuf_locked())
4032 {
4033 clearop(cap->oap);
4034 return;
4035 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004036#ifdef FEAT_PROP_POPUP
4037 if (ERROR_IF_TERM_POPUP_WINDOW)
4038 return;
4039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004041 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
4043 if (ptr != NULL)
4044 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004045 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004046 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004047 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004049 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004050 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004051 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004052 {
4053 curwin->w_cursor.lnum = lnum;
4054 check_cursor_lnum();
4055 beginline(BL_SOL | BL_FIX);
4056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 vim_free(ptr);
4058 }
4059 else
4060 clearop(cap->oap);
4061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063/*
4064 * <End> command: to end of current line or last line.
4065 */
4066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004069 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004073 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 nv_dollar(cap);
4076}
4077
4078/*
4079 * Handle the "$" command.
4080 */
4081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
4084 cap->oap->motion_type = MCHAR;
4085 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 // In virtual mode when off the edge of a line and an operator
4087 // is pending (whew!) keep the cursor where it is.
4088 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 if (!virtual_active() || gchar_cursor() != NUL
4090 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004091 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (cursor_down((long)(cap->count1 - 1),
4093 cap->oap->op_type == OP_NOP) == FAIL)
4094 clearopbeep(cap->oap);
4095#ifdef FEAT_FOLDING
4096 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4097 foldOpenCursor();
4098#endif
4099}
4100
4101/*
4102 * Implementation of '?' and '/' commands.
4103 * If cap->arg is TRUE don't set PC mark.
4104 */
4105 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004106nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107{
4108 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004109 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004113 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 cap->cmdchar = 'g';
4115 cap->nchar = '?';
4116 nv_operator(cap);
4117 return;
4118 }
4119
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004120 // When using 'incsearch' the cursor may be moved to set a different search
4121 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004122 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 if (cap->searchbuf == NULL)
4125 {
4126 clearop(oap);
4127 return;
4128 }
4129
Bram Moolenaar46539112015-02-17 15:43:57 +01004130 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004131 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004132 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133}
4134
LemonBoya4399382022-04-09 21:04:08 +01004135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136/*
4137 * Handle "N" and "n" commands.
4138 * cap->arg is SEARCH_REV for "N", 0 for "n".
4139 */
4140 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004141nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004143 pos_T old = curwin->w_cursor;
4144 int wrapped = FALSE;
4145 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004146
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004147 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 // Avoid getting stuck on the current cursor position, which can
4150 // happen when an offset is given and the cursor is on the last char
4151 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004152 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004153 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004154 cap->count1 -= 1;
4155 }
LemonBoya4399382022-04-09 21:04:08 +01004156
4157#ifdef FEAT_SEARCH_EXTRA
4158 // Redraw the window to refresh the highlighted matches.
4159 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004160 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
4164/*
4165 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4166 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004167 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004170normal_search(
4171 cmdarg_T *cap,
4172 int dir,
4173 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004174 int opt, // extra flags for do_search()
4175 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004178 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004179#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004180 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 cap->oap->motion_type = MCHAR;
4184 cap->oap->inclusive = FALSE;
4185 cap->oap->use_reg_one = TRUE;
4186 curwin->w_set_curswant = TRUE;
4187
Bram Moolenaara80faa82020-04-12 19:37:17 +02004188 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004189 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004190 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4191 if (wrapped != NULL)
4192 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (i == 0)
4194 clearop(cap->oap);
4195 else
4196 {
4197 if (i == 2)
4198 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200#ifdef FEAT_FOLDING
4201 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4202 foldOpenCursor();
4203#endif
4204 }
LemonBoya4399382022-04-09 21:04:08 +01004205#ifdef FEAT_SEARCH_EXTRA
4206 // Redraw the window to refresh the highlighted matches.
4207 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004208 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004211 // "/$" will put the cursor after the end of the line, may need to
4212 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004214 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215}
4216
4217/*
4218 * Character search commands.
4219 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4220 * ',' and FALSE for ';'.
4221 * cap->nchar is NUL for ',' and ';' (repeat the search)
4222 */
4223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004224nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int t_cmd;
4227
4228 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4229 t_cmd = TRUE;
4230 else
4231 t_cmd = FALSE;
4232
4233 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4235 clearopbeep(cap->oap);
4236 else
4237 {
4238 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004239 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4241 && (t_cmd || cap->oap->op_type != OP_NOP))
4242 {
4243 colnr_T scol, ecol;
4244
4245 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4246 curwin->w_cursor.coladd = ecol - scol;
4247 }
4248 else
4249 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#ifdef FEAT_FOLDING
4252 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4253 foldOpenCursor();
4254#endif
4255 }
4256}
4257
4258/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004259 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4260 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4261 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4262 * "[m" or "]m" search for prev/next start of (Java) method.
4263 * "[M" or "]M" search for prev/next end of (Java) method.
4264 */
4265 static void
4266nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4267{
4268 pos_T new_pos = {0, 0, 0};
4269 pos_T *pos = NULL; // init for GCC
4270 pos_T prev_pos;
4271 long n;
4272 int findc;
4273 int c;
4274
4275 if (cap->nchar == '*')
4276 cap->nchar = '/';
4277 prev_pos.lnum = 0;
4278 if (cap->nchar == 'm' || cap->nchar == 'M')
4279 {
4280 if (cap->cmdchar == '[')
4281 findc = '{';
4282 else
4283 findc = '}';
4284 n = 9999;
4285 }
4286 else
4287 {
4288 findc = cap->nchar;
4289 n = cap->count1;
4290 }
4291 for ( ; n > 0; --n)
4292 {
4293 if ((pos = findmatchlimit(cap->oap, findc,
4294 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4295 {
4296 if (new_pos.lnum == 0) // nothing found
4297 {
4298 if (cap->nchar != 'm' && cap->nchar != 'M')
4299 clearopbeep(cap->oap);
4300 }
4301 else
4302 pos = &new_pos; // use last one found
4303 break;
4304 }
4305 prev_pos = new_pos;
4306 curwin->w_cursor = *pos;
4307 new_pos = *pos;
4308 }
4309 curwin->w_cursor = *old_pos;
4310
4311 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4312 // brought us to the match for "[m" and "]M" when inside a method.
4313 // Try finding the '{' or '}' we want to be at.
4314 // Also repeat for the given count.
4315 if (cap->nchar == 'm' || cap->nchar == 'M')
4316 {
4317 // norm is TRUE for "]M" and "[m"
4318 int norm = ((findc == '{') == (cap->nchar == 'm'));
4319
4320 n = cap->count1;
4321 // found a match: we were inside a method
4322 if (prev_pos.lnum != 0)
4323 {
4324 pos = &prev_pos;
4325 curwin->w_cursor = prev_pos;
4326 if (norm)
4327 --n;
4328 }
4329 else
4330 pos = NULL;
4331 while (n > 0)
4332 {
4333 for (;;)
4334 {
4335 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4336 {
4337 // if not found anything, that's an error
4338 if (pos == NULL)
4339 clearopbeep(cap->oap);
4340 n = 0;
4341 break;
4342 }
4343 c = gchar_cursor();
4344 if (c == '{' || c == '}')
4345 {
4346 // Must have found end/start of class: use it.
4347 // Or found the place to be at.
4348 if ((c == findc && norm) || (n == 1 && !norm))
4349 {
4350 new_pos = curwin->w_cursor;
4351 pos = &new_pos;
4352 n = 0;
4353 }
4354 // if no match found at all, we started outside of the
4355 // class and we're inside now. Just go on.
4356 else if (new_pos.lnum == 0)
4357 {
4358 new_pos = curwin->w_cursor;
4359 pos = &new_pos;
4360 }
4361 // found start/end of other method: go to match
4362 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004363 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4364 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004365 n = 0;
4366 else
4367 curwin->w_cursor = *pos;
4368 break;
4369 }
4370 }
4371 --n;
4372 }
4373 curwin->w_cursor = *old_pos;
4374 if (pos == NULL && new_pos.lnum != 0)
4375 clearopbeep(cap->oap);
4376 }
4377 if (pos != NULL)
4378 {
4379 setpcmark();
4380 curwin->w_cursor = *pos;
4381 curwin->w_set_curswant = TRUE;
4382#ifdef FEAT_FOLDING
4383 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4384 && cap->oap->op_type == OP_NOP)
4385 foldOpenCursor();
4386#endif
4387 }
4388}
4389
4390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 * "[" and "]" commands.
4392 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4393 */
4394 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004395nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004398 pos_T *pos = NULL; // init for GCC
4399 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 int flag;
4401 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 cap->oap->motion_type = MCHAR;
4404 cap->oap->inclusive = FALSE;
4405 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004406 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004408 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 if (cap->nchar == 'f')
4410 nv_gotofile(cap);
4411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004414 // Find the occurrence(s) of the identifier or define under cursor
4415 // in current and included files or jump to the first occurrence.
4416 //
4417 // search list jump
4418 // fwd bwd fwd bwd fwd bwd
4419 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4420 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004421 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 char_u *ptr;
4424 int len;
4425
4426 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4427 clearop(cap->oap);
4428 else
4429 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004430 // Make a copy, if the line was changed it will be freed.
4431 ptr = vim_strnsave(ptr, len);
4432 if (ptr == NULL)
4433 return;
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 find_pattern_in_path(ptr, 0, len, TRUE,
4436 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4437 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4438 cap->count1,
4439 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4440 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4441 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4442 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004443 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 curwin->w_set_curswant = TRUE;
4445 }
4446 }
4447 else
4448#endif
4449
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004450 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4451 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4452 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4453 // "[m" or "]m" search for prev/next start of (Java) method.
4454 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if ( (cap->cmdchar == '['
4456 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4457 || (cap->cmdchar == ']'
4458 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004459 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004461 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else if (cap->nchar == '[' || cap->nchar == ']')
4463 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004464 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 flag = '{';
4466 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004467 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004470 // Imitate strange Vi behaviour: When using "]]" with an operator
4471 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004472 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 (cap->oap->op_type != OP_NOP
4474 && cap->arg == FORWARD && flag == '{')))
4475 clearopbeep(cap->oap);
4476 else
4477 {
4478 if (cap->oap->op_type == OP_NOP)
4479 beginline(BL_WHITE | BL_FIX);
4480#ifdef FEAT_FOLDING
4481 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4482 foldOpenCursor();
4483#endif
4484 }
4485 }
4486
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004487 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else if (cap->nchar == 'p' || cap->nchar == 'P')
4489 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004490 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004493 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 else if (cap->nchar == '\'' || cap->nchar == '`')
4495 {
4496 pos = &curwin->w_cursor;
4497 for (n = cap->count1; n > 0; --n)
4498 {
4499 prev_pos = *pos;
4500 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4501 cap->nchar == '\'');
4502 if (pos == NULL)
4503 break;
4504 }
4505 if (pos == NULL)
4506 pos = &prev_pos;
4507 nv_cursormark(cap, cap->nchar == '\'', pos);
4508 }
4509
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004510 // [ or ] followed by a middle mouse click: put selected text with
4511 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004512 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
4514 (void)do_mouse(cap->oap, cap->nchar,
4515 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4516 cap->count1, PUT_FIXINDENT);
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
4519#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004520 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 else if (cap->nchar == 'z')
4522 {
4523 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4524 cap->count1) == FAIL)
4525 clearopbeep(cap->oap);
4526 }
4527#endif
4528
4529#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004530 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 else if (cap->nchar == 'c')
4532 {
4533 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4534 cap->count1) == FAIL)
4535 clearopbeep(cap->oap);
4536 }
4537#endif
4538
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004539#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004540 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004541 else if (cap->nchar == 's' || cap->nchar == 'S')
4542 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004543 setpcmark();
4544 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004545 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4546 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004547 {
4548 clearopbeep(cap->oap);
4549 break;
4550 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004551 else
4552 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004553# ifdef FEAT_FOLDING
4554 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4555 foldOpenCursor();
4556# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004557 }
4558#endif
4559
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004560 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 else
4562 clearopbeep(cap->oap);
4563}
4564
4565/*
4566 * Handle Normal mode "%" command.
4567 */
4568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004569nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004572#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 linenr_T lnum = curwin->w_cursor.lnum;
4574#endif
4575
4576 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004577 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 if (cap->count0 > 100)
4580 clearopbeep(cap->oap);
4581 else
4582 {
4583 cap->oap->motion_type = MLINE;
4584 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004585 // Round up, so 'normal 100%' always jumps at the line line.
4586 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4587 // overflow on 32-bits, so use a formula with less accuracy
4588 // to avoid overflows.
4589 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4591 / 100L * cap->count0;
4592 else
4593 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4594 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004595 if (curwin->w_cursor.lnum < 1)
4596 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4598 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4599 beginline(BL_SOL | BL_FIX);
4600 }
4601 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 cap->oap->motion_type = MCHAR;
4605 cap->oap->use_reg_one = TRUE;
4606 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4607 clearopbeep(cap->oap);
4608 else
4609 {
4610 setpcmark();
4611 curwin->w_cursor = *pos;
4612 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616 }
4617#ifdef FEAT_FOLDING
4618 if (cap->oap->op_type == OP_NOP
4619 && lnum != curwin->w_cursor.lnum
4620 && (fdo_flags & FDO_PERCENT)
4621 && KeyTyped)
4622 foldOpenCursor();
4623#endif
4624}
4625
4626/*
4627 * Handle "(" and ")" commands.
4628 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4629 */
4630 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004631nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 cap->oap->motion_type = MCHAR;
4634 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004635 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004636 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 curwin->w_set_curswant = TRUE;
4638
4639 if (findsent(cap->arg, cap->count1) == FAIL)
4640 clearopbeep(cap->oap);
4641 else
4642 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004644 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#ifdef FEAT_FOLDING
4647 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4648 foldOpenCursor();
4649#endif
4650 }
4651}
4652
4653/*
4654 * "m" command: Mark a position.
4655 */
4656 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004657nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
4659 if (!checkclearop(cap->oap))
4660 {
4661 if (setmark(cap->nchar) == FAIL)
4662 clearopbeep(cap->oap);
4663 }
4664}
4665
4666/*
4667 * "{" and "}" commands.
4668 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4669 */
4670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004671nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 cap->oap->motion_type = MCHAR;
4674 cap->oap->inclusive = FALSE;
4675 cap->oap->use_reg_one = TRUE;
4676 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004677 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 clearopbeep(cap->oap);
4679 else
4680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#ifdef FEAT_FOLDING
4683 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4684 foldOpenCursor();
4685#endif
4686 }
4687}
4688
4689/*
4690 * "u" command: Undo or make lower case.
4691 */
4692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004693nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004695 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004697 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 cap->cmdchar = 'g';
4699 cap->nchar = 'u';
4700 nv_operator(cap);
4701 }
4702 else
4703 nv_kundo(cap);
4704}
4705
4706/*
4707 * <Undo> command.
4708 */
4709 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004710nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 if (!checkclearopq(cap->oap))
4713 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004714#ifdef FEAT_JOB_CHANNEL
4715 if (bt_prompt(curbuf))
4716 {
4717 clearopbeep(cap->oap);
4718 return;
4719 }
4720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 u_undo((int)cap->count1);
4722 curwin->w_set_curswant = TRUE;
4723 }
4724}
4725
4726/*
4727 * Handle the "r" command.
4728 */
4729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004730nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 char_u *ptr;
4733 int had_ctrl_v;
4734 long n;
4735
4736 if (checkclearop(cap->oap))
4737 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004738#ifdef FEAT_JOB_CHANNEL
4739 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4740 {
4741 clearopbeep(cap->oap);
4742 return;
4743 }
4744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (cap->nchar == Ctrl_V)
4748 {
4749 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004750 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004751 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if (cap->nchar > DEL)
4753 had_ctrl_v = NUL;
4754 }
4755 else
4756 had_ctrl_v = NUL;
4757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004758 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004759 if (IS_SPECIAL(cap->nchar))
4760 {
4761 clearopbeep(cap->oap);
4762 return;
4763 }
4764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004765 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (VIsual_active)
4767 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004768 if (got_int)
4769 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004770 if (had_ctrl_v)
4771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004772 // Use a special (negative) number to make a difference between a
4773 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004774 if (cap->nchar == CAR)
4775 cap->nchar = REPLACE_CR_NCHAR;
4776 else if (cap->nchar == NL)
4777 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 nv_operator(cap);
4780 return;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (virtual_active())
4785 {
4786 if (u_save_cursor() == FAIL)
4787 return;
4788 if (gchar_cursor() == NUL)
4789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004790 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 coladvance_force((colnr_T)(getviscol() + cap->count1));
4792 curwin->w_cursor.col -= cap->count1;
4793 }
4794 else if (gchar_cursor() == TAB)
4795 coladvance_force(getviscol());
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004800 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004801 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
4803 clearopbeep(cap->oap);
4804 return;
4805 }
4806
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004807 // Replacing with a TAB is done by edit() when it is complicated because
4808 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4809 // Other characters are done below to avoid problems with things like
4810 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4812 {
4813 stuffnumReadbuff(cap->count1);
4814 stuffcharReadbuff('R');
4815 stuffcharReadbuff('\t');
4816 stuffcharReadbuff(ESC);
4817 return;
4818 }
4819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004820 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (u_save_cursor() == FAIL)
4822 return;
4823
4824 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4825 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004826 // Replace character(s) by a single newline.
4827 // Strange vi behaviour: Only one newline is inserted.
4828 // Delete the characters here.
4829 // Insert the newline with an insert command, takes care of
4830 // autoindent. The insert command depends on being on the last
4831 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004832 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 stuffcharReadbuff('\r');
4834 stuffcharReadbuff(ESC);
4835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 invoke_edit(cap, TRUE, 'r', FALSE);
4838 }
4839 else
4840 {
4841 prep_redo(cap->oap->regname, cap->count1,
4842 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4843
4844 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (has_mbyte)
4846 {
4847 int old_State = State;
4848
4849 if (cap->ncharC1 != 0)
4850 AppendCharToRedobuff(cap->ncharC1);
4851 if (cap->ncharC2 != 0)
4852 AppendCharToRedobuff(cap->ncharC2);
4853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 // This is slow, but it handles replacing a single-byte with a
4855 // multi-byte and the other way around. Also handles adding
4856 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 for (n = cap->count1; n > 0; --n)
4858 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004859 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004860 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4861 {
4862 int c = ins_copychar(curwin->w_cursor.lnum
4863 + (cap->nchar == Ctrl_Y ? -1 : 1));
4864 if (c != NUL)
4865 ins_char(c);
4866 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004867 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004868 ++curwin->w_cursor.col;
4869 }
4870 else
4871 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 State = old_State;
4873 if (cap->ncharC1 != 0)
4874 ins_char(cap->ncharC1);
4875 if (cap->ncharC2 != 0)
4876 ins_char(cap->ncharC2);
4877 }
4878 }
4879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004881 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 for (n = cap->count1; n > 0; --n)
4883 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004884 // Get ptr again, because u_save and/or showmatch() will have
4885 // released the line. This may also happen in ins_copychar().
4886 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004887 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4888 {
4889 int c = ins_copychar(curwin->w_cursor.lnum
4890 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004891
4892 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004893 if (c != NUL)
4894 ptr[curwin->w_cursor.col] = c;
4895 }
4896 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004897 {
4898 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004899 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (p_sm && msg_silent == 0)
4902 showmatch(cap->nchar);
4903 ++curwin->w_cursor.col;
4904 }
4905#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004906 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004908 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar009b2592004-10-24 19:18:58 +00004910 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004911 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004913 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915#endif
4916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004917 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 changed_bytes(curwin->w_cursor.lnum,
4919 (colnr_T)(curwin->w_cursor.col - cap->count1));
4920 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004921 --curwin->w_cursor.col; // cursor on the last replaced char
4922 // if the character on the left of the current cursor is a multi-byte
4923 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 if (has_mbyte)
4925 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 curbuf->b_op_end = curwin->w_cursor;
4927 curwin->w_set_curswant = TRUE;
4928 set_last_insert(cap->nchar);
4929 }
4930}
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932/*
4933 * 'o': Exchange start and end of Visual area.
4934 * 'O': same, but in block mode exchange left and right corners.
4935 */
4936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004937v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 pos_T old_cursor;
4940 colnr_T left, right;
4941
4942 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4943 {
4944 old_cursor = curwin->w_cursor;
4945 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4946 curwin->w_cursor.lnum = VIsual.lnum;
4947 coladvance(left);
4948 VIsual = curwin->w_cursor;
4949
4950 curwin->w_cursor.lnum = old_cursor.lnum;
4951 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 // 'selection "exclusive" and cursor at right-bottom corner: move it
4953 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4955 ++curwin->w_curswant;
4956 coladvance(curwin->w_curswant);
4957 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004959 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 curwin->w_cursor.lnum = VIsual.lnum;
4962 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4963 ++right;
4964 coladvance(right);
4965 VIsual = curwin->w_cursor;
4966
4967 curwin->w_cursor.lnum = old_cursor.lnum;
4968 coladvance(left);
4969 curwin->w_curswant = left;
4970 }
4971 }
4972 else
4973 {
4974 old_cursor = curwin->w_cursor;
4975 curwin->w_cursor = VIsual;
4976 VIsual = old_cursor;
4977 curwin->w_set_curswant = TRUE;
4978 }
4979}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981/*
4982 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4983 */
4984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004985nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004987 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 cap->cmdchar = 'c';
4990 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004991 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 VIsual_mode = 'V';
4993 nv_operator(cap);
4994 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004995 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004998 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
5000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (virtual_active())
5002 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5004 }
5005 }
5006}
5007
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008/*
5009 * "gr".
5010 */
5011 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005012nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (VIsual_active)
5015 {
5016 cap->cmdchar = 'r';
5017 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005020 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005023 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 else
5025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005026 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005027 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 stuffcharReadbuff(cap->extra_char);
5029 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (virtual_active())
5031 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 invoke_edit(cap, TRUE, 'v', FALSE);
5033 }
5034 }
5035}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037/*
5038 * Swap case for "~" command, when it does not work like an operator.
5039 */
5040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005041n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 long n;
5044 pos_T startpos;
5045 int did_change = 0;
5046#ifdef FEAT_NETBEANS_INTG
5047 pos_T pos;
5048 char_u *ptr;
5049 int count;
5050#endif
5051
5052 if (checkclearopq(cap->oap))
5053 return;
5054
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005055 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 clearopbeep(cap->oap);
5058 return;
5059 }
5060
5061 prep_redo_cmd(cap);
5062
5063 if (u_save_cursor() == FAIL)
5064 return;
5065
5066 startpos = curwin->w_cursor;
5067#ifdef FEAT_NETBEANS_INTG
5068 pos = startpos;
5069#endif
5070 for (n = cap->count1; n > 0; --n)
5071 {
5072 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5073 inc_cursor();
5074 if (gchar_cursor() == NUL)
5075 {
5076 if (vim_strchr(p_ww, '~') != NULL
5077 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5078 {
5079#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005080 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 if (did_change)
5083 {
5084 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005085 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005086 netbeans_removed(curbuf, pos.lnum, pos.col,
5087 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005088 // line may have been flushed, get it again
5089 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005091 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 pos.col = 0;
5094 pos.lnum++;
5095 }
5096#endif
5097 ++curwin->w_cursor.lnum;
5098 curwin->w_cursor.col = 0;
5099 if (n > 1)
5100 {
5101 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5102 break;
5103 u_clearline();
5104 }
5105 }
5106 else
5107 break;
5108 }
5109 }
5110#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005111 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 ptr = ml_get(pos.lnum);
5114 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005115 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5116 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118#endif
5119
5120
5121 check_cursor();
5122 curwin->w_set_curswant = TRUE;
5123 if (did_change)
5124 {
5125 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5126 0L);
5127 curbuf->b_op_start = startpos;
5128 curbuf->b_op_end = curwin->w_cursor;
5129 if (curbuf->b_op_end.col > 0)
5130 --curbuf->b_op_end.col;
5131 }
5132}
5133
5134/*
5135 * Move cursor to mark.
5136 */
5137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005138nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
5140 if (check_mark(pos) == FAIL)
5141 clearop(cap->oap);
5142 else
5143 {
5144 if (cap->cmdchar == '\''
5145 || cap->cmdchar == '`'
5146 || cap->cmdchar == '['
5147 || cap->cmdchar == ']')
5148 setpcmark();
5149 curwin->w_cursor = *pos;
5150 if (flag)
5151 beginline(BL_WHITE | BL_FIX);
5152 else
5153 check_cursor();
5154 }
5155 cap->oap->motion_type = flag ? MLINE : MCHAR;
5156 if (cap->cmdchar == '`')
5157 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005158 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 curwin->w_set_curswant = TRUE;
5160}
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162/*
5163 * Handle commands that are operators in Visual mode.
5164 */
5165 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005166v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 // Uppercase means linewise, except in block mode, then "D" deletes till
5171 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (isupper(cap->cmdchar))
5173 {
5174 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005175 {
5176 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5180 curwin->w_curswant = MAXCOL;
5181 }
5182 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5183 nv_operator(cap);
5184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186/*
5187 * "s" and "S" commands.
5188 */
5189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005192#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005193 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005194 if (term_swap_diff() == OK)
5195 return;
5196#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005197#ifdef FEAT_JOB_CHANNEL
5198 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5199 {
5200 clearopbeep(cap->oap);
5201 return;
5202 }
5203#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005204 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005207 {
5208 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 cap->cmdchar = 'c';
5212 nv_operator(cap);
5213 }
5214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 nv_optrans(cap);
5216}
5217
5218/*
5219 * Abbreviated commands.
5220 */
5221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005225 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005227 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (VIsual_active)
5229 v_visop(cap);
5230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 nv_optrans(cap);
5232}
5233
5234/*
5235 * Translate a command into another command.
5236 */
5237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5241 (char_u *)"d$", (char_u *)"c$",
5242 (char_u *)"cl", (char_u *)"cc",
5243 (char_u *)"yy", (char_u *)":s\r"};
5244 static char_u *str = (char_u *)"xXDCsSY&";
5245
5246 if (!checkclearopq(cap->oap))
5247 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005248 // In Vi "2D" doesn't delete the next line. Can't translate it
5249 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005250 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5251 {
5252 cap->oap->start = curwin->w_cursor;
5253 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005254#ifdef FEAT_EVAL
5255 set_op_var(OP_DELETE);
5256#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005257 cap->count1 = 1;
5258 nv_dollar(cap);
5259 finish_op = TRUE;
5260 ResetRedobuff();
5261 AppendCharToRedobuff('D');
5262 }
5263 else
5264 {
5265 if (cap->count0)
5266 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005267 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270 cap->opcount = 0;
5271}
5272
5273/*
5274 * "'" and "`" commands. Also for "g'" and "g`".
5275 * cap->arg is TRUE for "'" and "g'".
5276 */
5277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 pos_T *pos;
5281 int c;
5282#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005283 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005284 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
5286
5287 if (cap->cmdchar == 'g')
5288 c = cap->extra_char;
5289 else
5290 c = cap->nchar;
5291 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005292 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 if (cap->arg)
5295 {
5296 check_cursor_lnum();
5297 beginline(BL_WHITE | BL_FIX);
5298 }
5299 else
5300 check_cursor();
5301 }
5302 else
5303 nv_cursormark(cap, cap->arg, pos);
5304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005305 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (!virtual_active())
5307 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005308 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309#ifdef FEAT_FOLDING
5310 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005311 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005312 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 && (fdo_flags & FDO_MARK)
5314 && old_KeyTyped)
5315 foldOpenCursor();
5316#endif
5317}
5318
5319/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005320 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 */
5322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005323nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005326#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 if (!checkclearopq(cap->oap))
5332 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005333 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5334 {
5335 if (goto_tabpage_lastused() == FAIL)
5336 clearopbeep(cap->oap);
5337 return;
5338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (cap->cmdchar == 'g')
5340 pos = movechangelist((int)cap->count1);
5341 else
5342 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
5345 curwin->w_set_curswant = TRUE;
5346 check_cursor();
5347 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005348 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 nv_cursormark(cap, FALSE, pos);
5350 else if (cap->cmdchar == 'g')
5351 {
5352 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005353 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005355 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005357 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 else
5360 clearopbeep(cap->oap);
5361# ifdef FEAT_FOLDING
5362 if (cap->oap->op_type == OP_NOP
5363 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5364 && (fdo_flags & FDO_MARK)
5365 && old_KeyTyped)
5366 foldOpenCursor();
5367# endif
5368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369}
5370
5371/*
5372 * Handle '"' command.
5373 */
5374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005375nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 if (checkclearop(cap->oap))
5378 return;
5379#ifdef FEAT_EVAL
5380 if (cap->nchar == '=')
5381 cap->nchar = get_expr_register();
5382#endif
5383 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5384 {
5385 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005386 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#ifdef FEAT_EVAL
5388 set_reg_var(cap->oap->regname);
5389#endif
5390 }
5391 else
5392 clearopbeep(cap->oap);
5393}
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395/*
5396 * Handle "v", "V" and "CTRL-V" commands.
5397 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5398 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005399 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 */
5401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005402nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005404 if (cap->cmdchar == Ctrl_Q)
5405 cap->cmdchar = Ctrl_V;
5406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005407 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5408 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 if (cap->oap->op_type != OP_NOP)
5410 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005411 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005412 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return;
5414 }
5415
5416 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005417 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005421 else // toggle char/block mode
5422 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 VIsual_mode = cap->cmdchar;
5424 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005425 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005427 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005432 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 VIsual = curwin->w_cursor;
5436
5437 VIsual_active = TRUE;
5438 VIsual_reselect = TRUE;
5439 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005443 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005445 // For V and ^V, we multiply the number of lines even if there
5446 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5448 {
5449 curwin->w_cursor.lnum +=
5450 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005451 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 VIsual_mode = resel_VIsual_mode;
5454 if (VIsual_mode == 'v')
5455 {
5456 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005457 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005458 update_curswant_force();
5459 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005462 curwin->w_curswant = resel_VIsual_vcol;
5463 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005465 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 curwin->w_curswant = MAXCOL;
5468 coladvance((colnr_T)MAXCOL);
5469 }
5470 else if (VIsual_mode == Ctrl_V)
5471 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005472 update_curswant_force();
5473 curwin->w_curswant += + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 coladvance(curwin->w_curswant);
5475 }
5476 else
5477 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005478 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480 else
5481 {
5482 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005483 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 may_start_select('c');
5485 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005486 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005488 if (cap->count0 > 0 && --cap->count1 > 0)
5489 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005490 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005491 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5492 nv_right(cap);
5493 else if (VIsual_mode == 'V')
5494 nv_down(cap);
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 }
5498}
5499
5500/*
5501 * Start selection for Shift-movement keys.
5502 */
5503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005506 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 may_start_select('k');
5508 n_start_visual_mode('v');
5509}
5510
5511/*
5512 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005513 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 */
5515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005516may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005518 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5519 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520}
5521
5522/*
5523 * Start Visual mode "c".
5524 * Should set VIsual_select before calling this.
5525 */
5526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005529#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005530 int cursor_line_was_concealed = curwin->w_p_cole > 0
5531 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005532#endif
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 VIsual_mode = c;
5535 VIsual_active = TRUE;
5536 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005537
5538 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005539 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005540 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005541 {
5542 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 VIsual = curwin->w_cursor;
5546
5547#ifdef FEAT_FOLDING
5548 foldAdjustVisual();
5549#endif
5550
LemonBoy2bf52dd2022-04-09 18:17:34 +01005551 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005553#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005554 // Check if redraw is needed after changing the state.
5555 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005556#endif
5557
Bram Moolenaar09df3122006-01-23 22:23:09 +00005558 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005559 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 // Make sure the clipboard gets updated. Needed because start and
5562 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 clip_star.vmode = NUL;
5564#endif
5565
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 // Only need to redraw this line, unless still need to redraw an old
5567 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005568 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
5570 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5571 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5572 }
5573}
5574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576/*
5577 * CTRL-W: Window commands
5578 */
5579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005580nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005582 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005583 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005584 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005585 cap->cmdchar = ':';
5586 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005587 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005588 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005589 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005590 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
5593/*
5594 * CTRL-Z: Suspend
5595 */
5596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005597nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005601 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005602 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005606 * "gv": Reselect the previous Visual area. If Visual already active,
5607 * exchange previous and current Visual area.
5608 */
5609 static void
5610nv_gv_cmd(cmdarg_T *cap)
5611{
5612 pos_T tpos;
5613 int i;
5614
5615 if (checkclearop(cap->oap))
5616 return;
5617
5618 if (curbuf->b_visual.vi_start.lnum == 0
5619 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5620 || curbuf->b_visual.vi_end.lnum == 0)
5621 {
5622 beep_flush();
5623 return;
5624 }
5625
5626 // set w_cursor to the start of the Visual area, tpos to the end
5627 if (VIsual_active)
5628 {
5629 i = VIsual_mode;
5630 VIsual_mode = curbuf->b_visual.vi_mode;
5631 curbuf->b_visual.vi_mode = i;
5632# ifdef FEAT_EVAL
5633 curbuf->b_visual_mode_eval = i;
5634# endif
5635 i = curwin->w_curswant;
5636 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5637 curbuf->b_visual.vi_curswant = i;
5638
5639 tpos = curbuf->b_visual.vi_end;
5640 curbuf->b_visual.vi_end = curwin->w_cursor;
5641 curwin->w_cursor = curbuf->b_visual.vi_start;
5642 curbuf->b_visual.vi_start = VIsual;
5643 }
5644 else
5645 {
5646 VIsual_mode = curbuf->b_visual.vi_mode;
5647 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5648 tpos = curbuf->b_visual.vi_end;
5649 curwin->w_cursor = curbuf->b_visual.vi_start;
5650 }
5651
5652 VIsual_active = TRUE;
5653 VIsual_reselect = TRUE;
5654
5655 // Set Visual to the start and w_cursor to the end of the Visual
5656 // area. Make sure they are on an existing character.
5657 check_cursor();
5658 VIsual = curwin->w_cursor;
5659 curwin->w_cursor = tpos;
5660 check_cursor();
5661 update_topline();
5662
5663 // When called from normal "g" command: start Select mode when
5664 // 'selectmode' contains "cmd". When called for K_SELECT, always
5665 // start Select mode.
5666 if (cap->arg)
5667 {
5668 VIsual_select = TRUE;
5669 VIsual_select_reg = 0;
5670 }
5671 else
5672 may_start_select('c');
5673 setmouse();
5674#ifdef FEAT_CLIPBOARD
5675 // Make sure the clipboard gets updated. Needed because start and
5676 // end are still the same, and the selection needs to be owned
5677 clip_star.vmode = NUL;
5678#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005679 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005680 showmode();
5681}
5682
5683/*
5684 * "g0", "g^" : Like "0" and "^" but for screen lines.
5685 * "gm": middle of "g0" and "g$".
5686 */
5687 static void
5688nv_g_home_m_cmd(cmdarg_T *cap)
5689{
5690 int i;
5691 int flag = FALSE;
5692
5693 if (cap->nchar == '^')
5694 flag = TRUE;
5695
5696 cap->oap->motion_type = MCHAR;
5697 cap->oap->inclusive = FALSE;
5698 if (curwin->w_p_wrap && curwin->w_width != 0)
5699 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005700 int width1 = curwin->w_width - curwin_col_off();
5701 int width2 = width1 + curwin_col_off2();
5702 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005703
5704 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005705 virtcol = curwin->w_virtcol
5706#ifdef FEAT_PROP_POPUP
5707 - curwin->w_virtcol_first_char
5708#endif
5709 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005710 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005711 if (virtcol >= (colnr_T)width1 && width2 > 0)
5712 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005713 }
5714 else
5715 i = curwin->w_leftcol;
5716 // Go to the middle of the screen line. When 'number' or
5717 // 'relativenumber' is on and lines are wrapping the middle can be more
5718 // to the left.
5719 if (cap->nchar == 'm')
5720 i += (curwin->w_width - curwin_col_off()
5721 + ((curwin->w_p_wrap && i > 0)
5722 ? curwin_col_off2() : 0)) / 2;
5723 coladvance((colnr_T)i);
5724 if (flag)
5725 {
5726 do
5727 i = gchar_cursor();
5728 while (VIM_ISWHITE(i) && oneright() == OK);
5729 curwin->w_valid &= ~VALID_WCOL;
5730 }
5731 curwin->w_set_curswant = TRUE;
5732}
5733
5734/*
5735 * "g_": to the last non-blank character in the line or <count> lines
5736 * downward.
5737 */
5738 static void
5739nv_g_underscore_cmd(cmdarg_T *cap)
5740{
5741 char_u *ptr;
5742
5743 cap->oap->motion_type = MCHAR;
5744 cap->oap->inclusive = TRUE;
5745 curwin->w_curswant = MAXCOL;
5746 if (cursor_down((long)(cap->count1 - 1),
5747 cap->oap->op_type == OP_NOP) == FAIL)
5748 {
5749 clearopbeep(cap->oap);
5750 return;
5751 }
5752
5753 ptr = ml_get_curline();
5754
5755 // In Visual mode we may end up after the line.
5756 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5757 --curwin->w_cursor.col;
5758
5759 // Decrease the cursor column until it's on a non-blank.
5760 while (curwin->w_cursor.col > 0
5761 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5762 --curwin->w_cursor.col;
5763 curwin->w_set_curswant = TRUE;
5764 adjust_for_sel(cap);
5765}
5766
5767/*
5768 * "g$" : Like "$" but for screen lines.
5769 */
5770 static void
5771nv_g_dollar_cmd(cmdarg_T *cap)
5772{
5773 oparg_T *oap = cap->oap;
5774 int i;
5775 int col_off = curwin_col_off();
5776
5777 oap->motion_type = MCHAR;
5778 oap->inclusive = TRUE;
5779 if (curwin->w_p_wrap && curwin->w_width != 0)
5780 {
5781 curwin->w_curswant = MAXCOL; // so we stay at the end
5782 if (cap->count1 == 1)
5783 {
5784 int width1 = curwin->w_width - col_off;
5785 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005786 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005787
5788 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005789 virtcol = curwin->w_virtcol
5790#ifdef FEAT_PROP_POPUP
5791 - curwin->w_virtcol_first_char
5792#endif
5793 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005794 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005795 if (virtcol >= (colnr_T)width1)
5796 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005797 * width2;
5798 coladvance((colnr_T)i);
5799
5800 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005801 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005802 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5803 {
5804 // Check for landing on a character that got split at
5805 // the end of the line. We do not want to advance to
5806 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005807 if (curwin->w_virtcol
5808#ifdef FEAT_PROP_POPUP
5809 - curwin->w_virtcol_first_char
5810#endif
5811 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005812 --curwin->w_cursor.col;
5813 }
5814 }
5815 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5816 clearopbeep(oap);
5817 }
5818 else
5819 {
5820 if (cap->count1 > 1)
5821 // if it fails, let the cursor still move to the last char
5822 (void)cursor_down(cap->count1 - 1, FALSE);
5823
5824 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5825 coladvance((colnr_T)i);
5826
5827 // if the character doesn't fit move one back
5828 if (curwin->w_cursor.col > 0
5829 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5830 {
5831 colnr_T vcol;
5832
5833 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5834 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5835 --curwin->w_cursor.col;
5836 }
5837
5838 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005839 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005840 }
5841}
5842
5843/*
5844 * "gi": start Insert at the last position.
5845 */
5846 static void
5847nv_gi_cmd(cmdarg_T *cap)
5848{
5849 int i;
5850
5851 if (curbuf->b_last_insert.lnum != 0)
5852 {
5853 curwin->w_cursor = curbuf->b_last_insert;
5854 check_cursor_lnum();
5855 i = (int)STRLEN(ml_get_curline());
5856 if (curwin->w_cursor.col > (colnr_T)i)
5857 {
5858 if (virtual_active())
5859 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5860 curwin->w_cursor.col = i;
5861 }
5862 }
5863 cap->cmdchar = 'i';
5864 nv_edit(cap);
5865}
5866
5867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 * Commands starting with "g".
5869 */
5870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005871nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 switch (cap->nchar)
5877 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005878 case Ctrl_A:
5879 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005881 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005882 if (!VIsual_active && cap->nchar == Ctrl_A)
5883 vim_mem_profile_dump();
5884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005886 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005887 if (VIsual_active)
5888 {
5889 cap->arg = TRUE;
5890 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005891 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005892 nv_addsub(cap);
5893 }
5894 else
5895 clearopbeep(oap);
5896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005898 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 case 'R':
5900 cap->arg = TRUE;
5901 nv_Replace(cap);
5902 break;
5903
5904 case 'r':
5905 nv_vreplace(cap);
5906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
5908 case '&':
5909 do_cmdline_cmd((char_u *)"%s//~/&");
5910 break;
5911
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005912 // "gv": Reselect the previous Visual area. If Visual already active,
5913 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005915 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005917
5918 // "gV": Don't reselect the previous Visual area after a Select mode
5919 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 case 'V':
5921 VIsual_reselect = FALSE;
5922 break;
5923
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005924 // "gh": start Select mode.
5925 // "gH": start Select line mode.
5926 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 case K_BS:
5928 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005929 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 case 'h':
5931 case 'H':
5932 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 cap->cmdchar = cap->nchar + ('v' - 'h');
5934 cap->arg = TRUE;
5935 nv_visual(cap);
5936 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005938 // "gn", "gN" visually select next/previous search match
5939 // "gn" selects next match
5940 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005941 case 'N':
5942 case 'n':
5943 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005944 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005947 // "gj" and "gk" two new funny movement keys -- up and down
5948 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case 'j':
5950 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005951 // with 'nowrap' it works just like the normal "j" command.
5952 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 oap->motion_type = MLINE;
5955 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5956 }
5957 else
5958 i = nv_screengo(oap, FORWARD, cap->count1);
5959 if (i == FAIL)
5960 clearopbeep(oap);
5961 break;
5962
5963 case 'k':
5964 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005965 // with 'nowrap' it works just like the normal "k" command.
5966 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 oap->motion_type = MLINE;
5969 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5970 }
5971 else
5972 i = nv_screengo(oap, BACKWARD, cap->count1);
5973 if (i == FAIL)
5974 clearopbeep(oap);
5975 break;
5976
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005977 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 case 'J':
5979 nv_join(cap);
5980 break;
5981
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005982 // "g0", "g^" : Like "0" and "^" but for screen lines.
5983 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 case '0':
5986 case 'm':
5987 case K_HOME:
5988 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005989 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 break;
5991
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005992 case 'M':
5993 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005994 oap->motion_type = MCHAR;
5995 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01005996 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005997 if (cap->count0 > 0 && cap->count0 <= 100)
5998 coladvance((colnr_T)(i * cap->count0 / 100));
5999 else
6000 coladvance((colnr_T)(i / 2));
6001 curwin->w_set_curswant = TRUE;
6002 }
6003 break;
6004
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006005 // "g_": to the last non-blank character in the line or <count> lines
6006 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 break;
6010
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006011 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 case '$':
6013 case K_END:
6014 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006015 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 break;
6017
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006018 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 case '*':
6020 case '#':
6021#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006022 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006024 case Ctrl_RSB: // :tag or :tselect for current identifier
6025 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 nv_ident(cap);
6027 break;
6028
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006029 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 case 'e':
6031 case 'E':
6032 oap->motion_type = MCHAR;
6033 curwin->w_set_curswant = TRUE;
6034 oap->inclusive = TRUE;
6035 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6036 clearopbeep(oap);
6037 break;
6038
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006039 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006041 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 break;
6043
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006044 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006046 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 break;
6048
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006049 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 case 'I':
6051 beginline(0);
6052 if (!checkclearopq(oap))
6053 invoke_edit(cap, FALSE, 'g', FALSE);
6054 break;
6055
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006056 // "gf": goto file, edit file under cursor
6057 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006059 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 nv_gotofile(cap);
6061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case '\'':
6065 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006066 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 case '`':
6068 nv_gomark(cap);
6069 break;
6070
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006071 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006073 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 break;
6075
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006076 // "ga": Display the ascii value of the character under the
6077 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case 'a':
6079 do_ascii(NULL);
6080 break;
6081
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "g8": Display the bytes used for the UTF-8 character under the
6083 // cursor. It is displayed in hex.
6084 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006086 if (cap->count0 == 8)
6087 utf_find_illegal();
6088 else
6089 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006092 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006093 case '<':
6094 show_sb_text();
6095 break;
6096
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006097 // "gg": Goto the first line in file. With a count it goes to
6098 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 case 'g':
6100 cap->arg = FALSE;
6101 nv_goto(cap);
6102 break;
6103
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006104 // Two-character operators:
6105 // "gq" Format text
6106 // "gw" Format text and keep cursor position
6107 // "g~" Toggle the case of the text.
6108 // "gu" Change text to lower case.
6109 // "gU" Change text to upper case.
6110 // "g?" rot13 encoding
6111 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case 'q':
6113 case 'w':
6114 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006115 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 case '~':
6117 case 'u':
6118 case 'U':
6119 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006120 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 nv_operator(cap);
6122 break;
6123
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006124 // "gd": Find first occurrence of pattern under the cursor in the
6125 // current function
6126 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 case 'd':
6128 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006129 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 break;
6131
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006132 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 case K_MIDDLEMOUSE:
6134 case K_MIDDLEDRAG:
6135 case K_MIDDLERELEASE:
6136 case K_LEFTMOUSE:
6137 case K_LEFTDRAG:
6138 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006139 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 case K_RIGHTMOUSE:
6141 case K_RIGHTDRAG:
6142 case K_RIGHTRELEASE:
6143 case K_X1MOUSE:
6144 case K_X1DRAG:
6145 case K_X1RELEASE:
6146 case K_X2MOUSE:
6147 case K_X2DRAG:
6148 case K_X2RELEASE:
6149 mod_mask = MOD_MASK_CTRL;
6150 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6151 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153 case K_IGNORE:
6154 break;
6155
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006156 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 case 'p':
6158 case 'P':
6159 nv_put(cap);
6160 break;
6161
6162#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006163 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 case 'o':
6165 goto_byte(cap->count0);
6166 break;
6167#endif
6168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006169 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006171 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 do_exmode(TRUE);
6173 break;
6174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 case ',':
6176 nv_pcmark(cap);
6177 break;
6178
6179 case ';':
6180 cap->count1 = -cap->count1;
6181 nv_pcmark(cap);
6182 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006185 if (!checkclearop(oap))
6186 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006187 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006188 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006189 if (!checkclearop(oap))
6190 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006191 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192
Bram Moolenaar62a23252020-08-09 14:04:42 +02006193 case TAB:
6194 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6195 clearopbeep(oap);
6196 break;
6197
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006198 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006199 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006200 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006201 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006202 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006203 break;
6204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 default:
6206 clearopbeep(oap);
6207 break;
6208 }
6209}
6210
6211/*
6212 * Handle "o" and "O" commands.
6213 */
6214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006215n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006217#ifdef FEAT_CONCEAL
6218 linenr_T oldline = curwin->w_cursor.lnum;
6219#endif
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (!checkclearopq(cap->oap))
6222 {
6223#ifdef FEAT_FOLDING
6224 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006225 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 (void)hasFolding(curwin->w_cursor.lnum,
6227 &curwin->w_cursor.lnum, NULL);
6228 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006229 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 (void)hasFolding(curwin->w_cursor.lnum,
6231 NULL, &curwin->w_cursor.lnum);
6232#endif
6233 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6234 (cap->cmdchar == 'O' ? 1 : 0)),
6235 (linenr_T)(curwin->w_cursor.lnum +
6236 (cap->cmdchar == 'o' ? 1 : 0))
6237 ) == OK
6238 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006239 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006240 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006242#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006243 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006244 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006245#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006246#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006247 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006248 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006249 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006250#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006251 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006252 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6253 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6255 }
6256 }
6257}
6258
6259/*
6260 * "." command: redo last change.
6261 */
6262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006263nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 if (!checkclearopq(cap->oap))
6266 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006267 // If "restart_edit" is TRUE, the last but one command is repeated
6268 // instead of the last command (inserting text). This is used for
6269 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6271 clearopbeep(cap->oap);
6272 }
6273}
6274
6275/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006276 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 */
6278 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006279nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006281 if (VIsual_select && VIsual_active)
6282 {
6283 int reg;
6284 // Get register name
6285 ++no_mapping;
6286 ++allow_keys;
6287 reg = plain_vgetc();
6288 LANGMAP_ADJUST(reg, TRUE);
6289 --no_mapping;
6290 --allow_keys;
6291
6292 if (reg == '"')
6293 // the unnamed register is 0
6294 reg = 0;
6295
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006296 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006297 return;
6298 }
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (!checkclearopq(cap->oap))
6301 {
6302 u_redo((int)cap->count1);
6303 curwin->w_set_curswant = TRUE;
6304 }
6305}
6306
6307/*
6308 * Handle "U" command.
6309 */
6310 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006311nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006313 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006314 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006316 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 cap->cmdchar = 'g';
6318 cap->nchar = 'U';
6319 nv_operator(cap);
6320 }
6321 else if (!checkclearopq(cap->oap))
6322 {
6323 u_undoline();
6324 curwin->w_set_curswant = TRUE;
6325 }
6326}
6327
6328/*
6329 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6330 * single character.
6331 */
6332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006333nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006335 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006336 {
6337#ifdef FEAT_JOB_CHANNEL
6338 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6339 {
6340 clearopbeep(cap->oap);
6341 return;
6342 }
6343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 else
6347 nv_operator(cap);
6348}
6349
6350/*
6351 * Handle an operator command.
6352 * The actual work is done by do_pending_operator().
6353 */
6354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006355nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
6357 int op_type;
6358
6359 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006360#ifdef FEAT_JOB_CHANNEL
6361 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6362 {
6363 clearopbeep(cap->oap);
6364 return;
6365 }
6366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006368 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 nv_lineop(cap);
6370 else if (!checkclearop(cap->oap))
6371 {
6372 cap->oap->start = curwin->w_cursor;
6373 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006374#ifdef FEAT_EVAL
6375 set_op_var(op_type);
6376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378}
6379
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006380#ifdef FEAT_EVAL
6381/*
6382 * Set v:operator to the characters for "optype".
6383 */
6384 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006385set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006386{
6387 char_u opchars[3];
6388
6389 if (optype == OP_NOP)
6390 set_vim_var_string(VV_OP, NULL, 0);
6391 else
6392 {
6393 opchars[0] = get_op_char(optype);
6394 opchars[1] = get_extra_op_char(optype);
6395 opchars[2] = NUL;
6396 set_vim_var_string(VV_OP, opchars, -1);
6397 }
6398}
6399#endif
6400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401/*
6402 * Handle linewise operator "dd", "yy", etc.
6403 *
6404 * "_" is is a strange motion command that helps make operators more logical.
6405 * It is actually implemented, but not documented in the real Vi. This motion
6406 * command actually refers to "the current line". Commands like "dd" and "yy"
6407 * are really an alternate form of "d_" and "y_". It does accept a count, so
6408 * "d3_" works to delete 3 lines.
6409 */
6410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006411nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 cap->oap->motion_type = MLINE;
6414 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6415 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006417 && cap->oap->motion_force != 'v'
6418 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 || cap->oap->op_type == OP_LSHIFT
6420 || cap->oap->op_type == OP_RSHIFT)
6421 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006422 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 beginline(BL_WHITE | BL_FIX);
6424}
6425
6426/*
6427 * <Home> command.
6428 */
6429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006430nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006432 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006433 if (mod_mask & MOD_MASK_CTRL)
6434 nv_goto(cap);
6435 else
6436 {
6437 cap->count0 = 1;
6438 nv_pipe(cap);
6439 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006440 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6441 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442}
6443
6444/*
6445 * "|" command.
6446 */
6447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 cap->oap->motion_type = MCHAR;
6451 cap->oap->inclusive = FALSE;
6452 beginline(0);
6453 if (cap->count0 > 0)
6454 {
6455 coladvance((colnr_T)(cap->count0 - 1));
6456 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6457 }
6458 else
6459 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006460 // keep curswant at the column where we wanted to go, not where
6461 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 curwin->w_set_curswant = FALSE;
6463}
6464
6465/*
6466 * Handle back-word command "b" and "B".
6467 * cap->arg is 1 for "B"
6468 */
6469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006470nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 cap->oap->motion_type = MCHAR;
6473 cap->oap->inclusive = FALSE;
6474 curwin->w_set_curswant = TRUE;
6475 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6476 clearopbeep(cap->oap);
6477#ifdef FEAT_FOLDING
6478 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6479 foldOpenCursor();
6480#endif
6481}
6482
6483/*
6484 * Handle word motion commands "e", "E", "w" and "W".
6485 * cap->arg is TRUE for "E" and "W".
6486 */
6487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006488nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 int n;
6491 int word_end;
6492 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006493 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006495 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6497 word_end = TRUE;
6498 else
6499 word_end = FALSE;
6500 cap->oap->inclusive = word_end;
6501
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006502 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (!word_end && cap->oap->op_type == OP_CHANGE)
6504 {
6505 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006506 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006508 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006510 // Reproduce a funny Vi behaviour: "cw" on a blank only
6511 // changes one character, not all blanks until the start of
6512 // the next word. Only do this when the 'w' flag is included
6513 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6515 {
6516 cap->oap->inclusive = TRUE;
6517 cap->oap->motion_type = MCHAR;
6518 return;
6519 }
6520 }
6521 else
6522 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006523 // This is a little strange. To match what the real Vi does,
6524 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6525 // that we are not on a space or a TAB. This seems impolite
6526 // at first, but it's really more what we mean when we say
6527 // 'cw'.
6528 // Another strangeness: When standing on the end of a word
6529 // "ce" will change until the end of the next word, but "cw"
6530 // will change only one character! This is done by setting
6531 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 cap->oap->inclusive = TRUE;
6533 word_end = TRUE;
6534 flag = TRUE;
6535 }
6536 }
6537 }
6538
6539 cap->oap->motion_type = MCHAR;
6540 curwin->w_set_curswant = TRUE;
6541 if (word_end)
6542 n = end_word(cap->count1, cap->arg, flag, FALSE);
6543 else
6544 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6545
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006546 // Don't leave the cursor on the NUL past the end of line. Unless we
6547 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006548 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006549 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551 if (n == FAIL && cap->oap->op_type == OP_NOP)
6552 clearopbeep(cap->oap);
6553 else
6554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556#ifdef FEAT_FOLDING
6557 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6558 foldOpenCursor();
6559#endif
6560 }
6561}
6562
6563/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006564 * Used after a movement command: If the cursor ends up on the NUL after the
6565 * end of the line, may move it back to the last character and make the motion
6566 * inclusive.
6567 */
6568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006569adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006570{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006571 // The cursor cannot remain on the NUL when:
6572 // - the column is > 0
6573 // - not in Visual mode or 'selection' is "o"
6574 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006575 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006576 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006577 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006578 {
6579 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006580 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006581 if (has_mbyte)
6582 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006583 oap->inclusive = TRUE;
6584 }
6585}
6586
6587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 * "0" and "^" commands.
6589 * cap->arg is the argument for beginline().
6590 */
6591 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006592nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 cap->oap->motion_type = MCHAR;
6595 cap->oap->inclusive = FALSE;
6596 beginline(cap->arg);
6597#ifdef FEAT_FOLDING
6598 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6599 foldOpenCursor();
6600#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6602 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605/*
6606 * In exclusive Visual mode, may include the last character.
6607 */
6608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006609adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006612 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if (has_mbyte)
6615 inc_cursor();
6616 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 ++curwin->w_cursor.col;
6618 cap->oap->inclusive = FALSE;
6619 }
6620}
6621
6622/*
6623 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6624 * Should check VIsual_mode before calling this.
6625 * Returns TRUE when backed up to the previous line.
6626 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006627 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 pos_T *pp;
6631
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006632 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006634 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 pp = &curwin->w_cursor;
6636 else
6637 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 if (pp->coladd > 0)
6639 --pp->coladd;
6640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (pp->col > 0)
6642 {
6643 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006644 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646 else if (pp->lnum > 1)
6647 {
6648 --pp->lnum;
6649 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6650 return TRUE;
6651 }
6652 }
6653 return FALSE;
6654}
6655
6656/*
6657 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6658 */
6659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006660nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006665 VIsual_select_reg = 0;
6666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 else if (VIsual_reselect)
6668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006669 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 cap->arg = TRUE;
6671 nv_g_cmd(cap);
6672 }
6673}
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
6676/*
6677 * "G", "gg", CTRL-END, CTRL-HOME.
6678 * cap->arg is TRUE for "G".
6679 */
6680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006681nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 linenr_T lnum;
6684
6685 if (cap->arg)
6686 lnum = curbuf->b_ml.ml_line_count;
6687 else
6688 lnum = 1L;
6689 cap->oap->motion_type = MLINE;
6690 setpcmark();
6691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006692 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (cap->count0 != 0)
6694 lnum = cap->count0;
6695 if (lnum < 1L)
6696 lnum = 1L;
6697 else if (lnum > curbuf->b_ml.ml_line_count)
6698 lnum = curbuf->b_ml.ml_line_count;
6699 curwin->w_cursor.lnum = lnum;
6700 beginline(BL_SOL | BL_FIX);
6701#ifdef FEAT_FOLDING
6702 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6703 foldOpenCursor();
6704#endif
6705}
6706
6707/*
6708 * CTRL-\ in Normal mode.
6709 */
6710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006711nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6714 {
6715 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006716 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006717 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (cmdwin_type != 0)
6720 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (VIsual_active)
6722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006723 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006724 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006726 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (cap->nchar == Ctrl_G && p_im)
6728 restart_edit = 'a';
6729 }
6730 else
6731 clearopbeep(cap->oap);
6732}
6733
6734/*
6735 * ESC in Normal mode: beep, but don't flush buffers.
6736 * Don't even beep if we are canceling a command.
6737 */
6738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006739nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 int no_reason;
6742
6743 no_reason = (cap->oap->op_type == OP_NOP
6744 && cap->opcount == 0
6745 && cap->count0 == 0
6746 && cap->oap->regname == 0
6747 && !p_im);
6748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006749 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006751 if (restart_edit == 0 && cmdwin_type == 0
6752 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006753 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006754 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6755
Bram Moolenaar5939c352022-09-24 12:50:45 +01006756 // The user may accidentally do "vim file | grep word" and then
6757 // CTRL-C doesn't show anything. With a changed buffer give the
6758 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006759 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006760 {
6761 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6762
6763 if (out_redir)
6764 mch_errmsg(ms);
6765 else
6766 msg(ms);
6767 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006768 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006769 {
6770 if (out_redir)
6771 {
6772 got_int = FALSE;
6773 do_cmdline_cmd((char_u *)"qa");
6774 }
6775 else
6776 msg(_("Type :qa and press <Enter> to exit Vim"));
6777 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006780 if (restart_edit != 0)
6781 redraw_mode = TRUE; // remove "-- (insert) --"
6782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006783 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6784 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (!p_im)
6786 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (cmdwin_type != 0)
6788 {
6789 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 return;
6792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006794 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006795 {
6796 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006797 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6798 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006799 cmdwin_result = K_IGNORE;
6800 return;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (VIsual_active)
6804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 end_visual_mode(); // stop Visual
6806 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006808 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006810 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006811 {
6812#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006813 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006814 popup_hide_message_win();
6815 else
6816#endif
6817 vim_beep(BO_ESC);
6818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 clearop(cap->oap);
6820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6822 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006823 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 restart_edit = 'a';
6825}
6826
6827/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006828 * Move the cursor for the "A" command.
6829 */
6830 void
6831set_cursor_for_append_to_line(void)
6832{
6833 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006834 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006835 {
6836 int save_State = State;
6837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006838 // Pretend Insert mode here to allow the cursor on the
6839 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006840 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006841 coladvance((colnr_T)MAXCOL);
6842 State = save_State;
6843 }
6844 else
6845 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6846}
6847
6848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006850 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 */
6852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006853nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006855 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6857 cap->cmdchar = 'i';
6858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006859 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006861 {
6862#ifdef FEAT_TERMINAL
6863 if (term_in_normal_mode())
6864 {
6865 end_visual_mode();
6866 clearop(cap->oap);
6867 term_enter_job_mode();
6868 return;
6869 }
6870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006875 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6876 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006880#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006881 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006882 {
6883 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006884 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006885 return;
6886 }
6887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 else if (!curbuf->b_p_ma && !p_im)
6889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006891 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006893 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006894 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006895 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006897 else if (cap->cmdchar == K_PS && VIsual_active)
6898 {
6899 pos_T old_pos = curwin->w_cursor;
6900 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006901 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006903 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006904 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6905 {
6906 shift_delete_registers();
6907 cap->oap->regname = '1';
6908 }
6909 else
6910 cap->oap->regname = '-';
6911 cap->cmdchar = 'd';
6912 cap->nchar = NUL;
6913 nv_operator(cap);
6914 do_pending_operator(cap, 0, FALSE);
6915 cap->cmdchar = K_PS;
6916
zeertzjq7a732522022-03-14 20:46:41 +00006917 if (*ml_get_cursor() != NUL)
6918 {
6919 if (old_visual_mode == 'V')
6920 {
6921 // In linewise Visual mode insert before the beginning of the
6922 // next line.
6923 // When the last line in the buffer was deleted then create a
6924 // new line, otherwise there is not need to move cursor.
6925 // Detect this by checking if cursor moved above Visual area.
6926 if (curwin->w_cursor.lnum < old_pos.lnum
6927 && curwin->w_cursor.lnum < old_visual.lnum)
6928 {
6929 if (u_save_cursor() == OK)
6930 {
6931 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6932 FALSE);
6933 appended_lines(curwin->w_cursor.lnum++, 1L);
6934 }
6935 }
6936 }
6937 // When the last char in the line was deleted then append.
6938 // Detect this by checking if cursor moved before Visual area.
6939 else if (curwin->w_cursor.col < old_pos.col
6940 && curwin->w_cursor.col < old_visual.col)
6941 inc_cursor();
6942 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006944 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006945 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 else if (!checkclearopq(cap->oap))
6948 {
6949 switch (cap->cmdchar)
6950 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006951 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006952 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 break;
6954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006956 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6957 beginline(BL_WHITE);
6958 else
6959 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 break;
6961
Bram Moolenaara1891842017-02-04 21:34:31 +01006962 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006963 // Bracketed paste works like "a"ppend, unless the cursor is in
6964 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006965 if (curwin->w_cursor.col == 0)
6966 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006969 case 'a': // "a"ppend is like "i"nsert on the next character.
6970 // increment coladd when in virtual space, increment the
6971 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (virtual_active()
6973 && (curwin->w_cursor.coladd > 0
6974 || *ml_get_cursor() == NUL
6975 || *ml_get_cursor() == TAB))
6976 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006977 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 inc_cursor();
6979 break;
6980 }
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6983 {
6984 int save_State = State;
6985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006986 // Pretend Insert mode here to allow the cursor on the
6987 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006988 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 coladvance(getviscol());
6990 State = save_State;
6991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
6993 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6994 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01006995 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006996 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006997 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
7000/*
7001 * Invoke edit() and take care of "restart_edit" and the return value.
7002 */
7003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007004invoke_edit(
7005 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007006 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007007 int cmd,
7008 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 int restart_edit_save = 0;
7011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007012 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7013 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7014 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (repl || !stuff_empty())
7016 restart_edit_save = restart_edit;
7017 else
7018 restart_edit_save = 0;
7019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007020 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 restart_edit = 0;
7022
7023 if (edit(cmd, startln, cap->count1))
7024 cap->retval |= CA_COMMAND_BUSY;
7025
7026 if (restart_edit == 0)
7027 restart_edit = restart_edit_save;
7028}
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030/*
7031 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7032 */
7033 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007034nv_object(
7035 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 int flag;
7038 int include;
7039 char_u *mps_save;
7040
7041 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007044 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007046 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 mps_save = curbuf->b_p_mps;
7048 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7049
7050 switch (cap->nchar)
7051 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007052 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 flag = current_word(cap->oap, cap->count1, include, FALSE);
7054 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 flag = current_word(cap->oap, cap->count1, include, TRUE);
7057 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 case '(':
7060 case ')':
7061 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7062 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007063 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 case '{':
7065 case '}':
7066 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7067 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 case ']':
7070 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7071 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007072 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 case '>':
7074 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7075 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007076#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 case 't': // "at" = a tag block (xml and html)
7078 // Do not adjust oap->end in do_pending_operator()
7079 // otherwise there are different results for 'dit'
7080 // (note leading whitespace in last line):
7081 // 1) <b> 2) <b>
7082 // foobar foobar
7083 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007084 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007085 flag = current_tagblock(cap->oap, cap->count1, include);
7086 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007087#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 flag = current_par(cap->oap, cap->count1, include, 'p');
7090 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007091 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 flag = current_sent(cap->oap, cap->count1, include);
7093 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case '"': // "a"" = a double quoted string
7095 case '\'': // "a'" = a single quoted string
7096 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007097 flag = current_quote(cap->oap, cap->count1, include,
7098 cap->nchar);
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100#if 0 // TODO
7101 case 'S': // "aS" = a section
7102 case 'f': // "af" = a filename
7103 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104#endif
7105 default:
7106 flag = FAIL;
7107 break;
7108 }
7109
7110 curbuf->b_p_mps = mps_save;
7111 if (flag == FAIL)
7112 clearopbeep(cap->oap);
7113 adjust_cursor_col();
7114 curwin->w_set_curswant = TRUE;
7115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
7117/*
7118 * "q" command: Start/stop recording.
7119 * "q:", "q/", "q?": edit command-line in command-line window.
7120 */
7121 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007122nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
7124 if (cap->oap->op_type == OP_FORMAT)
7125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007126 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 cap->cmdchar = 'g';
7128 cap->nchar = 'q';
7129 nv_operator(cap);
7130 }
7131 else if (!checkclearop(cap->oap))
7132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7134 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007135 if (cmdwin_type != 0)
7136 {
7137 emsg(_(e_cmdline_window_already_open));
7138 return;
7139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 stuffcharReadbuff(cap->nchar);
7141 stuffcharReadbuff(K_CMDWIN);
7142 }
7143 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 // (stop) recording into a named register, unless executing a
7145 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007146 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 clearopbeep(cap->oap);
7148 }
7149}
7150
7151/*
7152 * Handle the "@r" command.
7153 */
7154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007155nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 if (checkclearop(cap->oap))
7158 return;
7159#ifdef FEAT_EVAL
7160 if (cap->nchar == '=')
7161 {
7162 if (get_expr_register() == NUL)
7163 return;
7164 }
7165#endif
7166 while (cap->count1-- && !got_int)
7167 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007168 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
7170 clearopbeep(cap->oap);
7171 break;
7172 }
7173 line_breakcheck();
7174 }
7175}
7176
7177/*
7178 * Handle the CTRL-U and CTRL-D commands.
7179 */
7180 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007181nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7184 || (cap->cmdchar == Ctrl_D
7185 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7186 clearopbeep(cap->oap);
7187 else if (!checkclearop(cap->oap))
7188 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7189}
7190
7191/*
7192 * Handle "J" or "gJ" command.
7193 */
7194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007195nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007199 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
7201 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007202 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7204 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007206 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007207 if (cap->count0 <= 2)
7208 {
7209 clearopbeep(cap->oap);
7210 return;
7211 }
7212 cap->count0 = curbuf->b_ml.ml_line_count
7213 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007215
7216 prep_redo(cap->oap->regname, cap->count0,
7217 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7218 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220}
7221
7222/*
7223 * "P", "gP", "p" and "gp" commands.
7224 */
7225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007226nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007228 nv_put_opt(cap, FALSE);
7229}
7230
7231/*
7232 * "P", "gP", "p" and "gp" commands.
7233 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7234 */
7235 static void
7236nv_put_opt(cmdarg_T *cap, int fix_indent)
7237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 int regname = 0;
7239 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007240 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007241 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 int dir;
7243 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007244 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
7246 if (cap->oap->op_type != OP_NOP)
7247 {
7248#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007249 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7251 {
7252 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007253 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 else
7256#endif
7257 clearopbeep(cap->oap);
7258 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007259#ifdef FEAT_JOB_CHANNEL
7260 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7261 {
7262 clearopbeep(cap->oap);
7263 }
7264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
7266 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007267 if (fix_indent)
7268 {
7269 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7270 ? FORWARD : BACKWARD;
7271 flags |= PUT_FIXINDENT;
7272 }
7273 else
7274 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007275 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7276 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 prep_redo_cmd(cap);
7278 if (cap->cmdchar == 'g')
7279 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007280 else if (cap->cmdchar == 'z')
7281 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (VIsual_active)
7284 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007285 // Putting in Visual mode: The put text replaces the selected
7286 // text. First delete the selected text, then put the new text.
7287 // Need to save and restore the registers that the delete
7288 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007289 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007291 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007292#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007294#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007295 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007296 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007297#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301 )
7302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007303 // The delete is going to overwrite the register we want to
7304 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 reg1 = get_register(regname, TRUE);
7306 }
7307
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007308 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 cap->cmdchar = 'd';
7310 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007311 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007312 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 nv_operator(cap);
7314 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007315 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007316 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007318 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 cap->oap->regname = regname;
7320
7321 if (reg1 != NULL)
7322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007323 // Delete probably changed the register we want to put, save
7324 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 reg2 = get_register(regname, FALSE);
7326 put_register(regname, reg1);
7327 }
7328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007329 // When deleted a linewise Visual area, put the register as
7330 // lines to avoid it joined with the next line. When deletion was
7331 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (VIsual_mode == 'V')
7333 flags |= PUT_LINE;
7334 else if (VIsual_mode == 'v')
7335 flags |= PUT_LINE_SPLIT;
7336 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7337 flags |= PUT_LINE_FORWARD;
7338 dir = BACKWARD;
7339 if ((VIsual_mode != 'V'
7340 && curwin->w_cursor.col < curbuf->b_op_start.col)
7341 || (VIsual_mode == 'V'
7342 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007343 // cursor is at the end of the line or end of file, put
7344 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007346 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007347 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007349 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007351 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (reg2 != NULL)
7353 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 // What to reselect with "gv"? Selecting the just put text seems to
7356 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007357 if (was_visual)
7358 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007359 curbuf->b_visual.vi_start = curbuf->b_op_start;
7360 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007362 if (*p_sel == 'e')
7363 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007364 }
7365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // When all lines were selected and deleted do_put() leaves an empty
7367 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007368 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007369 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007370 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007371 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // If the cursor was in that line, move it to the end of the last
7374 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007375 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7376 {
7377 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7378 coladvance((colnr_T)MAXCOL);
7379 }
7380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 auto_format(FALSE, TRUE);
7382 }
7383}
7384
7385/*
7386 * "o" and "O" commands.
7387 */
7388 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007389nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7394 {
7395 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007396 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
7398 else
7399#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007400 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007402#ifdef FEAT_JOB_CHANNEL
7403 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007404 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 n_opencmd(cap);
7408}
7409
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410#ifdef FEAT_NETBEANS_INTG
7411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007412nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 netbeans_keycommand(cap->nchar);
7415}
7416#endif
7417
7418#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007420nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007422 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423}
7424#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007425
Bram Moolenaar3918c952005-03-15 22:34:55 +00007426/*
7427 * Trigger CursorHold event.
7428 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7429 * input buffer. "did_cursorhold" is set to avoid retriggering.
7430 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007432nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007433{
7434 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7435 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007436 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007437}