blob: c319be599ad7df552400e6ff874a54244bd712e4 [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 {
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000191 if (oap != NULL)
192 clearopbeep(oap);
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100193 text_locked_msg();
194 return TRUE;
195 }
196 return FALSE;
197}
198
199/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000200 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
201 * Give an error message, possibly beep and return TRUE.
202 * "oap" may be NULL.
203 */
204 int
205check_text_or_curbuf_locked(oparg_T *oap)
206{
207 if (check_text_locked(oap))
208 return TRUE;
209 if (curbuf_locked())
210 {
211 if (oap != NULL)
212 clearop(oap);
213 return TRUE;
214 }
215 return FALSE;
216}
217
218/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000219 * Handle the count before a normal command and set cap->count0.
220 */
221 static int
222normal_cmd_get_count(
223 cmdarg_T *cap,
224 int c,
225 int toplevel UNUSED,
226 int set_prevcount UNUSED,
227 int *ctrl_w,
228 int *need_flushbuf UNUSED)
229{
230getcount:
231 if (!(VIsual_active && VIsual_select))
232 {
233 // Handle a count before a command and compute ca.count0.
234 // Note that '0' is a command and not the start of a count, but it's
235 // part of a count after other digits.
236 while ((c >= '1' && c <= '9')
237 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
238 || c == '0')))
239 {
240 if (c == K_DEL || c == K_KDEL)
241 {
242 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000244 }
245 else if (cap->count0 > 99999999L)
246 {
247 cap->count0 = 999999999L;
248 }
249 else
250 {
251 cap->count0 = cap->count0 * 10 + (c - '0');
252 }
253#ifdef FEAT_EVAL
254 // Set v:count here, when called from main() and not a stuffed
255 // command, so that v:count can be used in an expression mapping
256 // right after the count. Do set it for redo.
257 if (toplevel && readbuf1_empty())
258 set_vcount_ca(cap, &set_prevcount);
259#endif
260 if (*ctrl_w)
261 {
262 ++no_mapping;
263 ++allow_keys; // no mapping for nchar, but keys
264 }
265 ++no_zero_mapping; // don't map zero here
266 c = plain_vgetc();
267 LANGMAP_ADJUST(c, TRUE);
268 --no_zero_mapping;
269 if (*ctrl_w)
270 {
271 --no_mapping;
272 --allow_keys;
273 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000275 }
276
277 // If we got CTRL-W there may be a/another count
278 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
279 {
280 *ctrl_w = TRUE;
281 cap->opcount = cap->count0; // remember first count
282 cap->count0 = 0;
283 ++no_mapping;
284 ++allow_keys; // no mapping for nchar, but keys
285 c = plain_vgetc(); // get next character
286 LANGMAP_ADJUST(c, TRUE);
287 --no_mapping;
288 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000290 goto getcount; // jump back
291 }
292 }
293
294 if (c == K_CURSORHOLD)
295 {
296 // Save the count values so that ca.opcount and ca.count0 are exactly
297 // the same when coming back here after handling K_CURSORHOLD.
298 cap->oap->prev_opcount = cap->opcount;
299 cap->oap->prev_count0 = cap->count0;
300 }
301 else if (cap->opcount != 0)
302 {
303 // If we're in the middle of an operator (including after entering a
304 // yank buffer with '"') AND we had a count before the operator, then
305 // that count overrides the current value of ca.count0.
306 // What this means effectively, is that commands like "3dw" get turned
307 // into "d3w" which makes things fall into place pretty neatly.
308 // If you give a count before AND after the operator, they are
309 // multiplied.
310 if (cap->count0)
311 {
312 if (cap->opcount >= 999999999L / cap->count0)
313 cap->count0 = 999999999L;
314 else
315 cap->count0 *= cap->opcount;
316 }
317 else
318 cap->count0 = cap->opcount;
319 }
320
321 // Always remember the count. It will be set to zero (on the next call,
322 // above) when there is no pending operator.
323 // When called from main(), save the count for use by the "count" built-in
324 // variable.
325 cap->opcount = cap->count0;
326 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
327
328#ifdef FEAT_EVAL
329 // Only set v:count when called from main() and not a stuffed command.
330 // Do set it for redo.
331 if (toplevel && readbuf1_empty())
332 set_vcount(cap->count0, cap->count1, set_prevcount);
333#endif
334
335 return c;
336}
337
338/*
339 * Returns TRUE if the normal command (cap) needs a second character.
340 */
341 static int
342normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
343{
344 return ((cmd_flags & NV_NCH)
345 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
346 && cap->oap->op_type == OP_NOP)
347 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
348 || (cap->cmdchar == 'q'
349 && cap->oap->op_type == OP_NOP
350 && reg_recording == 0
351 && reg_executing == 0)
352 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
353 && (cap->oap->op_type != OP_NOP || VIsual_active))));
354}
355
356/*
357 * Get one or more additional characters for a normal command.
358 * Return the updated command index (if changed).
359 */
360 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000361normal_cmd_get_more_chars(
362 int idx_arg,
363 cmdarg_T *cap,
364 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000365{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000366 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000367 int c;
368 int *cp;
369 int repl = FALSE; // get character for replace mode
370 int lit = FALSE; // get extra character literally
371 int langmap_active = FALSE; // using :lmap mappings
372 int lang; // getting a text character
373#ifdef HAVE_INPUT_METHOD
374 int save_smd; // saved value of p_smd
375#endif
376
377 ++no_mapping;
378 ++allow_keys; // no mapping for nchar, but allow key codes
379 // Don't generate a CursorHold event here, most commands can't handle
380 // it, e.g., nv_replace(), nv_csearch().
381 did_cursorhold = TRUE;
382 if (cap->cmdchar == 'g')
383 {
384 /*
385 * For 'g' get the next character now, so that we can check for
386 * "gr", "g'" and "g`".
387 */
388 cap->nchar = plain_vgetc();
389 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000391 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
392 || cap->nchar == Ctrl_BSL)
393 {
394 cp = &cap->extra_char; // need to get a third character
395 if (cap->nchar != 'r')
396 lit = TRUE; // get it literally
397 else
398 repl = TRUE; // get it in replace mode
399 }
400 else
401 cp = NULL; // no third character needed
402 }
403 else
404 {
405 if (cap->cmdchar == 'r') // get it in replace mode
406 repl = TRUE;
407 cp = &cap->nchar;
408 }
409 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
410
411 /*
412 * Get a second or third character.
413 */
414 if (cp != NULL)
415 {
416 if (repl)
417 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100418 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000419#ifdef CURSOR_SHAPE
420 ui_cursor_shape(); // show different cursor shape
421#endif
422 }
423 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
424 {
425 // Allow mappings defined with ":lmap".
426 --no_mapping;
427 --allow_keys;
428 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100429 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000430 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100431 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000432 langmap_active = TRUE;
433 }
434#ifdef HAVE_INPUT_METHOD
435 save_smd = p_smd;
436 p_smd = FALSE; // Don't let the IM code show the mode here
437 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
438 im_set_active(TRUE);
439#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100440 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000441 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100442 MAY_WANT_TO_LOG_THIS;
443
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000444 // Disable bracketed paste and modifyOtherKeys here, we won't
445 // recognize the escape sequences with 'esckeys' off.
446 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000447 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000448 }
449
450 *cp = plain_vgetc();
451
Bram Moolenaar24959102022-05-07 20:01:16 +0100452 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000453 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100454 MAY_WANT_TO_LOG_THIS;
455
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000456 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000457 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000458 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000459 }
460
461 if (langmap_active)
462 {
463 // Undo the decrement done above
464 ++no_mapping;
465 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100466 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000467 }
468#ifdef HAVE_INPUT_METHOD
469 if (lang)
470 {
471 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
472 im_save_status(&curbuf->b_p_iminsert);
473 im_set_active(FALSE);
474 }
475 p_smd = save_smd;
476#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100477 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000479
480 if (!lit)
481 {
482#ifdef FEAT_DIGRAPHS
483 // Typing CTRL-K gets a digraph.
484 if (*cp == Ctrl_K
485 && ((nv_cmds[idx].cmd_flags & NV_LANG)
486 || cp == &cap->extra_char)
487 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
488 {
489 c = get_digraph(FALSE);
490 if (c > 0)
491 {
492 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000493 // Guessing how to update showcmd here...
494 del_from_showcmd(3);
495 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000496 }
497 }
498#endif
499
500 // adjust chars > 127, except after "tTfFr" commands
501 LANGMAP_ADJUST(*cp, !lang);
502#ifdef FEAT_RIGHTLEFT
503 // adjust Hebrew mapped char
504 if (p_hkmap && lang && KeyTyped)
505 *cp = hkmap(*cp);
506#endif
507 }
508
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000509 // When the next character is CTRL-\ a following CTRL-N means the
510 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000511 if (cp == &cap->extra_char
512 && cap->nchar == Ctrl_BSL
513 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
514 {
515 cap->cmdchar = Ctrl_BSL;
516 cap->nchar = cap->extra_char;
517 idx = find_command(cap->cmdchar);
518 }
519 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
546 // When getting a text character and the next character is a
547 // multi-byte character, it could be a composing character.
548 // However, don't wait for it to arrive. Also, do enable mapping,
549 // because if it's put back with vungetc() it's too late to apply
550 // mapping.
551 --no_mapping;
552 while (enc_utf8 && lang && (c = vpeekc()) > 0
553 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
554 {
555 c = plain_vgetc();
556 if (!utf_iscomposing(c))
557 {
558 vungetc(c); // it wasn't, put it back
559 break;
560 }
561 else if (cap->ncharC1 == 0)
562 cap->ncharC1 = c;
563 else
564 cap->ncharC2 = c;
565 }
566 ++no_mapping;
567 }
568 --no_mapping;
569 --allow_keys;
570
571 return idx;
572}
573
574/*
575 * Returns TRUE if after processing a normal mode command, need to wait for a
576 * moment when a message is displayed that will be overwritten by the mode
577 * message.
578 */
579 static int
580normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
581{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000582 // In Visual mode and with "^O" in Insert mode, a short message will be
583 // overwritten by the mode message. Wait a bit, until a key is hit.
584 // In Visual mode, it's more important to keep the Visual area updated
585 // than keeping a message (e.g. from a /pat search).
586 // Only do this if the command was typed, not from a mapping.
587 // Don't wait when emsg_silent is non-zero.
588 // Also wait a bit after an error message, e.g. for "^O:".
589 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000590 return ( ((p_smd
591 && msg_silent == 0
592 && (restart_edit != 0
593 || (VIsual_active
594 && old_pos->lnum == curwin->w_cursor.lnum
595 && old_pos->col == curwin->w_cursor.col)
596 )
597 && (clear_cmdline
598 || redraw_cmdline)
599 && (msg_didout || (msg_didany && msg_scroll))
600 && !msg_nowait
601 && KeyTyped)
602 || (restart_edit != 0
603 && !VIsual_active
604 && (msg_scroll
605 || emsg_on_display)))
606 && cap->oap->regname == 0
607 && !(cap->retval & CA_COMMAND_BUSY)
608 && stuff_empty()
609 && typebuf_typed()
610 && emsg_silent == 0
611 && !in_assert_fails
612 && !did_wait_return
613 && cap->oap->op_type == OP_NOP);
614}
615
616/*
617 * After processing a normal mode command, wait for a moment when a message is
618 * displayed that will be overwritten by the mode message.
619 */
620 static void
621normal_cmd_wait_for_msg(void)
622{
623 int save_State = State;
624
625 // Draw the cursor with the right shape here
626 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100627 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000628
629 // If need to redraw, and there is a "keep_msg", redraw before the
630 // delay
631 if (must_redraw && keep_msg != NULL && !emsg_on_display)
632 {
633 char_u *kmsg;
634
635 kmsg = keep_msg;
636 keep_msg = NULL;
637 // Showmode() will clear keep_msg, but we want to use it anyway.
638 // First update w_topline.
639 setcursor();
640 update_screen(0);
641 // now reset it, otherwise it's put in the history again
642 keep_msg = kmsg;
643
644 kmsg = vim_strsave(keep_msg);
645 if (kmsg != NULL)
646 {
647 msg_attr((char *)kmsg, keep_msg_attr);
648 vim_free(kmsg);
649 }
650 }
651 setcursor();
652#ifdef CURSOR_SHAPE
653 ui_cursor_shape(); // may show different cursor shape
654#endif
655 cursor_on();
656 out_flush();
657 if (msg_scroll || emsg_on_display)
658 ui_delay(1003L, TRUE); // wait at least one second
659 ui_delay(3003L, FALSE); // wait up to three seconds
660 State = save_State;
661
662 msg_scroll = FALSE;
663 emsg_on_display = FALSE;
664}
665
666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * Execute a command in Normal mode.
668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670normal_cmd(
671 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000678 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 static int old_mapped_len = 0;
682 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000683 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200684 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaara80faa82020-04-12 19:37:17 +0200686 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // Use a count remembered from before entering an operator. After typing
690 // "3d" we return from normal_cmd() and come back here, the "3" is
691 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ca.opcount = opcount;
693
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000694 // If there is an operator pending, then the command we take this time
695 // will terminate it. Finish_op tells us to finish the operation before
696 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef CURSOR_SHAPE
698 c = finish_op;
699#endif
700 finish_op = (oap->op_type != OP_NOP);
701#ifdef CURSOR_SHAPE
702 if (finish_op != c)
703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705# ifdef FEAT_MOUSESHAPE
706 update_mouseshape(-1);
707# endif
708 }
709#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100710 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // When not finishing an operator and no register name typed, reset the
713 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000717#ifdef FEAT_EVAL
718 set_prevcount = TRUE;
719#endif
720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // Restore counts from before receiving K_CURSORHOLD. This means after
723 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
724 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000725 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
726 {
727 ca.opcount = oap->prev_opcount;
728 ca.count0 = oap->prev_count0;
729 oap->prev_opcount = 0;
730 oap->prev_count0 = 0;
731 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar24959102022-05-07 20:01:16 +0100735 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#endif
739
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100740#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100741 // Set v:count here, when called from main() and not a stuffed
742 // command, so that v:count can be used in an expression mapping
743 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100744 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100745 set_vcount_ca(&ca, &set_prevcount);
746#endif
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
749 * Get the command character from the user.
750 */
751 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100752 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000754 // If a mapping was started in Visual or Select mode, remember the length
755 // of the mapping. This is used below to not return to Insert mode for as
756 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (restart_edit == 0)
758 old_mapped_len = 0;
759 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000760 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 old_mapped_len = typebuf_maplen();
762
763 if (c == NUL)
764 c = K_ZERO;
765
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000766 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (VIsual_active
768 && VIsual_select
769 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
770 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000771 int len;
772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100773 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
774 // 'insertmode' is set) fake a "d"elete command, Insert mode will
775 // restart automatically.
776 // Insert the typed character in the typeahead buffer, so that it can
777 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000778 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
779
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000780 // When recording and gotchars() was called the character will be
781 // recorded again, remove the previous recording.
782 if (KeyTyped)
783 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000784
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000785 if (restart_edit != 0)
786 c = 'd';
787 else
788 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 msg_nowait = TRUE; // don't delay going to insert mode
790 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000793 // If the window was made so small that nothing shows, make it at least one
794 // line and one column when typing a command.
795 if (KeyTyped && !KeyStuffed)
796 win_ensure_size();
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000800 // Get the command count
801 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
802 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000804 // Find the command character in the table of commands.
805 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (ctrl_w)
807 {
808 ca.nchar = c;
809 ca.cmdchar = Ctrl_W;
810 }
811 else
812 ca.cmdchar = c;
813 idx = find_command(ca.cmdchar);
814 if (idx < 0)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
818 goto normal_end;
819 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000820
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100822 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000823 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000825 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (VIsual_active)
827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (km_stopsel
830 && (nv_cmds[idx].cmd_flags & NV_STS)
831 && !(mod_mask & MOD_MASK_SHIFT))
832 {
833 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100834 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (km_startsel)
839 {
840 if (nv_cmds[idx].cmd_flags & NV_SS)
841 {
842 unshift_special(&ca);
843 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 if (idx < 0)
845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000847 clearopbeep(oap);
848 goto normal_end;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
852 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857#ifdef FEAT_RIGHTLEFT
858 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
859 && (nv_cmds[idx].cmd_flags & NV_RL))
860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 // Invert horizontal movements and operations. Only when typed by the
862 // user directly, not when the result of a mapping or "x" translated
863 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 switch (ca.cmdchar)
865 {
866 case 'l': ca.cmdchar = 'h'; break;
867 case K_RIGHT: ca.cmdchar = K_LEFT; break;
868 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
869 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
870 case 'h': ca.cmdchar = 'l'; break;
871 case K_LEFT: ca.cmdchar = K_RIGHT; break;
872 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
873 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
874 case '>': ca.cmdchar = '<'; break;
875 case '<': ca.cmdchar = '>'; break;
876 }
877 idx = find_command(ca.cmdchar);
878 }
879#endif
880
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000881 // Get additional characters if we need them.
882 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
883 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000885 // Flush the showcmd characters onto the screen so we can see them while
886 // the command is being executed. Only do this when the shown command was
887 // actually displayed, otherwise this will slow down a lot when executing
888 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (need_flushbuf)
890 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100891
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000892 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200893 {
894 if (ex_normal_busy)
895 did_cursorhold = save_did_cursorhold;
896 else
897 did_cursorhold = FALSE;
898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 if (ca.nchar == ESC)
903 {
904 clearop(oap);
905 if (restart_edit == 0 && goto_im())
906 restart_edit = 'a';
907 goto normal_end;
908 }
909
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000910 if (ca.cmdchar != K_IGNORE)
911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000913 msg_col = 0;
914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When 'keymodel' contains "startsel" some keys start Select/Visual
919 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (!VIsual_active && km_startsel)
921 {
922 if (nv_cmds[idx].cmd_flags & NV_SS)
923 {
924 start_selection();
925 unshift_special(&ca);
926 idx = find_command(ca.cmdchar);
927 }
928 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
929 && (mod_mask & MOD_MASK_SHIFT))
930 {
931 start_selection();
932 mod_mask &= ~MOD_MASK_SHIFT;
933 }
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000936 // Execute the command!
937 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ca.arg = nv_cmds[idx].cmd_arg;
939 (nv_cmds[idx].cmd_func)(&ca);
940
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000941 // If we didn't start or finish an operator, reset oap->regname, unless we
942 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (!finish_op
944 && !oap->op_type
945 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
946 {
947 clearop(oap);
948#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200949 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951 }
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // Get the length of mapped chars again after typing a count, second
954 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000955 if (old_mapped_len > 0)
956 old_mapped_len = typebuf_maplen();
957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000958 // If an operation is pending, handle it. But not for K_IGNORE or
959 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200960 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100961 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000963 // Wait for a moment when a message is displayed that will be overwritten
964 // by the mode message.
965 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
966 normal_cmd_wait_for_msg();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969normal_end:
970
971 msg_nowait = FALSE;
972
Bram Moolenaarcc613032020-06-07 21:31:18 +0200973#ifdef FEAT_EVAL
974 if (finish_op)
975 reset_reg_var();
976#endif
977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100978 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef CURSOR_SHAPE
980 c = finish_op;
981#endif
982 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100983 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100985 // Redraw the cursor with another shape, if we were in Operator-pending
986 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (c || ca.cmdchar == 'r')
988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# ifdef FEAT_MOUSESHAPE
991 update_mouseshape(-1);
992# endif
993 }
994#endif
995
Bram Moolenaara983fe92008-07-31 20:04:27 +0000996 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100997 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 vim_free(ca.searchbuf);
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (has_mbyte)
1004 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (curwin->w_p_scb && toplevel)
1007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001008 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 do_check_scrollbind(TRUE);
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar860cae12010-06-05 23:22:07 +02001012 if (curwin->w_p_crb && toplevel)
1013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001015 do_check_cursorbind();
1016 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001018#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001020 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001021 restart_edit = 0;
1022#endif
1023
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001024 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1025 // if still inside a mapping that started in Visual mode).
1026 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1029 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !(ca.retval & CA_COMMAND_BUSY)
1031 && stuff_empty()
1032 && oap->regname == 0)
1033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_VIsual_select == 1)
1035 {
1036 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001037 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 showmode();
1039 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001040 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001042 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)edit(restart_edit, FALSE, 1L);
1044 }
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (restart_VIsual_select == 2)
1047 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 opcount = ca.opcount;
1051}
1052
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053#ifdef FEAT_EVAL
1054/*
1055 * Set v:count and v:count1 according to "cap".
1056 * Set v:prevcount only when "set_prevcount" is TRUE.
1057 */
1058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001060{
1061 long count = cap->count0;
1062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064 if (cap->opcount != 0)
1065 count = cap->opcount * (count == 0 ? 1 : count);
1066 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068}
1069#endif
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001072 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * if not.
1074 */
1075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 static int did_check = FALSE;
1079
1080 if (full_screen)
1081 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001082 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 did_check = TRUE;
1085 }
1086}
1087
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001088#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1089/*
1090 * Call yank_do_autocmd() for "regname".
1091 */
1092 static void
1093call_yank_do_autocmd(int regname)
1094{
1095 oparg_T oa;
1096 yankreg_T *reg;
1097
1098 clear_oparg(&oa);
1099 oa.regname = regname;
1100 oa.op_type = OP_YANK;
1101 oa.is_VIsual = TRUE;
1102 reg = get_register(regname, TRUE);
1103 yank_do_autocmd(&oa, reg);
1104 free_register(reg);
1105}
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001109 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001110 * This function or the next should ALWAYS be called to end Visual mode, except
1111 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114end_visual_mode()
1115{
1116 end_visual_mode_keep_button();
1117 reset_held_button();
1118}
1119
1120 void
1121end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001124 // If we are using the clipboard, then remember what was selected in case
1125 // we need to paste it somewhere while we still own the selection.
1126 // Only do this when the clipboard is already owned. Don't want to grab
1127 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (clip_star.available && clip_star.owned)
1129 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001130
1131# if defined(FEAT_EVAL)
1132 // Emit a TextYankPost for the automatic copy of the selection into the
1133 // star and/or plus register.
1134 if (has_textyankpost())
1135 {
1136 if (clip_isautosel_star())
1137 call_yank_do_autocmd('*');
1138 if (clip_isautosel_plus())
1139 call_yank_do_autocmd('+');
1140 }
1141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 setmouse();
1146 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001149 curbuf->b_visual.vi_mode = VIsual_mode;
1150 curbuf->b_visual.vi_start = VIsual;
1151 curbuf->b_visual.vi_end = curwin->w_cursor;
1152 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153#ifdef FEAT_EVAL
1154 curbuf->b_visual_mode_eval = VIsual_mode;
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!virtual_active())
1157 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001158 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001160 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001161 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162}
1163
1164/*
1165 * Reset VIsual_active and VIsual_reselect.
1166 */
1167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001168reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 if (VIsual_active)
1171 {
1172 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001173 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 VIsual_reselect = FALSE;
1176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect if it's set.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 VIsual_reselect = FALSE;
1189 }
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001192 void
1193restore_visual_mode(void)
1194{
1195 if (VIsual_mode_orig != NUL)
1196 {
1197 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1198 VIsual_mode_orig = NUL;
1199 }
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Check for a balloon-eval special item to include when searching for an
1204 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1205 * Returns TRUE if the character at "*ptr" should be included.
1206 * "dir" is FORWARD or BACKWARD, the direction of searching.
1207 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1208 * "bnp" points to a counter for square brackets.
1209 */
1210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001211find_is_eval_item(
1212 char_u *ptr,
1213 int *colp,
1214 int *bnp,
1215 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1219 ++*bnp;
1220 if (*bnp > 0)
1221 {
1222 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1223 --*bnp;
1224 return TRUE;
1225 }
1226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (*ptr == '.')
1229 return TRUE;
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1233 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1234 {
1235 *colp += dir;
1236 return TRUE;
1237 }
1238 return FALSE;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241/*
1242 * Find the identifier under or to the right of the cursor.
1243 * "find_type" can have one of three values:
1244 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * FIND_STRING: find any non-white text
1246 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001247 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *
1249 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001250 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * This doesn't match the real Vi but I like it a little better and it
1254 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001255 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * When FIND_IDENT isn't defined, we backup until a blank.
1257 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001258 * Returns the length of the text, or zero if no text is found.
1259 * If text is found, a pointer to the text is put in "*text". This
1260 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
1262 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001263find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267}
1268
1269/*
1270 * Like find_ident_under_cursor(), but for any window and any position.
1271 * However: Uses 'iskeyword' from the current window!.
1272 */
1273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274find_ident_at_pos(
1275 win_T *wp,
1276 linenr_T lnum,
1277 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001278 char_u **text,
1279 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001280 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001283 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int this_class = 0;
1286 int prev_class;
1287 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001288 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001290 // if i == 0: try to find an identifier
1291 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1293 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1294 {
1295 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (has_mbyte)
1300 {
1301 while (ptr[col] != NUL)
1302 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001303 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 this_class = mb_get_class(ptr + col);
1307 if (this_class != 0 && (i == 1 || this_class != 1))
1308 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 )
1317 ++col;
1318
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001319 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (has_mbyte)
1326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1329 this_class = mb_get_class((char_u *)"a");
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001332 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1335 prev_class = mb_get_class(ptr + prevcol);
1336 if (this_class != prev_class
1337 && (i == 0
1338 || prev_class == 0
1339 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && (!(find_type & FIND_EVAL)
1341 || prevcol == 0
1342 || !find_is_eval_item(ptr + prevcol, &prevcol,
1343 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 )
1345 break;
1346 col = prevcol;
1347 }
1348
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001349 // If we don't want just any old text, or we've found an
1350 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (this_class > 2)
1352 this_class = 2;
1353 if (!(find_type & FIND_STRING) || this_class == 2)
1354 break;
1355 }
1356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 while (col > 0
1359 && ((i == 0
1360 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001361 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 && (!(find_type & FIND_IDENT)
1363 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || ((find_type & FIND_EVAL)
1365 && col > 1
1366 && find_is_eval_item(ptr + col - 1, &col,
1367 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ))
1369 --col;
1370
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001371 // If we don't want just any old text, or we've found an
1372 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1374 break;
1375 }
1376 }
1377
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001378 if (ptr[col] == NUL || (i == 0
1379 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001381 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001382 if ((find_type & FIND_NOERROR) == 0)
1383 {
1384 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001385 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return 0;
1390 }
1391 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001392 *text = ptr;
1393 if (textcol != NULL)
1394 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 bn = 0;
1400 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (has_mbyte)
1403 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001404 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 this_class = mb_get_class(ptr);
1406 while (ptr[col] != NUL
1407 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1408 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || ((find_type & FIND_EVAL)
1410 && col <= (int)startcol
1411 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001413 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001417 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 || ((find_type & FIND_EVAL)
1419 && col <= (int)startcol
1420 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 return col;
1425}
1426
1427/*
1428 * Prepare for redo of a normal command.
1429 */
1430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001431prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 prep_redo(cap->oap->regname, cap->count0,
1434 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1435}
1436
1437/*
1438 * Prepare for redo of any command.
1439 * Note that only the last argument can be a multi-byte char.
1440 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001442prep_redo(
1443 int regname,
1444 long num,
1445 int cmd1,
1446 int cmd2,
1447 int cmd3,
1448 int cmd4,
1449 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001451 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1452}
1453
1454/*
1455 * Prepare for redo of any command with extra count after "cmd2".
1456 */
1457 void
1458prep_redo_num2(
1459 int regname,
1460 long num1,
1461 int cmd1,
1462 int cmd2,
1463 long num2,
1464 int cmd3,
1465 int cmd4,
1466 int cmd5)
1467{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001469
1470#ifdef FEAT_EVAL
1471 // Put info about a mapping in the redo buffer, so that "." will use the
1472 // same script context.
1473 may_add_last_used_map_to_redobuff();
1474#endif
1475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
1478 AppendCharToRedobuff('"');
1479 AppendCharToRedobuff(regname);
1480 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001481 if (num1 != 0)
1482 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (cmd1 != NUL)
1484 AppendCharToRedobuff(cmd1);
1485 if (cmd2 != NUL)
1486 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001487 if (num2 != 0)
1488 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (cmd3 != NUL)
1490 AppendCharToRedobuff(cmd3);
1491 if (cmd4 != NUL)
1492 AppendCharToRedobuff(cmd4);
1493 if (cmd5 != NUL)
1494 AppendCharToRedobuff(cmd5);
1495}
1496
1497/*
1498 * check for operator active and clear it
1499 *
1500 * return TRUE if operator was active
1501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 if (oap->op_type == OP_NOP)
1506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
1511/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001512 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001514 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001519 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001526clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 oap->op_type = OP_NOP;
1529 oap->regname = 0;
1530 oap->motion_force = NUL;
1531 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001532 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 clearop(oap);
1539 beep_flush();
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
1543 * Remove the shift modifier from a special key.
1544 */
1545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 switch (cap->cmdchar)
1549 {
1550 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1551 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1552 case K_S_UP: cap->cmdchar = K_UP; break;
1553 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1554 case K_S_HOME: cap->cmdchar = K_HOME; break;
1555 case K_S_END: cap->cmdchar = K_END; break;
1556 }
1557 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001560/*
1561 * If the mode is currently displayed clear the command line or update the
1562 * command displayed.
1563 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566{
1567 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001569 else
1570 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001571}
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/*
1574 * Routines for displaying a partly typed command
1575 */
1576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001577static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static int showcmd_is_clear = TRUE;
1579static int showcmd_visual = FALSE;
1580
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001581static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001584clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 if (!p_sc)
1587 return;
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (VIsual_active && !char_avail())
1590 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001591 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 long lines;
1593 colnr_T leftcol, rightcol;
1594 linenr_T top, bot;
1595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001597 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 top = VIsual.lnum;
1600 bot = curwin->w_cursor.lnum;
1601 }
1602 else
1603 {
1604 top = curwin->w_cursor.lnum;
1605 bot = VIsual.lnum;
1606 }
1607# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001609 (void)hasFolding(top, &top, NULL);
1610 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611# endif
1612 lines = bot - top + 1;
1613
1614 if (VIsual_mode == Ctrl_V)
1615 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001616# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001618 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001622 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001626 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001627 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1630 (long)(rightcol - leftcol + 1));
1631 }
1632 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1633 sprintf((char *)showcmd_buf, "%ld", lines);
1634 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001635 {
1636 char_u *s, *e;
1637 int l;
1638 int bytes = 0;
1639 int chars = 0;
1640
1641 if (cursor_bot)
1642 {
1643 s = ml_get_pos(&VIsual);
1644 e = ml_get_cursor();
1645 }
1646 else
1647 {
1648 s = ml_get_cursor();
1649 e = ml_get_pos(&VIsual);
1650 }
1651 while ((*p_sel != 'e') ? s <= e : s < e)
1652 {
1653 l = (*mb_ptr2len)(s);
1654 if (l == 0)
1655 {
1656 ++bytes;
1657 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001658 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001659 }
1660 bytes += l;
1661 ++chars;
1662 s += l;
1663 }
1664 if (bytes == chars)
1665 sprintf((char *)showcmd_buf, "%d", chars);
1666 else
1667 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 showcmd_visual = TRUE;
1671 }
1672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 showcmd_buf[0] = NUL;
1675 showcmd_visual = FALSE;
1676
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001677 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (showcmd_is_clear)
1679 return;
1680 }
1681
1682 display_showcmd();
1683}
1684
1685/*
1686 * Add 'c' to string of shown command chars.
1687 * Return TRUE if output has been written (and setcursor() has been called).
1688 */
1689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001690add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 char_u *p;
1693 int old_len;
1694 int extra_len;
1695 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int i;
1697 static int ignore[] =
1698 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1701 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001702#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001703 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001704 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1706 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001707 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001709 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 0
1711 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar09df3122006-01-23 22:23:09 +00001713 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return FALSE;
1715
1716 if (showcmd_visual)
1717 {
1718 showcmd_buf[0] = NUL;
1719 showcmd_visual = FALSE;
1720 }
1721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (IS_SPECIAL(c))
1724 for (i = 0; ignore[i] != 0; ++i)
1725 if (ignore[i] == c)
1726 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001729 if (*p == ' ')
1730 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 old_len = (int)STRLEN(showcmd_buf);
1732 extra_len = (int)STRLEN(p);
1733 overflow = old_len + extra_len - SHOWCMD_COLS;
1734 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001735 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1736 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 STRCAT(showcmd_buf, p);
1738
1739 if (char_avail())
1740 return FALSE;
1741
1742 display_showcmd();
1743
1744 return TRUE;
1745}
1746
1747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001748add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 if (!add_to_showcmd(c))
1751 setcursor();
1752}
1753
1754/*
1755 * Delete 'len' characters from the end of the shown command.
1756 */
1757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 int old_len;
1761
1762 if (!p_sc)
1763 return;
1764
1765 old_len = (int)STRLEN(showcmd_buf);
1766 if (len > old_len)
1767 len = old_len;
1768 showcmd_buf[old_len - len] = NUL;
1769
1770 if (!char_avail())
1771 display_showcmd();
1772}
1773
1774/*
1775 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1776 * something and there is a partial mapping.
1777 */
1778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 if (p_sc)
1782 STRCPY(old_showcmd_buf, showcmd_buf);
1783}
1784
1785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 if (!p_sc)
1789 return;
1790
1791 STRCPY(showcmd_buf, old_showcmd_buf);
1792
1793 display_showcmd();
1794}
1795
1796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Luuk van Baalba936f62022-12-15 13:15:39 +00001799 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
Luuk van Baalba936f62022-12-15 13:15:39 +00001801 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 cursor_off();
1803
Luuk van Baalba936f62022-12-15 13:15:39 +00001804 if (*p_sloc == 's')
1805 win_redr_status(curwin, FALSE);
1806 else if (*p_sloc == 't')
1807 draw_tabline();
1808 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001810 if (!showcmd_is_clear)
1811 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Luuk van Baalba936f62022-12-15 13:15:39 +00001813 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1814 // spaces
1815 screen_puts((char_u *)" " + len,
1816 (int)Rows - 1, sc_col + len, 0);
1817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * When "check" is FALSE, prepare for commands that scroll the window.
1824 * When "check" is TRUE, take care of scroll-binding after the window has
1825 * scrolled. Called from normal_cmd() and edit().
1826 */
1827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001828do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 static win_T *old_curwin = NULL;
1831 static linenr_T old_topline = 0;
1832#ifdef FEAT_DIFF
1833 static int old_topfill = 0;
1834#endif
1835 static buf_T *old_buf = NULL;
1836 static colnr_T old_leftcol = 0;
1837
1838 if (check && curwin->w_p_scb)
1839 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001840 // If a ":syncbind" command was just used, don't scroll, only reset
1841 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (did_syncbind)
1843 did_syncbind = FALSE;
1844 else if (curwin == old_curwin)
1845 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001846 // Synchronize other windows, as necessary according to
1847 // 'scrollbind'. Don't do this after an ":edit" command, except
1848 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if ((curwin->w_buffer == old_buf
1850#ifdef FEAT_DIFF
1851 || curwin->w_p_diff
1852#endif
1853 )
1854 && (curwin->w_topline != old_topline
1855#ifdef FEAT_DIFF
1856 || curwin->w_topfill != old_topfill
1857#endif
1858 || curwin->w_leftcol != old_leftcol))
1859 {
1860 check_scrollbind(curwin->w_topline - old_topline,
1861 (long)(curwin->w_leftcol - old_leftcol));
1862 }
1863 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001866 // When switching between windows, make sure that the relative
1867 // vertical offset is valid for the new window. The relative
1868 // offset is invalid whenever another 'scrollbind' window has
1869 // scrolled to a point that would force the current window to
1870 // scroll past the beginning or end of its buffer. When the
1871 // resync is performed, some of the other 'scrollbind' windows may
1872 // need to jump so that the current window's relative position is
1873 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1875 }
1876 curwin->w_scbind_pos = curwin->w_topline;
1877 }
1878
1879 old_curwin = curwin;
1880 old_topline = curwin->w_topline;
1881#ifdef FEAT_DIFF
1882 old_topfill = curwin->w_topfill;
1883#endif
1884 old_buf = curwin->w_buffer;
1885 old_leftcol = curwin->w_leftcol;
1886}
1887
1888/*
1889 * Synchronize any windows that have "scrollbind" set, based on the
1890 * number of rows by which the current window has changed
1891 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1892 */
1893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001894check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 int want_ver;
1897 int want_hor;
1898 win_T *old_curwin = curwin;
1899 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int old_VIsual_select = VIsual_select;
1901 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 colnr_T tgt_leftcol = curwin->w_leftcol;
1903 long topline;
1904 long y;
1905
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001906 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1908#ifdef FEAT_DIFF
1909 want_ver |= old_curwin->w_p_diff;
1910#endif
1911 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1912
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001913 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001915 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001918 // skip original window and windows with 'noscrollbind'
1919 if (curwin == old_curwin || !curwin->w_p_scb)
1920 continue;
1921
1922 // do the vertical scroll
1923 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001926 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
zeertzjq101d57b2022-07-31 18:34:32 +01001928 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
zeertzjq101d57b2022-07-31 18:34:32 +01001930 else
1931#endif
1932 {
1933 curwin->w_scbind_pos += topline_diff;
1934 topline = curwin->w_scbind_pos;
1935 if (topline > curbuf->b_ml.ml_line_count)
1936 topline = curbuf->b_ml.ml_line_count;
1937 if (topline < 1)
1938 topline = 1;
1939
1940 y = topline - curwin->w_topline;
1941 if (y > 0)
1942 scrollup(y, FALSE);
1943 else
1944 scrolldown(-y, FALSE);
1945 }
1946
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001947 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001948 cursor_correct();
1949 curwin->w_redr_status = TRUE;
1950 }
1951
1952 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001953 if (want_hor)
1954 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001957 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 VIsual_select = old_VIsual_select;
1959 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 curwin = old_curwin;
1961 curbuf = old_curbuf;
1962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964/*
1965 * Command character that's ignored.
1966 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001967 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001970nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001972 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
1975/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001976 * Command character that doesn't do anything, but unlike nv_ignore() does
1977 * start edit(). Used for "startinsert" executed while starting up.
1978 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001981{
1982}
1983
1984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 * Command character doesn't exist.
1986 */
1987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001988nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 clearopbeep(cap->oap);
1991}
1992
1993/*
1994 * <Help> and <F1> commands.
1995 */
1996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 if (!checkclearopq(cap->oap))
2000 ex_help(NULL);
2001}
2002
2003/*
2004 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2005 */
2006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002009#ifdef FEAT_JOB_CHANNEL
2010 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2011 clearopbeep(cap->oap);
2012 else
2013#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002014 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002015 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002016 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002017 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2018 op_addsub(cap->oap, cap->count1, cap->arg);
2019 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002020 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002021 else if (VIsual_active)
2022 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002023 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002024 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025}
2026
2027/*
2028 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2029 */
2030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002034 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002035 if (mod_mask & MOD_MASK_CTRL)
2036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002038 if (cap->arg == BACKWARD)
2039 goto_tabpage(-(int)cap->count1);
2040 else
2041 goto_tabpage((int)cap->count0);
2042 }
2043 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002044 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046}
2047
2048/*
2049 * Implementation of "gd" and "gD" command.
2050 */
2051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002052nv_gd(
2053 oparg_T *oap,
2054 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 char_u *ptr;
2059
Bram Moolenaard9d30582005-05-18 22:10:28 +00002060 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002061 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002062 == FAIL)
2063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002065 }
2066 else
2067 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002068#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002069 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2070 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002071#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002072 // clear any search statistics
2073 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2074 clear_cmdline = TRUE;
2075 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002076}
2077
2078/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002079 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2080 * otherwise.
2081 */
2082 static int
2083is_ident(char_u *line, int offset)
2084{
2085 int i;
2086 int incomment = FALSE;
2087 int instring = 0;
2088 int prev = 0;
2089
2090 for (i = 0; i < offset && line[i] != NUL; i++)
2091 {
2092 if (instring != 0)
2093 {
2094 if (prev != '\\' && line[i] == instring)
2095 instring = 0;
2096 }
2097 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2098 {
2099 instring = line[i];
2100 }
2101 else
2102 {
2103 if (incomment)
2104 {
2105 if (prev == '*' && line[i] == '/')
2106 incomment = FALSE;
2107 }
2108 else if (prev == '/' && line[i] == '*')
2109 {
2110 incomment = TRUE;
2111 }
2112 else if (prev == '/' && line[i] == '/')
2113 {
2114 return FALSE;
2115 }
2116 }
2117
2118 prev = line[i];
2119 }
2120
2121 return incomment == FALSE && instring == 0;
2122}
2123
2124/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002125 * Search for variable declaration of "ptr[len]".
2126 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2127 * current file ("gD").
2128 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002129 * Return FAIL when not found.
2130 */
2131 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002132find_decl(
2133 char_u *ptr,
2134 int len,
2135 int locally,
2136 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002137 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002138{
2139 char_u *pat;
2140 pos_T old_pos;
2141 pos_T par_pos;
2142 pos_T found_pos;
2143 int t;
2144 int save_p_ws;
2145 int save_p_scs;
2146 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002147 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002148 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002149 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002150
2151 if ((pat = alloc(len + 7)) == NULL)
2152 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002154 // Put "\V" before the pattern to avoid that the special meaning of "."
2155 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002156 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2157 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 old_pos = curwin->w_cursor;
2159 save_p_ws = p_ws;
2160 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002161 p_ws = FALSE; // don't wrap around end of file now
2162 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002164 // With "gD" go to line 1.
2165 // With "gd" Search back for the start of the current function, then go
2166 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002167 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002169 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002171 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173 else
2174 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002175 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2177 --curwin->w_cursor.lnum;
2178 }
2179 curwin->w_cursor.col = 0;
2180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002181 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002182 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002183 for (;;)
2184 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002185 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002186 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002188 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002189
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002190 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002191 {
2192 pos_T *pos;
2193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // Check that the block the match is in doesn't end before the
2195 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002196 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2197 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2198 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // There can't be a useful match before the end of this block.
2201 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002202 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002203 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002204 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002205 }
2206
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002207 if (t == FAIL)
2208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 if (found_pos.lnum != 0)
2211 {
2212 curwin->w_cursor = found_pos;
2213 t = OK;
2214 }
2215 break;
2216 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002217 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 ++curwin->w_cursor.lnum;
2221 curwin->w_cursor.col = 0;
2222 continue;
2223 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002224 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002226 // If the current position is not a valid identifier and a previous
2227 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002228 if (!valid && found_pos.lnum != 0)
2229 {
2230 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002231 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002232 }
2233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002234 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002235 if (valid && !locally)
2236 break;
2237 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002238 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002240 if (found_pos.lnum != 0)
2241 curwin->w_cursor = found_pos;
2242 break;
2243 }
2244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // For finding a local variable and the match is before the "{" or
2246 // inside a comment, continue searching. For K&R style function
2247 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002248 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002249 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002250 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002251 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002252 // Remove SEARCH_START from flags to avoid getting stuck at one
2253 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002254 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002256
2257 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002259 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 curwin->w_cursor = old_pos;
2261 }
2262 else
2263 {
2264 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002265 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 reset_search_dir();
2267 }
2268
2269 vim_free(pat);
2270 p_ws = save_p_ws;
2271 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272
2273 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274}
2275
2276/*
2277 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2278 * lines rather than lines in the file.
2279 * 'dist' must be positive.
2280 *
2281 * Return OK if able to move cursor, FAIL otherwise.
2282 */
2283 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002286 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 int retval = OK;
2288 int atend = FALSE;
2289 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002290 int col_off1; // margin offset for first screen line
2291 int col_off2; // margin offset for wrapped screen line
2292 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002293 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002296 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 col_off1 = curwin_col_off();
2299 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002300 width1 = curwin->w_width - col_off1;
2301 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002302 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002306 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002307 // Instead of sticking at the last character of the buffer line we
2308 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (curwin->w_curswant == MAXCOL)
2310 {
2311 atend = TRUE;
2312 validate_virtcol();
2313 if (width1 <= 0)
2314 curwin->w_curswant = 0;
2315 else
2316 {
2317 curwin->w_curswant = width1 - 1;
2318 if (curwin->w_virtcol > curwin->w_curswant)
2319 curwin->w_curswant += ((curwin->w_virtcol
2320 - curwin->w_curswant - 1) / width2 + 1) * width2;
2321 }
2322 }
2323 else
2324 {
2325 if (linelen > width1)
2326 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2327 else
2328 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002329 if (curwin->w_curswant >= (colnr_T)n)
2330 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332
2333 while (dist--)
2334 {
2335 if (dir == BACKWARD)
2336 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002337 if ((long)curwin->w_curswant >= width1
2338#ifdef FEAT_FOLDING
2339 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2340#endif
2341 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002342 // Move back within the line. This can give a negative value
2343 // for w_curswant if width1 < width2 (with cpoptions+=n),
2344 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 curwin->w_curswant -= width2;
2346 else
2347 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002348 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002350 // Move to the start of a closed fold. Don't do that when
2351 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (!(fdo_flags & FDO_ALL))
2353 (void)hasFolding(curwin->w_cursor.lnum,
2354 &curwin->w_cursor.lnum, NULL);
2355#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002356 if (curwin->w_cursor.lnum == 1)
2357 {
2358 retval = FAIL;
2359 break;
2360 }
2361 --curwin->w_cursor.lnum;
2362
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002363 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (linelen > width1)
2365 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2366 + 1) * width2;
2367 }
2368 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002369 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 if (linelen > width1)
2372 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2373 else
2374 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002375 if (curwin->w_curswant + width2 < (colnr_T)n
2376#ifdef FEAT_FOLDING
2377 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2378#endif
2379 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 curwin->w_curswant += width2;
2382 else
2383 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002384 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2388 &curwin->w_cursor.lnum);
2389#endif
2390 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2391 {
2392 retval = FAIL;
2393 break;
2394 }
2395 curwin->w_cursor.lnum++;
2396 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002397 // Check if the cursor has moved below the number display
2398 // when width1 < width2 (with cpoptions+=n). Subtract width2
2399 // to get a negative value for w_curswant, which will get
2400 // clipped to column 0.
2401 if (curwin->w_curswant >= width1)
2402 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002403 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405 }
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002409 if (virtual_active() && atend)
2410 coladvance(MAXCOL);
2411 else
2412 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2415 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002416 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002417 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002418
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002419 // Check for landing on a character that got split at the end of the
2420 // last line. We want to advance a screenline, not end up in the same
2421 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002423 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002424#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002425 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2426 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002427#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002428
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002429 c = (*mb_ptr2char)(ml_get_cursor());
2430 if (dir == FORWARD && virtcol < curwin->w_curswant
2431 && (curwin->w_curswant <= (colnr_T)width1)
2432 && !vim_isprintc(c) && c > 255)
2433 oneright();
2434
Bram Moolenaar773b1582014-08-29 14:20:51 +02002435 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 && (curwin->w_curswant < (colnr_T)width1
2437 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2438 : ((curwin->w_curswant - width1) % width2
2439 > (colnr_T)width2 / 2)))
2440 --curwin->w_cursor.col;
2441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002444 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002445 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 return retval;
2448}
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450/*
2451 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2452 * cap->arg must be TRUE for CTRL-E.
2453 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002455nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 if (!checkclearop(cap->oap))
2458 scroll_redraw(cap->arg, cap->count1);
2459}
2460
2461/*
2462 * Scroll "count" lines up or down, and redraw.
2463 */
2464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002468 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469#ifdef FEAT_DIFF
2470 int prev_topfill = curwin->w_topfill;
2471#endif
2472 linenr_T prev_lnum = curwin->w_cursor.lnum;
2473
2474 if (up)
2475 scrollup(count, TRUE);
2476 else
2477 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002478 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2481 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 cursor_correct();
2483 check_cursor_moved(curwin);
2484 curwin->w_valid |= VALID_TOPLINE;
2485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 // If moved back to where we were, at least move the cursor, otherwise
2487 // we get stuck at one position. Don't move the cursor up if the
2488 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002490 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491#ifdef FEAT_DIFF
2492 && curwin->w_topfill == prev_topfill
2493#endif
2494 )
2495 {
2496 if (up)
2497 {
2498 if (curwin->w_cursor.lnum > prev_lnum
2499 || cursor_down(1L, FALSE) == FAIL)
2500 break;
2501 }
2502 else
2503 {
2504 if (curwin->w_cursor.lnum < prev_lnum
2505 || prev_topline == 1L
2506 || cursor_up(1L, FALSE) == FAIL)
2507 break;
2508 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002509 // Mark w_topline as valid, otherwise the screen jumps back at the
2510 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 check_cursor_moved(curwin);
2512 curwin->w_valid |= VALID_TOPLINE;
2513 }
2514 }
2515 if (curwin->w_cursor.lnum != prev_lnum)
2516 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002517 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518}
2519
2520/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002521 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2522 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2523 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002524 */
2525 static int
2526nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2527{
2528 int nchar = *nchar_arg;
2529 long n;
2530
2531 // "z123{nchar}": edit the count before obtaining {nchar}
2532 if (checkclearop(cap->oap))
2533 return FALSE;
2534 n = nchar - '0';
2535
2536 for (;;)
2537 {
2538#ifdef USE_ON_FLY_SCROLL
2539 dont_scroll = TRUE; // disallow scrolling here
2540#endif
2541 ++no_mapping;
2542 ++allow_keys; // no mapping for nchar, but allow key codes
2543 nchar = plain_vgetc();
2544 LANGMAP_ADJUST(nchar, TRUE);
2545 --no_mapping;
2546 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002547 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002548
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002549 if (nchar == K_DEL || nchar == K_KDEL)
2550 n /= 10;
2551 else if (VIM_ISDIGIT(nchar))
2552 n = n * 10 + (nchar - '0');
2553 else if (nchar == CAR)
2554 {
2555#ifdef FEAT_GUI
2556 need_mouse_correct = TRUE;
2557#endif
2558 win_setheight((int)n);
2559 break;
2560 }
2561 else if (nchar == 'l'
2562 || nchar == 'h'
2563 || nchar == K_LEFT
2564 || nchar == K_RIGHT)
2565 {
2566 cap->count1 = n ? n * cap->count1 : cap->count1;
2567 *nchar_arg = nchar;
2568 return TRUE;
2569 }
2570 else
2571 {
2572 clearopbeep(cap->oap);
2573 break;
2574 }
2575 }
2576 cap->oap->op_type = OP_NOP;
2577 return FALSE;
2578}
2579
2580#ifdef FEAT_SPELL
2581/*
2582 * "zug" and "zuw": undo "zg" and "zw"
2583 * "zg": add good word to word list
2584 * "zw": add wrong word to word list
2585 * "zG": add good word to temp word list
2586 * "zW": add wrong word to temp word list
2587 */
2588 static int
2589nv_zg_zw(cmdarg_T *cap, int nchar)
2590{
2591 char_u *ptr = NULL;
2592 int len;
2593 int undo = FALSE;
2594
2595 if (nchar == 'u')
2596 {
2597 ++no_mapping;
2598 ++allow_keys; // no mapping for nchar, but allow key codes
2599 nchar = plain_vgetc();
2600 LANGMAP_ADJUST(nchar, TRUE);
2601 --no_mapping;
2602 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002603 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002604
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002605 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2606 {
2607 clearopbeep(cap->oap);
2608 return OK;
2609 }
2610 undo = TRUE;
2611 }
2612
2613 if (checkclearop(cap->oap))
2614 return OK;
2615 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2616 return FAIL;
2617 if (ptr == NULL)
2618 {
2619 pos_T pos = curwin->w_cursor;
2620
2621 // Find bad word under the cursor. When 'spell' is
2622 // off this fails and find_ident_under_cursor() is
2623 // used below.
2624 emsg_off++;
2625 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2626 emsg_off--;
2627 if (len != 0 && curwin->w_cursor.col <= pos.col)
2628 ptr = ml_get_pos(&curwin->w_cursor);
2629 curwin->w_cursor = pos;
2630 }
2631
2632 if (ptr == NULL
2633 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2634 return FAIL;
2635 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2636 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2637 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2638
2639 return OK;
2640}
2641#endif
2642
2643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * Commands that start with "z".
2645 */
2646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002647nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 long n;
2650 colnr_T col;
2651 int nchar = cap->nchar;
2652#ifdef FEAT_FOLDING
2653 long old_fdl = curwin->w_p_fdl;
2654 int old_fen = curwin->w_p_fen;
2655#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002656 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002658 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (
2662#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002663 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2664 // and "zC" only in Visual mode. "zj" and "zk" are motion
2665 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 cap->nchar != 'f' && cap->nchar != 'F'
2667 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2668 && cap->nchar != 'j' && cap->nchar != 'k'
2669 &&
2670#endif
2671 checkclearop(cap->oap))
2672 return;
2673
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002674 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2675 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2677 && cap->count0
2678 && cap->count0 != curwin->w_cursor.lnum)
2679 {
2680 setpcmark();
2681 if (cap->count0 > curbuf->b_ml.ml_line_count)
2682 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2683 else
2684 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002685 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687
2688 switch (nchar)
2689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002690 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 case '+':
2692 if (cap->count0 == 0)
2693 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002694 // No count given: put cursor at the line below screen
2695 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2697 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2698 else
2699 curwin->w_cursor.lnum = curwin->w_botline;
2700 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 case NL:
2703 case CAR:
2704 case K_KENTER:
2705 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002709 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002710 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002718 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002719 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 break;
2721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // "z^", "z-" and "zb": put cursor at bottom of screen
2723 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2724 // when <count> is at bottom of window, and puts that one at
2725 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if (cap->count0 != 0)
2727 {
2728 scroll_cursor_bot(0, TRUE);
2729 curwin->w_cursor.lnum = curwin->w_topline;
2730 }
2731 else if (curwin->w_topline == 1)
2732 curwin->w_cursor.lnum = 1;
2733 else
2734 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 case '-':
2737 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
2740 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002741 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002742 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 break;
2744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 case 'h':
2752 case K_LEFT:
2753 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002754 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2755 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 break;
2757
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002758 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002759 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002762 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 case 'l':
2764 case K_RIGHT:
2765 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002766 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 break;
2768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case 's': if (!curwin->w_p_wrap)
2771 {
2772#ifdef FEAT_FOLDING
2773 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
2776#endif
2777 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002778 if ((long)col > siso)
2779 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781 col = 0;
2782 if (curwin->w_leftcol != col)
2783 {
2784 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002785 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787 }
2788 break;
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 case 'e': if (!curwin->w_p_wrap)
2792 {
2793#ifdef FEAT_FOLDING
2794 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else
2797#endif
2798 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002800 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 col = 0;
2802 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002803 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (curwin->w_leftcol != col)
2805 {
2806 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002807 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 }
2810 break;
2811
Christian Brabandt2fa93842021-05-30 22:17:25 +02002812 // "zp", "zP" in block mode put without addind trailing spaces
2813 case 'P':
2814 case 'p': nv_put(cap);
2815 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002816 // "zy" Yank without trailing spaces
2817 case 'y': nv_operator(cap);
2818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // "zF": create fold command
2821 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case 'F':
2823 case 'f': if (foldManualAllowed(TRUE))
2824 {
2825 cap->nchar = 'f';
2826 nv_operator(cap);
2827 curwin->w_p_fen = TRUE;
2828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2831 {
2832 nv_operator(cap);
2833 finish_op = TRUE;
2834 }
2835 }
2836 else
2837 clearopbeep(cap->oap);
2838 break;
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // "zd": delete fold at cursor
2841 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case 'd':
2843 case 'D': if (foldManualAllowed(FALSE))
2844 {
2845 if (VIsual_active)
2846 nv_operator(cap);
2847 else
2848 deleteFold(curwin->w_cursor.lnum,
2849 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2850 }
2851 break;
2852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case 'E': if (foldmethodIsManual(curwin))
2855 {
2856 clearFolding(curwin);
2857 changed_window_setting();
2858 }
2859 else if (foldmethodIsMarker(curwin))
2860 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2861 TRUE, FALSE);
2862 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002863 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case 'n': curwin->w_p_fen = FALSE;
2868 break;
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case 'N': curwin->w_p_fen = TRUE;
2872 break;
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2876 break;
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2880 openFold(curwin->w_cursor.lnum, cap->count1);
2881 else
2882 {
2883 closeFold(curwin->w_cursor.lnum, cap->count1);
2884 curwin->w_p_fen = TRUE;
2885 }
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2890 openFoldRecurse(curwin->w_cursor.lnum);
2891 else
2892 {
2893 closeFoldRecurse(curwin->w_cursor.lnum);
2894 curwin->w_p_fen = TRUE;
2895 }
2896 break;
2897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 case 'o': if (VIsual_active)
2900 nv_operator(cap);
2901 else
2902 openFold(curwin->w_cursor.lnum, cap->count1);
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'O': if (VIsual_active)
2907 nv_operator(cap);
2908 else
2909 openFoldRecurse(curwin->w_cursor.lnum);
2910 break;
2911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'c': if (VIsual_active)
2914 nv_operator(cap);
2915 else
2916 closeFold(curwin->w_cursor.lnum, cap->count1);
2917 curwin->w_p_fen = TRUE;
2918 break;
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 case 'C': if (VIsual_active)
2922 nv_operator(cap);
2923 else
2924 closeFoldRecurse(curwin->w_cursor.lnum);
2925 curwin->w_p_fen = TRUE;
2926 break;
2927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 case 'v': foldOpenCursor();
2930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 curwin->w_foldinvalid = TRUE; // recompute folds
2935 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 foldOpenCursor();
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 curwin->w_foldinvalid = TRUE; // recompute folds
2942 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002947 {
2948 curwin->w_p_fdl -= cap->count1;
2949 if (curwin->w_p_fdl < 0)
2950 curwin->w_p_fdl = 0;
2951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 curwin->w_p_fen = TRUE;
2954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 curwin->w_p_fen = TRUE;
2960 break;
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002963 case 'r': curwin->w_p_fdl += cap->count1;
2964 {
2965 int d = getDeepestNesting();
2966
2967 if (curwin->w_p_fdl >= d)
2968 curwin->w_p_fdl = d;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 break;
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 case 'j': // "zj" move to next fold downwards
2978 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2980 cap->count1) == FAIL)
2981 clearopbeep(cap->oap);
2982 break;
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002986#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case 'g': // "zg": add good word to word list
2989 case 'w': // "zw": add wrong word to word list
2990 case 'G': // "zG": add good word to temp word list
2991 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002992 if (nv_zg_zw(cap, nchar) == FAIL)
2993 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002994 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002997 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002998 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 default: clearopbeep(cap->oap);
3003 }
3004
3005#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (old_fen != curwin->w_p_fen)
3008 {
3009# ifdef FEAT_DIFF
3010 win_T *wp;
3011
3012 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003014 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 FOR_ALL_WINDOWS(wp)
3016 {
3017 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3018 {
3019 wp->w_p_fen = curwin->w_p_fen;
3020 changed_window_setting_win(wp);
3021 }
3022 }
3023 }
3024# endif
3025 changed_window_setting();
3026 }
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (old_fdl != curwin->w_p_fdl)
3030 newFoldLevel();
3031#endif
3032}
3033
3034#ifdef FEAT_GUI
3035/*
3036 * Vertical scrollbar movement.
3037 */
3038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003039nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 if (cap->oap->op_type != OP_NOP)
3042 clearopbeep(cap->oap);
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 gui_do_scroll();
3046}
3047
3048/*
3049 * Horizontal scrollbar movement.
3050 */
3051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 if (cap->oap->op_type != OP_NOP)
3055 clearopbeep(cap->oap);
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003058 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059}
3060#endif
3061
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003062#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003063/*
3064 * Click in GUI tab.
3065 */
3066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003068{
3069 if (cap->oap->op_type != OP_NOP)
3070 clearopbeep(cap->oap);
3071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073 goto_tabpage(current_tab);
3074}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003075
3076/*
3077 * Selected item in tab line menu.
3078 */
3079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003081{
3082 if (cap->oap->op_type != OP_NOP)
3083 clearopbeep(cap->oap);
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003086 handle_tabmenu();
3087}
3088
3089/*
3090 * Handle selecting an item of the GUI tab line menu.
3091 * Used in Normal and Insert mode.
3092 */
3093 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003094handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003095{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003096 switch (current_tabmenu)
3097 {
3098 case TABLINE_MENU_CLOSE:
3099 if (current_tab == 0)
3100 do_cmdline_cmd((char_u *)"tabclose");
3101 else
3102 {
3103 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3104 current_tab);
3105 do_cmdline_cmd(IObuff);
3106 }
3107 break;
3108
3109 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003110 if (current_tab == 0)
3111 do_cmdline_cmd((char_u *)"$tabnew");
3112 else
3113 {
3114 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3115 current_tab - 1);
3116 do_cmdline_cmd(IObuff);
3117 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003118 break;
3119
3120 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003121 if (current_tab == 0)
3122 do_cmdline_cmd((char_u *)"browse $tabnew");
3123 else
3124 {
3125 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3126 current_tab - 1);
3127 do_cmdline_cmd(IObuff);
3128 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003129 break;
3130 }
3131}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003132#endif
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * "Q" command.
3136 */
3137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003138nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003140 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003142 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003143 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 do_exmode(FALSE);
3145}
3146
3147/*
3148 * Handle a ":" command.
3149 */
3150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003151nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003153 int old_p_im;
3154 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003155 int is_cmdkey = cap->cmdchar == K_COMMAND
3156 || cap->cmdchar == K_SCRIPT_COMMAND;
3157 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar957cf672020-11-12 14:21:06 +01003159 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 nv_operator(cap);
3161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 if (cap->oap->op_type != OP_NOP)
3164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 cap->oap->motion_type = MCHAR;
3167 cap->oap->inclusive = FALSE;
3168 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003169 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 stuffcharReadbuff('.');
3173 if (cap->count0 > 1)
3174 {
3175 stuffReadbuff((char_u *)",.+");
3176 stuffnumReadbuff((long)cap->count0 - 1L);
3177 }
3178 }
3179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (KeyTyped)
3182 compute_cmdrow();
3183
3184 old_p_im = p_im;
3185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003186 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003187 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3188 if (is_cmdkey)
3189 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3190 else
3191 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (p_im != old_p_im)
3195 {
3196 if (p_im)
3197 restart_edit = 'i';
3198 else
3199 restart_edit = 0;
3200 }
3201
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003202 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003204 clearop(cap->oap);
3205 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3207 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003208 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3209 || did_emsg
3210 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 clearopbeep(cap->oap);
3213 }
3214}
3215
3216/*
3217 * Handle CTRL-G command.
3218 */
3219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003220nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003225 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 showmode();
3227 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003228 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 fileinfo((int)cap->count0, FALSE, TRUE);
3231}
3232
3233/*
3234 * Handle CTRL-H <Backspace> command.
3235 */
3236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003237nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (VIsual_active && VIsual_select)
3240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003241 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 v_visop(cap);
3243 }
3244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 nv_left(cap);
3246}
3247
3248/*
3249 * CTRL-L: clear screen and redraw.
3250 */
3251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003252nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 if (!checkclearop(cap->oap))
3255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003258 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003259# ifdef FEAT_RELTIME
3260 {
3261 win_T *wp;
3262
3263 FOR_ALL_WINDOWS(wp)
3264 wp->w_s->b_syn_slow = FALSE;
3265 }
3266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003268 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003269#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3270# ifdef VIMDLL
3271 if (!gui.in_use)
3272# endif
3273 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276}
3277
3278/*
3279 * CTRL-O: In Select mode: switch to Visual mode for one command.
3280 * Otherwise: Go to older pcmark.
3281 */
3282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (VIsual_active && VIsual_select)
3286 {
3287 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003288 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 cap->count1 = -cap->count1;
3295 nv_pcmark(cap);
3296 }
3297}
3298
3299/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003300 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3301 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
3303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 if (!checkclearopq(cap->oap))
3307 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3308 GETF_SETMARK|GETF_ALT, FALSE);
3309}
3310
3311/*
3312 * "Z" commands.
3313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (!checkclearopq(cap->oap))
3318 {
3319 switch (cap->nchar)
3320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 case 'Z': do_cmdline_cmd((char_u *)"x");
3323 break;
3324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 case 'Q': do_cmdline_cmd((char_u *)"q!");
3327 break;
3328
3329 default: clearopbeep(cap->oap);
3330 }
3331 }
3332}
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3336 */
3337 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003338do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 oparg_T oa;
3341 cmdarg_T ca;
3342
3343 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003344 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 ca.oap = &oa;
3346 ca.cmdchar = c1;
3347 ca.nchar = c2;
3348 nv_ident(&ca);
3349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003352 * 'K' normal-mode command. Get the command to lookup the keyword under the
3353 * cursor.
3354 */
3355 static int
3356nv_K_getcmd(
3357 cmdarg_T *cap,
3358 char_u *kp,
3359 int kp_help,
3360 int kp_ex,
3361 char_u **ptr_arg,
3362 int n,
3363 char_u *buf,
3364 unsigned buflen)
3365{
3366 char_u *ptr = *ptr_arg;
3367 int isman;
3368 int isman_s;
3369
3370 if (kp_help)
3371 {
3372 // in the help buffer
3373 STRCPY(buf, "he! ");
3374 return n;
3375 }
3376
3377 if (kp_ex)
3378 {
3379 // 'keywordprog' is an ex command
3380 if (cap->count0 != 0)
3381 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3382 else
3383 STRCPY(buf, kp);
3384 STRCAT(buf, " ");
3385 return n;
3386 }
3387
3388 // An external command will probably use an argument starting
3389 // with "-" as an option. To avoid trouble we skip the "-".
3390 while (*ptr == '-' && n > 0)
3391 {
3392 ++ptr;
3393 --n;
3394 }
3395 if (n == 0)
3396 {
3397 // found dashes only
3398 emsg(_(e_no_identifier_under_cursor));
3399 vim_free(buf);
3400 *ptr_arg = ptr;
3401 return 0;
3402 }
3403
3404 // When a count is given, turn it into a range. Is this
3405 // really what we want?
3406 isman = (STRCMP(kp, "man") == 0);
3407 isman_s = (STRCMP(kp, "man -s") == 0);
3408 if (cap->count0 != 0 && !(isman || isman_s))
3409 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3410
3411 STRCAT(buf, "! ");
3412 if (cap->count0 == 0 && isman_s)
3413 STRCAT(buf, "man");
3414 else
3415 STRCAT(buf, kp);
3416 STRCAT(buf, " ");
3417 if (cap->count0 != 0 && (isman || isman_s))
3418 {
3419 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3420 STRCAT(buf, " ");
3421 }
3422
3423 *ptr_arg = ptr;
3424 return n;
3425}
3426
3427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 * Handle the commands that use the word under the cursor.
3429 * [g] CTRL-] :ta to current identifier
3430 * [g] 'K' run program for current identifier
3431 * [g] '*' / to current identifier or string
3432 * [g] '#' ? to current identifier or string
3433 * g ']' :tselect for current identifier
3434 */
3435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003436nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 char_u *ptr = NULL;
3439 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003440 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003441 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 char_u *kp; // value of 'keywordprg'
3444 int kp_help; // 'keywordprg' is ":he"
3445 int kp_ex; // 'keywordprg' starts with ":"
3446 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003449 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 cmdchar = cap->nchar;
3455 g_cmd = TRUE;
3456 }
3457 else
3458 {
3459 cmdchar = cap->cmdchar;
3460 g_cmd = FALSE;
3461 }
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 cmdchar = '#';
3465
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003466 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (checkclearopq(cap->oap))
3472 return;
3473 }
3474
3475 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3476 (cmdchar == '*' || cmdchar == '#')
3477 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3478 {
3479 clearop(cap->oap);
3480 return;
3481 }
3482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // Allocate buffer to put the command in. Inserting backslashes can
3484 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3485 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3487 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3488 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003489 if (kp_help && *skipwhite(ptr) == NUL)
3490 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003491 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003492 return;
3493 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003494 kp_ex = (*kp == ':');
3495 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3496 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (buf == NULL)
3498 return;
3499 buf[0] = NUL;
3500
3501 switch (cmdchar)
3502 {
3503 case '*':
3504 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003505 // Put cursor at start of word, makes search skip the word
3506 // under the cursor.
3507 // Call setpcmark() first, so "*``" puts the cursor back where
3508 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 setpcmark();
3510 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3511
3512 if (!g_cmd && vim_iswordp(ptr))
3513 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
3516
3517 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003518 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3519 if (n == 0)
3520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 break;
3522
3523 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003524 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#ifdef FEAT_CSCOPE
3526 if (p_cst)
3527 STRCPY(buf, "cstag ");
3528 else
3529#endif
3530 STRCPY(buf, "ts ");
3531 break;
3532
3533 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003534 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (curbuf->b_help)
3536 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003539 if (g_cmd)
3540 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003541 else if (cap->count0 == 0)
3542 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003543 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003544 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003548 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003549 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003551 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003552 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003554 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003555 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003556 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003557 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003559 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 vim_free(buf);
3562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003564 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003565 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003566 {
3567 vim_free(buf);
3568 vim_free(p);
3569 return;
3570 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003571 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003572 STRCAT(buf, p);
3573 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 else
3576 {
3577 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003578 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003579 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003580 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003581 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003582 {
3583 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003585 aux_ptr = (char_u *)"";
3586 else
3587 aux_ptr = (char_u *)"\\|\"\n[";
3588 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003589 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003590 aux_ptr = (char_u *)"\\|\"\n*?[";
3591
3592 p = buf + STRLEN(buf);
3593 while (n-- > 0)
3594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003595 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596 if (vim_strchr(aux_ptr, *ptr) != NULL)
3597 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 // When current byte is a part of multibyte character, copy all
3599 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 if (has_mbyte)
3601 {
3602 int i;
3603 int len = (*mb_ptr2len)(ptr) - 1;
3604
3605 for (i = 0; i < len && n >= 1; ++i, --n)
3606 *p++ = *ptr++;
3607 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608 *p++ = *ptr++;
3609 }
3610 *p = NUL;
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003613 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (cmdchar == '*' || cmdchar == '#')
3615 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003616 if (!g_cmd && (has_mbyte
3617 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3618 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003620
3621 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003622 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003624
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003625 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003628 {
3629 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003631 g_tag_at_cursor = FALSE;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 vim_free(buf);
3635}
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637/*
3638 * Get visually selected text, within one line only.
3639 * Returns FAIL if more than one line selected.
3640 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003641 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003642get_visual_text(
3643 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 char_u **pp, // return: start of selected text
3645 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 if (VIsual_mode != 'V')
3648 unadjust_for_sel();
3649 if (VIsual.lnum != curwin->w_cursor.lnum)
3650 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003651 if (cap != NULL)
3652 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return FAIL;
3654 }
3655 if (VIsual_mode == 'V')
3656 {
3657 *pp = ml_get_curline();
3658 *lenp = (int)STRLEN(*pp);
3659 }
3660 else
3661 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003662 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 *pp = ml_get_pos(&curwin->w_cursor);
3665 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3666 }
3667 else
3668 {
3669 *pp = ml_get_pos(&VIsual);
3670 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3671 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003672 if (**pp == NUL)
3673 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003674 if (*lenp > 0)
3675 {
3676 if (has_mbyte)
3677 // Correct the length to include all bytes of the last
3678 // character.
3679 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3680 else if ((*pp)[*lenp - 1] == NUL)
3681 // Do not include a trailing NUL.
3682 *lenp -= 1;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 reset_VIsual_and_resel();
3686 return OK;
3687}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
3689/*
3690 * CTRL-T: backwards in tag stack
3691 */
3692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003693nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 if (!checkclearopq(cap->oap))
3696 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3697}
3698
3699/*
3700 * Handle scrolling command 'H', 'L' and 'M'.
3701 */
3702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003703nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 int used = 0;
3706 long n;
3707#ifdef FEAT_FOLDING
3708 linenr_T lnum;
3709#endif
3710 int half;
3711
3712 cap->oap->motion_type = MLINE;
3713 setpcmark();
3714
3715 if (cap->cmdchar == 'L')
3716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003717 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 curwin->w_cursor.lnum = curwin->w_botline - 1;
3719 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3720 curwin->w_cursor.lnum = 1;
3721 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003722 {
3723#ifdef FEAT_FOLDING
3724 if (hasAnyFolding(curwin))
3725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003727 for (n = cap->count1 - 1; n > 0
3728 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3729 {
3730 (void)hasFolding(curwin->w_cursor.lnum,
3731 &curwin->w_cursor.lnum, NULL);
3732 --curwin->w_cursor.lnum;
3733 }
3734 }
3735 else
3736#endif
3737 curwin->w_cursor.lnum -= cap->count1 - 1;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 else
3741 {
3742 if (cap->cmdchar == 'M')
3743 {
3744#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003745 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 used -= diff_check_fill(curwin, curwin->w_topline)
3747 - curwin->w_topfill;
3748#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003749 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3751 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3752 {
3753#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // Count half he number of filler lines to be "below this
3755 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3757 + n) / 2 >= half)
3758 {
3759 --n;
3760 break;
3761 }
3762#endif
3763 used += plines(curwin->w_topline + n);
3764 if (used >= half)
3765 break;
3766#ifdef FEAT_FOLDING
3767 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3768 n = lnum - curwin->w_topline;
3769#endif
3770 }
3771 if (n > 0 && used > curwin->w_height)
3772 --n;
3773 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003774 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003777#ifdef FEAT_FOLDING
3778 if (hasAnyFolding(curwin))
3779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003780 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003781 lnum = curwin->w_topline;
3782 while (n-- > 0 && lnum < curwin->w_botline - 1)
3783 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003784 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003785 ++lnum;
3786 }
3787 n = lnum - curwin->w_topline;
3788 }
3789#endif
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 curwin->w_cursor.lnum = curwin->w_topline + n;
3792 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3793 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3794 }
3795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003796 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003797 if (cap->oap->op_type == OP_NOP)
3798 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 beginline(BL_SOL | BL_FIX);
3800}
3801
3802/*
3803 * Cursor right commands.
3804 */
3805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003806nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003809 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003814 if (mod_mask & MOD_MASK_CTRL)
3815 cap->arg = TRUE;
3816 nv_wordcmd(cap);
3817 return;
3818 }
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 cap->oap->motion_type = MCHAR;
3821 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003822 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003824 // In virtual edit mode, there's no such thing as "past_line", as lines
3825 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003827 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 for (n = cap->count1; n > 0; --n)
3830 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003831 if ((!past_line && oneright() == FAIL)
3832 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003833 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003835 // <Space> wraps to next line if 'whichwrap' has 's'.
3836 // 'l' wraps to next line if 'whichwrap' has 'l'.
3837 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if ( ((cap->cmdchar == ' '
3839 && vim_strchr(p_ww, 's') != NULL)
3840 || (cap->cmdchar == 'l'
3841 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003842 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 && vim_strchr(p_ww, '>') != NULL))
3844 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003846 // When deleting we also count the NL as a character.
3847 // Set cap->oap->inclusive when last char in the line is
3848 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003849 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003851 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 cap->oap->inclusive = TRUE;
3853 else
3854 {
3855 ++curwin->w_cursor.lnum;
3856 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 curwin->w_set_curswant = TRUE;
3859 cap->oap->inclusive = FALSE;
3860 }
3861 continue;
3862 }
3863 if (cap->oap->op_type == OP_NOP)
3864 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003865 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (n == cap->count1)
3867 beep_flush();
3868 }
3869 else
3870 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003871 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 cap->oap->inclusive = TRUE;
3873 }
3874 break;
3875 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003876 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (virtual_active())
3880 oneright();
3881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003884 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 ++curwin->w_cursor.col;
3887 }
3888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890#ifdef FEAT_FOLDING
3891 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3892 && cap->oap->op_type == OP_NOP)
3893 foldOpenCursor();
3894#endif
3895}
3896
3897/*
3898 * Cursor left commands.
3899 *
3900 * Returns TRUE when operator end should not be adjusted.
3901 */
3902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003903nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 long n;
3906
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003907 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003909 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003910 if (mod_mask & MOD_MASK_CTRL)
3911 cap->arg = 1;
3912 nv_bck_word(cap);
3913 return;
3914 }
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 cap->oap->motion_type = MCHAR;
3917 cap->oap->inclusive = FALSE;
3918 for (n = cap->count1; n > 0; --n)
3919 {
3920 if (oneleft() == FAIL)
3921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3923 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3924 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if ( (((cap->cmdchar == K_BS
3926 || cap->cmdchar == Ctrl_H)
3927 && vim_strchr(p_ww, 'b') != NULL)
3928 || (cap->cmdchar == 'h'
3929 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003930 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 && vim_strchr(p_ww, '<') != NULL))
3932 && curwin->w_cursor.lnum > 1)
3933 {
3934 --(curwin->w_cursor.lnum);
3935 coladvance((colnr_T)MAXCOL);
3936 curwin->w_set_curswant = TRUE;
3937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 // When the NL before the first char has to be deleted we
3939 // put the cursor on the NUL after the previous line.
3940 // This is a very special case, be careful!
3941 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if ( (cap->oap->op_type == OP_DELETE
3943 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003944 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003946 char_u *cp = ml_get_cursor();
3947
3948 if (*cp != NUL)
3949 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003950 if (has_mbyte)
3951 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3952 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003953 ++curwin->w_cursor.col;
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 cap->retval |= CA_NO_ADJ_OP_END;
3956 }
3957 continue;
3958 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003959 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3961 beep_flush();
3962 break;
3963 }
3964 }
3965#ifdef FEAT_FOLDING
3966 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3967 && cap->oap->op_type == OP_NOP)
3968 foldOpenCursor();
3969#endif
3970}
3971
3972/*
3973 * Cursor up commands.
3974 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3975 */
3976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003977nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003979 if (mod_mask & MOD_MASK_SHIFT)
3980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003981 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 cap->arg = BACKWARD;
3983 nv_page(cap);
3984 }
3985 else
3986 {
3987 cap->oap->motion_type = MLINE;
3988 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3989 clearopbeep(cap->oap);
3990 else if (cap->arg)
3991 beginline(BL_WHITE | BL_FIX);
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993}
3994
3995/*
3996 * Cursor down commands.
3997 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3998 */
3999 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004000nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004002 if (mod_mask & MOD_MASK_SHIFT)
4003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004005 cap->arg = FORWARD;
4006 nv_page(cap);
4007 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004008#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004010 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4011 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004013 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004015 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004016 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 cmdwin_result = CAR;
4018 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004019#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004021 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4022 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4023 {
4024 invoke_prompt_callback();
4025 if (restart_edit == 0)
4026 restart_edit = 'a';
4027 }
4028 else
4029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
4031 cap->oap->motion_type = MLINE;
4032 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4033 clearopbeep(cap->oap);
4034 else if (cap->arg)
4035 beginline(BL_WHITE | BL_FIX);
4036 }
4037 }
4038}
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040/*
4041 * Grab the file name under the cursor and edit it.
4042 */
4043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004044nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004047 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004049 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004051
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004052#ifdef FEAT_PROP_POPUP
4053 if (ERROR_IF_TERM_POPUP_WINDOW)
4054 return;
4055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004057 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 if (ptr != NULL)
4060 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004061 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004062 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004063 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004065 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004066 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004067 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004068 {
4069 curwin->w_cursor.lnum = lnum;
4070 check_cursor_lnum();
4071 beginline(BL_SOL | BL_FIX);
4072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 vim_free(ptr);
4074 }
4075 else
4076 clearop(cap->oap);
4077}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079/*
4080 * <End> command: to end of current line or last line.
4081 */
4082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004083nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004085 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004087 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004089 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 nv_dollar(cap);
4092}
4093
4094/*
4095 * Handle the "$" command.
4096 */
4097 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004098nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 cap->oap->motion_type = MCHAR;
4101 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 // In virtual mode when off the edge of a line and an operator
4103 // is pending (whew!) keep the cursor where it is.
4104 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (!virtual_active() || gchar_cursor() != NUL
4106 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004107 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (cursor_down((long)(cap->count1 - 1),
4109 cap->oap->op_type == OP_NOP) == FAIL)
4110 clearopbeep(cap->oap);
4111#ifdef FEAT_FOLDING
4112 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4113 foldOpenCursor();
4114#endif
4115}
4116
4117/*
4118 * Implementation of '?' and '/' commands.
4119 * If cap->arg is TRUE don't set PC mark.
4120 */
4121 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004122nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004125 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4128 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004129 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 cap->cmdchar = 'g';
4131 cap->nchar = '?';
4132 nv_operator(cap);
4133 return;
4134 }
4135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // When using 'incsearch' the cursor may be moved to set a different search
4137 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004138 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 if (cap->searchbuf == NULL)
4141 {
4142 clearop(oap);
4143 return;
4144 }
4145
Bram Moolenaar46539112015-02-17 15:43:57 +01004146 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004147 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004148 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149}
4150
LemonBoya4399382022-04-09 21:04:08 +01004151
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152/*
4153 * Handle "N" and "n" commands.
4154 * cap->arg is SEARCH_REV for "N", 0 for "n".
4155 */
4156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004157nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004159 pos_T old = curwin->w_cursor;
4160 int wrapped = FALSE;
4161 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004162
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004163 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004165 // Avoid getting stuck on the current cursor position, which can
4166 // happen when an offset is given and the cursor is on the last char
4167 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004168 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004169 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004170 cap->count1 -= 1;
4171 }
LemonBoya4399382022-04-09 21:04:08 +01004172
4173#ifdef FEAT_SEARCH_EXTRA
4174 // Redraw the window to refresh the highlighted matches.
4175 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004176 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178}
4179
4180/*
4181 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4182 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004183 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004185 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004186normal_search(
4187 cmdarg_T *cap,
4188 int dir,
4189 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004190 int opt, // extra flags for do_search()
4191 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004194 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004195#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004196 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
4199 cap->oap->motion_type = MCHAR;
4200 cap->oap->inclusive = FALSE;
4201 cap->oap->use_reg_one = TRUE;
4202 curwin->w_set_curswant = TRUE;
4203
Bram Moolenaara80faa82020-04-12 19:37:17 +02004204 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004205 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004206 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4207 if (wrapped != NULL)
4208 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (i == 0)
4210 clearop(cap->oap);
4211 else
4212 {
4213 if (i == 2)
4214 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216#ifdef FEAT_FOLDING
4217 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4218 foldOpenCursor();
4219#endif
4220 }
LemonBoya4399382022-04-09 21:04:08 +01004221#ifdef FEAT_SEARCH_EXTRA
4222 // Redraw the window to refresh the highlighted matches.
4223 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004224 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004227 // "/$" will put the cursor after the end of the line, may need to
4228 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004230 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
4233/*
4234 * Character search commands.
4235 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4236 * ',' and FALSE for ';'.
4237 * cap->nchar is NUL for ',' and ';' (repeat the search)
4238 */
4239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004240nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
4242 int t_cmd;
4243
4244 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4245 t_cmd = TRUE;
4246 else
4247 t_cmd = FALSE;
4248
4249 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4251 clearopbeep(cap->oap);
4252 else
4253 {
4254 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004255 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4257 && (t_cmd || cap->oap->op_type != OP_NOP))
4258 {
4259 colnr_T scol, ecol;
4260
4261 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4262 curwin->w_cursor.coladd = ecol - scol;
4263 }
4264 else
4265 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267#ifdef FEAT_FOLDING
4268 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4269 foldOpenCursor();
4270#endif
4271 }
4272}
4273
4274/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004275 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4276 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4277 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4278 * "[m" or "]m" search for prev/next start of (Java) method.
4279 * "[M" or "]M" search for prev/next end of (Java) method.
4280 */
4281 static void
4282nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4283{
4284 pos_T new_pos = {0, 0, 0};
4285 pos_T *pos = NULL; // init for GCC
4286 pos_T prev_pos;
4287 long n;
4288 int findc;
4289 int c;
4290
4291 if (cap->nchar == '*')
4292 cap->nchar = '/';
4293 prev_pos.lnum = 0;
4294 if (cap->nchar == 'm' || cap->nchar == 'M')
4295 {
4296 if (cap->cmdchar == '[')
4297 findc = '{';
4298 else
4299 findc = '}';
4300 n = 9999;
4301 }
4302 else
4303 {
4304 findc = cap->nchar;
4305 n = cap->count1;
4306 }
4307 for ( ; n > 0; --n)
4308 {
4309 if ((pos = findmatchlimit(cap->oap, findc,
4310 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4311 {
4312 if (new_pos.lnum == 0) // nothing found
4313 {
4314 if (cap->nchar != 'm' && cap->nchar != 'M')
4315 clearopbeep(cap->oap);
4316 }
4317 else
4318 pos = &new_pos; // use last one found
4319 break;
4320 }
4321 prev_pos = new_pos;
4322 curwin->w_cursor = *pos;
4323 new_pos = *pos;
4324 }
4325 curwin->w_cursor = *old_pos;
4326
4327 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4328 // brought us to the match for "[m" and "]M" when inside a method.
4329 // Try finding the '{' or '}' we want to be at.
4330 // Also repeat for the given count.
4331 if (cap->nchar == 'm' || cap->nchar == 'M')
4332 {
4333 // norm is TRUE for "]M" and "[m"
4334 int norm = ((findc == '{') == (cap->nchar == 'm'));
4335
4336 n = cap->count1;
4337 // found a match: we were inside a method
4338 if (prev_pos.lnum != 0)
4339 {
4340 pos = &prev_pos;
4341 curwin->w_cursor = prev_pos;
4342 if (norm)
4343 --n;
4344 }
4345 else
4346 pos = NULL;
4347 while (n > 0)
4348 {
4349 for (;;)
4350 {
4351 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4352 {
4353 // if not found anything, that's an error
4354 if (pos == NULL)
4355 clearopbeep(cap->oap);
4356 n = 0;
4357 break;
4358 }
4359 c = gchar_cursor();
4360 if (c == '{' || c == '}')
4361 {
4362 // Must have found end/start of class: use it.
4363 // Or found the place to be at.
4364 if ((c == findc && norm) || (n == 1 && !norm))
4365 {
4366 new_pos = curwin->w_cursor;
4367 pos = &new_pos;
4368 n = 0;
4369 }
4370 // if no match found at all, we started outside of the
4371 // class and we're inside now. Just go on.
4372 else if (new_pos.lnum == 0)
4373 {
4374 new_pos = curwin->w_cursor;
4375 pos = &new_pos;
4376 }
4377 // found start/end of other method: go to match
4378 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004379 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4380 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004381 n = 0;
4382 else
4383 curwin->w_cursor = *pos;
4384 break;
4385 }
4386 }
4387 --n;
4388 }
4389 curwin->w_cursor = *old_pos;
4390 if (pos == NULL && new_pos.lnum != 0)
4391 clearopbeep(cap->oap);
4392 }
4393 if (pos != NULL)
4394 {
4395 setpcmark();
4396 curwin->w_cursor = *pos;
4397 curwin->w_set_curswant = TRUE;
4398#ifdef FEAT_FOLDING
4399 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4400 && cap->oap->op_type == OP_NOP)
4401 foldOpenCursor();
4402#endif
4403 }
4404}
4405
4406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 * "[" and "]" commands.
4408 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4409 */
4410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004411nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004414 pos_T *pos = NULL; // init for GCC
4415 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 int flag;
4417 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 cap->oap->motion_type = MCHAR;
4420 cap->oap->inclusive = FALSE;
4421 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004422 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004424 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (cap->nchar == 'f')
4426 nv_gotofile(cap);
4427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004430 // Find the occurrence(s) of the identifier or define under cursor
4431 // in current and included files or jump to the first occurrence.
4432 //
4433 // search list jump
4434 // fwd bwd fwd bwd fwd bwd
4435 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4436 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004437 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 char_u *ptr;
4440 int len;
4441
4442 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4443 clearop(cap->oap);
4444 else
4445 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004446 // Make a copy, if the line was changed it will be freed.
4447 ptr = vim_strnsave(ptr, len);
4448 if (ptr == NULL)
4449 return;
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 find_pattern_in_path(ptr, 0, len, TRUE,
4452 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4453 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4454 cap->count1,
4455 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4456 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4457 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4458 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004459 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 curwin->w_set_curswant = TRUE;
4461 }
4462 }
4463 else
4464#endif
4465
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004466 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4467 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4468 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4469 // "[m" or "]m" search for prev/next start of (Java) method.
4470 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 if ( (cap->cmdchar == '['
4472 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4473 || (cap->cmdchar == ']'
4474 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004475 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004477 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else if (cap->nchar == '[' || cap->nchar == ']')
4479 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004480 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 flag = '{';
4482 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004483 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004486 // Imitate strange Vi behaviour: When using "]]" with an operator
4487 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004488 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 (cap->oap->op_type != OP_NOP
4490 && cap->arg == FORWARD && flag == '{')))
4491 clearopbeep(cap->oap);
4492 else
4493 {
4494 if (cap->oap->op_type == OP_NOP)
4495 beginline(BL_WHITE | BL_FIX);
4496#ifdef FEAT_FOLDING
4497 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4498 foldOpenCursor();
4499#endif
4500 }
4501 }
4502
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004503 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else if (cap->nchar == 'p' || cap->nchar == 'P')
4505 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004506 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004509 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 else if (cap->nchar == '\'' || cap->nchar == '`')
4511 {
4512 pos = &curwin->w_cursor;
4513 for (n = cap->count1; n > 0; --n)
4514 {
4515 prev_pos = *pos;
4516 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4517 cap->nchar == '\'');
4518 if (pos == NULL)
4519 break;
4520 }
4521 if (pos == NULL)
4522 pos = &prev_pos;
4523 nv_cursormark(cap, cap->nchar == '\'', pos);
4524 }
4525
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004526 // [ or ] followed by a middle mouse click: put selected text with
4527 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004528 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 (void)do_mouse(cap->oap, cap->nchar,
4531 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4532 cap->count1, PUT_FIXINDENT);
4533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004536 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else if (cap->nchar == 'z')
4538 {
4539 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4540 cap->count1) == FAIL)
4541 clearopbeep(cap->oap);
4542 }
4543#endif
4544
4545#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004546 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else if (cap->nchar == 'c')
4548 {
4549 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4550 cap->count1) == FAIL)
4551 clearopbeep(cap->oap);
4552 }
4553#endif
4554
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004555#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004556 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004557 else if (cap->nchar == 's' || cap->nchar == 'S')
4558 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004559 setpcmark();
4560 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004561 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4562 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004563 {
4564 clearopbeep(cap->oap);
4565 break;
4566 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004567 else
4568 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004569# ifdef FEAT_FOLDING
4570 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4571 foldOpenCursor();
4572# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 }
4574#endif
4575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004576 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 clearopbeep(cap->oap);
4579}
4580
4581/*
4582 * Handle Normal mode "%" command.
4583 */
4584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004585nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586{
4587 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004588#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 linenr_T lnum = curwin->w_cursor.lnum;
4590#endif
4591
4592 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
4595 if (cap->count0 > 100)
4596 clearopbeep(cap->oap);
4597 else
4598 {
4599 cap->oap->motion_type = MLINE;
4600 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004601 // Round up, so 'normal 100%' always jumps at the line line.
4602 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4603 // overflow on 32-bits, so use a formula with less accuracy
4604 // to avoid overflows.
4605 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4607 / 100L * cap->count0;
4608 else
4609 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4610 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004611 if (curwin->w_cursor.lnum < 1)
4612 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4614 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4615 beginline(BL_SOL | BL_FIX);
4616 }
4617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 cap->oap->motion_type = MCHAR;
4621 cap->oap->use_reg_one = TRUE;
4622 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4623 clearopbeep(cap->oap);
4624 else
4625 {
4626 setpcmark();
4627 curwin->w_cursor = *pos;
4628 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 }
4633#ifdef FEAT_FOLDING
4634 if (cap->oap->op_type == OP_NOP
4635 && lnum != curwin->w_cursor.lnum
4636 && (fdo_flags & FDO_PERCENT)
4637 && KeyTyped)
4638 foldOpenCursor();
4639#endif
4640}
4641
4642/*
4643 * Handle "(" and ")" commands.
4644 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4645 */
4646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004647nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
4649 cap->oap->motion_type = MCHAR;
4650 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004652 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 curwin->w_set_curswant = TRUE;
4654
4655 if (findsent(cap->arg, cap->count1) == FAIL)
4656 clearopbeep(cap->oap);
4657 else
4658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004659 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004660 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#ifdef FEAT_FOLDING
4663 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4664 foldOpenCursor();
4665#endif
4666 }
4667}
4668
4669/*
4670 * "m" command: Mark a position.
4671 */
4672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004673nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 if (!checkclearop(cap->oap))
4676 {
4677 if (setmark(cap->nchar) == FAIL)
4678 clearopbeep(cap->oap);
4679 }
4680}
4681
4682/*
4683 * "{" and "}" commands.
4684 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4685 */
4686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004687nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689 cap->oap->motion_type = MCHAR;
4690 cap->oap->inclusive = FALSE;
4691 cap->oap->use_reg_one = TRUE;
4692 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004693 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 clearopbeep(cap->oap);
4695 else
4696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698#ifdef FEAT_FOLDING
4699 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4700 foldOpenCursor();
4701#endif
4702 }
4703}
4704
4705/*
4706 * "u" command: Undo or make lower case.
4707 */
4708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004709nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004711 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004713 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 cap->cmdchar = 'g';
4715 cap->nchar = 'u';
4716 nv_operator(cap);
4717 }
4718 else
4719 nv_kundo(cap);
4720}
4721
4722/*
4723 * <Undo> command.
4724 */
4725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004726nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
4728 if (!checkclearopq(cap->oap))
4729 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004730#ifdef FEAT_JOB_CHANNEL
4731 if (bt_prompt(curbuf))
4732 {
4733 clearopbeep(cap->oap);
4734 return;
4735 }
4736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 u_undo((int)cap->count1);
4738 curwin->w_set_curswant = TRUE;
4739 }
4740}
4741
4742/*
4743 * Handle the "r" command.
4744 */
4745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 char_u *ptr;
4749 int had_ctrl_v;
4750 long n;
4751
4752 if (checkclearop(cap->oap))
4753 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004754#ifdef FEAT_JOB_CHANNEL
4755 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4756 {
4757 clearopbeep(cap->oap);
4758 return;
4759 }
4760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004762 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (cap->nchar == Ctrl_V)
4764 {
4765 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004766 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004767 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (cap->nchar > DEL)
4769 had_ctrl_v = NUL;
4770 }
4771 else
4772 had_ctrl_v = NUL;
4773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004775 if (IS_SPECIAL(cap->nchar))
4776 {
4777 clearopbeep(cap->oap);
4778 return;
4779 }
4780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004781 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (VIsual_active)
4783 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004784 if (got_int)
4785 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004786 if (had_ctrl_v)
4787 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // Use a special (negative) number to make a difference between a
4789 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004790 if (cap->nchar == CAR)
4791 cap->nchar = REPLACE_CR_NCHAR;
4792 else if (cap->nchar == NL)
4793 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 nv_operator(cap);
4796 return;
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004799 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if (virtual_active())
4801 {
4802 if (u_save_cursor() == FAIL)
4803 return;
4804 if (gchar_cursor() == NUL)
4805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004806 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 coladvance_force((colnr_T)(getviscol() + cap->count1));
4808 curwin->w_cursor.col -= cap->count1;
4809 }
4810 else if (gchar_cursor() == TAB)
4811 coladvance_force(getviscol());
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004814 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004816 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004817 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 clearopbeep(cap->oap);
4820 return;
4821 }
4822
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004823 // Replacing with a TAB is done by edit() when it is complicated because
4824 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4825 // Other characters are done below to avoid problems with things like
4826 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4828 {
4829 stuffnumReadbuff(cap->count1);
4830 stuffcharReadbuff('R');
4831 stuffcharReadbuff('\t');
4832 stuffcharReadbuff(ESC);
4833 return;
4834 }
4835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if (u_save_cursor() == FAIL)
4838 return;
4839
4840 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4841 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004842 // Replace character(s) by a single newline.
4843 // Strange vi behaviour: Only one newline is inserted.
4844 // Delete the characters here.
4845 // Insert the newline with an insert command, takes care of
4846 // autoindent. The insert command depends on being on the last
4847 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004848 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 stuffcharReadbuff('\r');
4850 stuffcharReadbuff(ESC);
4851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004852 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 invoke_edit(cap, TRUE, 'r', FALSE);
4854 }
4855 else
4856 {
4857 prep_redo(cap->oap->regname, cap->count1,
4858 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4859
4860 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (has_mbyte)
4862 {
4863 int old_State = State;
4864
4865 if (cap->ncharC1 != 0)
4866 AppendCharToRedobuff(cap->ncharC1);
4867 if (cap->ncharC2 != 0)
4868 AppendCharToRedobuff(cap->ncharC2);
4869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 // This is slow, but it handles replacing a single-byte with a
4871 // multi-byte and the other way around. Also handles adding
4872 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 for (n = cap->count1; n > 0; --n)
4874 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004875 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004876 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4877 {
4878 int c = ins_copychar(curwin->w_cursor.lnum
4879 + (cap->nchar == Ctrl_Y ? -1 : 1));
4880 if (c != NUL)
4881 ins_char(c);
4882 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004883 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004884 ++curwin->w_cursor.col;
4885 }
4886 else
4887 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 State = old_State;
4889 if (cap->ncharC1 != 0)
4890 ins_char(cap->ncharC1);
4891 if (cap->ncharC2 != 0)
4892 ins_char(cap->ncharC2);
4893 }
4894 }
4895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004897 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 for (n = cap->count1; n > 0; --n)
4899 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004900 // Get ptr again, because u_save and/or showmatch() will have
4901 // released the line. This may also happen in ins_copychar().
4902 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004903 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4904 {
4905 int c = ins_copychar(curwin->w_cursor.lnum
4906 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004907
4908 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004909 if (c != NUL)
4910 ptr[curwin->w_cursor.col] = c;
4911 }
4912 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004913 {
4914 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004915 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (p_sm && msg_silent == 0)
4918 showmatch(cap->nchar);
4919 ++curwin->w_cursor.col;
4920 }
4921#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004922 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004924 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
Bram Moolenaar009b2592004-10-24 19:18:58 +00004926 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004927 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004929 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931#endif
4932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004933 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 changed_bytes(curwin->w_cursor.lnum,
4935 (colnr_T)(curwin->w_cursor.col - cap->count1));
4936 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004937 --curwin->w_cursor.col; // cursor on the last replaced char
4938 // if the character on the left of the current cursor is a multi-byte
4939 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (has_mbyte)
4941 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 curbuf->b_op_end = curwin->w_cursor;
4943 curwin->w_set_curswant = TRUE;
4944 set_last_insert(cap->nchar);
4945 }
4946}
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948/*
4949 * 'o': Exchange start and end of Visual area.
4950 * 'O': same, but in block mode exchange left and right corners.
4951 */
4952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004953v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 pos_T old_cursor;
4956 colnr_T left, right;
4957
4958 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4959 {
4960 old_cursor = curwin->w_cursor;
4961 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4962 curwin->w_cursor.lnum = VIsual.lnum;
4963 coladvance(left);
4964 VIsual = curwin->w_cursor;
4965
4966 curwin->w_cursor.lnum = old_cursor.lnum;
4967 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004968 // 'selection "exclusive" and cursor at right-bottom corner: move it
4969 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4971 ++curwin->w_curswant;
4972 coladvance(curwin->w_curswant);
4973 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004975 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 curwin->w_cursor.lnum = VIsual.lnum;
4978 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4979 ++right;
4980 coladvance(right);
4981 VIsual = curwin->w_cursor;
4982
4983 curwin->w_cursor.lnum = old_cursor.lnum;
4984 coladvance(left);
4985 curwin->w_curswant = left;
4986 }
4987 }
4988 else
4989 {
4990 old_cursor = curwin->w_cursor;
4991 curwin->w_cursor = VIsual;
4992 VIsual = old_cursor;
4993 curwin->w_set_curswant = TRUE;
4994 }
4995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997/*
4998 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4999 */
5000 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005001nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 cap->cmdchar = 'c';
5006 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005007 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 VIsual_mode = 'V';
5009 nv_operator(cap);
5010 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005011 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005014 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 else
5016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (virtual_active())
5018 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5020 }
5021 }
5022}
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024/*
5025 * "gr".
5026 */
5027 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005028nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (VIsual_active)
5031 {
5032 cap->cmdchar = 'r';
5033 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005034 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005036 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
5038 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005039 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 else
5041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005042 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005043 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 stuffcharReadbuff(cap->extra_char);
5045 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (virtual_active())
5047 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 invoke_edit(cap, TRUE, 'v', FALSE);
5049 }
5050 }
5051}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053/*
5054 * Swap case for "~" command, when it does not work like an operator.
5055 */
5056 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005057n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
5059 long n;
5060 pos_T startpos;
5061 int did_change = 0;
5062#ifdef FEAT_NETBEANS_INTG
5063 pos_T pos;
5064 char_u *ptr;
5065 int count;
5066#endif
5067
5068 if (checkclearopq(cap->oap))
5069 return;
5070
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005071 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 clearopbeep(cap->oap);
5074 return;
5075 }
5076
5077 prep_redo_cmd(cap);
5078
5079 if (u_save_cursor() == FAIL)
5080 return;
5081
5082 startpos = curwin->w_cursor;
5083#ifdef FEAT_NETBEANS_INTG
5084 pos = startpos;
5085#endif
5086 for (n = cap->count1; n > 0; --n)
5087 {
5088 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5089 inc_cursor();
5090 if (gchar_cursor() == NUL)
5091 {
5092 if (vim_strchr(p_ww, '~') != NULL
5093 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5094 {
5095#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005096 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 if (did_change)
5099 {
5100 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005101 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005102 netbeans_removed(curbuf, pos.lnum, pos.col,
5103 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005104 // line may have been flushed, get it again
5105 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005107 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 pos.col = 0;
5110 pos.lnum++;
5111 }
5112#endif
5113 ++curwin->w_cursor.lnum;
5114 curwin->w_cursor.col = 0;
5115 if (n > 1)
5116 {
5117 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5118 break;
5119 u_clearline();
5120 }
5121 }
5122 else
5123 break;
5124 }
5125 }
5126#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005127 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 ptr = ml_get(pos.lnum);
5130 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005131 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5132 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134#endif
5135
5136
5137 check_cursor();
5138 curwin->w_set_curswant = TRUE;
5139 if (did_change)
5140 {
5141 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5142 0L);
5143 curbuf->b_op_start = startpos;
5144 curbuf->b_op_end = curwin->w_cursor;
5145 if (curbuf->b_op_end.col > 0)
5146 --curbuf->b_op_end.col;
5147 }
5148}
5149
5150/*
5151 * Move cursor to mark.
5152 */
5153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005154nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 if (check_mark(pos) == FAIL)
5157 clearop(cap->oap);
5158 else
5159 {
5160 if (cap->cmdchar == '\''
5161 || cap->cmdchar == '`'
5162 || cap->cmdchar == '['
5163 || cap->cmdchar == ']')
5164 setpcmark();
5165 curwin->w_cursor = *pos;
5166 if (flag)
5167 beginline(BL_WHITE | BL_FIX);
5168 else
5169 check_cursor();
5170 }
5171 cap->oap->motion_type = flag ? MLINE : MCHAR;
5172 if (cap->cmdchar == '`')
5173 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005174 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 curwin->w_set_curswant = TRUE;
5176}
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178/*
5179 * Handle commands that are operators in Visual mode.
5180 */
5181 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005182v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005186 // Uppercase means linewise, except in block mode, then "D" deletes till
5187 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (isupper(cap->cmdchar))
5189 {
5190 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005191 {
5192 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5196 curwin->w_curswant = MAXCOL;
5197 }
5198 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5199 nv_operator(cap);
5200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202/*
5203 * "s" and "S" commands.
5204 */
5205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005206nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005208#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005209 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005210 if (term_swap_diff() == OK)
5211 return;
5212#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005213#ifdef FEAT_JOB_CHANNEL
5214 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5215 {
5216 clearopbeep(cap->oap);
5217 return;
5218 }
5219#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005220 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005223 {
5224 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 cap->cmdchar = 'c';
5228 nv_operator(cap);
5229 }
5230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 nv_optrans(cap);
5232}
5233
5234/*
5235 * Abbreviated commands.
5236 */
5237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005241 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005243 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (VIsual_active)
5245 v_visop(cap);
5246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 nv_optrans(cap);
5248}
5249
5250/*
5251 * Translate a command into another command.
5252 */
5253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5257 (char_u *)"d$", (char_u *)"c$",
5258 (char_u *)"cl", (char_u *)"cc",
5259 (char_u *)"yy", (char_u *)":s\r"};
5260 static char_u *str = (char_u *)"xXDCsSY&";
5261
5262 if (!checkclearopq(cap->oap))
5263 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005264 // In Vi "2D" doesn't delete the next line. Can't translate it
5265 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005266 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5267 {
5268 cap->oap->start = curwin->w_cursor;
5269 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005270#ifdef FEAT_EVAL
5271 set_op_var(OP_DELETE);
5272#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005273 cap->count1 = 1;
5274 nv_dollar(cap);
5275 finish_op = TRUE;
5276 ResetRedobuff();
5277 AppendCharToRedobuff('D');
5278 }
5279 else
5280 {
5281 if (cap->count0)
5282 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005283 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 cap->opcount = 0;
5287}
5288
5289/*
5290 * "'" and "`" commands. Also for "g'" and "g`".
5291 * cap->arg is TRUE for "'" and "g'".
5292 */
5293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005294nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 pos_T *pos;
5297 int c;
5298#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005299 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005300 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#endif
5302
5303 if (cap->cmdchar == 'g')
5304 c = cap->extra_char;
5305 else
5306 c = cap->nchar;
5307 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005308 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 if (cap->arg)
5311 {
5312 check_cursor_lnum();
5313 beginline(BL_WHITE | BL_FIX);
5314 }
5315 else
5316 check_cursor();
5317 }
5318 else
5319 nv_cursormark(cap, cap->arg, pos);
5320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (!virtual_active())
5323 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005324 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#ifdef FEAT_FOLDING
5326 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005327 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005328 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 && (fdo_flags & FDO_MARK)
5330 && old_KeyTyped)
5331 foldOpenCursor();
5332#endif
5333}
5334
5335/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005336 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
5338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005339nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005342#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005344 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 if (!checkclearopq(cap->oap))
5348 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005349 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5350 {
5351 if (goto_tabpage_lastused() == FAIL)
5352 clearopbeep(cap->oap);
5353 return;
5354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (cap->cmdchar == 'g')
5356 pos = movechangelist((int)cap->count1);
5357 else
5358 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005359 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
5361 curwin->w_set_curswant = TRUE;
5362 check_cursor();
5363 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005364 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 nv_cursormark(cap, FALSE, pos);
5366 else if (cap->cmdchar == 'g')
5367 {
5368 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005369 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005371 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005373 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 else
5376 clearopbeep(cap->oap);
5377# ifdef FEAT_FOLDING
5378 if (cap->oap->op_type == OP_NOP
5379 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5380 && (fdo_flags & FDO_MARK)
5381 && old_KeyTyped)
5382 foldOpenCursor();
5383# endif
5384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385}
5386
5387/*
5388 * Handle '"' command.
5389 */
5390 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005391nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 if (checkclearop(cap->oap))
5394 return;
5395#ifdef FEAT_EVAL
5396 if (cap->nchar == '=')
5397 cap->nchar = get_expr_register();
5398#endif
5399 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5400 {
5401 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005402 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#ifdef FEAT_EVAL
5404 set_reg_var(cap->oap->regname);
5405#endif
5406 }
5407 else
5408 clearopbeep(cap->oap);
5409}
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411/*
5412 * Handle "v", "V" and "CTRL-V" commands.
5413 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5414 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005415 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 */
5417 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005418nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005420 if (cap->cmdchar == Ctrl_Q)
5421 cap->cmdchar = Ctrl_V;
5422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005423 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5424 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (cap->oap->op_type != OP_NOP)
5426 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005427 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005428 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 return;
5430 }
5431
5432 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005435 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005437 else // toggle char/block mode
5438 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 VIsual_mode = cap->cmdchar;
5440 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005441 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005443 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005445 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005448 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 VIsual = curwin->w_cursor;
5452
5453 VIsual_active = TRUE;
5454 VIsual_reselect = TRUE;
5455 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005459 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005460 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005461 // For V and ^V, we multiply the number of lines even if there
5462 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5464 {
5465 curwin->w_cursor.lnum +=
5466 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005467 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 VIsual_mode = resel_VIsual_mode;
5470 if (VIsual_mode == 'v')
5471 {
5472 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005473 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005474 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005475 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5476 if (*p_sel != 'e')
5477 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005480 curwin->w_curswant = resel_VIsual_vcol;
5481 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005483 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 curwin->w_curswant = MAXCOL;
5486 coladvance((colnr_T)MAXCOL);
5487 }
5488 else if (VIsual_mode == Ctrl_V)
5489 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005490 update_curswant_force();
5491 curwin->w_curswant += + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 coladvance(curwin->w_curswant);
5493 }
5494 else
5495 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005496 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
5498 else
5499 {
5500 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005501 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 may_start_select('c');
5503 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005504 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005505 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005506 if (cap->count0 > 0 && --cap->count1 > 0)
5507 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005508 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005509 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5510 nv_right(cap);
5511 else if (VIsual_mode == 'V')
5512 nv_down(cap);
5513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 }
5515 }
5516}
5517
5518/*
5519 * Start selection for Shift-movement keys.
5520 */
5521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005522start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 may_start_select('k');
5526 n_start_visual_mode('v');
5527}
5528
5529/*
5530 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005531 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 */
5533 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005534may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005536 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5537 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
5539
5540/*
5541 * Start Visual mode "c".
5542 * Should set VIsual_select before calling this.
5543 */
5544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005545n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005547#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005548 int cursor_line_was_concealed = curwin->w_p_cole > 0
5549 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005550#endif
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 VIsual_mode = c;
5553 VIsual_active = TRUE;
5554 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005555
5556 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005557 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005558 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005559 {
5560 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 VIsual = curwin->w_cursor;
5564
5565#ifdef FEAT_FOLDING
5566 foldAdjustVisual();
5567#endif
5568
LemonBoy2bf52dd2022-04-09 18:17:34 +01005569 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005571#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005572 // Check if redraw is needed after changing the state.
5573 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005574#endif
5575
Bram Moolenaar09df3122006-01-23 22:23:09 +00005576 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005577 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005579 // Make sure the clipboard gets updated. Needed because start and
5580 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 clip_star.vmode = NUL;
5582#endif
5583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005584 // Only need to redraw this line, unless still need to redraw an old
5585 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005586 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5589 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5590 }
5591}
5592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
5594/*
5595 * CTRL-W: Window commands
5596 */
5597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005598nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005600 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005602 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005603 cap->cmdchar = ':';
5604 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005605 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005606 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005607 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609}
5610
5611/*
5612 * CTRL-Z: Suspend
5613 */
5614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005615nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005620 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622
5623/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005624 * "gv": Reselect the previous Visual area. If Visual already active,
5625 * exchange previous and current Visual area.
5626 */
5627 static void
5628nv_gv_cmd(cmdarg_T *cap)
5629{
5630 pos_T tpos;
5631 int i;
5632
5633 if (checkclearop(cap->oap))
5634 return;
5635
5636 if (curbuf->b_visual.vi_start.lnum == 0
5637 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5638 || curbuf->b_visual.vi_end.lnum == 0)
5639 {
5640 beep_flush();
5641 return;
5642 }
5643
5644 // set w_cursor to the start of the Visual area, tpos to the end
5645 if (VIsual_active)
5646 {
5647 i = VIsual_mode;
5648 VIsual_mode = curbuf->b_visual.vi_mode;
5649 curbuf->b_visual.vi_mode = i;
5650# ifdef FEAT_EVAL
5651 curbuf->b_visual_mode_eval = i;
5652# endif
5653 i = curwin->w_curswant;
5654 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5655 curbuf->b_visual.vi_curswant = i;
5656
5657 tpos = curbuf->b_visual.vi_end;
5658 curbuf->b_visual.vi_end = curwin->w_cursor;
5659 curwin->w_cursor = curbuf->b_visual.vi_start;
5660 curbuf->b_visual.vi_start = VIsual;
5661 }
5662 else
5663 {
5664 VIsual_mode = curbuf->b_visual.vi_mode;
5665 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5666 tpos = curbuf->b_visual.vi_end;
5667 curwin->w_cursor = curbuf->b_visual.vi_start;
5668 }
5669
5670 VIsual_active = TRUE;
5671 VIsual_reselect = TRUE;
5672
5673 // Set Visual to the start and w_cursor to the end of the Visual
5674 // area. Make sure they are on an existing character.
5675 check_cursor();
5676 VIsual = curwin->w_cursor;
5677 curwin->w_cursor = tpos;
5678 check_cursor();
5679 update_topline();
5680
5681 // When called from normal "g" command: start Select mode when
5682 // 'selectmode' contains "cmd". When called for K_SELECT, always
5683 // start Select mode.
5684 if (cap->arg)
5685 {
5686 VIsual_select = TRUE;
5687 VIsual_select_reg = 0;
5688 }
5689 else
5690 may_start_select('c');
5691 setmouse();
5692#ifdef FEAT_CLIPBOARD
5693 // Make sure the clipboard gets updated. Needed because start and
5694 // end are still the same, and the selection needs to be owned
5695 clip_star.vmode = NUL;
5696#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005697 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005698 showmode();
5699}
5700
5701/*
5702 * "g0", "g^" : Like "0" and "^" but for screen lines.
5703 * "gm": middle of "g0" and "g$".
5704 */
5705 static void
5706nv_g_home_m_cmd(cmdarg_T *cap)
5707{
5708 int i;
5709 int flag = FALSE;
5710
5711 if (cap->nchar == '^')
5712 flag = TRUE;
5713
5714 cap->oap->motion_type = MCHAR;
5715 cap->oap->inclusive = FALSE;
5716 if (curwin->w_p_wrap && curwin->w_width != 0)
5717 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005718 int width1 = curwin->w_width - curwin_col_off();
5719 int width2 = width1 + curwin_col_off2();
5720 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005721
5722 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005723 virtcol = curwin->w_virtcol
5724#ifdef FEAT_PROP_POPUP
5725 - curwin->w_virtcol_first_char
5726#endif
5727 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005728 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005729 if (virtcol >= (colnr_T)width1 && width2 > 0)
5730 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005731 }
5732 else
5733 i = curwin->w_leftcol;
5734 // Go to the middle of the screen line. When 'number' or
5735 // 'relativenumber' is on and lines are wrapping the middle can be more
5736 // to the left.
5737 if (cap->nchar == 'm')
5738 i += (curwin->w_width - curwin_col_off()
5739 + ((curwin->w_p_wrap && i > 0)
5740 ? curwin_col_off2() : 0)) / 2;
5741 coladvance((colnr_T)i);
5742 if (flag)
5743 {
5744 do
5745 i = gchar_cursor();
5746 while (VIM_ISWHITE(i) && oneright() == OK);
5747 curwin->w_valid &= ~VALID_WCOL;
5748 }
5749 curwin->w_set_curswant = TRUE;
5750}
5751
5752/*
5753 * "g_": to the last non-blank character in the line or <count> lines
5754 * downward.
5755 */
5756 static void
5757nv_g_underscore_cmd(cmdarg_T *cap)
5758{
5759 char_u *ptr;
5760
5761 cap->oap->motion_type = MCHAR;
5762 cap->oap->inclusive = TRUE;
5763 curwin->w_curswant = MAXCOL;
5764 if (cursor_down((long)(cap->count1 - 1),
5765 cap->oap->op_type == OP_NOP) == FAIL)
5766 {
5767 clearopbeep(cap->oap);
5768 return;
5769 }
5770
5771 ptr = ml_get_curline();
5772
5773 // In Visual mode we may end up after the line.
5774 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5775 --curwin->w_cursor.col;
5776
5777 // Decrease the cursor column until it's on a non-blank.
5778 while (curwin->w_cursor.col > 0
5779 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5780 --curwin->w_cursor.col;
5781 curwin->w_set_curswant = TRUE;
5782 adjust_for_sel(cap);
5783}
5784
5785/*
5786 * "g$" : Like "$" but for screen lines.
5787 */
5788 static void
5789nv_g_dollar_cmd(cmdarg_T *cap)
5790{
5791 oparg_T *oap = cap->oap;
5792 int i;
5793 int col_off = curwin_col_off();
5794
5795 oap->motion_type = MCHAR;
5796 oap->inclusive = TRUE;
5797 if (curwin->w_p_wrap && curwin->w_width != 0)
5798 {
5799 curwin->w_curswant = MAXCOL; // so we stay at the end
5800 if (cap->count1 == 1)
5801 {
5802 int width1 = curwin->w_width - col_off;
5803 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005804 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005805
5806 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005807 virtcol = curwin->w_virtcol
5808#ifdef FEAT_PROP_POPUP
5809 - curwin->w_virtcol_first_char
5810#endif
5811 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005812 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005813 if (virtcol >= (colnr_T)width1)
5814 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005815 * width2;
5816 coladvance((colnr_T)i);
5817
5818 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005819 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005820 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5821 {
5822 // Check for landing on a character that got split at
5823 // the end of the line. We do not want to advance to
5824 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005825 if (curwin->w_virtcol
5826#ifdef FEAT_PROP_POPUP
5827 - curwin->w_virtcol_first_char
5828#endif
5829 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005830 --curwin->w_cursor.col;
5831 }
5832 }
5833 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5834 clearopbeep(oap);
5835 }
5836 else
5837 {
5838 if (cap->count1 > 1)
5839 // if it fails, let the cursor still move to the last char
5840 (void)cursor_down(cap->count1 - 1, FALSE);
5841
5842 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5843 coladvance((colnr_T)i);
5844
5845 // if the character doesn't fit move one back
5846 if (curwin->w_cursor.col > 0
5847 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5848 {
5849 colnr_T vcol;
5850
5851 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5852 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5853 --curwin->w_cursor.col;
5854 }
5855
5856 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005857 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005858 }
5859}
5860
5861/*
5862 * "gi": start Insert at the last position.
5863 */
5864 static void
5865nv_gi_cmd(cmdarg_T *cap)
5866{
5867 int i;
5868
5869 if (curbuf->b_last_insert.lnum != 0)
5870 {
5871 curwin->w_cursor = curbuf->b_last_insert;
5872 check_cursor_lnum();
5873 i = (int)STRLEN(ml_get_curline());
5874 if (curwin->w_cursor.col > (colnr_T)i)
5875 {
5876 if (virtual_active())
5877 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5878 curwin->w_cursor.col = i;
5879 }
5880 }
5881 cap->cmdchar = 'i';
5882 nv_edit(cap);
5883}
5884
5885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 * Commands starting with "g".
5887 */
5888 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005889nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 switch (cap->nchar)
5895 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005896 case Ctrl_A:
5897 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005899 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005900 if (!VIsual_active && cap->nchar == Ctrl_A)
5901 vim_mem_profile_dump();
5902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005904 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005905 if (VIsual_active)
5906 {
5907 cap->arg = TRUE;
5908 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005909 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005910 nv_addsub(cap);
5911 }
5912 else
5913 clearopbeep(oap);
5914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005916 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 case 'R':
5918 cap->arg = TRUE;
5919 nv_Replace(cap);
5920 break;
5921
5922 case 'r':
5923 nv_vreplace(cap);
5924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
5926 case '&':
5927 do_cmdline_cmd((char_u *)"%s//~/&");
5928 break;
5929
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005930 // "gv": Reselect the previous Visual area. If Visual already active,
5931 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005933 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005935
5936 // "gV": Don't reselect the previous Visual area after a Select mode
5937 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 case 'V':
5939 VIsual_reselect = FALSE;
5940 break;
5941
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005942 // "gh": start Select mode.
5943 // "gH": start Select line mode.
5944 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 case K_BS:
5946 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005947 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 case 'h':
5949 case 'H':
5950 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 cap->cmdchar = cap->nchar + ('v' - 'h');
5952 cap->arg = TRUE;
5953 nv_visual(cap);
5954 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005956 // "gn", "gN" visually select next/previous search match
5957 // "gn" selects next match
5958 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005959 case 'N':
5960 case 'n':
5961 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005962 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005965 // "gj" and "gk" two new funny movement keys -- up and down
5966 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 case 'j':
5968 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005969 // with 'nowrap' it works just like the normal "j" command.
5970 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
5972 oap->motion_type = MLINE;
5973 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5974 }
5975 else
5976 i = nv_screengo(oap, FORWARD, cap->count1);
5977 if (i == FAIL)
5978 clearopbeep(oap);
5979 break;
5980
5981 case 'k':
5982 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005983 // with 'nowrap' it works just like the normal "k" command.
5984 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986 oap->motion_type = MLINE;
5987 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5988 }
5989 else
5990 i = nv_screengo(oap, BACKWARD, cap->count1);
5991 if (i == FAIL)
5992 clearopbeep(oap);
5993 break;
5994
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005995 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 case 'J':
5997 nv_join(cap);
5998 break;
5999
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006000 // "g0", "g^" : Like "0" and "^" but for screen lines.
6001 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case '0':
6004 case 'm':
6005 case K_HOME:
6006 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006007 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 break;
6009
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006010 case 'M':
6011 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006012 oap->motion_type = MCHAR;
6013 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006014 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006015 if (cap->count0 > 0 && cap->count0 <= 100)
6016 coladvance((colnr_T)(i * cap->count0 / 100));
6017 else
6018 coladvance((colnr_T)(i / 2));
6019 curwin->w_set_curswant = TRUE;
6020 }
6021 break;
6022
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006023 // "g_": to the last non-blank character in the line or <count> lines
6024 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006026 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 break;
6028
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006029 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 case '$':
6031 case K_END:
6032 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 break;
6035
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006036 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 case '*':
6038 case '#':
6039#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006040 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006042 case Ctrl_RSB: // :tag or :tselect for current identifier
6043 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 nv_ident(cap);
6045 break;
6046
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006047 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 case 'e':
6049 case 'E':
6050 oap->motion_type = MCHAR;
6051 curwin->w_set_curswant = TRUE;
6052 oap->inclusive = TRUE;
6053 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6054 clearopbeep(oap);
6055 break;
6056
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006057 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006059 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 break;
6061
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006062 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006064 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 break;
6066
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006067 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 case 'I':
6069 beginline(0);
6070 if (!checkclearopq(oap))
6071 invoke_edit(cap, FALSE, 'g', FALSE);
6072 break;
6073
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 // "gf": goto file, edit file under cursor
6075 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006077 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 nv_gotofile(cap);
6079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006081 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 case '\'':
6083 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006084 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case '`':
6086 nv_gomark(cap);
6087 break;
6088
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006089 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006091 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 break;
6093
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006094 // "ga": Display the ascii value of the character under the
6095 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 case 'a':
6097 do_ascii(NULL);
6098 break;
6099
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006100 // "g8": Display the bytes used for the UTF-8 character under the
6101 // cursor. It is displayed in hex.
6102 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006104 if (cap->count0 == 8)
6105 utf_find_illegal();
6106 else
6107 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006110 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006111 case '<':
6112 show_sb_text();
6113 break;
6114
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006115 // "gg": Goto the first line in file. With a count it goes to
6116 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 case 'g':
6118 cap->arg = FALSE;
6119 nv_goto(cap);
6120 break;
6121
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006122 // Two-character operators:
6123 // "gq" Format text
6124 // "gw" Format text and keep cursor position
6125 // "g~" Toggle the case of the text.
6126 // "gu" Change text to lower case.
6127 // "gU" Change text to upper case.
6128 // "g?" rot13 encoding
6129 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 case 'q':
6131 case 'w':
6132 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006133 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 case '~':
6135 case 'u':
6136 case 'U':
6137 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006138 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 nv_operator(cap);
6140 break;
6141
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006142 // "gd": Find first occurrence of pattern under the cursor in the
6143 // current function
6144 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 case 'd':
6146 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006147 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 break;
6149
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006150 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 case K_MIDDLEMOUSE:
6152 case K_MIDDLEDRAG:
6153 case K_MIDDLERELEASE:
6154 case K_LEFTMOUSE:
6155 case K_LEFTDRAG:
6156 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006157 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 case K_RIGHTMOUSE:
6159 case K_RIGHTDRAG:
6160 case K_RIGHTRELEASE:
6161 case K_X1MOUSE:
6162 case K_X1DRAG:
6163 case K_X1RELEASE:
6164 case K_X2MOUSE:
6165 case K_X2DRAG:
6166 case K_X2RELEASE:
6167 mod_mask = MOD_MASK_CTRL;
6168 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
6171 case K_IGNORE:
6172 break;
6173
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006174 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 case 'p':
6176 case 'P':
6177 nv_put(cap);
6178 break;
6179
6180#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006181 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 case 'o':
6183 goto_byte(cap->count0);
6184 break;
6185#endif
6186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006187 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006189 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 do_exmode(TRUE);
6191 break;
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 case ',':
6194 nv_pcmark(cap);
6195 break;
6196
6197 case ';':
6198 cap->count1 = -cap->count1;
6199 nv_pcmark(cap);
6200 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006203 if (!checkclearop(oap))
6204 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006206 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006207 if (!checkclearop(oap))
6208 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006209 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210
Bram Moolenaar62a23252020-08-09 14:04:42 +02006211 case TAB:
6212 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6213 clearopbeep(oap);
6214 break;
6215
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006216 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006217 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006218 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006219 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006220 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006221 break;
6222
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 default:
6224 clearopbeep(oap);
6225 break;
6226 }
6227}
6228
6229/*
6230 * Handle "o" and "O" commands.
6231 */
6232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006233n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006235#ifdef FEAT_CONCEAL
6236 linenr_T oldline = curwin->w_cursor.lnum;
6237#endif
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (!checkclearopq(cap->oap))
6240 {
6241#ifdef FEAT_FOLDING
6242 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006243 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 (void)hasFolding(curwin->w_cursor.lnum,
6245 &curwin->w_cursor.lnum, NULL);
6246 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006247 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 (void)hasFolding(curwin->w_cursor.lnum,
6249 NULL, &curwin->w_cursor.lnum);
6250#endif
6251 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6252 (cap->cmdchar == 'O' ? 1 : 0)),
6253 (linenr_T)(curwin->w_cursor.lnum +
6254 (cap->cmdchar == 'o' ? 1 : 0))
6255 ) == OK
6256 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006257 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006258 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006260#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006261 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006262 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006263#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006264#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006265 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006266 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006267 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006268#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006269 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006270 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6271 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6273 }
6274 }
6275}
6276
6277/*
6278 * "." command: redo last change.
6279 */
6280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006281nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 if (!checkclearopq(cap->oap))
6284 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006285 // If "restart_edit" is TRUE, the last but one command is repeated
6286 // instead of the last command (inserting text). This is used for
6287 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6289 clearopbeep(cap->oap);
6290 }
6291}
6292
6293/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006294 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 */
6296 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006297nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006299 if (VIsual_select && VIsual_active)
6300 {
6301 int reg;
6302 // Get register name
6303 ++no_mapping;
6304 ++allow_keys;
6305 reg = plain_vgetc();
6306 LANGMAP_ADJUST(reg, TRUE);
6307 --no_mapping;
6308 --allow_keys;
6309
6310 if (reg == '"')
6311 // the unnamed register is 0
6312 reg = 0;
6313
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006314 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006315 return;
6316 }
6317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if (!checkclearopq(cap->oap))
6319 {
6320 u_redo((int)cap->count1);
6321 curwin->w_set_curswant = TRUE;
6322 }
6323}
6324
6325/*
6326 * Handle "U" command.
6327 */
6328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006329nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006331 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006332 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006334 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 cap->cmdchar = 'g';
6336 cap->nchar = 'U';
6337 nv_operator(cap);
6338 }
6339 else if (!checkclearopq(cap->oap))
6340 {
6341 u_undoline();
6342 curwin->w_set_curswant = TRUE;
6343 }
6344}
6345
6346/*
6347 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6348 * single character.
6349 */
6350 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006353 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006354 {
6355#ifdef FEAT_JOB_CHANNEL
6356 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6357 {
6358 clearopbeep(cap->oap);
6359 return;
6360 }
6361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 else
6365 nv_operator(cap);
6366}
6367
6368/*
6369 * Handle an operator command.
6370 * The actual work is done by do_pending_operator().
6371 */
6372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006373nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 int op_type;
6376
6377 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006378#ifdef FEAT_JOB_CHANNEL
6379 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6380 {
6381 clearopbeep(cap->oap);
6382 return;
6383 }
6384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006386 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 nv_lineop(cap);
6388 else if (!checkclearop(cap->oap))
6389 {
6390 cap->oap->start = curwin->w_cursor;
6391 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006392#ifdef FEAT_EVAL
6393 set_op_var(op_type);
6394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 }
6396}
6397
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006398#ifdef FEAT_EVAL
6399/*
6400 * Set v:operator to the characters for "optype".
6401 */
6402 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006403set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006404{
6405 char_u opchars[3];
6406
6407 if (optype == OP_NOP)
6408 set_vim_var_string(VV_OP, NULL, 0);
6409 else
6410 {
6411 opchars[0] = get_op_char(optype);
6412 opchars[1] = get_extra_op_char(optype);
6413 opchars[2] = NUL;
6414 set_vim_var_string(VV_OP, opchars, -1);
6415 }
6416}
6417#endif
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419/*
6420 * Handle linewise operator "dd", "yy", etc.
6421 *
6422 * "_" is is a strange motion command that helps make operators more logical.
6423 * It is actually implemented, but not documented in the real Vi. This motion
6424 * command actually refers to "the current line". Commands like "dd" and "yy"
6425 * are really an alternate form of "d_" and "y_". It does accept a count, so
6426 * "d3_" works to delete 3 lines.
6427 */
6428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006429nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 cap->oap->motion_type = MLINE;
6432 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6433 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006434 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006435 && cap->oap->motion_force != 'v'
6436 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 || cap->oap->op_type == OP_LSHIFT
6438 || cap->oap->op_type == OP_RSHIFT)
6439 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006440 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 beginline(BL_WHITE | BL_FIX);
6442}
6443
6444/*
6445 * <Home> command.
6446 */
6447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006451 if (mod_mask & MOD_MASK_CTRL)
6452 nv_goto(cap);
6453 else
6454 {
6455 cap->count0 = 1;
6456 nv_pipe(cap);
6457 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006458 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6459 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460}
6461
6462/*
6463 * "|" command.
6464 */
6465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006466nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 cap->oap->motion_type = MCHAR;
6469 cap->oap->inclusive = FALSE;
6470 beginline(0);
6471 if (cap->count0 > 0)
6472 {
6473 coladvance((colnr_T)(cap->count0 - 1));
6474 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6475 }
6476 else
6477 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006478 // keep curswant at the column where we wanted to go, not where
6479 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 curwin->w_set_curswant = FALSE;
6481}
6482
6483/*
6484 * Handle back-word command "b" and "B".
6485 * cap->arg is 1 for "B"
6486 */
6487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006488nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 cap->oap->motion_type = MCHAR;
6491 cap->oap->inclusive = FALSE;
6492 curwin->w_set_curswant = TRUE;
6493 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6494 clearopbeep(cap->oap);
6495#ifdef FEAT_FOLDING
6496 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6497 foldOpenCursor();
6498#endif
6499}
6500
6501/*
6502 * Handle word motion commands "e", "E", "w" and "W".
6503 * cap->arg is TRUE for "E" and "W".
6504 */
6505 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006506nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 int n;
6509 int word_end;
6510 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006511 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006513 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6515 word_end = TRUE;
6516 else
6517 word_end = FALSE;
6518 cap->oap->inclusive = word_end;
6519
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006520 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if (!word_end && cap->oap->op_type == OP_CHANGE)
6522 {
6523 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006524 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006526 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006528 // Reproduce a funny Vi behaviour: "cw" on a blank only
6529 // changes one character, not all blanks until the start of
6530 // the next word. Only do this when the 'w' flag is included
6531 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6533 {
6534 cap->oap->inclusive = TRUE;
6535 cap->oap->motion_type = MCHAR;
6536 return;
6537 }
6538 }
6539 else
6540 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006541 // This is a little strange. To match what the real Vi does,
6542 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6543 // that we are not on a space or a TAB. This seems impolite
6544 // at first, but it's really more what we mean when we say
6545 // 'cw'.
6546 // Another strangeness: When standing on the end of a word
6547 // "ce" will change until the end of the next word, but "cw"
6548 // will change only one character! This is done by setting
6549 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 cap->oap->inclusive = TRUE;
6551 word_end = TRUE;
6552 flag = TRUE;
6553 }
6554 }
6555 }
6556
6557 cap->oap->motion_type = MCHAR;
6558 curwin->w_set_curswant = TRUE;
6559 if (word_end)
6560 n = end_word(cap->count1, cap->arg, flag, FALSE);
6561 else
6562 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006564 // Don't leave the cursor on the NUL past the end of line. Unless we
6565 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006566 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006567 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 if (n == FAIL && cap->oap->op_type == OP_NOP)
6570 clearopbeep(cap->oap);
6571 else
6572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574#ifdef FEAT_FOLDING
6575 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6576 foldOpenCursor();
6577#endif
6578 }
6579}
6580
6581/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006582 * Used after a movement command: If the cursor ends up on the NUL after the
6583 * end of the line, may move it back to the last character and make the motion
6584 * inclusive.
6585 */
6586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006587adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006588{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006589 // The cursor cannot remain on the NUL when:
6590 // - the column is > 0
6591 // - not in Visual mode or 'selection' is "o"
6592 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006593 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006594 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006595 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006596 {
6597 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006598 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006599 if (has_mbyte)
6600 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006601 oap->inclusive = TRUE;
6602 }
6603}
6604
6605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 * "0" and "^" commands.
6607 * cap->arg is the argument for beginline().
6608 */
6609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006610nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 cap->oap->motion_type = MCHAR;
6613 cap->oap->inclusive = FALSE;
6614 beginline(cap->arg);
6615#ifdef FEAT_FOLDING
6616 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6617 foldOpenCursor();
6618#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006619 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6620 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621}
6622
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623/*
6624 * In exclusive Visual mode, may include the last character.
6625 */
6626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006627adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006630 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (has_mbyte)
6633 inc_cursor();
6634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 ++curwin->w_cursor.col;
6636 cap->oap->inclusive = FALSE;
6637 }
6638}
6639
6640/*
6641 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6642 * Should check VIsual_mode before calling this.
6643 * Returns TRUE when backed up to the previous line.
6644 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006645 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006646unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 pos_T *pp;
6649
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006650 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006652 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 pp = &curwin->w_cursor;
6654 else
6655 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (pp->coladd > 0)
6657 --pp->coladd;
6658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (pp->col > 0)
6660 {
6661 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006662 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664 else if (pp->lnum > 1)
6665 {
6666 --pp->lnum;
6667 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6668 return TRUE;
6669 }
6670 }
6671 return FALSE;
6672}
6673
6674/*
6675 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6676 */
6677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006678nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006683 VIsual_select_reg = 0;
6684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 else if (VIsual_reselect)
6686 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006687 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 cap->arg = TRUE;
6689 nv_g_cmd(cap);
6690 }
6691}
6692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694/*
6695 * "G", "gg", CTRL-END, CTRL-HOME.
6696 * cap->arg is TRUE for "G".
6697 */
6698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006699nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
6701 linenr_T lnum;
6702
6703 if (cap->arg)
6704 lnum = curbuf->b_ml.ml_line_count;
6705 else
6706 lnum = 1L;
6707 cap->oap->motion_type = MLINE;
6708 setpcmark();
6709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006710 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 if (cap->count0 != 0)
6712 lnum = cap->count0;
6713 if (lnum < 1L)
6714 lnum = 1L;
6715 else if (lnum > curbuf->b_ml.ml_line_count)
6716 lnum = curbuf->b_ml.ml_line_count;
6717 curwin->w_cursor.lnum = lnum;
6718 beginline(BL_SOL | BL_FIX);
6719#ifdef FEAT_FOLDING
6720 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6721 foldOpenCursor();
6722#endif
6723}
6724
6725/*
6726 * CTRL-\ in Normal mode.
6727 */
6728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006729nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6732 {
6733 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006734 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006735 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (cmdwin_type != 0)
6738 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (VIsual_active)
6740 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006741 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006742 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (cap->nchar == Ctrl_G && p_im)
6746 restart_edit = 'a';
6747 }
6748 else
6749 clearopbeep(cap->oap);
6750}
6751
6752/*
6753 * ESC in Normal mode: beep, but don't flush buffers.
6754 * Don't even beep if we are canceling a command.
6755 */
6756 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006757nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
6759 int no_reason;
6760
6761 no_reason = (cap->oap->op_type == OP_NOP
6762 && cap->opcount == 0
6763 && cap->count0 == 0
6764 && cap->oap->regname == 0
6765 && !p_im);
6766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006767 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006769 if (restart_edit == 0 && cmdwin_type == 0
6770 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006771 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006772 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6773
Bram Moolenaar5939c352022-09-24 12:50:45 +01006774 // The user may accidentally do "vim file | grep word" and then
6775 // CTRL-C doesn't show anything. With a changed buffer give the
6776 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006777 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006778 {
6779 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6780
6781 if (out_redir)
6782 mch_errmsg(ms);
6783 else
6784 msg(ms);
6785 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006786 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006787 {
6788 if (out_redir)
6789 {
6790 got_int = FALSE;
6791 do_cmdline_cmd((char_u *)"qa");
6792 }
6793 else
6794 msg(_("Type :qa and press <Enter> to exit Vim"));
6795 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006798 if (restart_edit != 0)
6799 redraw_mode = TRUE; // remove "-- (insert) --"
6800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006801 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6802 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (!p_im)
6804 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (cmdwin_type != 0)
6806 {
6807 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006808 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 return;
6810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006812 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006813 {
6814 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006815 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6816 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006817 cmdwin_result = K_IGNORE;
6818 return;
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (VIsual_active)
6822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006823 end_visual_mode(); // stop Visual
6824 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006826 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006828 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006829 {
6830#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006831 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006832 popup_hide_message_win();
6833 else
6834#endif
6835 vim_beep(BO_ESC);
6836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 clearop(cap->oap);
6838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006839 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6840 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006841 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 restart_edit = 'a';
6843}
6844
6845/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006846 * Move the cursor for the "A" command.
6847 */
6848 void
6849set_cursor_for_append_to_line(void)
6850{
6851 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006852 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006853 {
6854 int save_State = State;
6855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006856 // Pretend Insert mode here to allow the cursor on the
6857 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006858 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006859 coladvance((colnr_T)MAXCOL);
6860 State = save_State;
6861 }
6862 else
6863 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6864}
6865
6866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006868 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 */
6870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006873 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6875 cap->cmdchar = 'i';
6876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006877 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006879 {
6880#ifdef FEAT_TERMINAL
6881 if (term_in_normal_mode())
6882 {
6883 end_visual_mode();
6884 clearop(cap->oap);
6885 term_enter_job_mode();
6886 return;
6887 }
6888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006892 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006893 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6894 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006898#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006899 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006900 {
6901 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006902 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006903 return;
6904 }
6905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 else if (!curbuf->b_p_ma && !p_im)
6907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006908 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006909 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006911 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006913 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006915 else if (cap->cmdchar == K_PS && VIsual_active)
6916 {
6917 pos_T old_pos = curwin->w_cursor;
6918 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006919 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006922 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6923 {
6924 shift_delete_registers();
6925 cap->oap->regname = '1';
6926 }
6927 else
6928 cap->oap->regname = '-';
6929 cap->cmdchar = 'd';
6930 cap->nchar = NUL;
6931 nv_operator(cap);
6932 do_pending_operator(cap, 0, FALSE);
6933 cap->cmdchar = K_PS;
6934
zeertzjq7a732522022-03-14 20:46:41 +00006935 if (*ml_get_cursor() != NUL)
6936 {
6937 if (old_visual_mode == 'V')
6938 {
6939 // In linewise Visual mode insert before the beginning of the
6940 // next line.
6941 // When the last line in the buffer was deleted then create a
6942 // new line, otherwise there is not need to move cursor.
6943 // Detect this by checking if cursor moved above Visual area.
6944 if (curwin->w_cursor.lnum < old_pos.lnum
6945 && curwin->w_cursor.lnum < old_visual.lnum)
6946 {
6947 if (u_save_cursor() == OK)
6948 {
6949 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6950 FALSE);
6951 appended_lines(curwin->w_cursor.lnum++, 1L);
6952 }
6953 }
6954 }
6955 // When the last char in the line was deleted then append.
6956 // Detect this by checking if cursor moved before Visual area.
6957 else if (curwin->w_cursor.col < old_pos.col
6958 && curwin->w_cursor.col < old_visual.col)
6959 inc_cursor();
6960 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006962 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006963 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 else if (!checkclearopq(cap->oap))
6966 {
6967 switch (cap->cmdchar)
6968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006969 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006970 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 break;
6972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006973 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006974 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6975 beginline(BL_WHITE);
6976 else
6977 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 break;
6979
Bram Moolenaara1891842017-02-04 21:34:31 +01006980 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006981 // Bracketed paste works like "a"ppend, unless the cursor is in
6982 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006983 if (curwin->w_cursor.col == 0)
6984 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006987 case 'a': // "a"ppend is like "i"nsert on the next character.
6988 // increment coladd when in virtual space, increment the
6989 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (virtual_active()
6991 && (curwin->w_cursor.coladd > 0
6992 || *ml_get_cursor() == NUL
6993 || *ml_get_cursor() == TAB))
6994 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006995 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 inc_cursor();
6997 break;
6998 }
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7001 {
7002 int save_State = State;
7003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007004 // Pretend Insert mode here to allow the cursor on the
7005 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007006 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 coladvance(getviscol());
7008 State = save_State;
7009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7012 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007013 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007014 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007015 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016}
7017
7018/*
7019 * Invoke edit() and take care of "restart_edit" and the return value.
7020 */
7021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007022invoke_edit(
7023 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007025 int cmd,
7026 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 int restart_edit_save = 0;
7029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7031 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7032 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (repl || !stuff_empty())
7034 restart_edit_save = restart_edit;
7035 else
7036 restart_edit_save = 0;
7037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 restart_edit = 0;
7040
7041 if (edit(cmd, startln, cap->count1))
7042 cap->retval |= CA_COMMAND_BUSY;
7043
7044 if (restart_edit == 0)
7045 restart_edit = restart_edit_save;
7046}
7047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048/*
7049 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7050 */
7051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007052nv_object(
7053 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055 int flag;
7056 int include;
7057 char_u *mps_save;
7058
7059 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 mps_save = curbuf->b_p_mps;
7066 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7067
7068 switch (cap->nchar)
7069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 flag = current_word(cap->oap, cap->count1, include, FALSE);
7072 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 flag = current_word(cap->oap, cap->count1, include, TRUE);
7075 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 case '(':
7078 case ')':
7079 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7080 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 case '{':
7083 case '}':
7084 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7085 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 case ']':
7088 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7089 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 case '>':
7092 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7093 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007094#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007095 case 't': // "at" = a tag block (xml and html)
7096 // Do not adjust oap->end in do_pending_operator()
7097 // otherwise there are different results for 'dit'
7098 // (note leading whitespace in last line):
7099 // 1) <b> 2) <b>
7100 // foobar foobar
7101 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007102 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007103 flag = current_tagblock(cap->oap, cap->count1, include);
7104 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007105#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 flag = current_par(cap->oap, cap->count1, include, 'p');
7108 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 flag = current_sent(cap->oap, cap->count1, include);
7111 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 case '"': // "a"" = a double quoted string
7113 case '\'': // "a'" = a single quoted string
7114 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007115 flag = current_quote(cap->oap, cap->count1, include,
7116 cap->nchar);
7117 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007118#if 0 // TODO
7119 case 'S': // "aS" = a section
7120 case 'f': // "af" = a filename
7121 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122#endif
7123 default:
7124 flag = FAIL;
7125 break;
7126 }
7127
7128 curbuf->b_p_mps = mps_save;
7129 if (flag == FAIL)
7130 clearopbeep(cap->oap);
7131 adjust_cursor_col();
7132 curwin->w_set_curswant = TRUE;
7133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
7135/*
7136 * "q" command: Start/stop recording.
7137 * "q:", "q/", "q?": edit command-line in command-line window.
7138 */
7139 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007140nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
7142 if (cap->oap->op_type == OP_FORMAT)
7143 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 cap->cmdchar = 'g';
7146 cap->nchar = 'q';
7147 nv_operator(cap);
7148 }
7149 else if (!checkclearop(cap->oap))
7150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7152 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007153 if (cmdwin_type != 0)
7154 {
7155 emsg(_(e_cmdline_window_already_open));
7156 return;
7157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 stuffcharReadbuff(cap->nchar);
7159 stuffcharReadbuff(K_CMDWIN);
7160 }
7161 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007162 // (stop) recording into a named register, unless executing a
7163 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007164 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 clearopbeep(cap->oap);
7166 }
7167}
7168
7169/*
7170 * Handle the "@r" command.
7171 */
7172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007173nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 if (checkclearop(cap->oap))
7176 return;
7177#ifdef FEAT_EVAL
7178 if (cap->nchar == '=')
7179 {
7180 if (get_expr_register() == NUL)
7181 return;
7182 }
7183#endif
7184 while (cap->count1-- && !got_int)
7185 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007186 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 clearopbeep(cap->oap);
7189 break;
7190 }
7191 line_breakcheck();
7192 }
7193}
7194
7195/*
7196 * Handle the CTRL-U and CTRL-D commands.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7202 || (cap->cmdchar == Ctrl_D
7203 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7204 clearopbeep(cap->oap);
7205 else if (!checkclearop(cap->oap))
7206 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7207}
7208
7209/*
7210 * Handle "J" or "gJ" command.
7211 */
7212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007213nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007217 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
7219 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7222 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007225 if (cap->count0 <= 2)
7226 {
7227 clearopbeep(cap->oap);
7228 return;
7229 }
7230 cap->count0 = curbuf->b_ml.ml_line_count
7231 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007233
7234 prep_redo(cap->oap->regname, cap->count0,
7235 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7236 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238}
7239
7240/*
7241 * "P", "gP", "p" and "gp" commands.
7242 */
7243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007244nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007246 nv_put_opt(cap, FALSE);
7247}
7248
7249/*
7250 * "P", "gP", "p" and "gp" commands.
7251 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7252 */
7253 static void
7254nv_put_opt(cmdarg_T *cap, int fix_indent)
7255{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 int regname = 0;
7257 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007258 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007259 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 int dir;
7261 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007262 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 if (cap->oap->op_type != OP_NOP)
7265 {
7266#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007267 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7269 {
7270 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007271 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273 else
7274#endif
7275 clearopbeep(cap->oap);
7276 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007277#ifdef FEAT_JOB_CHANNEL
7278 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7279 {
7280 clearopbeep(cap->oap);
7281 }
7282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else
7284 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007285 if (fix_indent)
7286 {
7287 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7288 ? FORWARD : BACKWARD;
7289 flags |= PUT_FIXINDENT;
7290 }
7291 else
7292 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007293 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7294 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 prep_redo_cmd(cap);
7296 if (cap->cmdchar == 'g')
7297 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007298 else if (cap->cmdchar == 'z')
7299 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (VIsual_active)
7302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007303 // Putting in Visual mode: The put text replaces the selected
7304 // text. First delete the selected text, then put the new text.
7305 // Need to save and restore the registers that the delete
7306 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007307 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007309 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007312#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007313 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007314 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007315#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 )
7320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007321 // The delete is going to overwrite the register we want to
7322 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 reg1 = get_register(regname, TRUE);
7324 }
7325
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007326 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 cap->cmdchar = 'd';
7328 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007329 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007330 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 nv_operator(cap);
7332 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007333 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007334 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 cap->oap->regname = regname;
7338
7339 if (reg1 != NULL)
7340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007341 // Delete probably changed the register we want to put, save
7342 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 reg2 = get_register(regname, FALSE);
7344 put_register(regname, reg1);
7345 }
7346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007347 // When deleted a linewise Visual area, put the register as
7348 // lines to avoid it joined with the next line. When deletion was
7349 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (VIsual_mode == 'V')
7351 flags |= PUT_LINE;
7352 else if (VIsual_mode == 'v')
7353 flags |= PUT_LINE_SPLIT;
7354 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7355 flags |= PUT_LINE_FORWARD;
7356 dir = BACKWARD;
7357 if ((VIsual_mode != 'V'
7358 && curwin->w_cursor.col < curbuf->b_op_start.col)
7359 || (VIsual_mode == 'V'
7360 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // cursor is at the end of the line or end of file, put
7362 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007365 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007367 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007369 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (reg2 != NULL)
7371 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // What to reselect with "gv"? Selecting the just put text seems to
7374 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007375 if (was_visual)
7376 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007377 curbuf->b_visual.vi_start = curbuf->b_op_start;
7378 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007379 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007380 if (*p_sel == 'e')
7381 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007382 }
7383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007384 // When all lines were selected and deleted do_put() leaves an empty
7385 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007386 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007387 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007388 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007389 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007390
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007391 // If the cursor was in that line, move it to the end of the last
7392 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007393 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7394 {
7395 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7396 coladvance((colnr_T)MAXCOL);
7397 }
7398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 auto_format(FALSE, TRUE);
7400 }
7401}
7402
7403/*
7404 * "o" and "O" commands.
7405 */
7406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007407nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007410 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7412 {
7413 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007414 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416 else
7417#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007418 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007420#ifdef FEAT_JOB_CHANNEL
7421 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007422 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 n_opencmd(cap);
7426}
7427
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#ifdef FEAT_NETBEANS_INTG
7429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007430nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 netbeans_keycommand(cap->nchar);
7433}
7434#endif
7435
7436#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007438nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007440 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441}
7442#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007443
Bram Moolenaar3918c952005-03-15 22:34:55 +00007444/*
7445 * Trigger CursorHold event.
7446 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7447 * input buffer. "did_cursorhold" is set to avoid retriggering.
7448 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007450nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007451{
7452 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7453 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007454 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007455}