blob: 9a3006705cb4ce33b27757a4ed68026fff4c3394 [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{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
421 }
422 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
423 {
424 // Allow mappings defined with ":lmap".
425 --no_mapping;
426 --allow_keys;
427 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100428 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000429 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 langmap_active = TRUE;
432 }
433#ifdef HAVE_INPUT_METHOD
434 save_smd = p_smd;
435 p_smd = FALSE; // Don't let the IM code show the mode here
436 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
437 im_set_active(TRUE);
438#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000440 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100441 MAY_WANT_TO_LOG_THIS;
442
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 // Disable bracketed paste and modifyOtherKeys here, we won't
444 // recognize the escape sequences with 'esckeys' off.
445 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000446 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448
449 *cp = plain_vgetc();
450
Bram Moolenaar24959102022-05-07 20:01:16 +0100451 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100453 MAY_WANT_TO_LOG_THIS;
454
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000456 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000457 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 }
459
460 if (langmap_active)
461 {
462 // Undo the decrement done above
463 ++no_mapping;
464 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100465 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000466 }
467#ifdef HAVE_INPUT_METHOD
468 if (lang)
469 {
470 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
471 im_save_status(&curbuf->b_p_iminsert);
472 im_set_active(FALSE);
473 }
474 p_smd = save_smd;
475#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100476 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000477 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478
479 if (!lit)
480 {
481#ifdef FEAT_DIGRAPHS
482 // Typing CTRL-K gets a digraph.
483 if (*cp == Ctrl_K
484 && ((nv_cmds[idx].cmd_flags & NV_LANG)
485 || cp == &cap->extra_char)
486 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
487 {
488 c = get_digraph(FALSE);
489 if (c > 0)
490 {
491 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000492 // Guessing how to update showcmd here...
493 del_from_showcmd(3);
494 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 }
496 }
497#endif
498
499 // adjust chars > 127, except after "tTfFr" commands
500 LANGMAP_ADJUST(*cp, !lang);
501#ifdef FEAT_RIGHTLEFT
502 // adjust Hebrew mapped char
503 if (p_hkmap && lang && KeyTyped)
504 *cp = hkmap(*cp);
505#endif
506 }
507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000508 // When the next character is CTRL-\ a following CTRL-N means the
509 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000510 if (cp == &cap->extra_char
511 && cap->nchar == Ctrl_BSL
512 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
513 {
514 cap->cmdchar = Ctrl_BSL;
515 cap->nchar = cap->extra_char;
516 idx = find_command(cap->cmdchar);
517 }
518 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
519 cap->oap->op_type = get_op_type(*cp, NUL);
520 else if (*cp == Ctrl_BSL)
521 {
522 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
523
524 // There is a busy wait here when typing "f<C-\>" and then
525 // something different from CTRL-N. Can't be avoided.
526 while ((c = vpeekc()) <= 0 && towait > 0L)
527 {
528 do_sleep(towait > 50L ? 50L : towait, FALSE);
529 towait -= 50L;
530 }
531 if (c > 0)
532 {
533 c = plain_vgetc();
534 if (c != Ctrl_N && c != Ctrl_G)
535 vungetc(c);
536 else
537 {
538 cap->cmdchar = Ctrl_BSL;
539 cap->nchar = c;
540 idx = find_command(cap->cmdchar);
541 }
542 }
543 }
544
545 // When getting a text character and the next character is a
546 // multi-byte character, it could be a composing character.
547 // However, don't wait for it to arrive. Also, do enable mapping,
548 // because if it's put back with vungetc() it's too late to apply
549 // mapping.
550 --no_mapping;
551 while (enc_utf8 && lang && (c = vpeekc()) > 0
552 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
553 {
554 c = plain_vgetc();
555 if (!utf_iscomposing(c))
556 {
557 vungetc(c); // it wasn't, put it back
558 break;
559 }
560 else if (cap->ncharC1 == 0)
561 cap->ncharC1 = c;
562 else
563 cap->ncharC2 = c;
564 }
565 ++no_mapping;
566 }
567 --no_mapping;
568 --allow_keys;
569
570 return idx;
571}
572
573/*
574 * Returns TRUE if after processing a normal mode command, need to wait for a
575 * moment when a message is displayed that will be overwritten by the mode
576 * message.
577 */
578 static int
579normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
580{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000581 // In Visual mode and with "^O" in Insert mode, a short message will be
582 // overwritten by the mode message. Wait a bit, until a key is hit.
583 // In Visual mode, it's more important to keep the Visual area updated
584 // than keeping a message (e.g. from a /pat search).
585 // Only do this if the command was typed, not from a mapping.
586 // Don't wait when emsg_silent is non-zero.
587 // Also wait a bit after an error message, e.g. for "^O:".
588 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000589 return ( ((p_smd
590 && msg_silent == 0
591 && (restart_edit != 0
592 || (VIsual_active
593 && old_pos->lnum == curwin->w_cursor.lnum
594 && old_pos->col == curwin->w_cursor.col)
595 )
596 && (clear_cmdline
597 || redraw_cmdline)
598 && (msg_didout || (msg_didany && msg_scroll))
599 && !msg_nowait
600 && KeyTyped)
601 || (restart_edit != 0
602 && !VIsual_active
603 && (msg_scroll
604 || emsg_on_display)))
605 && cap->oap->regname == 0
606 && !(cap->retval & CA_COMMAND_BUSY)
607 && stuff_empty()
608 && typebuf_typed()
609 && emsg_silent == 0
610 && !in_assert_fails
611 && !did_wait_return
612 && cap->oap->op_type == OP_NOP);
613}
614
615/*
616 * After processing a normal mode command, wait for a moment when a message is
617 * displayed that will be overwritten by the mode message.
618 */
619 static void
620normal_cmd_wait_for_msg(void)
621{
622 int save_State = State;
623
624 // Draw the cursor with the right shape here
625 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100626 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000627
628 // If need to redraw, and there is a "keep_msg", redraw before the
629 // delay
630 if (must_redraw && keep_msg != NULL && !emsg_on_display)
631 {
632 char_u *kmsg;
633
634 kmsg = keep_msg;
635 keep_msg = NULL;
636 // Showmode() will clear keep_msg, but we want to use it anyway.
637 // First update w_topline.
638 setcursor();
639 update_screen(0);
640 // now reset it, otherwise it's put in the history again
641 keep_msg = kmsg;
642
643 kmsg = vim_strsave(keep_msg);
644 if (kmsg != NULL)
645 {
646 msg_attr((char *)kmsg, keep_msg_attr);
647 vim_free(kmsg);
648 }
649 }
650 setcursor();
651#ifdef CURSOR_SHAPE
652 ui_cursor_shape(); // may show different cursor shape
653#endif
654 cursor_on();
655 out_flush();
656 if (msg_scroll || emsg_on_display)
657 ui_delay(1003L, TRUE); // wait at least one second
658 ui_delay(3003L, FALSE); // wait up to three seconds
659 State = save_State;
660
661 msg_scroll = FALSE;
662 emsg_on_display = FALSE;
663}
664
665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 * Execute a command in Normal mode.
667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100669normal_cmd(
670 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100671 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100673 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100675 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000677 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 static int old_mapped_len = 0;
681 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000682 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200683 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaara80faa82020-04-12 19:37:17 +0200685 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000687
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100688 // Use a count remembered from before entering an operator. After typing
689 // "3d" we return from normal_cmd() and come back here, the "3" is
690 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ca.opcount = opcount;
692
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000693 // If there is an operator pending, then the command we take this time
694 // will terminate it. Finish_op tells us to finish the operation before
695 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#ifdef CURSOR_SHAPE
697 c = finish_op;
698#endif
699 finish_op = (oap->op_type != OP_NOP);
700#ifdef CURSOR_SHAPE
701 if (finish_op != c)
702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704# ifdef FEAT_MOUSESHAPE
705 update_mouseshape(-1);
706# endif
707 }
708#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100709 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100711 // When not finishing an operator and no register name typed, reset the
712 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000716#ifdef FEAT_EVAL
717 set_prevcount = TRUE;
718#endif
719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // Restore counts from before receiving K_CURSORHOLD. This means after
722 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
723 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000724 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
725 {
726 ca.opcount = oap->prev_opcount;
727 ca.count0 = oap->prev_count0;
728 oap->prev_opcount = 0;
729 oap->prev_count0 = 0;
730 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Bram Moolenaar24959102022-05-07 20:01:16 +0100734 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#endif
738
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100739#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100740 // Set v:count here, when called from main() and not a stuffed
741 // command, so that v:count can be used in an expression mapping
742 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100743 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100744 set_vcount_ca(&ca, &set_prevcount);
745#endif
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 /*
748 * Get the command character from the user.
749 */
750 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100751 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000753 // If a mapping was started in Visual or Select mode, remember the length
754 // of the mapping. This is used below to not return to Insert mode for as
755 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 if (restart_edit == 0)
757 old_mapped_len = 0;
758 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000759 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 old_mapped_len = typebuf_maplen();
761
762 if (c == NUL)
763 c = K_ZERO;
764
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000765 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (VIsual_active
767 && VIsual_select
768 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
769 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000770 int len;
771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100772 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
773 // 'insertmode' is set) fake a "d"elete command, Insert mode will
774 // restart automatically.
775 // Insert the typed character in the typeahead buffer, so that it can
776 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000777 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
778
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000779 // When recording and gotchars() was called the character will be
780 // recorded again, remove the previous recording.
781 if (KeyTyped)
782 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000783
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000784 if (restart_edit != 0)
785 c = 'd';
786 else
787 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100788 msg_nowait = TRUE; // don't delay going to insert mode
789 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000792 // If the window was made so small that nothing shows, make it at least one
793 // line and one column when typing a command.
794 if (KeyTyped && !KeyStuffed)
795 win_ensure_size();
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000799 // Get the command count
800 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
801 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000803 // Find the command character in the table of commands.
804 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 if (ctrl_w)
806 {
807 ca.nchar = c;
808 ca.cmdchar = Ctrl_W;
809 }
810 else
811 ca.cmdchar = c;
812 idx = find_command(ca.cmdchar);
813 if (idx < 0)
814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100815 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 clearopbeep(oap);
817 goto normal_end;
818 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000819
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000820 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100821 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000822 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000824 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (VIsual_active)
826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (km_stopsel
829 && (nv_cmds[idx].cmd_flags & NV_STS)
830 && !(mod_mask & MOD_MASK_SHIFT))
831 {
832 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100833 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (km_startsel)
838 {
839 if (nv_cmds[idx].cmd_flags & NV_SS)
840 {
841 unshift_special(&ca);
842 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000843 if (idx < 0)
844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000846 clearopbeep(oap);
847 goto normal_end;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
851 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856#ifdef FEAT_RIGHTLEFT
857 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
858 && (nv_cmds[idx].cmd_flags & NV_RL))
859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 // Invert horizontal movements and operations. Only when typed by the
861 // user directly, not when the result of a mapping or "x" translated
862 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 switch (ca.cmdchar)
864 {
865 case 'l': ca.cmdchar = 'h'; break;
866 case K_RIGHT: ca.cmdchar = K_LEFT; break;
867 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
868 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
869 case 'h': ca.cmdchar = 'l'; break;
870 case K_LEFT: ca.cmdchar = K_RIGHT; break;
871 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
872 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
873 case '>': ca.cmdchar = '<'; break;
874 case '<': ca.cmdchar = '>'; break;
875 }
876 idx = find_command(ca.cmdchar);
877 }
878#endif
879
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000880 // Get additional characters if we need them.
881 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
882 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000884 // Flush the showcmd characters onto the screen so we can see them while
885 // the command is being executed. Only do this when the shown command was
886 // actually displayed, otherwise this will slow down a lot when executing
887 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (need_flushbuf)
889 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100890
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000891 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200892 {
893 if (ex_normal_busy)
894 did_cursorhold = save_did_cursorhold;
895 else
896 did_cursorhold = FALSE;
897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaar24959102022-05-07 20:01:16 +0100899 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
901 if (ca.nchar == ESC)
902 {
903 clearop(oap);
904 if (restart_edit == 0 && goto_im())
905 restart_edit = 'a';
906 goto normal_end;
907 }
908
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000909 if (ca.cmdchar != K_IGNORE)
910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100911 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000912 msg_col = 0;
913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100915 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // When 'keymodel' contains "startsel" some keys start Select/Visual
918 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (!VIsual_active && km_startsel)
920 {
921 if (nv_cmds[idx].cmd_flags & NV_SS)
922 {
923 start_selection();
924 unshift_special(&ca);
925 idx = find_command(ca.cmdchar);
926 }
927 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
928 && (mod_mask & MOD_MASK_SHIFT))
929 {
930 start_selection();
931 mod_mask &= ~MOD_MASK_SHIFT;
932 }
933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000935 // Execute the command!
936 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 ca.arg = nv_cmds[idx].cmd_arg;
938 (nv_cmds[idx].cmd_func)(&ca);
939
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000940 // If we didn't start or finish an operator, reset oap->regname, unless we
941 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 if (!finish_op
943 && !oap->op_type
944 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
945 {
946 clearop(oap);
947#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200948 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
950 }
951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100952 // Get the length of mapped chars again after typing a count, second
953 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000954 if (old_mapped_len > 0)
955 old_mapped_len = typebuf_maplen();
956
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000957 // If an operation is pending, handle it. But not for K_IGNORE or
958 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200959 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100960 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000962 // Wait for a moment when a message is displayed that will be overwritten
963 // by the mode message.
964 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
965 normal_cmd_wait_for_msg();
966
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000967 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968normal_end:
969
970 msg_nowait = FALSE;
971
Bram Moolenaarcc613032020-06-07 21:31:18 +0200972#ifdef FEAT_EVAL
973 if (finish_op)
974 reset_reg_var();
975#endif
976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100977 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef CURSOR_SHAPE
979 c = finish_op;
980#endif
981 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100982 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100984 // Redraw the cursor with another shape, if we were in Operator-pending
985 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if (c || ca.cmdchar == 'r')
987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100988 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989# ifdef FEAT_MOUSESHAPE
990 update_mouseshape(-1);
991# endif
992 }
993#endif
994
Bram Moolenaara983fe92008-07-31 20:04:27 +0000995 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100996 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100999 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 vim_free(ca.searchbuf);
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (has_mbyte)
1003 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (curwin->w_p_scb && toplevel)
1006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001007 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 do_check_scrollbind(TRUE);
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar860cae12010-06-05 23:22:07 +02001011 if (curwin->w_p_crb && toplevel)
1012 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001014 do_check_cursorbind();
1015 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001016
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001017#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001019 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001020 restart_edit = 0;
1021#endif
1022
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001023 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1024 // if still inside a mapping that started in Visual mode).
1025 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1028 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && !(ca.retval & CA_COMMAND_BUSY)
1030 && stuff_empty()
1031 && oap->regname == 0)
1032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (restart_VIsual_select == 1)
1034 {
1035 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001036 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 showmode();
1038 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001039 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001041 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 (void)edit(restart_edit, FALSE, 1L);
1043 }
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (restart_VIsual_select == 2)
1046 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001048 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 opcount = ca.opcount;
1050}
1051
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001052#ifdef FEAT_EVAL
1053/*
1054 * Set v:count and v:count1 according to "cap".
1055 * Set v:prevcount only when "set_prevcount" is TRUE.
1056 */
1057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001058set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001059{
1060 long count = cap->count0;
1061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001062 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001063 if (cap->opcount != 0)
1064 count = cap->opcount * (count == 0 ? 1 : count);
1065 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001067}
1068#endif
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001071 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 * if not.
1073 */
1074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 static int did_check = FALSE;
1078
1079 if (full_screen)
1080 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001081 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001082 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 did_check = TRUE;
1084 }
1085}
1086
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001087#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1088/*
1089 * Call yank_do_autocmd() for "regname".
1090 */
1091 static void
1092call_yank_do_autocmd(int regname)
1093{
1094 oparg_T oa;
1095 yankreg_T *reg;
1096
1097 clear_oparg(&oa);
1098 oa.regname = regname;
1099 oa.op_type = OP_YANK;
1100 oa.is_VIsual = TRUE;
1101 reg = get_register(regname, TRUE);
1102 yank_do_autocmd(&oa, reg);
1103 free_register(reg);
1104}
1105#endif
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001108 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001109 * This function or the next should ALWAYS be called to end Visual mode, except
1110 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 */
1112 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001113end_visual_mode()
1114{
1115 end_visual_mode_keep_button();
1116 reset_held_button();
1117}
1118
1119 void
1120end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
1122#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001123 // If we are using the clipboard, then remember what was selected in case
1124 // we need to paste it somewhere while we still own the selection.
1125 // Only do this when the clipboard is already owned. Don't want to grab
1126 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (clip_star.available && clip_star.owned)
1128 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001129
1130# if defined(FEAT_EVAL)
1131 // Emit a TextYankPost for the automatic copy of the selection into the
1132 // star and/or plus register.
1133 if (has_textyankpost())
1134 {
1135 if (clip_isautosel_star())
1136 call_yank_do_autocmd('*');
1137 if (clip_isautosel_plus())
1138 call_yank_do_autocmd('+');
1139 }
1140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
1142
1143 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 setmouse();
1145 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001147 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001148 curbuf->b_visual.vi_mode = VIsual_mode;
1149 curbuf->b_visual.vi_start = VIsual;
1150 curbuf->b_visual.vi_end = curwin->w_cursor;
1151 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#ifdef FEAT_EVAL
1153 curbuf->b_visual_mode_eval = VIsual_mode;
1154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!virtual_active())
1156 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001157 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001159 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001160 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161}
1162
1163/*
1164 * Reset VIsual_active and VIsual_reselect.
1165 */
1166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001167reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 if (VIsual_active)
1170 {
1171 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001172 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 VIsual_reselect = FALSE;
1175}
1176
1177/*
1178 * Reset VIsual_active and VIsual_reselect if it's set.
1179 */
1180 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001181reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
1183 if (VIsual_active)
1184 {
1185 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001186 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 VIsual_reselect = FALSE;
1188 }
1189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001191 void
1192restore_visual_mode(void)
1193{
1194 if (VIsual_mode_orig != NUL)
1195 {
1196 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1197 VIsual_mode_orig = NUL;
1198 }
1199}
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201/*
1202 * Check for a balloon-eval special item to include when searching for an
1203 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1204 * Returns TRUE if the character at "*ptr" should be included.
1205 * "dir" is FORWARD or BACKWARD, the direction of searching.
1206 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1207 * "bnp" points to a counter for square brackets.
1208 */
1209 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001210find_is_eval_item(
1211 char_u *ptr,
1212 int *colp,
1213 int *bnp,
1214 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001216 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1218 ++*bnp;
1219 if (*bnp > 0)
1220 {
1221 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1222 --*bnp;
1223 return TRUE;
1224 }
1225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (*ptr == '.')
1228 return TRUE;
1229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001230 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1232 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1233 {
1234 *colp += dir;
1235 return TRUE;
1236 }
1237 return FALSE;
1238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240/*
1241 * Find the identifier under or to the right of the cursor.
1242 * "find_type" can have one of three values:
1243 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001244 * FIND_STRING: find any non-white text
1245 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001246 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 *
1248 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001249 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001251 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 * This doesn't match the real Vi but I like it a little better and it
1253 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001254 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * When FIND_IDENT isn't defined, we backup until a blank.
1256 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001257 * Returns the length of the text, or zero if no text is found.
1258 * If text is found, a pointer to the text is put in "*text". This
1259 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 */
1261 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001262find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263{
1264 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001265 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266}
1267
1268/*
1269 * Like find_ident_under_cursor(), but for any window and any position.
1270 * However: Uses 'iskeyword' from the current window!.
1271 */
1272 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001273find_ident_at_pos(
1274 win_T *wp,
1275 linenr_T lnum,
1276 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001277 char_u **text,
1278 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001279 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001282 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int this_class = 0;
1285 int prev_class;
1286 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001287 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001289 // if i == 0: try to find an identifier
1290 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1292 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1293 {
1294 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001295 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 */
1297 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (has_mbyte)
1299 {
1300 while (ptr[col] != NUL)
1301 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001302 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 this_class = mb_get_class(ptr + col);
1306 if (this_class != 0 && (i == 1 || this_class != 1))
1307 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001308 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 }
1311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001313 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 )
1316 ++col;
1317
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001318 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001322 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (has_mbyte)
1325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001326 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1328 this_class = mb_get_class((char_u *)"a");
1329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001331 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1334 prev_class = mb_get_class(ptr + prevcol);
1335 if (this_class != prev_class
1336 && (i == 0
1337 || prev_class == 0
1338 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && (!(find_type & FIND_EVAL)
1340 || prevcol == 0
1341 || !find_is_eval_item(ptr + prevcol, &prevcol,
1342 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 )
1344 break;
1345 col = prevcol;
1346 }
1347
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001348 // If we don't want just any old text, or we've found an
1349 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (this_class > 2)
1351 this_class = 2;
1352 if (!(find_type & FIND_STRING) || this_class == 2)
1353 break;
1354 }
1355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 while (col > 0
1358 && ((i == 0
1359 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001360 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 && (!(find_type & FIND_IDENT)
1362 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 || ((find_type & FIND_EVAL)
1364 && col > 1
1365 && find_is_eval_item(ptr + col - 1, &col,
1366 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ))
1368 --col;
1369
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001370 // If we don't want just any old text, or we've found an
1371 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1373 break;
1374 }
1375 }
1376
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001377 if (ptr[col] == NUL || (i == 0
1378 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001380 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001381 if ((find_type & FIND_NOERROR) == 0)
1382 {
1383 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001384 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001385 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001386 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 return 0;
1389 }
1390 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001391 *text = ptr;
1392 if (textcol != NULL)
1393 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001396 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 bn = 0;
1399 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (has_mbyte)
1402 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001403 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 this_class = mb_get_class(ptr);
1405 while (ptr[col] != NUL
1406 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1407 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 || ((find_type & FIND_EVAL)
1409 && col <= (int)startcol
1410 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001412 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001416 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 || ((find_type & FIND_EVAL)
1418 && col <= (int)startcol
1419 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423 return col;
1424}
1425
1426/*
1427 * Prepare for redo of a normal command.
1428 */
1429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001430prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 prep_redo(cap->oap->regname, cap->count0,
1433 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1434}
1435
1436/*
1437 * Prepare for redo of any command.
1438 * Note that only the last argument can be a multi-byte char.
1439 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001440 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001441prep_redo(
1442 int regname,
1443 long num,
1444 int cmd1,
1445 int cmd2,
1446 int cmd3,
1447 int cmd4,
1448 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001450 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1451}
1452
1453/*
1454 * Prepare for redo of any command with extra count after "cmd2".
1455 */
1456 void
1457prep_redo_num2(
1458 int regname,
1459 long num1,
1460 int cmd1,
1461 int cmd2,
1462 long num2,
1463 int cmd3,
1464 int cmd4,
1465 int cmd5)
1466{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001468
1469#ifdef FEAT_EVAL
1470 // Put info about a mapping in the redo buffer, so that "." will use the
1471 // same script context.
1472 may_add_last_used_map_to_redobuff();
1473#endif
1474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 AppendCharToRedobuff('"');
1478 AppendCharToRedobuff(regname);
1479 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001480 if (num1 != 0)
1481 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (cmd1 != NUL)
1483 AppendCharToRedobuff(cmd1);
1484 if (cmd2 != NUL)
1485 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001486 if (num2 != 0)
1487 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (cmd3 != NUL)
1489 AppendCharToRedobuff(cmd3);
1490 if (cmd4 != NUL)
1491 AppendCharToRedobuff(cmd4);
1492 if (cmd5 != NUL)
1493 AppendCharToRedobuff(cmd5);
1494}
1495
1496/*
1497 * check for operator active and clear it
1498 *
1499 * return TRUE if operator was active
1500 */
1501 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001502checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 if (oap->op_type == OP_NOP)
1505 return FALSE;
1506 clearopbeep(oap);
1507 return TRUE;
1508}
1509
1510/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001511 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001513 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 */
1515 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001516checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001518 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 return FALSE;
1520 clearopbeep(oap);
1521 return TRUE;
1522}
1523
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001524 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001525clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 oap->op_type = OP_NOP;
1528 oap->regname = 0;
1529 oap->motion_force = NUL;
1530 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001531 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532}
1533
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001535clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 clearop(oap);
1538 beep_flush();
1539}
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541/*
1542 * Remove the shift modifier from a special key.
1543 */
1544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001545unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 switch (cap->cmdchar)
1548 {
1549 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1550 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1551 case K_S_UP: cap->cmdchar = K_UP; break;
1552 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1553 case K_S_HOME: cap->cmdchar = K_HOME; break;
1554 case K_S_END: cap->cmdchar = K_END; break;
1555 }
1556 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001559/*
1560 * If the mode is currently displayed clear the command line or update the
1561 * command displayed.
1562 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001564may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001565{
1566 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001567 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001568 else
1569 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001570}
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572/*
1573 * Routines for displaying a partly typed command
1574 */
1575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001576static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577static int showcmd_is_clear = TRUE;
1578static int showcmd_visual = FALSE;
1579
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001580static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001583clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 if (!p_sc)
1586 return;
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (VIsual_active && !char_avail())
1589 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001590 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 long lines;
1592 colnr_T leftcol, rightcol;
1593 linenr_T top, bot;
1594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001595 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001596 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 top = VIsual.lnum;
1599 bot = curwin->w_cursor.lnum;
1600 }
1601 else
1602 {
1603 top = curwin->w_cursor.lnum;
1604 bot = VIsual.lnum;
1605 }
1606# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001608 (void)hasFolding(top, &top, NULL);
1609 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610# endif
1611 lines = bot - top + 1;
1612
1613 if (VIsual_mode == Ctrl_V)
1614 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001615# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001616 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001617 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001619 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001620 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001621 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001624# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001625 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001626 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1629 (long)(rightcol - leftcol + 1));
1630 }
1631 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1632 sprintf((char *)showcmd_buf, "%ld", lines);
1633 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001634 {
1635 char_u *s, *e;
1636 int l;
1637 int bytes = 0;
1638 int chars = 0;
1639
1640 if (cursor_bot)
1641 {
1642 s = ml_get_pos(&VIsual);
1643 e = ml_get_cursor();
1644 }
1645 else
1646 {
1647 s = ml_get_cursor();
1648 e = ml_get_pos(&VIsual);
1649 }
1650 while ((*p_sel != 'e') ? s <= e : s < e)
1651 {
1652 l = (*mb_ptr2len)(s);
1653 if (l == 0)
1654 {
1655 ++bytes;
1656 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001657 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001658 }
1659 bytes += l;
1660 ++chars;
1661 s += l;
1662 }
1663 if (bytes == chars)
1664 sprintf((char *)showcmd_buf, "%d", chars);
1665 else
1666 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1667 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001668 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 showcmd_visual = TRUE;
1670 }
1671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 {
1673 showcmd_buf[0] = NUL;
1674 showcmd_visual = FALSE;
1675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001676 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (showcmd_is_clear)
1678 return;
1679 }
1680
1681 display_showcmd();
1682}
1683
1684/*
1685 * Add 'c' to string of shown command chars.
1686 * Return TRUE if output has been written (and setcursor() has been called).
1687 */
1688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001689add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 char_u *p;
1692 int old_len;
1693 int extra_len;
1694 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int i;
1696 static int ignore[] =
1697 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001698#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1700 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001701#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001702 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001703 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1705 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001706 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001708 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 0
1710 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar09df3122006-01-23 22:23:09 +00001712 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 return FALSE;
1714
1715 if (showcmd_visual)
1716 {
1717 showcmd_buf[0] = NUL;
1718 showcmd_visual = FALSE;
1719 }
1720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001721 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (IS_SPECIAL(c))
1723 for (i = 0; ignore[i] != 0; ++i)
1724 if (ignore[i] == c)
1725 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001728 if (*p == ' ')
1729 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 old_len = (int)STRLEN(showcmd_buf);
1731 extra_len = (int)STRLEN(p);
1732 overflow = old_len + extra_len - SHOWCMD_COLS;
1733 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001734 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1735 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 STRCAT(showcmd_buf, p);
1737
1738 if (char_avail())
1739 return FALSE;
1740
1741 display_showcmd();
1742
1743 return TRUE;
1744}
1745
1746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001747add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 if (!add_to_showcmd(c))
1750 setcursor();
1751}
1752
1753/*
1754 * Delete 'len' characters from the end of the shown command.
1755 */
1756 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001757del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 int old_len;
1760
1761 if (!p_sc)
1762 return;
1763
1764 old_len = (int)STRLEN(showcmd_buf);
1765 if (len > old_len)
1766 len = old_len;
1767 showcmd_buf[old_len - len] = NUL;
1768
1769 if (!char_avail())
1770 display_showcmd();
1771}
1772
1773/*
1774 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1775 * something and there is a partial mapping.
1776 */
1777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001778push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 if (p_sc)
1781 STRCPY(old_showcmd_buf, showcmd_buf);
1782}
1783
1784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001785pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 if (!p_sc)
1788 return;
1789
1790 STRCPY(showcmd_buf, old_showcmd_buf);
1791
1792 display_showcmd();
1793}
1794
1795 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001796display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
Luuk van Baalba936f62022-12-15 13:15:39 +00001798 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Luuk van Baalba936f62022-12-15 13:15:39 +00001800 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 cursor_off();
1802
Luuk van Baalba936f62022-12-15 13:15:39 +00001803 if (*p_sloc == 's')
1804 win_redr_status(curwin, FALSE);
1805 else if (*p_sloc == 't')
1806 draw_tabline();
1807 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001809 if (!showcmd_is_clear)
1810 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
Luuk van Baalba936f62022-12-15 13:15:39 +00001812 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1813 // spaces
1814 screen_puts((char_u *)" " + len,
1815 (int)Rows - 1, sc_col + len, 0);
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001818 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * When "check" is FALSE, prepare for commands that scroll the window.
1823 * When "check" is TRUE, take care of scroll-binding after the window has
1824 * scrolled. Called from normal_cmd() and edit().
1825 */
1826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001827do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 static win_T *old_curwin = NULL;
1830 static linenr_T old_topline = 0;
1831#ifdef FEAT_DIFF
1832 static int old_topfill = 0;
1833#endif
1834 static buf_T *old_buf = NULL;
1835 static colnr_T old_leftcol = 0;
1836
1837 if (check && curwin->w_p_scb)
1838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001839 // If a ":syncbind" command was just used, don't scroll, only reset
1840 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (did_syncbind)
1842 did_syncbind = FALSE;
1843 else if (curwin == old_curwin)
1844 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001845 // Synchronize other windows, as necessary according to
1846 // 'scrollbind'. Don't do this after an ":edit" command, except
1847 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if ((curwin->w_buffer == old_buf
1849#ifdef FEAT_DIFF
1850 || curwin->w_p_diff
1851#endif
1852 )
1853 && (curwin->w_topline != old_topline
1854#ifdef FEAT_DIFF
1855 || curwin->w_topfill != old_topfill
1856#endif
1857 || curwin->w_leftcol != old_leftcol))
1858 {
1859 check_scrollbind(curwin->w_topline - old_topline,
1860 (long)(curwin->w_leftcol - old_leftcol));
1861 }
1862 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001865 // When switching between windows, make sure that the relative
1866 // vertical offset is valid for the new window. The relative
1867 // offset is invalid whenever another 'scrollbind' window has
1868 // scrolled to a point that would force the current window to
1869 // scroll past the beginning or end of its buffer. When the
1870 // resync is performed, some of the other 'scrollbind' windows may
1871 // need to jump so that the current window's relative position is
1872 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1874 }
1875 curwin->w_scbind_pos = curwin->w_topline;
1876 }
1877
1878 old_curwin = curwin;
1879 old_topline = curwin->w_topline;
1880#ifdef FEAT_DIFF
1881 old_topfill = curwin->w_topfill;
1882#endif
1883 old_buf = curwin->w_buffer;
1884 old_leftcol = curwin->w_leftcol;
1885}
1886
1887/*
1888 * Synchronize any windows that have "scrollbind" set, based on the
1889 * number of rows by which the current window has changed
1890 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1891 */
1892 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001893check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int want_ver;
1896 int want_hor;
1897 win_T *old_curwin = curwin;
1898 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 int old_VIsual_select = VIsual_select;
1900 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 colnr_T tgt_leftcol = curwin->w_leftcol;
1902 long topline;
1903 long y;
1904
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001905 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1907#ifdef FEAT_DIFF
1908 want_ver |= old_curwin->w_p_diff;
1909#endif
1910 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1911
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001912 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001914 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001917 // skip original window and windows with 'noscrollbind'
1918 if (curwin == old_curwin || !curwin->w_p_scb)
1919 continue;
1920
1921 // do the vertical scroll
1922 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001925 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
zeertzjq101d57b2022-07-31 18:34:32 +01001927 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
zeertzjq101d57b2022-07-31 18:34:32 +01001929 else
1930#endif
1931 {
1932 curwin->w_scbind_pos += topline_diff;
1933 topline = curwin->w_scbind_pos;
1934 if (topline > curbuf->b_ml.ml_line_count)
1935 topline = curbuf->b_ml.ml_line_count;
1936 if (topline < 1)
1937 topline = 1;
1938
1939 y = topline - curwin->w_topline;
1940 if (y > 0)
1941 scrollup(y, FALSE);
1942 else
1943 scrolldown(-y, FALSE);
1944 }
1945
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001946 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001947 cursor_correct();
1948 curwin->w_redr_status = TRUE;
1949 }
1950
1951 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001952 if (want_hor)
1953 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
1955
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001956 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 VIsual_select = old_VIsual_select;
1958 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 curwin = old_curwin;
1960 curbuf = old_curbuf;
1961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963/*
1964 * Command character that's ignored.
1965 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001966 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001969nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001971 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972}
1973
1974/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001975 * Command character that doesn't do anything, but unlike nv_ignore() does
1976 * start edit(). Used for "startinsert" executed while starting up.
1977 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001979nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001980{
1981}
1982
1983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 * Command character doesn't exist.
1985 */
1986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001987nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 clearopbeep(cap->oap);
1990}
1991
1992/*
1993 * <Help> and <F1> commands.
1994 */
1995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001996nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 if (!checkclearopq(cap->oap))
1999 ex_help(NULL);
2000}
2001
2002/*
2003 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2004 */
2005 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002006nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002008#ifdef FEAT_JOB_CHANNEL
2009 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2010 clearopbeep(cap->oap);
2011 else
2012#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002013 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002014 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002015 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002016 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2017 op_addsub(cap->oap, cap->count1, cap->arg);
2018 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002019 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002020 else if (VIsual_active)
2021 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002022 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002023 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
2026/*
2027 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2028 */
2029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002032 if (checkclearop(cap->oap))
2033 return;
2034
2035 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002036 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002037 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2038 if (cap->arg == BACKWARD)
2039 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002040 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002041 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002042 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002043 else
2044 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045}
2046
2047/*
2048 * Implementation of "gd" and "gD" command.
2049 */
2050 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002051nv_gd(
2052 oparg_T *oap,
2053 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002054 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 char_u *ptr;
2058
Bram Moolenaard9d30582005-05-18 22:10:28 +00002059 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002060 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002061 == FAIL)
2062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002064 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002065 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002066
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002067#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002068 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2069 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002070#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002071 // clear any search statistics
2072 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2073 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002074}
2075
2076/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002077 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2078 * otherwise.
2079 */
2080 static int
2081is_ident(char_u *line, int offset)
2082{
2083 int i;
2084 int incomment = FALSE;
2085 int instring = 0;
2086 int prev = 0;
2087
2088 for (i = 0; i < offset && line[i] != NUL; i++)
2089 {
2090 if (instring != 0)
2091 {
2092 if (prev != '\\' && line[i] == instring)
2093 instring = 0;
2094 }
2095 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2096 {
2097 instring = line[i];
2098 }
2099 else
2100 {
2101 if (incomment)
2102 {
2103 if (prev == '*' && line[i] == '/')
2104 incomment = FALSE;
2105 }
2106 else if (prev == '/' && line[i] == '*')
2107 {
2108 incomment = TRUE;
2109 }
2110 else if (prev == '/' && line[i] == '/')
2111 {
2112 return FALSE;
2113 }
2114 }
2115
2116 prev = line[i];
2117 }
2118
2119 return incomment == FALSE && instring == 0;
2120}
2121
2122/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002123 * Search for variable declaration of "ptr[len]".
2124 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2125 * current file ("gD").
2126 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002127 * Return FAIL when not found.
2128 */
2129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002130find_decl(
2131 char_u *ptr,
2132 int len,
2133 int locally,
2134 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002136{
2137 char_u *pat;
2138 pos_T old_pos;
2139 pos_T par_pos;
2140 pos_T found_pos;
2141 int t;
2142 int save_p_ws;
2143 int save_p_scs;
2144 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002145 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002146 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002147 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002148
2149 if ((pat = alloc(len + 7)) == NULL)
2150 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002152 // Put "\V" before the pattern to avoid that the special meaning of "."
2153 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002154 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2155 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 old_pos = curwin->w_cursor;
2157 save_p_ws = p_ws;
2158 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002159 p_ws = FALSE; // don't wrap around end of file now
2160 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002162 // With "gD" go to line 1.
2163 // With "gd" Search back for the start of the current function, then go
2164 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002165 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002167 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002169 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171 else
2172 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002173 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2175 --curwin->w_cursor.lnum;
2176 }
2177 curwin->w_cursor.col = 0;
2178
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002179 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002180 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002181 for (;;)
2182 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002183 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002184 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002185 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002186 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002187
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002188 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002189 {
2190 pos_T *pos;
2191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 // Check that the block the match is in doesn't end before the
2193 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002194 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2195 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2196 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002197 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 // There can't be a useful match before the end of this block.
2199 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002200 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002201 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002202 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002203 }
2204
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002205 if (t == FAIL)
2206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002208 if (found_pos.lnum != 0)
2209 {
2210 curwin->w_cursor = found_pos;
2211 t = OK;
2212 }
2213 break;
2214 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002215 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002218 ++curwin->w_cursor.lnum;
2219 curwin->w_cursor.col = 0;
2220 continue;
2221 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002222 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002224 // If the current position is not a valid identifier and a previous
2225 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002226 if (!valid && found_pos.lnum != 0)
2227 {
2228 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002229 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002230 }
2231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002233 if (valid && !locally)
2234 break;
2235 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002238 if (found_pos.lnum != 0)
2239 curwin->w_cursor = found_pos;
2240 break;
2241 }
2242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002243 // For finding a local variable and the match is before the "{" or
2244 // inside a comment, continue searching. For K&R style function
2245 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002246 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002247 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002248 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002249 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002250 // Remove SEARCH_START from flags to avoid getting stuck at one
2251 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002252 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002254
2255 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002257 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 curwin->w_cursor = old_pos;
2259 }
2260 else
2261 {
2262 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002263 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 reset_search_dir();
2265 }
2266
2267 vim_free(pat);
2268 p_ws = save_p_ws;
2269 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002270
2271 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272}
2273
2274/*
2275 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2276 * lines rather than lines in the file.
2277 * 'dist' must be positive.
2278 *
2279 * Return OK if able to move cursor, FAIL otherwise.
2280 */
2281 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002282nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002284 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 int retval = OK;
2286 int atend = FALSE;
2287 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002288 int col_off1; // margin offset for first screen line
2289 int col_off2; // margin offset for wrapped screen line
2290 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002291 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
2293 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002294 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 col_off1 = curwin_col_off();
2297 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002298 width1 = curwin->w_width - col_off1;
2299 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002300 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002301 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002304 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002305 // Instead of sticking at the last character of the buffer line we
2306 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (curwin->w_curswant == MAXCOL)
2308 {
2309 atend = TRUE;
2310 validate_virtcol();
2311 if (width1 <= 0)
2312 curwin->w_curswant = 0;
2313 else
2314 {
2315 curwin->w_curswant = width1 - 1;
2316 if (curwin->w_virtcol > curwin->w_curswant)
2317 curwin->w_curswant += ((curwin->w_virtcol
2318 - curwin->w_curswant - 1) / width2 + 1) * width2;
2319 }
2320 }
2321 else
2322 {
2323 if (linelen > width1)
2324 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2325 else
2326 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002327 if (curwin->w_curswant >= (colnr_T)n)
2328 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330
2331 while (dist--)
2332 {
2333 if (dir == BACKWARD)
2334 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002335 if ((long)curwin->w_curswant >= width1
2336#ifdef FEAT_FOLDING
2337 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2338#endif
2339 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002340 // Move back within the line. This can give a negative value
2341 // for w_curswant if width1 < width2 (with cpoptions+=n),
2342 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 curwin->w_curswant -= width2;
2344 else
2345 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002346 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002348 // Move to the start of a closed fold. Don't do that when
2349 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (!(fdo_flags & FDO_ALL))
2351 (void)hasFolding(curwin->w_cursor.lnum,
2352 &curwin->w_cursor.lnum, NULL);
2353#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002354 if (curwin->w_cursor.lnum == 1)
2355 {
2356 retval = FAIL;
2357 break;
2358 }
2359 --curwin->w_cursor.lnum;
2360
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002361 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (linelen > width1)
2363 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2364 + 1) * width2;
2365 }
2366 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002367 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
2369 if (linelen > width1)
2370 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2371 else
2372 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002373 if (curwin->w_curswant + width2 < (colnr_T)n
2374#ifdef FEAT_FOLDING
2375 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2376#endif
2377 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002378 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 curwin->w_curswant += width2;
2380 else
2381 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002384 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2386 &curwin->w_cursor.lnum);
2387#endif
2388 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2389 {
2390 retval = FAIL;
2391 break;
2392 }
2393 curwin->w_cursor.lnum++;
2394 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002395 // Check if the cursor has moved below the number display
2396 // when width1 < width2 (with cpoptions+=n). Subtract width2
2397 // to get a negative value for w_curswant, which will get
2398 // clipped to column 0.
2399 if (curwin->w_curswant >= width1)
2400 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002401 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 }
2404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002407 if (virtual_active() && atend)
2408 coladvance(MAXCOL);
2409 else
2410 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2413 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002414 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002415 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002416
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002417 // Check for landing on a character that got split at the end of the
2418 // last line. We want to advance a screenline, not end up in the same
2419 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002421 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002422#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002423 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2424 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002425#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002426
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002427 c = (*mb_ptr2char)(ml_get_cursor());
2428 if (dir == FORWARD && virtcol < curwin->w_curswant
2429 && (curwin->w_curswant <= (colnr_T)width1)
2430 && !vim_isprintc(c) && c > 255)
2431 oneright();
2432
Bram Moolenaar773b1582014-08-29 14:20:51 +02002433 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 && (curwin->w_curswant < (colnr_T)width1
2435 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2436 : ((curwin->w_curswant - width1) % width2
2437 > (colnr_T)width2 / 2)))
2438 --curwin->w_cursor.col;
2439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002443 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 return retval;
2446}
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448/*
2449 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2450 * cap->arg must be TRUE for CTRL-E.
2451 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002453nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 if (!checkclearop(cap->oap))
2456 scroll_redraw(cap->arg, cap->count1);
2457}
2458
2459/*
2460 * Scroll "count" lines up or down, and redraw.
2461 */
2462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002466 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifdef FEAT_DIFF
2468 int prev_topfill = curwin->w_topfill;
2469#endif
2470 linenr_T prev_lnum = curwin->w_cursor.lnum;
2471
2472 if (up)
2473 scrollup(count, TRUE);
2474 else
2475 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002476 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002478 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2479 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 cursor_correct();
2481 check_cursor_moved(curwin);
2482 curwin->w_valid |= VALID_TOPLINE;
2483
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002484 // If moved back to where we were, at least move the cursor, otherwise
2485 // we get stuck at one position. Don't move the cursor up if the
2486 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002488 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489#ifdef FEAT_DIFF
2490 && curwin->w_topfill == prev_topfill
2491#endif
2492 )
2493 {
2494 if (up)
2495 {
2496 if (curwin->w_cursor.lnum > prev_lnum
2497 || cursor_down(1L, FALSE) == FAIL)
2498 break;
2499 }
2500 else
2501 {
2502 if (curwin->w_cursor.lnum < prev_lnum
2503 || prev_topline == 1L
2504 || cursor_up(1L, FALSE) == FAIL)
2505 break;
2506 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002507 // Mark w_topline as valid, otherwise the screen jumps back at the
2508 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 check_cursor_moved(curwin);
2510 curwin->w_valid |= VALID_TOPLINE;
2511 }
2512 }
2513 if (curwin->w_cursor.lnum != prev_lnum)
2514 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002515 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002519 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2520 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2521 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002522 */
2523 static int
2524nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2525{
2526 int nchar = *nchar_arg;
2527 long n;
2528
2529 // "z123{nchar}": edit the count before obtaining {nchar}
2530 if (checkclearop(cap->oap))
2531 return FALSE;
2532 n = nchar - '0';
2533
2534 for (;;)
2535 {
2536#ifdef USE_ON_FLY_SCROLL
2537 dont_scroll = TRUE; // disallow scrolling here
2538#endif
2539 ++no_mapping;
2540 ++allow_keys; // no mapping for nchar, but allow key codes
2541 nchar = plain_vgetc();
2542 LANGMAP_ADJUST(nchar, TRUE);
2543 --no_mapping;
2544 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002545 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002547 if (nchar == K_DEL || nchar == K_KDEL)
2548 n /= 10;
2549 else if (VIM_ISDIGIT(nchar))
2550 n = n * 10 + (nchar - '0');
2551 else if (nchar == CAR)
2552 {
2553#ifdef FEAT_GUI
2554 need_mouse_correct = TRUE;
2555#endif
2556 win_setheight((int)n);
2557 break;
2558 }
2559 else if (nchar == 'l'
2560 || nchar == 'h'
2561 || nchar == K_LEFT
2562 || nchar == K_RIGHT)
2563 {
2564 cap->count1 = n ? n * cap->count1 : cap->count1;
2565 *nchar_arg = nchar;
2566 return TRUE;
2567 }
2568 else
2569 {
2570 clearopbeep(cap->oap);
2571 break;
2572 }
2573 }
2574 cap->oap->op_type = OP_NOP;
2575 return FALSE;
2576}
2577
2578#ifdef FEAT_SPELL
2579/*
2580 * "zug" and "zuw": undo "zg" and "zw"
2581 * "zg": add good word to word list
2582 * "zw": add wrong word to word list
2583 * "zG": add good word to temp word list
2584 * "zW": add wrong word to temp word list
2585 */
2586 static int
2587nv_zg_zw(cmdarg_T *cap, int nchar)
2588{
2589 char_u *ptr = NULL;
2590 int len;
2591 int undo = FALSE;
2592
2593 if (nchar == 'u')
2594 {
2595 ++no_mapping;
2596 ++allow_keys; // no mapping for nchar, but allow key codes
2597 nchar = plain_vgetc();
2598 LANGMAP_ADJUST(nchar, TRUE);
2599 --no_mapping;
2600 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002601 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002602
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002603 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2604 {
2605 clearopbeep(cap->oap);
2606 return OK;
2607 }
2608 undo = TRUE;
2609 }
2610
2611 if (checkclearop(cap->oap))
2612 return OK;
2613 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2614 return FAIL;
2615 if (ptr == NULL)
2616 {
2617 pos_T pos = curwin->w_cursor;
2618
2619 // Find bad word under the cursor. When 'spell' is
2620 // off this fails and find_ident_under_cursor() is
2621 // used below.
2622 emsg_off++;
2623 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2624 emsg_off--;
2625 if (len != 0 && curwin->w_cursor.col <= pos.col)
2626 ptr = ml_get_pos(&curwin->w_cursor);
2627 curwin->w_cursor = pos;
2628 }
2629
2630 if (ptr == NULL
2631 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2632 return FAIL;
2633 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2634 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2635 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2636
2637 return OK;
2638}
2639#endif
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Commands that start with "z".
2643 */
2644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002645nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 long n;
2648 colnr_T col;
2649 int nchar = cap->nchar;
2650#ifdef FEAT_FOLDING
2651 long old_fdl = curwin->w_p_fdl;
2652 int old_fen = curwin->w_p_fen;
2653#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002654 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002656 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (
2660#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002661 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2662 // and "zC" only in Visual mode. "zj" and "zk" are motion
2663 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 cap->nchar != 'f' && cap->nchar != 'F'
2665 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2666 && cap->nchar != 'j' && cap->nchar != 'k'
2667 &&
2668#endif
2669 checkclearop(cap->oap))
2670 return;
2671
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002672 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2673 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2675 && cap->count0
2676 && cap->count0 != curwin->w_cursor.lnum)
2677 {
2678 setpcmark();
2679 if (cap->count0 > curbuf->b_ml.ml_line_count)
2680 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2681 else
2682 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002683 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685
2686 switch (nchar)
2687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002688 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 case '+':
2690 if (cap->count0 == 0)
2691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // No count given: put cursor at the line below screen
2693 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2696 else
2697 curwin->w_cursor.lnum = curwin->w_botline;
2698 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 case NL:
2701 case CAR:
2702 case K_KENTER:
2703 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002707 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002708 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 break;
2710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002716 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002717 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 break;
2719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // "z^", "z-" and "zb": put cursor at bottom of screen
2721 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2722 // when <count> is at bottom of window, and puts that one at
2723 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (cap->count0 != 0)
2725 {
2726 scroll_cursor_bot(0, TRUE);
2727 curwin->w_cursor.lnum = curwin->w_topline;
2728 }
2729 else if (curwin->w_topline == 1)
2730 curwin->w_cursor.lnum = 1;
2731 else
2732 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 case '-':
2735 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002739 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002740 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
2742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 case 'h':
2750 case K_LEFT:
2751 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002752 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2753 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 break;
2755
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002756 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002760 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 case 'l':
2762 case K_RIGHT:
2763 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002764 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 break;
2766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 case 's': if (!curwin->w_p_wrap)
2769 {
2770#ifdef FEAT_FOLDING
2771 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
2774#endif
2775 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002776 if ((long)col > siso)
2777 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else
2779 col = 0;
2780 if (curwin->w_leftcol != col)
2781 {
2782 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002783 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 }
2786 break;
2787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002788 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 case 'e': if (!curwin->w_p_wrap)
2790 {
2791#ifdef FEAT_FOLDING
2792 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else
2795#endif
2796 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002797 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002798 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 col = 0;
2800 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002801 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (curwin->w_leftcol != col)
2803 {
2804 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002805 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 }
2808 break;
2809
Christian Brabandt2fa93842021-05-30 22:17:25 +02002810 // "zp", "zP" in block mode put without addind trailing spaces
2811 case 'P':
2812 case 'p': nv_put(cap);
2813 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002814 // "zy" Yank without trailing spaces
2815 case 'y': nv_operator(cap);
2816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 // "zF": create fold command
2819 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case 'F':
2821 case 'f': if (foldManualAllowed(TRUE))
2822 {
2823 cap->nchar = 'f';
2824 nv_operator(cap);
2825 curwin->w_p_fen = TRUE;
2826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2829 {
2830 nv_operator(cap);
2831 finish_op = TRUE;
2832 }
2833 }
2834 else
2835 clearopbeep(cap->oap);
2836 break;
2837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // "zd": delete fold at cursor
2839 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 case 'd':
2841 case 'D': if (foldManualAllowed(FALSE))
2842 {
2843 if (VIsual_active)
2844 nv_operator(cap);
2845 else
2846 deleteFold(curwin->w_cursor.lnum,
2847 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2848 }
2849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case 'E': if (foldmethodIsManual(curwin))
2853 {
2854 clearFolding(curwin);
2855 changed_window_setting();
2856 }
2857 else if (foldmethodIsMarker(curwin))
2858 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2859 TRUE, FALSE);
2860 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002861 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 break;
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case 'n': curwin->w_p_fen = FALSE;
2866 break;
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 case 'N': curwin->w_p_fen = TRUE;
2870 break;
2871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2874 break;
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2878 openFold(curwin->w_cursor.lnum, cap->count1);
2879 else
2880 {
2881 closeFold(curwin->w_cursor.lnum, cap->count1);
2882 curwin->w_p_fen = TRUE;
2883 }
2884 break;
2885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2888 openFoldRecurse(curwin->w_cursor.lnum);
2889 else
2890 {
2891 closeFoldRecurse(curwin->w_cursor.lnum);
2892 curwin->w_p_fen = TRUE;
2893 }
2894 break;
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 case 'o': if (VIsual_active)
2898 nv_operator(cap);
2899 else
2900 openFold(curwin->w_cursor.lnum, cap->count1);
2901 break;
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 case 'O': if (VIsual_active)
2905 nv_operator(cap);
2906 else
2907 openFoldRecurse(curwin->w_cursor.lnum);
2908 break;
2909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case 'c': if (VIsual_active)
2912 nv_operator(cap);
2913 else
2914 closeFold(curwin->w_cursor.lnum, cap->count1);
2915 curwin->w_p_fen = TRUE;
2916 break;
2917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 case 'C': if (VIsual_active)
2920 nv_operator(cap);
2921 else
2922 closeFoldRecurse(curwin->w_cursor.lnum);
2923 curwin->w_p_fen = TRUE;
2924 break;
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 case 'v': foldOpenCursor();
2928 break;
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 curwin->w_foldinvalid = TRUE; // recompute folds
2933 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 foldOpenCursor();
2935 break;
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 curwin->w_foldinvalid = TRUE; // recompute folds
2940 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002945 {
2946 curwin->w_p_fdl -= cap->count1;
2947 if (curwin->w_p_fdl < 0)
2948 curwin->w_p_fdl = 0;
2949 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 curwin->w_p_fen = TRUE;
2952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 curwin->w_p_fen = TRUE;
2958 break;
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002961 case 'r': curwin->w_p_fdl += cap->count1;
2962 {
2963 int d = getDeepestNesting();
2964
2965 if (curwin->w_p_fdl >= d)
2966 curwin->w_p_fdl = d;
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 break;
2969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 break;
2974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 case 'j': // "zj" move to next fold downwards
2976 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2978 cap->count1) == FAIL)
2979 clearopbeep(cap->oap);
2980 break;
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002984#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 case 'g': // "zg": add good word to word list
2987 case 'w': // "zw": add wrong word to word list
2988 case 'G': // "zG": add good word to temp word list
2989 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002990 if (nv_zg_zw(cap, nchar) == FAIL)
2991 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002992 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002995 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002996 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002997 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 default: clearopbeep(cap->oap);
3001 }
3002
3003#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (old_fen != curwin->w_p_fen)
3006 {
3007# ifdef FEAT_DIFF
3008 win_T *wp;
3009
3010 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 FOR_ALL_WINDOWS(wp)
3014 {
3015 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3016 {
3017 wp->w_p_fen = curwin->w_p_fen;
3018 changed_window_setting_win(wp);
3019 }
3020 }
3021 }
3022# endif
3023 changed_window_setting();
3024 }
3025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003026 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (old_fdl != curwin->w_p_fdl)
3028 newFoldLevel();
3029#endif
3030}
3031
3032#ifdef FEAT_GUI
3033/*
3034 * Vertical scrollbar movement.
3035 */
3036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003037nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 if (cap->oap->op_type != OP_NOP)
3040 clearopbeep(cap->oap);
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 gui_do_scroll();
3044}
3045
3046/*
3047 * Horizontal scrollbar movement.
3048 */
3049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (cap->oap->op_type != OP_NOP)
3053 clearopbeep(cap->oap);
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003056 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057}
3058#endif
3059
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003060#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003061/*
3062 * Click in GUI tab.
3063 */
3064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003065nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003066{
3067 if (cap->oap->op_type != OP_NOP)
3068 clearopbeep(cap->oap);
3069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003071 goto_tabpage(current_tab);
3072}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003073
3074/*
3075 * Selected item in tab line menu.
3076 */
3077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003079{
3080 if (cap->oap->op_type != OP_NOP)
3081 clearopbeep(cap->oap);
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003084 handle_tabmenu();
3085}
3086
3087/*
3088 * Handle selecting an item of the GUI tab line menu.
3089 * Used in Normal and Insert mode.
3090 */
3091 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003092handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003093{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003094 switch (current_tabmenu)
3095 {
3096 case TABLINE_MENU_CLOSE:
3097 if (current_tab == 0)
3098 do_cmdline_cmd((char_u *)"tabclose");
3099 else
3100 {
3101 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3102 current_tab);
3103 do_cmdline_cmd(IObuff);
3104 }
3105 break;
3106
3107 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003108 if (current_tab == 0)
3109 do_cmdline_cmd((char_u *)"$tabnew");
3110 else
3111 {
3112 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3113 current_tab - 1);
3114 do_cmdline_cmd(IObuff);
3115 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003116 break;
3117
3118 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003119 if (current_tab == 0)
3120 do_cmdline_cmd((char_u *)"browse $tabnew");
3121 else
3122 {
3123 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3124 current_tab - 1);
3125 do_cmdline_cmd(IObuff);
3126 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003127 break;
3128 }
3129}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003130#endif
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132/*
3133 * "Q" command.
3134 */
3135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003136nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003138 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003140 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003141 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 do_exmode(FALSE);
3143}
3144
3145/*
3146 * Handle a ":" command.
3147 */
3148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003151 int old_p_im;
3152 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003153 int is_cmdkey = cap->cmdchar == K_COMMAND
3154 || cap->cmdchar == K_SCRIPT_COMMAND;
3155 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar957cf672020-11-12 14:21:06 +01003157 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003159 nv_operator(cap);
3160 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003162
3163 if (cap->oap->op_type != OP_NOP)
3164 {
3165 // Using ":" as a movement is characterwise exclusive.
3166 cap->oap->motion_type = MCHAR;
3167 cap->oap->inclusive = FALSE;
3168 }
3169 else if (cap->count0 && !is_cmdkey)
3170 {
3171 // translate "count:" into ":.,.+(count - 1)"
3172 stuffcharReadbuff('.');
3173 if (cap->count0 > 1)
3174 {
3175 stuffReadbuff((char_u *)",.+");
3176 stuffnumReadbuff((long)cap->count0 - 1L);
3177 }
3178 }
3179
3180 // When typing, don't type below an old message
3181 if (KeyTyped)
3182 compute_cmdrow();
3183
3184 old_p_im = p_im;
3185
3186 // get a command line and execute it
3187 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);
3192
3193 // If 'insertmode' changed, enter or exit Insert mode
3194 if (p_im != old_p_im)
3195 {
3196 if (p_im)
3197 restart_edit = 'i';
3198 else
3199 restart_edit = 0;
3200 }
3201
3202 if (cmd_result == FAIL)
3203 // The Ex command failed, do not execute the operator.
3204 clearop(cap->oap);
3205 else if (cap->oap->op_type != OP_NOP
3206 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3207 || cap->oap->start.col >
3208 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3209 || did_emsg
3210 ))
3211 // The start of the operator has become invalid by the Ex command.
3212 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213}
3214
3215/*
3216 * Handle CTRL-G command.
3217 */
3218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003219nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
3223 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003224 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 showmode();
3226 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003227 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003228 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 fileinfo((int)cap->count0, FALSE, TRUE);
3230}
3231
3232/*
3233 * Handle CTRL-H <Backspace> command.
3234 */
3235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (VIsual_active && VIsual_select)
3239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 v_visop(cap);
3242 }
3243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 nv_left(cap);
3245}
3246
3247/*
3248 * CTRL-L: clear screen and redraw.
3249 */
3250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003251nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003253 if (checkclearop(cap->oap))
3254 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003255
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003256#ifdef FEAT_SYN_HL
3257 // Clear all syntax states to force resyncing.
3258 syn_stack_free_all(curwin->w_s);
3259# ifdef FEAT_RELTIME
3260 {
3261 win_T *wp;
3262
3263 FOR_ALL_WINDOWS(wp)
3264 wp->w_s->b_syn_slow = FALSE;
3265 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003268 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003269#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3270# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003271 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003272# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003273 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275}
3276
3277/*
3278 * CTRL-O: In Select mode: switch to Visual mode for one command.
3279 * Otherwise: Go to older pcmark.
3280 */
3281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003282nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (VIsual_active && VIsual_select)
3285 {
3286 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003287 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003289 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 cap->count1 = -cap->count1;
3294 nv_pcmark(cap);
3295 }
3296}
3297
3298/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003299 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3300 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
3302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003303nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 if (!checkclearopq(cap->oap))
3306 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3307 GETF_SETMARK|GETF_ALT, FALSE);
3308}
3309
3310/*
3311 * "Z" commands.
3312 */
3313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003316 if (checkclearopq(cap->oap))
3317 return;
3318
3319 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003321 // "ZZ": equivalent to ":x".
3322 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003326 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 break;
3328
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003329 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
3334 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3335 */
3336 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003337do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 oparg_T oa;
3340 cmdarg_T ca;
3341
3342 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003343 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 ca.oap = &oa;
3345 ca.cmdchar = c1;
3346 ca.nchar = c2;
3347 nv_ident(&ca);
3348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003351 * 'K' normal-mode command. Get the command to lookup the keyword under the
3352 * cursor.
3353 */
3354 static int
3355nv_K_getcmd(
3356 cmdarg_T *cap,
3357 char_u *kp,
3358 int kp_help,
3359 int kp_ex,
3360 char_u **ptr_arg,
3361 int n,
3362 char_u *buf,
3363 unsigned buflen)
3364{
3365 char_u *ptr = *ptr_arg;
3366 int isman;
3367 int isman_s;
3368
3369 if (kp_help)
3370 {
3371 // in the help buffer
3372 STRCPY(buf, "he! ");
3373 return n;
3374 }
3375
3376 if (kp_ex)
3377 {
3378 // 'keywordprog' is an ex command
3379 if (cap->count0 != 0)
3380 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3381 else
3382 STRCPY(buf, kp);
3383 STRCAT(buf, " ");
3384 return n;
3385 }
3386
3387 // An external command will probably use an argument starting
3388 // with "-" as an option. To avoid trouble we skip the "-".
3389 while (*ptr == '-' && n > 0)
3390 {
3391 ++ptr;
3392 --n;
3393 }
3394 if (n == 0)
3395 {
3396 // found dashes only
3397 emsg(_(e_no_identifier_under_cursor));
3398 vim_free(buf);
3399 *ptr_arg = ptr;
3400 return 0;
3401 }
3402
3403 // When a count is given, turn it into a range. Is this
3404 // really what we want?
3405 isman = (STRCMP(kp, "man") == 0);
3406 isman_s = (STRCMP(kp, "man -s") == 0);
3407 if (cap->count0 != 0 && !(isman || isman_s))
3408 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3409
3410 STRCAT(buf, "! ");
3411 if (cap->count0 == 0 && isman_s)
3412 STRCAT(buf, "man");
3413 else
3414 STRCAT(buf, kp);
3415 STRCAT(buf, " ");
3416 if (cap->count0 != 0 && (isman || isman_s))
3417 {
3418 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3419 STRCAT(buf, " ");
3420 }
3421
3422 *ptr_arg = ptr;
3423 return n;
3424}
3425
3426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * Handle the commands that use the word under the cursor.
3428 * [g] CTRL-] :ta to current identifier
3429 * [g] 'K' run program for current identifier
3430 * [g] '*' / to current identifier or string
3431 * [g] '#' ? to current identifier or string
3432 * g ']' :tselect for current identifier
3433 */
3434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003435nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 char_u *ptr = NULL;
3438 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003439 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003440 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 char_u *kp; // value of 'keywordprg'
3443 int kp_help; // 'keywordprg' is ":he"
3444 int kp_ex; // 'keywordprg' starts with ":"
3445 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003448 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003451 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 cmdchar = cap->nchar;
3454 g_cmd = TRUE;
3455 }
3456 else
3457 {
3458 cmdchar = cap->cmdchar;
3459 g_cmd = FALSE;
3460 }
3461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 cmdchar = '#';
3464
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003465 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3469 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (checkclearopq(cap->oap))
3471 return;
3472 }
3473
3474 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3475 (cmdchar == '*' || cmdchar == '#')
3476 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3477 {
3478 clearop(cap->oap);
3479 return;
3480 }
3481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // Allocate buffer to put the command in. Inserting backslashes can
3483 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3484 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3486 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3487 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003488 if (kp_help && *skipwhite(ptr) == NUL)
3489 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003490 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003491 return;
3492 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003493 kp_ex = (*kp == ':');
3494 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3495 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (buf == NULL)
3497 return;
3498 buf[0] = NUL;
3499
3500 switch (cmdchar)
3501 {
3502 case '*':
3503 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003504 // Put cursor at start of word, makes search skip the word
3505 // under the cursor.
3506 // Call setpcmark() first, so "*``" puts the cursor back where
3507 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 setpcmark();
3509 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3510
3511 if (!g_cmd && vim_iswordp(ptr))
3512 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003513 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
3515
3516 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003517 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3518 if (n == 0)
3519 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 break;
3521
3522 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003523 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#ifdef FEAT_CSCOPE
3525 if (p_cst)
3526 STRCPY(buf, "cstag ");
3527 else
3528#endif
3529 STRCPY(buf, "ts ");
3530 break;
3531
3532 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003533 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (curbuf->b_help)
3535 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003537 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 if (g_cmd)
3539 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003540 else if (cap->count0 == 0)
3541 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003542 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003543 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003547 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003548 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003550 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003551 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003552 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003553 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003554 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003556 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003557 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003560 vim_free(buf);
3561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003563 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003564 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003565 {
3566 vim_free(buf);
3567 vim_free(p);
3568 return;
3569 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003570 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 STRCAT(buf, p);
3572 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003574 else
3575 {
3576 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003577 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003579 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003580 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003581 {
3582 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003584 aux_ptr = (char_u *)"";
3585 else
3586 aux_ptr = (char_u *)"\\|\"\n[";
3587 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003588 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 aux_ptr = (char_u *)"\\|\"\n*?[";
3590
3591 p = buf + STRLEN(buf);
3592 while (n-- > 0)
3593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003595 if (vim_strchr(aux_ptr, *ptr) != NULL)
3596 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // When current byte is a part of multibyte character, copy all
3598 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003599 if (has_mbyte)
3600 {
3601 int i;
3602 int len = (*mb_ptr2len)(ptr) - 1;
3603
3604 for (i = 0; i < len && n >= 1; ++i, --n)
3605 *p++ = *ptr++;
3606 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 *p++ = *ptr++;
3608 }
3609 *p = NUL;
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003612 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (cmdchar == '*' || cmdchar == '#')
3614 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003615 if (!g_cmd && (has_mbyte
3616 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3617 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003619
3620 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003621 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003623
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003624 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003627 {
3628 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003630 g_tag_at_cursor = FALSE;
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 vim_free(buf);
3634}
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636/*
3637 * Get visually selected text, within one line only.
3638 * Returns FAIL if more than one line selected.
3639 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003641get_visual_text(
3642 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 char_u **pp, // return: start of selected text
3644 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 if (VIsual_mode != 'V')
3647 unadjust_for_sel();
3648 if (VIsual.lnum != curwin->w_cursor.lnum)
3649 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003650 if (cap != NULL)
3651 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return FAIL;
3653 }
3654 if (VIsual_mode == 'V')
3655 {
3656 *pp = ml_get_curline();
3657 *lenp = (int)STRLEN(*pp);
3658 }
3659 else
3660 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003661 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 *pp = ml_get_pos(&curwin->w_cursor);
3664 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3665 }
3666 else
3667 {
3668 *pp = ml_get_pos(&VIsual);
3669 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3670 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003671 if (**pp == NUL)
3672 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003673 if (*lenp > 0)
3674 {
3675 if (has_mbyte)
3676 // Correct the length to include all bytes of the last
3677 // character.
3678 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3679 else if ((*pp)[*lenp - 1] == NUL)
3680 // Do not include a trailing NUL.
3681 *lenp -= 1;
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 reset_VIsual_and_resel();
3685 return OK;
3686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
3688/*
3689 * CTRL-T: backwards in tag stack
3690 */
3691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003692nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 if (!checkclearopq(cap->oap))
3695 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3696}
3697
3698/*
3699 * Handle scrolling command 'H', 'L' and 'M'.
3700 */
3701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003702nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 int used = 0;
3705 long n;
3706#ifdef FEAT_FOLDING
3707 linenr_T lnum;
3708#endif
3709 int half;
3710
3711 cap->oap->motion_type = MLINE;
3712 setpcmark();
3713
3714 if (cap->cmdchar == 'L')
3715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 curwin->w_cursor.lnum = curwin->w_botline - 1;
3718 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3719 curwin->w_cursor.lnum = 1;
3720 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003721 {
3722#ifdef FEAT_FOLDING
3723 if (hasAnyFolding(curwin))
3724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003725 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003726 for (n = cap->count1 - 1; n > 0
3727 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3728 {
3729 (void)hasFolding(curwin->w_cursor.lnum,
3730 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003731 if (curwin->w_cursor.lnum > curwin->w_topline)
3732 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003733 }
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);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003984 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003985 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003986
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);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993
3994/*
3995 * Cursor down commands.
3996 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3997 */
3998 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003999nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 if (mod_mask & MOD_MASK_SHIFT)
4002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004 cap->arg = FORWARD;
4005 nv_page(cap);
4006 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004007#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004008 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004009 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4010 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004015 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 cmdwin_result = CAR;
4017 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004018#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004020 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4021 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4022 {
4023 invoke_prompt_callback();
4024 if (restart_edit == 0)
4025 restart_edit = 'a';
4026 }
4027 else
4028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 cap->oap->motion_type = MLINE;
4031 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4032 clearopbeep(cap->oap);
4033 else if (cap->arg)
4034 beginline(BL_WHITE | BL_FIX);
4035 }
4036 }
4037}
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039/*
4040 * Grab the file name under the cursor and edit it.
4041 */
4042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004043nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004046 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004048 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004050
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004051#ifdef FEAT_PROP_POPUP
4052 if (ERROR_IF_TERM_POPUP_WINDOW)
4053 return;
4054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004056 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 if (ptr != NULL)
4059 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004060 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004061 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004062 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004064 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004065 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004066 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004067 {
4068 curwin->w_cursor.lnum = lnum;
4069 check_cursor_lnum();
4070 beginline(BL_SOL | BL_FIX);
4071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 vim_free(ptr);
4073 }
4074 else
4075 clearop(cap->oap);
4076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078/*
4079 * <End> command: to end of current line or last line.
4080 */
4081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004084 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004086 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004088 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090 nv_dollar(cap);
4091}
4092
4093/*
4094 * Handle the "$" command.
4095 */
4096 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004097nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 cap->oap->motion_type = MCHAR;
4100 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // In virtual mode when off the edge of a line and an operator
4102 // is pending (whew!) keep the cursor where it is.
4103 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (!virtual_active() || gchar_cursor() != NUL
4105 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (cursor_down((long)(cap->count1 - 1),
4108 cap->oap->op_type == OP_NOP) == FAIL)
4109 clearopbeep(cap->oap);
4110#ifdef FEAT_FOLDING
4111 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4112 foldOpenCursor();
4113#endif
4114}
4115
4116/*
4117 * Implementation of '?' and '/' commands.
4118 * If cap->arg is TRUE don't set PC mark.
4119 */
4120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004124 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004128 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 cap->cmdchar = 'g';
4130 cap->nchar = '?';
4131 nv_operator(cap);
4132 return;
4133 }
4134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004135 // When using 'incsearch' the cursor may be moved to set a different search
4136 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004137 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 if (cap->searchbuf == NULL)
4140 {
4141 clearop(oap);
4142 return;
4143 }
4144
Bram Moolenaar46539112015-02-17 15:43:57 +01004145 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004146 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004147 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
LemonBoya4399382022-04-09 21:04:08 +01004150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151/*
4152 * Handle "N" and "n" commands.
4153 * cap->arg is SEARCH_REV for "N", 0 for "n".
4154 */
4155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004156nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004158 pos_T old = curwin->w_cursor;
4159 int wrapped = FALSE;
4160 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004161
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004162 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004164 // Avoid getting stuck on the current cursor position, which can
4165 // happen when an offset is given and the cursor is on the last char
4166 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004167 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004168 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 cap->count1 -= 1;
4170 }
LemonBoya4399382022-04-09 21:04:08 +01004171
4172#ifdef FEAT_SEARCH_EXTRA
4173 // Redraw the window to refresh the highlighted matches.
4174 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004175 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177}
4178
4179/*
4180 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4181 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004182 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004184 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185normal_search(
4186 cmdarg_T *cap,
4187 int dir,
4188 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004189 int opt, // extra flags for do_search()
4190 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004193 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004194#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004195 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 cap->oap->motion_type = MCHAR;
4199 cap->oap->inclusive = FALSE;
4200 cap->oap->use_reg_one = TRUE;
4201 curwin->w_set_curswant = TRUE;
4202
Bram Moolenaara80faa82020-04-12 19:37:17 +02004203 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004204 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004205 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4206 if (wrapped != NULL)
4207 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (i == 0)
4209 clearop(cap->oap);
4210 else
4211 {
4212 if (i == 2)
4213 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#ifdef FEAT_FOLDING
4216 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4217 foldOpenCursor();
4218#endif
4219 }
LemonBoya4399382022-04-09 21:04:08 +01004220#ifdef FEAT_SEARCH_EXTRA
4221 // Redraw the window to refresh the highlighted matches.
4222 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004223 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // "/$" will put the cursor after the end of the line, may need to
4227 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004229 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
4232/*
4233 * Character search commands.
4234 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4235 * ',' and FALSE for ';'.
4236 * cap->nchar is NUL for ',' and ';' (repeat the search)
4237 */
4238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004239nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
4241 int t_cmd;
4242
4243 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4244 t_cmd = TRUE;
4245 else
4246 t_cmd = FALSE;
4247
4248 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004251 clearopbeep(cap->oap);
4252 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004254
4255 curwin->w_set_curswant = TRUE;
4256 // Include a Tab for "tx" and for "dfx".
4257 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4258 && (t_cmd || cap->oap->op_type != OP_NOP))
4259 {
4260 colnr_T scol, ecol;
4261
4262 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4263 curwin->w_cursor.coladd = ecol - scol;
4264 }
4265 else
4266 curwin->w_cursor.coladd = 0;
4267 adjust_for_sel(cap);
4268#ifdef FEAT_FOLDING
4269 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4270 foldOpenCursor();
4271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272}
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)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004657 clearopbeep(cap->oap);
4658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004660
4661 // Don't leave the cursor on the NUL past end of line.
4662 adjust_cursor(cap->oap);
4663 curwin->w_cursor.coladd = 0;
4664#ifdef FEAT_FOLDING
4665 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4666 foldOpenCursor();
4667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
4670/*
4671 * "m" command: Mark a position.
4672 */
4673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004674nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004676 if (checkclearop(cap->oap))
4677 return;
4678
4679 if (setmark(cap->nchar) == FAIL)
4680 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681}
4682
4683/*
4684 * "{" and "}" commands.
4685 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4686 */
4687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 cap->oap->motion_type = MCHAR;
4691 cap->oap->inclusive = FALSE;
4692 cap->oap->use_reg_one = TRUE;
4693 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004694 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004696 clearopbeep(cap->oap);
4697 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004699
4700 curwin->w_cursor.coladd = 0;
4701#ifdef FEAT_FOLDING
4702 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4703 foldOpenCursor();
4704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705}
4706
4707/*
4708 * "u" command: Undo or make lower case.
4709 */
4710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004711nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004713 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004715 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 cap->cmdchar = 'g';
4717 cap->nchar = 'u';
4718 nv_operator(cap);
4719 }
4720 else
4721 nv_kundo(cap);
4722}
4723
4724/*
4725 * <Undo> command.
4726 */
4727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004728nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004730 if (checkclearopq(cap->oap))
4731 return;
4732
Bram Moolenaarf2732452018-06-03 14:47:35 +02004733#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004734 if (bt_prompt(curbuf))
4735 {
4736 clearopbeep(cap->oap);
4737 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004739#endif
4740 u_undo((int)cap->count1);
4741 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
4744/*
4745 * Handle the "r" command.
4746 */
4747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 char_u *ptr;
4751 int had_ctrl_v;
4752 long n;
4753
4754 if (checkclearop(cap->oap))
4755 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004756#ifdef FEAT_JOB_CHANNEL
4757 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4758 {
4759 clearopbeep(cap->oap);
4760 return;
4761 }
4762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004764 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (cap->nchar == Ctrl_V)
4766 {
4767 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004768 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (cap->nchar > DEL)
4771 had_ctrl_v = NUL;
4772 }
4773 else
4774 had_ctrl_v = NUL;
4775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004776 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004777 if (IS_SPECIAL(cap->nchar))
4778 {
4779 clearopbeep(cap->oap);
4780 return;
4781 }
4782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (VIsual_active)
4785 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004786 if (got_int)
4787 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004788 if (had_ctrl_v)
4789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004790 // Use a special (negative) number to make a difference between a
4791 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004792 if (cap->nchar == CAR)
4793 cap->nchar = REPLACE_CR_NCHAR;
4794 else if (cap->nchar == NL)
4795 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 nv_operator(cap);
4798 return;
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (virtual_active())
4803 {
4804 if (u_save_cursor() == FAIL)
4805 return;
4806 if (gchar_cursor() == NUL)
4807 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004808 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 coladvance_force((colnr_T)(getviscol() + cap->count1));
4810 curwin->w_cursor.col -= cap->count1;
4811 }
4812 else if (gchar_cursor() == TAB)
4813 coladvance_force(getviscol());
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004816 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004818 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004819 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 clearopbeep(cap->oap);
4822 return;
4823 }
4824
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004825 // Replacing with a TAB is done by edit() when it is complicated because
4826 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4827 // Other characters are done below to avoid problems with things like
4828 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4830 {
4831 stuffnumReadbuff(cap->count1);
4832 stuffcharReadbuff('R');
4833 stuffcharReadbuff('\t');
4834 stuffcharReadbuff(ESC);
4835 return;
4836 }
4837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004838 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (u_save_cursor() == FAIL)
4840 return;
4841
4842 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4843 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004844 // Replace character(s) by a single newline.
4845 // Strange vi behaviour: Only one newline is inserted.
4846 // Delete the characters here.
4847 // Insert the newline with an insert command, takes care of
4848 // autoindent. The insert command depends on being on the last
4849 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 stuffcharReadbuff('\r');
4852 stuffcharReadbuff(ESC);
4853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 invoke_edit(cap, TRUE, 'r', FALSE);
4856 }
4857 else
4858 {
4859 prep_redo(cap->oap->regname, cap->count1,
4860 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4861
4862 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (has_mbyte)
4864 {
4865 int old_State = State;
4866
4867 if (cap->ncharC1 != 0)
4868 AppendCharToRedobuff(cap->ncharC1);
4869 if (cap->ncharC2 != 0)
4870 AppendCharToRedobuff(cap->ncharC2);
4871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // This is slow, but it handles replacing a single-byte with a
4873 // multi-byte and the other way around. Also handles adding
4874 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 for (n = cap->count1; n > 0; --n)
4876 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004877 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004878 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4879 {
4880 int c = ins_copychar(curwin->w_cursor.lnum
4881 + (cap->nchar == Ctrl_Y ? -1 : 1));
4882 if (c != NUL)
4883 ins_char(c);
4884 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004886 ++curwin->w_cursor.col;
4887 }
4888 else
4889 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 State = old_State;
4891 if (cap->ncharC1 != 0)
4892 ins_char(cap->ncharC1);
4893 if (cap->ncharC2 != 0)
4894 ins_char(cap->ncharC2);
4895 }
4896 }
4897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004899 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 for (n = cap->count1; n > 0; --n)
4901 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004902 // Get ptr again, because u_save and/or showmatch() will have
4903 // released the line. This may also happen in ins_copychar().
4904 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004905 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4906 {
4907 int c = ins_copychar(curwin->w_cursor.lnum
4908 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004909
4910 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004911 if (c != NUL)
4912 ptr[curwin->w_cursor.col] = c;
4913 }
4914 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004915 {
4916 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004917 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (p_sm && msg_silent == 0)
4920 showmatch(cap->nchar);
4921 ++curwin->w_cursor.col;
4922 }
4923#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004924 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004926 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaar009b2592004-10-24 19:18:58 +00004928 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004929 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004931 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933#endif
4934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004935 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 changed_bytes(curwin->w_cursor.lnum,
4937 (colnr_T)(curwin->w_cursor.col - cap->count1));
4938 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004939 --curwin->w_cursor.col; // cursor on the last replaced char
4940 // if the character on the left of the current cursor is a multi-byte
4941 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (has_mbyte)
4943 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 curbuf->b_op_end = curwin->w_cursor;
4945 curwin->w_set_curswant = TRUE;
4946 set_last_insert(cap->nchar);
4947 }
4948}
4949
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950/*
4951 * 'o': Exchange start and end of Visual area.
4952 * 'O': same, but in block mode exchange left and right corners.
4953 */
4954 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004955v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 pos_T old_cursor;
4958 colnr_T left, right;
4959
4960 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4961 {
4962 old_cursor = curwin->w_cursor;
4963 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4964 curwin->w_cursor.lnum = VIsual.lnum;
4965 coladvance(left);
4966 VIsual = curwin->w_cursor;
4967
4968 curwin->w_cursor.lnum = old_cursor.lnum;
4969 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 // 'selection "exclusive" and cursor at right-bottom corner: move it
4971 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4973 ++curwin->w_curswant;
4974 coladvance(curwin->w_curswant);
4975 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004977 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 curwin->w_cursor.lnum = VIsual.lnum;
4980 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4981 ++right;
4982 coladvance(right);
4983 VIsual = curwin->w_cursor;
4984
4985 curwin->w_cursor.lnum = old_cursor.lnum;
4986 coladvance(left);
4987 curwin->w_curswant = left;
4988 }
4989 }
4990 else
4991 {
4992 old_cursor = curwin->w_cursor;
4993 curwin->w_cursor = VIsual;
4994 VIsual = old_cursor;
4995 curwin->w_set_curswant = TRUE;
4996 }
4997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999/*
5000 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5001 */
5002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005003nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005005 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 cap->cmdchar = 'c';
5008 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005009 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 VIsual_mode = 'V';
5011 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005012 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005014
5015 if (checkclearopq(cap->oap))
5016 return;
5017
5018 if (!curbuf->b_p_ma)
5019 emsg(_(e_cannot_make_changes_modifiable_is_off));
5020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005022 if (virtual_active())
5023 coladvance(getviscol());
5024 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026}
5027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028/*
5029 * "gr".
5030 */
5031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (VIsual_active)
5035 {
5036 cap->cmdchar = 'r';
5037 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005038 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005039 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005041
5042 if (checkclearopq(cap->oap))
5043 return;
5044
5045 if (!curbuf->b_p_ma)
5046 emsg(_(e_cannot_make_changes_modifiable_is_off));
5047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005049 if (cap->extra_char == Ctrl_V) // get another character
5050 cap->extra_char = get_literal(FALSE);
5051 stuffcharReadbuff(cap->extra_char);
5052 stuffcharReadbuff(ESC);
5053 if (virtual_active())
5054 coladvance(getviscol());
5055 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059/*
5060 * Swap case for "~" command, when it does not work like an operator.
5061 */
5062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005063n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 long n;
5066 pos_T startpos;
5067 int did_change = 0;
5068#ifdef FEAT_NETBEANS_INTG
5069 pos_T pos;
5070 char_u *ptr;
5071 int count;
5072#endif
5073
5074 if (checkclearopq(cap->oap))
5075 return;
5076
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005077 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
5079 clearopbeep(cap->oap);
5080 return;
5081 }
5082
5083 prep_redo_cmd(cap);
5084
5085 if (u_save_cursor() == FAIL)
5086 return;
5087
5088 startpos = curwin->w_cursor;
5089#ifdef FEAT_NETBEANS_INTG
5090 pos = startpos;
5091#endif
5092 for (n = cap->count1; n > 0; --n)
5093 {
5094 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5095 inc_cursor();
5096 if (gchar_cursor() == NUL)
5097 {
5098 if (vim_strchr(p_ww, '~') != NULL
5099 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5100 {
5101#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005102 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 if (did_change)
5105 {
5106 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005107 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005108 netbeans_removed(curbuf, pos.lnum, pos.col,
5109 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005110 // line may have been flushed, get it again
5111 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005113 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 pos.col = 0;
5116 pos.lnum++;
5117 }
5118#endif
5119 ++curwin->w_cursor.lnum;
5120 curwin->w_cursor.col = 0;
5121 if (n > 1)
5122 {
5123 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5124 break;
5125 u_clearline();
5126 }
5127 }
5128 else
5129 break;
5130 }
5131 }
5132#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005133 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 ptr = ml_get(pos.lnum);
5136 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005137 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5138 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140#endif
5141
5142
5143 check_cursor();
5144 curwin->w_set_curswant = TRUE;
5145 if (did_change)
5146 {
5147 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5148 0L);
5149 curbuf->b_op_start = startpos;
5150 curbuf->b_op_end = curwin->w_cursor;
5151 if (curbuf->b_op_end.col > 0)
5152 --curbuf->b_op_end.col;
5153 }
5154}
5155
5156/*
5157 * Move cursor to mark.
5158 */
5159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005160nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 if (check_mark(pos) == FAIL)
5163 clearop(cap->oap);
5164 else
5165 {
5166 if (cap->cmdchar == '\''
5167 || cap->cmdchar == '`'
5168 || cap->cmdchar == '['
5169 || cap->cmdchar == ']')
5170 setpcmark();
5171 curwin->w_cursor = *pos;
5172 if (flag)
5173 beginline(BL_WHITE | BL_FIX);
5174 else
5175 check_cursor();
5176 }
5177 cap->oap->motion_type = flag ? MLINE : MCHAR;
5178 if (cap->cmdchar == '`')
5179 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005180 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 curwin->w_set_curswant = TRUE;
5182}
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184/*
5185 * Handle commands that are operators in Visual mode.
5186 */
5187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005188v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005192 // Uppercase means linewise, except in block mode, then "D" deletes till
5193 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (isupper(cap->cmdchar))
5195 {
5196 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005197 {
5198 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5202 curwin->w_curswant = MAXCOL;
5203 }
5204 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5205 nv_operator(cap);
5206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208/*
5209 * "s" and "S" commands.
5210 */
5211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005214#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005215 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005216 if (term_swap_diff() == OK)
5217 return;
5218#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005219#ifdef FEAT_JOB_CHANNEL
5220 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5221 {
5222 clearopbeep(cap->oap);
5223 return;
5224 }
5225#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005226 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005229 {
5230 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 cap->cmdchar = 'c';
5234 nv_operator(cap);
5235 }
5236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 nv_optrans(cap);
5238}
5239
5240/*
5241 * Abbreviated commands.
5242 */
5243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005244nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005247 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005249 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (VIsual_active)
5251 v_visop(cap);
5252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 nv_optrans(cap);
5254}
5255
5256/*
5257 * Translate a command into another command.
5258 */
5259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5263 (char_u *)"d$", (char_u *)"c$",
5264 (char_u *)"cl", (char_u *)"cc",
5265 (char_u *)"yy", (char_u *)":s\r"};
5266 static char_u *str = (char_u *)"xXDCsSY&";
5267
5268 if (!checkclearopq(cap->oap))
5269 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005270 // In Vi "2D" doesn't delete the next line. Can't translate it
5271 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005272 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5273 {
5274 cap->oap->start = curwin->w_cursor;
5275 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005276#ifdef FEAT_EVAL
5277 set_op_var(OP_DELETE);
5278#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005279 cap->count1 = 1;
5280 nv_dollar(cap);
5281 finish_op = TRUE;
5282 ResetRedobuff();
5283 AppendCharToRedobuff('D');
5284 }
5285 else
5286 {
5287 if (cap->count0)
5288 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005289 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 cap->opcount = 0;
5293}
5294
5295/*
5296 * "'" and "`" commands. Also for "g'" and "g`".
5297 * cap->arg is TRUE for "'" and "g'".
5298 */
5299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005300nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
5302 pos_T *pos;
5303 int c;
5304#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005305 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005306 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#endif
5308
5309 if (cap->cmdchar == 'g')
5310 c = cap->extra_char;
5311 else
5312 c = cap->nchar;
5313 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005314 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
5316 if (cap->arg)
5317 {
5318 check_cursor_lnum();
5319 beginline(BL_WHITE | BL_FIX);
5320 }
5321 else
5322 check_cursor();
5323 }
5324 else
5325 nv_cursormark(cap, cap->arg, pos);
5326
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005327 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (!virtual_active())
5329 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005330 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#ifdef FEAT_FOLDING
5332 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005333 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005334 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 && (fdo_flags & FDO_MARK)
5336 && old_KeyTyped)
5337 foldOpenCursor();
5338#endif
5339}
5340
5341/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005342 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
5344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005345nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005348#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005350 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005353 if (checkclearopq(cap->oap))
5354 return;
5355
5356 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005358 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005360 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005362 if (cap->cmdchar == 'g')
5363 pos = movechangelist((int)cap->count1);
5364 else
5365 pos = movemark((int)cap->count1);
5366 if (pos == (pos_T *)-1) // jump to other file
5367 {
5368 curwin->w_set_curswant = TRUE;
5369 check_cursor();
5370 }
5371 else if (pos != NULL) // can jump
5372 nv_cursormark(cap, FALSE, pos);
5373 else if (cap->cmdchar == 'g')
5374 {
5375 if (curbuf->b_changelistlen == 0)
5376 emsg(_(e_changelist_is_empty));
5377 else if (cap->count1 < 0)
5378 emsg(_(e_at_start_of_changelist));
5379 else
5380 emsg(_(e_at_end_of_changelist));
5381 }
5382 else
5383 clearopbeep(cap->oap);
5384# ifdef FEAT_FOLDING
5385 if (cap->oap->op_type == OP_NOP
5386 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5387 && (fdo_flags & FDO_MARK)
5388 && old_KeyTyped)
5389 foldOpenCursor();
5390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391}
5392
5393/*
5394 * Handle '"' command.
5395 */
5396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005397nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 if (checkclearop(cap->oap))
5400 return;
5401#ifdef FEAT_EVAL
5402 if (cap->nchar == '=')
5403 cap->nchar = get_expr_register();
5404#endif
5405 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5406 {
5407 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005408 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#ifdef FEAT_EVAL
5410 set_reg_var(cap->oap->regname);
5411#endif
5412 }
5413 else
5414 clearopbeep(cap->oap);
5415}
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417/*
5418 * Handle "v", "V" and "CTRL-V" commands.
5419 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5420 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005421 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 */
5423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005424nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005426 if (cap->cmdchar == Ctrl_Q)
5427 cap->cmdchar = Ctrl_V;
5428
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5430 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (cap->oap->op_type != OP_NOP)
5432 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005433 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 return;
5436 }
5437
5438 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005439 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005443 else // toggle char/block mode
5444 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 VIsual_mode = cap->cmdchar;
5446 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005447 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005449 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005451 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
5453 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005454 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 VIsual = curwin->w_cursor;
5458
5459 VIsual_active = TRUE;
5460 VIsual_reselect = TRUE;
5461 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005462 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005465 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005467 // For V and ^V, we multiply the number of lines even if there
5468 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5470 {
5471 curwin->w_cursor.lnum +=
5472 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005473 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475 VIsual_mode = resel_VIsual_mode;
5476 if (VIsual_mode == 'v')
5477 {
5478 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005479 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005480 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005481 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5482 if (*p_sel != 'e')
5483 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005486 curwin->w_curswant = resel_VIsual_vcol;
5487 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005489 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 curwin->w_curswant = MAXCOL;
5492 coladvance((colnr_T)MAXCOL);
5493 }
5494 else if (VIsual_mode == Ctrl_V)
5495 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005496 // Update curswant on the original line, that is where "col" is
5497 // valid.
5498 linenr_T lnum = curwin->w_cursor.lnum;
5499 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005500 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005501 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5502 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 coladvance(curwin->w_curswant);
5504 }
5505 else
5506 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005507 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 else
5510 {
5511 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005512 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 may_start_select('c');
5514 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005515 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005516 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005517 if (cap->count0 > 0 && --cap->count1 > 0)
5518 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005519 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005520 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5521 nv_right(cap);
5522 else if (VIsual_mode == 'V')
5523 nv_down(cap);
5524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
5526 }
5527}
5528
5529/*
5530 * Start selection for Shift-movement keys.
5531 */
5532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005533start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005535 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 may_start_select('k');
5537 n_start_visual_mode('v');
5538}
5539
5540/*
5541 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005542 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 */
5544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005545may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005547 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5548 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549}
5550
5551/*
5552 * Start Visual mode "c".
5553 * Should set VIsual_select before calling this.
5554 */
5555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005556n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005558#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005559 int cursor_line_was_concealed = curwin->w_p_cole > 0
5560 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005561#endif
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 VIsual_mode = c;
5564 VIsual_active = TRUE;
5565 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005566
5567 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005568 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005569 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005570 {
5571 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 VIsual = curwin->w_cursor;
5575
5576#ifdef FEAT_FOLDING
5577 foldAdjustVisual();
5578#endif
5579
LemonBoy2bf52dd2022-04-09 18:17:34 +01005580 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005582#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005583 // Check if redraw is needed after changing the state.
5584 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005585#endif
5586
Bram Moolenaar09df3122006-01-23 22:23:09 +00005587 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005588 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005590 // Make sure the clipboard gets updated. Needed because start and
5591 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 clip_star.vmode = NUL;
5593#endif
5594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005595 // Only need to redraw this line, unless still need to redraw an old
5596 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005597 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5600 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5601 }
5602}
5603
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605/*
5606 * CTRL-W: Window commands
5607 */
5608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005609nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005611 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005614 cap->cmdchar = ':';
5615 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005616 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005617 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005618 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
5621
5622/*
5623 * CTRL-Z: Suspend
5624 */
5625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005626nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005631 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632}
5633
5634/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005635 * "gv": Reselect the previous Visual area. If Visual already active,
5636 * exchange previous and current Visual area.
5637 */
5638 static void
5639nv_gv_cmd(cmdarg_T *cap)
5640{
5641 pos_T tpos;
5642 int i;
5643
5644 if (checkclearop(cap->oap))
5645 return;
5646
5647 if (curbuf->b_visual.vi_start.lnum == 0
5648 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5649 || curbuf->b_visual.vi_end.lnum == 0)
5650 {
5651 beep_flush();
5652 return;
5653 }
5654
5655 // set w_cursor to the start of the Visual area, tpos to the end
5656 if (VIsual_active)
5657 {
5658 i = VIsual_mode;
5659 VIsual_mode = curbuf->b_visual.vi_mode;
5660 curbuf->b_visual.vi_mode = i;
5661# ifdef FEAT_EVAL
5662 curbuf->b_visual_mode_eval = i;
5663# endif
5664 i = curwin->w_curswant;
5665 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5666 curbuf->b_visual.vi_curswant = i;
5667
5668 tpos = curbuf->b_visual.vi_end;
5669 curbuf->b_visual.vi_end = curwin->w_cursor;
5670 curwin->w_cursor = curbuf->b_visual.vi_start;
5671 curbuf->b_visual.vi_start = VIsual;
5672 }
5673 else
5674 {
5675 VIsual_mode = curbuf->b_visual.vi_mode;
5676 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5677 tpos = curbuf->b_visual.vi_end;
5678 curwin->w_cursor = curbuf->b_visual.vi_start;
5679 }
5680
5681 VIsual_active = TRUE;
5682 VIsual_reselect = TRUE;
5683
5684 // Set Visual to the start and w_cursor to the end of the Visual
5685 // area. Make sure they are on an existing character.
5686 check_cursor();
5687 VIsual = curwin->w_cursor;
5688 curwin->w_cursor = tpos;
5689 check_cursor();
5690 update_topline();
5691
5692 // When called from normal "g" command: start Select mode when
5693 // 'selectmode' contains "cmd". When called for K_SELECT, always
5694 // start Select mode.
5695 if (cap->arg)
5696 {
5697 VIsual_select = TRUE;
5698 VIsual_select_reg = 0;
5699 }
5700 else
5701 may_start_select('c');
5702 setmouse();
5703#ifdef FEAT_CLIPBOARD
5704 // Make sure the clipboard gets updated. Needed because start and
5705 // end are still the same, and the selection needs to be owned
5706 clip_star.vmode = NUL;
5707#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005708 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005709 showmode();
5710}
5711
5712/*
5713 * "g0", "g^" : Like "0" and "^" but for screen lines.
5714 * "gm": middle of "g0" and "g$".
5715 */
5716 static void
5717nv_g_home_m_cmd(cmdarg_T *cap)
5718{
5719 int i;
5720 int flag = FALSE;
5721
5722 if (cap->nchar == '^')
5723 flag = TRUE;
5724
5725 cap->oap->motion_type = MCHAR;
5726 cap->oap->inclusive = FALSE;
5727 if (curwin->w_p_wrap && curwin->w_width != 0)
5728 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005729 int width1 = curwin->w_width - curwin_col_off();
5730 int width2 = width1 + curwin_col_off2();
5731 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005732
5733 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005734 virtcol = curwin->w_virtcol
5735#ifdef FEAT_PROP_POPUP
5736 - curwin->w_virtcol_first_char
5737#endif
5738 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005739 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005740 if (virtcol >= (colnr_T)width1 && width2 > 0)
5741 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005742 }
5743 else
5744 i = curwin->w_leftcol;
5745 // Go to the middle of the screen line. When 'number' or
5746 // 'relativenumber' is on and lines are wrapping the middle can be more
5747 // to the left.
5748 if (cap->nchar == 'm')
5749 i += (curwin->w_width - curwin_col_off()
5750 + ((curwin->w_p_wrap && i > 0)
5751 ? curwin_col_off2() : 0)) / 2;
5752 coladvance((colnr_T)i);
5753 if (flag)
5754 {
5755 do
5756 i = gchar_cursor();
5757 while (VIM_ISWHITE(i) && oneright() == OK);
5758 curwin->w_valid &= ~VALID_WCOL;
5759 }
5760 curwin->w_set_curswant = TRUE;
5761}
5762
5763/*
5764 * "g_": to the last non-blank character in the line or <count> lines
5765 * downward.
5766 */
5767 static void
5768nv_g_underscore_cmd(cmdarg_T *cap)
5769{
5770 char_u *ptr;
5771
5772 cap->oap->motion_type = MCHAR;
5773 cap->oap->inclusive = TRUE;
5774 curwin->w_curswant = MAXCOL;
5775 if (cursor_down((long)(cap->count1 - 1),
5776 cap->oap->op_type == OP_NOP) == FAIL)
5777 {
5778 clearopbeep(cap->oap);
5779 return;
5780 }
5781
5782 ptr = ml_get_curline();
5783
5784 // In Visual mode we may end up after the line.
5785 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5786 --curwin->w_cursor.col;
5787
5788 // Decrease the cursor column until it's on a non-blank.
5789 while (curwin->w_cursor.col > 0
5790 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5791 --curwin->w_cursor.col;
5792 curwin->w_set_curswant = TRUE;
5793 adjust_for_sel(cap);
5794}
5795
5796/*
5797 * "g$" : Like "$" but for screen lines.
5798 */
5799 static void
5800nv_g_dollar_cmd(cmdarg_T *cap)
5801{
5802 oparg_T *oap = cap->oap;
5803 int i;
5804 int col_off = curwin_col_off();
5805
5806 oap->motion_type = MCHAR;
5807 oap->inclusive = TRUE;
5808 if (curwin->w_p_wrap && curwin->w_width != 0)
5809 {
5810 curwin->w_curswant = MAXCOL; // so we stay at the end
5811 if (cap->count1 == 1)
5812 {
5813 int width1 = curwin->w_width - col_off;
5814 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005815 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005816
5817 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005818 virtcol = curwin->w_virtcol
5819#ifdef FEAT_PROP_POPUP
5820 - curwin->w_virtcol_first_char
5821#endif
5822 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005823 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005824 if (virtcol >= (colnr_T)width1)
5825 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005826 * width2;
5827 coladvance((colnr_T)i);
5828
5829 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005830 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005831 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5832 {
5833 // Check for landing on a character that got split at
5834 // the end of the line. We do not want to advance to
5835 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005836 if (curwin->w_virtcol
5837#ifdef FEAT_PROP_POPUP
5838 - curwin->w_virtcol_first_char
5839#endif
5840 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005841 --curwin->w_cursor.col;
5842 }
5843 }
5844 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5845 clearopbeep(oap);
5846 }
5847 else
5848 {
5849 if (cap->count1 > 1)
5850 // if it fails, let the cursor still move to the last char
5851 (void)cursor_down(cap->count1 - 1, FALSE);
5852
5853 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5854 coladvance((colnr_T)i);
5855
5856 // if the character doesn't fit move one back
5857 if (curwin->w_cursor.col > 0
5858 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5859 {
5860 colnr_T vcol;
5861
5862 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5863 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5864 --curwin->w_cursor.col;
5865 }
5866
5867 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005868 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005869 }
5870}
5871
5872/*
5873 * "gi": start Insert at the last position.
5874 */
5875 static void
5876nv_gi_cmd(cmdarg_T *cap)
5877{
5878 int i;
5879
5880 if (curbuf->b_last_insert.lnum != 0)
5881 {
5882 curwin->w_cursor = curbuf->b_last_insert;
5883 check_cursor_lnum();
5884 i = (int)STRLEN(ml_get_curline());
5885 if (curwin->w_cursor.col > (colnr_T)i)
5886 {
5887 if (virtual_active())
5888 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5889 curwin->w_cursor.col = i;
5890 }
5891 }
5892 cap->cmdchar = 'i';
5893 nv_edit(cap);
5894}
5895
5896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 * Commands starting with "g".
5898 */
5899 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005900nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
5905 switch (cap->nchar)
5906 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005907 case Ctrl_A:
5908 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005910 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005911 if (!VIsual_active && cap->nchar == Ctrl_A)
5912 vim_mem_profile_dump();
5913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005915 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005916 if (VIsual_active)
5917 {
5918 cap->arg = TRUE;
5919 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005920 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005921 nv_addsub(cap);
5922 }
5923 else
5924 clearopbeep(oap);
5925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005927 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 case 'R':
5929 cap->arg = TRUE;
5930 nv_Replace(cap);
5931 break;
5932
5933 case 'r':
5934 nv_vreplace(cap);
5935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936
5937 case '&':
5938 do_cmdline_cmd((char_u *)"%s//~/&");
5939 break;
5940
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005941 // "gv": Reselect the previous Visual area. If Visual already active,
5942 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005944 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005946
5947 // "gV": Don't reselect the previous Visual area after a Select mode
5948 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 case 'V':
5950 VIsual_reselect = FALSE;
5951 break;
5952
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005953 // "gh": start Select mode.
5954 // "gH": start Select line mode.
5955 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 case K_BS:
5957 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005958 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 case 'h':
5960 case 'H':
5961 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 cap->cmdchar = cap->nchar + ('v' - 'h');
5963 cap->arg = TRUE;
5964 nv_visual(cap);
5965 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005967 // "gn", "gN" visually select next/previous search match
5968 // "gn" selects next match
5969 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005970 case 'N':
5971 case 'n':
5972 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005973 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005976 // "gj" and "gk" two new funny movement keys -- up and down
5977 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 case 'j':
5979 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005980 // with 'nowrap' it works just like the normal "j" command.
5981 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 oap->motion_type = MLINE;
5984 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5985 }
5986 else
5987 i = nv_screengo(oap, FORWARD, cap->count1);
5988 if (i == FAIL)
5989 clearopbeep(oap);
5990 break;
5991
5992 case 'k':
5993 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005994 // with 'nowrap' it works just like the normal "k" command.
5995 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 {
5997 oap->motion_type = MLINE;
5998 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5999 }
6000 else
6001 i = nv_screengo(oap, BACKWARD, cap->count1);
6002 if (i == FAIL)
6003 clearopbeep(oap);
6004 break;
6005
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006006 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 case 'J':
6008 nv_join(cap);
6009 break;
6010
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006011 // "g0", "g^" : Like "0" and "^" but for screen lines.
6012 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 case '0':
6015 case 'm':
6016 case K_HOME:
6017 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006018 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 break;
6020
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006021 case 'M':
6022 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006023 oap->motion_type = MCHAR;
6024 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006025 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006026 if (cap->count0 > 0 && cap->count0 <= 100)
6027 coladvance((colnr_T)(i * cap->count0 / 100));
6028 else
6029 coladvance((colnr_T)(i / 2));
6030 curwin->w_set_curswant = TRUE;
6031 }
6032 break;
6033
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006034 // "g_": to the last non-blank character in the line or <count> lines
6035 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006037 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 break;
6039
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006040 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 case '$':
6042 case K_END:
6043 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006044 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 break;
6046
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006047 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 case '*':
6049 case '#':
6050#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006051 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006053 case Ctrl_RSB: // :tag or :tselect for current identifier
6054 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 nv_ident(cap);
6056 break;
6057
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 case 'e':
6060 case 'E':
6061 oap->motion_type = MCHAR;
6062 curwin->w_set_curswant = TRUE;
6063 oap->inclusive = TRUE;
6064 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6065 clearopbeep(oap);
6066 break;
6067
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006068 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006070 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 break;
6072
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006073 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 break;
6077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 case 'I':
6080 beginline(0);
6081 if (!checkclearopq(oap))
6082 invoke_edit(cap, FALSE, 'g', FALSE);
6083 break;
6084
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006085 // "gf": goto file, edit file under cursor
6086 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006088 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 nv_gotofile(cap);
6090 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006092 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 case '\'':
6094 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006095 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 case '`':
6097 nv_gomark(cap);
6098 break;
6099
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006100 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006102 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 break;
6104
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006105 // "ga": Display the ascii value of the character under the
6106 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 case 'a':
6108 do_ascii(NULL);
6109 break;
6110
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006111 // "g8": Display the bytes used for the UTF-8 character under the
6112 // cursor. It is displayed in hex.
6113 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006115 if (cap->count0 == 8)
6116 utf_find_illegal();
6117 else
6118 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006121 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006122 case '<':
6123 show_sb_text();
6124 break;
6125
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006126 // "gg": Goto the first line in file. With a count it goes to
6127 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 case 'g':
6129 cap->arg = FALSE;
6130 nv_goto(cap);
6131 break;
6132
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006133 // Two-character operators:
6134 // "gq" Format text
6135 // "gw" Format text and keep cursor position
6136 // "g~" Toggle the case of the text.
6137 // "gu" Change text to lower case.
6138 // "gU" Change text to upper case.
6139 // "g?" rot13 encoding
6140 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 case 'q':
6142 case 'w':
6143 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006144 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 case '~':
6146 case 'u':
6147 case 'U':
6148 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006149 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 nv_operator(cap);
6151 break;
6152
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006153 // "gd": Find first occurrence of pattern under the cursor in the
6154 // current function
6155 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 case 'd':
6157 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006158 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 break;
6160
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006161 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 case K_MIDDLEMOUSE:
6163 case K_MIDDLEDRAG:
6164 case K_MIDDLERELEASE:
6165 case K_LEFTMOUSE:
6166 case K_LEFTDRAG:
6167 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006168 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 case K_RIGHTMOUSE:
6170 case K_RIGHTDRAG:
6171 case K_RIGHTRELEASE:
6172 case K_X1MOUSE:
6173 case K_X1DRAG:
6174 case K_X1RELEASE:
6175 case K_X2MOUSE:
6176 case K_X2DRAG:
6177 case K_X2RELEASE:
6178 mod_mask = MOD_MASK_CTRL;
6179 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182 case K_IGNORE:
6183 break;
6184
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006185 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 case 'p':
6187 case 'P':
6188 nv_put(cap);
6189 break;
6190
6191#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006192 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 case 'o':
6194 goto_byte(cap->count0);
6195 break;
6196#endif
6197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006198 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006200 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 do_exmode(TRUE);
6202 break;
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case ',':
6205 nv_pcmark(cap);
6206 break;
6207
6208 case ';':
6209 cap->count1 = -cap->count1;
6210 nv_pcmark(cap);
6211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006214 if (!checkclearop(oap))
6215 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006217 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006218 if (!checkclearop(oap))
6219 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006220 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221
Bram Moolenaar62a23252020-08-09 14:04:42 +02006222 case TAB:
6223 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6224 clearopbeep(oap);
6225 break;
6226
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006227 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006228 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006229 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006230 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006231 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006232 break;
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 default:
6235 clearopbeep(oap);
6236 break;
6237 }
6238}
6239
6240/*
6241 * Handle "o" and "O" commands.
6242 */
6243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006244n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006246#ifdef FEAT_CONCEAL
6247 linenr_T oldline = curwin->w_cursor.lnum;
6248#endif
6249
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006250 if (checkclearopq(cap->oap))
6251 return;
6252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006254 if (cap->cmdchar == 'O')
6255 // Open above the first line of a folded sequence of lines
6256 (void)hasFolding(curwin->w_cursor.lnum,
6257 &curwin->w_cursor.lnum, NULL);
6258 else
6259 // Open below the last line of a folded sequence of lines
6260 (void)hasFolding(curwin->w_cursor.lnum,
6261 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006263 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6264 (cap->cmdchar == 'O' ? 1 : 0)),
6265 (linenr_T)(curwin->w_cursor.lnum +
6266 (cap->cmdchar == 'o' ? 1 : 0))
6267 ) == OK
6268 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6269 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6270 0, NULL) == OK)
6271 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006272#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006273 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6274 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006275#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006276#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006277 if (curwin->w_p_cul)
6278 // force redraw of cursorline
6279 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006280#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006281 // When '#' is in 'cpoptions' ignore the count.
6282 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6283 cap->count1 = 1;
6284 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 }
6286}
6287
6288/*
6289 * "." command: redo last change.
6290 */
6291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006292nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006294 if (checkclearopq(cap->oap))
6295 return;
6296
6297 // If "restart_edit" is TRUE, the last but one command is repeated
6298 // instead of the last command (inserting text). This is used for
6299 // CTRL-O <.> in insert mode.
6300 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6301 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302}
6303
6304/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006305 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 */
6307 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006308nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006310 if (VIsual_select && VIsual_active)
6311 {
6312 int reg;
6313 // Get register name
6314 ++no_mapping;
6315 ++allow_keys;
6316 reg = plain_vgetc();
6317 LANGMAP_ADJUST(reg, TRUE);
6318 --no_mapping;
6319 --allow_keys;
6320
6321 if (reg == '"')
6322 // the unnamed register is 0
6323 reg = 0;
6324
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006325 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006326 return;
6327 }
6328
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006329 if (checkclearopq(cap->oap))
6330 return;
6331
6332 u_redo((int)cap->count1);
6333 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334}
6335
6336/*
6337 * Handle "U" command.
6338 */
6339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006340nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006342 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006343 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006345 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 cap->cmdchar = 'g';
6347 cap->nchar = 'U';
6348 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006349 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006351
6352 if (checkclearopq(cap->oap))
6353 return;
6354
6355 u_undoline();
6356 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
6359/*
6360 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6361 * single character.
6362 */
6363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006364nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006366 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006367 {
6368#ifdef FEAT_JOB_CHANNEL
6369 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6370 {
6371 clearopbeep(cap->oap);
6372 return;
6373 }
6374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 else
6378 nv_operator(cap);
6379}
6380
6381/*
6382 * Handle an operator command.
6383 * The actual work is done by do_pending_operator().
6384 */
6385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006386nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 int op_type;
6389
6390 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006391#ifdef FEAT_JOB_CHANNEL
6392 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6393 {
6394 clearopbeep(cap->oap);
6395 return;
6396 }
6397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006399 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 nv_lineop(cap);
6401 else if (!checkclearop(cap->oap))
6402 {
6403 cap->oap->start = curwin->w_cursor;
6404 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006405#ifdef FEAT_EVAL
6406 set_op_var(op_type);
6407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409}
6410
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006411#ifdef FEAT_EVAL
6412/*
6413 * Set v:operator to the characters for "optype".
6414 */
6415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006416set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006417{
6418 char_u opchars[3];
6419
6420 if (optype == OP_NOP)
6421 set_vim_var_string(VV_OP, NULL, 0);
6422 else
6423 {
6424 opchars[0] = get_op_char(optype);
6425 opchars[1] = get_extra_op_char(optype);
6426 opchars[2] = NUL;
6427 set_vim_var_string(VV_OP, opchars, -1);
6428 }
6429}
6430#endif
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432/*
6433 * Handle linewise operator "dd", "yy", etc.
6434 *
6435 * "_" is is a strange motion command that helps make operators more logical.
6436 * It is actually implemented, but not documented in the real Vi. This motion
6437 * command actually refers to "the current line". Commands like "dd" and "yy"
6438 * are really an alternate form of "d_" and "y_". It does accept a count, so
6439 * "d3_" works to delete 3 lines.
6440 */
6441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006442nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 cap->oap->motion_type = MLINE;
6445 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6446 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006447 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006448 && cap->oap->motion_force != 'v'
6449 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 || cap->oap->op_type == OP_LSHIFT
6451 || cap->oap->op_type == OP_RSHIFT)
6452 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006453 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 beginline(BL_WHITE | BL_FIX);
6455}
6456
6457/*
6458 * <Home> command.
6459 */
6460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006461nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006463 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006464 if (mod_mask & MOD_MASK_CTRL)
6465 nv_goto(cap);
6466 else
6467 {
6468 cap->count0 = 1;
6469 nv_pipe(cap);
6470 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006471 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6472 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474
6475/*
6476 * "|" command.
6477 */
6478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006479nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 cap->oap->motion_type = MCHAR;
6482 cap->oap->inclusive = FALSE;
6483 beginline(0);
6484 if (cap->count0 > 0)
6485 {
6486 coladvance((colnr_T)(cap->count0 - 1));
6487 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6488 }
6489 else
6490 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006491 // keep curswant at the column where we wanted to go, not where
6492 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 curwin->w_set_curswant = FALSE;
6494}
6495
6496/*
6497 * Handle back-word command "b" and "B".
6498 * cap->arg is 1 for "B"
6499 */
6500 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006501nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 cap->oap->motion_type = MCHAR;
6504 cap->oap->inclusive = FALSE;
6505 curwin->w_set_curswant = TRUE;
6506 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6507 clearopbeep(cap->oap);
6508#ifdef FEAT_FOLDING
6509 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6510 foldOpenCursor();
6511#endif
6512}
6513
6514/*
6515 * Handle word motion commands "e", "E", "w" and "W".
6516 * cap->arg is TRUE for "E" and "W".
6517 */
6518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006519nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 int n;
6522 int word_end;
6523 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006524 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006526 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6528 word_end = TRUE;
6529 else
6530 word_end = FALSE;
6531 cap->oap->inclusive = word_end;
6532
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006533 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (!word_end && cap->oap->op_type == OP_CHANGE)
6535 {
6536 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006537 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006539 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006541 // Reproduce a funny Vi behaviour: "cw" on a blank only
6542 // changes one character, not all blanks until the start of
6543 // the next word. Only do this when the 'w' flag is included
6544 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6546 {
6547 cap->oap->inclusive = TRUE;
6548 cap->oap->motion_type = MCHAR;
6549 return;
6550 }
6551 }
6552 else
6553 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006554 // This is a little strange. To match what the real Vi does,
6555 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6556 // that we are not on a space or a TAB. This seems impolite
6557 // at first, but it's really more what we mean when we say
6558 // 'cw'.
6559 // Another strangeness: When standing on the end of a word
6560 // "ce" will change until the end of the next word, but "cw"
6561 // will change only one character! This is done by setting
6562 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 cap->oap->inclusive = TRUE;
6564 word_end = TRUE;
6565 flag = TRUE;
6566 }
6567 }
6568 }
6569
6570 cap->oap->motion_type = MCHAR;
6571 curwin->w_set_curswant = TRUE;
6572 if (word_end)
6573 n = end_word(cap->count1, cap->arg, flag, FALSE);
6574 else
6575 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006577 // Don't leave the cursor on the NUL past the end of line. Unless we
6578 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006579 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006580 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 if (n == FAIL && cap->oap->op_type == OP_NOP)
6583 clearopbeep(cap->oap);
6584 else
6585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587#ifdef FEAT_FOLDING
6588 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6589 foldOpenCursor();
6590#endif
6591 }
6592}
6593
6594/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006595 * Used after a movement command: If the cursor ends up on the NUL after the
6596 * end of the line, may move it back to the last character and make the motion
6597 * inclusive.
6598 */
6599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006600adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006601{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 // The cursor cannot remain on the NUL when:
6603 // - the column is > 0
6604 // - not in Visual mode or 'selection' is "o"
6605 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006606 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006607 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006608 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006609 {
6610 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006611 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006612 if (has_mbyte)
6613 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006614 oap->inclusive = TRUE;
6615 }
6616}
6617
6618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 * "0" and "^" commands.
6620 * cap->arg is the argument for beginline().
6621 */
6622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006623nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 cap->oap->motion_type = MCHAR;
6626 cap->oap->inclusive = FALSE;
6627 beginline(cap->arg);
6628#ifdef FEAT_FOLDING
6629 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6630 foldOpenCursor();
6631#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006632 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6633 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634}
6635
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636/*
6637 * In exclusive Visual mode, may include the last character.
6638 */
6639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006640adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006643 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (has_mbyte)
6646 inc_cursor();
6647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 ++curwin->w_cursor.col;
6649 cap->oap->inclusive = FALSE;
6650 }
6651}
6652
6653/*
6654 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6655 * Should check VIsual_mode before calling this.
6656 * Returns TRUE when backed up to the previous line.
6657 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006658 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006659unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 pos_T *pp;
6662
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006663 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006665 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 pp = &curwin->w_cursor;
6667 else
6668 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (pp->coladd > 0)
6670 --pp->coladd;
6671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (pp->col > 0)
6673 {
6674 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006675 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
6677 else if (pp->lnum > 1)
6678 {
6679 --pp->lnum;
6680 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6681 return TRUE;
6682 }
6683 }
6684 return FALSE;
6685}
6686
6687/*
6688 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6689 */
6690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006696 VIsual_select_reg = 0;
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else if (VIsual_reselect)
6699 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006700 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 cap->arg = TRUE;
6702 nv_g_cmd(cap);
6703 }
6704}
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
6707/*
6708 * "G", "gg", CTRL-END, CTRL-HOME.
6709 * cap->arg is TRUE for "G".
6710 */
6711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006712nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 linenr_T lnum;
6715
6716 if (cap->arg)
6717 lnum = curbuf->b_ml.ml_line_count;
6718 else
6719 lnum = 1L;
6720 cap->oap->motion_type = MLINE;
6721 setpcmark();
6722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006723 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (cap->count0 != 0)
6725 lnum = cap->count0;
6726 if (lnum < 1L)
6727 lnum = 1L;
6728 else if (lnum > curbuf->b_ml.ml_line_count)
6729 lnum = curbuf->b_ml.ml_line_count;
6730 curwin->w_cursor.lnum = lnum;
6731 beginline(BL_SOL | BL_FIX);
6732#ifdef FEAT_FOLDING
6733 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6734 foldOpenCursor();
6735#endif
6736}
6737
6738/*
6739 * CTRL-\ in Normal mode.
6740 */
6741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
6744 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6745 {
6746 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006747 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006748 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 if (cmdwin_type != 0)
6751 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (VIsual_active)
6753 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006755 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006757 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (cap->nchar == Ctrl_G && p_im)
6759 restart_edit = 'a';
6760 }
6761 else
6762 clearopbeep(cap->oap);
6763}
6764
6765/*
6766 * ESC in Normal mode: beep, but don't flush buffers.
6767 * Don't even beep if we are canceling a command.
6768 */
6769 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006770nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
6772 int no_reason;
6773
6774 no_reason = (cap->oap->op_type == OP_NOP
6775 && cap->opcount == 0
6776 && cap->count0 == 0
6777 && cap->oap->regname == 0
6778 && !p_im);
6779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006780 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006782 if (restart_edit == 0 && cmdwin_type == 0
6783 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006784 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006785 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6786
Bram Moolenaar5939c352022-09-24 12:50:45 +01006787 // The user may accidentally do "vim file | grep word" and then
6788 // CTRL-C doesn't show anything. With a changed buffer give the
6789 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006790 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006791 {
6792 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6793
6794 if (out_redir)
6795 mch_errmsg(ms);
6796 else
6797 msg(ms);
6798 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006799 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006800 {
6801 if (out_redir)
6802 {
6803 got_int = FALSE;
6804 do_cmdline_cmd((char_u *)"qa");
6805 }
6806 else
6807 msg(_("Type :qa and press <Enter> to exit Vim"));
6808 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006811 if (restart_edit != 0)
6812 redraw_mode = TRUE; // remove "-- (insert) --"
6813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006814 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6815 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (!p_im)
6817 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (cmdwin_type != 0)
6819 {
6820 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 return;
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006825 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006826 {
6827 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006828 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6829 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006830 cmdwin_result = K_IGNORE;
6831 return;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (VIsual_active)
6835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006836 end_visual_mode(); // stop Visual
6837 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006839 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006841 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006842 {
6843#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006844 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006845 popup_hide_message_win();
6846 else
6847#endif
6848 vim_beep(BO_ESC);
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 clearop(cap->oap);
6851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006852 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6853 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006854 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 restart_edit = 'a';
6856}
6857
6858/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006859 * Move the cursor for the "A" command.
6860 */
6861 void
6862set_cursor_for_append_to_line(void)
6863{
6864 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006865 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006866 {
6867 int save_State = State;
6868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006869 // Pretend Insert mode here to allow the cursor on the
6870 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006871 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006872 coladvance((colnr_T)MAXCOL);
6873 State = save_State;
6874 }
6875 else
6876 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6877}
6878
6879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006881 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 */
6883 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006884nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006886 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6888 cap->cmdchar = 'i';
6889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006892 {
6893#ifdef FEAT_TERMINAL
6894 if (term_in_normal_mode())
6895 {
6896 end_visual_mode();
6897 clearop(cap->oap);
6898 term_enter_job_mode();
6899 return;
6900 }
6901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006906 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6907 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006911#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006912 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006913 {
6914 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006915 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006916 return;
6917 }
6918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 else if (!curbuf->b_p_ma && !p_im)
6920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006922 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006924 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006926 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006928 else if (cap->cmdchar == K_PS && VIsual_active)
6929 {
6930 pos_T old_pos = curwin->w_cursor;
6931 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006932 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006934 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006935 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6936 {
6937 shift_delete_registers();
6938 cap->oap->regname = '1';
6939 }
6940 else
6941 cap->oap->regname = '-';
6942 cap->cmdchar = 'd';
6943 cap->nchar = NUL;
6944 nv_operator(cap);
6945 do_pending_operator(cap, 0, FALSE);
6946 cap->cmdchar = K_PS;
6947
zeertzjq7a732522022-03-14 20:46:41 +00006948 if (*ml_get_cursor() != NUL)
6949 {
6950 if (old_visual_mode == 'V')
6951 {
6952 // In linewise Visual mode insert before the beginning of the
6953 // next line.
6954 // When the last line in the buffer was deleted then create a
6955 // new line, otherwise there is not need to move cursor.
6956 // Detect this by checking if cursor moved above Visual area.
6957 if (curwin->w_cursor.lnum < old_pos.lnum
6958 && curwin->w_cursor.lnum < old_visual.lnum)
6959 {
6960 if (u_save_cursor() == OK)
6961 {
6962 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6963 FALSE);
6964 appended_lines(curwin->w_cursor.lnum++, 1L);
6965 }
6966 }
6967 }
6968 // When the last char in the line was deleted then append.
6969 // Detect this by checking if cursor moved before Visual area.
6970 else if (curwin->w_cursor.col < old_pos.col
6971 && curwin->w_cursor.col < old_visual.col)
6972 inc_cursor();
6973 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006976 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 else if (!checkclearopq(cap->oap))
6979 {
6980 switch (cap->cmdchar)
6981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006982 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006983 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 break;
6985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006986 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006987 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6988 beginline(BL_WHITE);
6989 else
6990 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 break;
6992
Bram Moolenaara1891842017-02-04 21:34:31 +01006993 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // Bracketed paste works like "a"ppend, unless the cursor is in
6995 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006996 if (curwin->w_cursor.col == 0)
6997 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006998 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 case 'a': // "a"ppend is like "i"nsert on the next character.
7001 // increment coladd when in virtual space, increment the
7002 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (virtual_active()
7004 && (curwin->w_cursor.coladd > 0
7005 || *ml_get_cursor() == NUL
7006 || *ml_get_cursor() == TAB))
7007 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007008 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 inc_cursor();
7010 break;
7011 }
7012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7014 {
7015 int save_State = State;
7016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 // Pretend Insert mode here to allow the cursor on the
7018 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007019 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 coladvance(getviscol());
7021 State = save_State;
7022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7025 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007026 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007027 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007028 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
7030
7031/*
7032 * Invoke edit() and take care of "restart_edit" and the return value.
7033 */
7034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007035invoke_edit(
7036 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007037 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007038 int cmd,
7039 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 int restart_edit_save = 0;
7042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007043 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7044 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7045 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (repl || !stuff_empty())
7047 restart_edit_save = restart_edit;
7048 else
7049 restart_edit_save = 0;
7050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 restart_edit = 0;
7053
7054 if (edit(cmd, startln, cap->count1))
7055 cap->retval |= CA_COMMAND_BUSY;
7056
7057 if (restart_edit == 0)
7058 restart_edit = restart_edit_save;
7059}
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061/*
7062 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7063 */
7064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007065nv_object(
7066 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068 int flag;
7069 int include;
7070 char_u *mps_save;
7071
7072 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 mps_save = curbuf->b_p_mps;
7079 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7080
7081 switch (cap->nchar)
7082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 flag = current_word(cap->oap, cap->count1, include, FALSE);
7085 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 flag = current_word(cap->oap, cap->count1, include, TRUE);
7088 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 case '(':
7091 case ')':
7092 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7093 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 case '{':
7096 case '}':
7097 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7098 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 case ']':
7101 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7102 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 case '>':
7105 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7106 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007107#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 case 't': // "at" = a tag block (xml and html)
7109 // Do not adjust oap->end in do_pending_operator()
7110 // otherwise there are different results for 'dit'
7111 // (note leading whitespace in last line):
7112 // 1) <b> 2) <b>
7113 // foobar foobar
7114 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007115 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007116 flag = current_tagblock(cap->oap, cap->count1, include);
7117 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007118#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 flag = current_par(cap->oap, cap->count1, include, 'p');
7121 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007122 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 flag = current_sent(cap->oap, cap->count1, include);
7124 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 case '"': // "a"" = a double quoted string
7126 case '\'': // "a'" = a single quoted string
7127 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007128 flag = current_quote(cap->oap, cap->count1, include,
7129 cap->nchar);
7130 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131#if 0 // TODO
7132 case 'S': // "aS" = a section
7133 case 'f': // "af" = a filename
7134 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135#endif
7136 default:
7137 flag = FAIL;
7138 break;
7139 }
7140
7141 curbuf->b_p_mps = mps_save;
7142 if (flag == FAIL)
7143 clearopbeep(cap->oap);
7144 adjust_cursor_col();
7145 curwin->w_set_curswant = TRUE;
7146}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148/*
7149 * "q" command: Start/stop recording.
7150 * "q:", "q/", "q?": edit command-line in command-line window.
7151 */
7152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007153nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154{
7155 if (cap->oap->op_type == OP_FORMAT)
7156 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007157 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 cap->cmdchar = 'g';
7159 cap->nchar = 'q';
7160 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007161 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007163
7164 if (checkclearop(cap->oap))
7165 return;
7166
7167 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007169 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007171 emsg(_(e_cmdline_window_already_open));
7172 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007174 stuffcharReadbuff(cap->nchar);
7175 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007177 else
7178 // (stop) recording into a named register, unless executing a
7179 // register
7180 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7181 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182}
7183
7184/*
7185 * Handle the "@r" command.
7186 */
7187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007188nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 if (checkclearop(cap->oap))
7191 return;
7192#ifdef FEAT_EVAL
7193 if (cap->nchar == '=')
7194 {
7195 if (get_expr_register() == NUL)
7196 return;
7197 }
7198#endif
7199 while (cap->count1-- && !got_int)
7200 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007201 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 clearopbeep(cap->oap);
7204 break;
7205 }
7206 line_breakcheck();
7207 }
7208}
7209
7210/*
7211 * Handle the CTRL-U and CTRL-D commands.
7212 */
7213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007214nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7217 || (cap->cmdchar == Ctrl_D
7218 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7219 clearopbeep(cap->oap);
7220 else if (!checkclearop(cap->oap))
7221 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7222}
7223
7224/*
7225 * Handle "J" or "gJ" command.
7226 */
7227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007228nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007230 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007232 nv_operator(cap);
7233 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007235
7236 if (checkclearop(cap->oap))
7237 return;
7238
7239 if (cap->count0 <= 1)
7240 cap->count0 = 2; // default for join is two lines!
7241 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7242 curbuf->b_ml.ml_line_count)
7243 {
7244 // can't join when on the last line
7245 if (cap->count0 <= 2)
7246 {
7247 clearopbeep(cap->oap);
7248 return;
7249 }
7250 cap->count0 = curbuf->b_ml.ml_line_count
7251 - curwin->w_cursor.lnum + 1;
7252 }
7253
7254 prep_redo(cap->oap->regname, cap->count0,
7255 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7256 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257}
7258
7259/*
7260 * "P", "gP", "p" and "gp" commands.
7261 */
7262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007263nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007265 nv_put_opt(cap, FALSE);
7266}
7267
7268/*
7269 * "P", "gP", "p" and "gp" commands.
7270 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7271 */
7272 static void
7273nv_put_opt(cmdarg_T *cap, int fix_indent)
7274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int regname = 0;
7276 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007277 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007278 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 int dir;
7280 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007281 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 if (cap->oap->op_type != OP_NOP)
7284 {
7285#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007286 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7288 {
7289 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007290 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292 else
7293#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007294 clearopbeep(cap->oap);
7295 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007297
Bram Moolenaarf2732452018-06-03 14:47:35 +02007298#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007299 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007300 {
7301 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007302 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007303 }
7304#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007305
7306 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007308 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7309 ? FORWARD : BACKWARD;
7310 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007312 else
7313 dir = (cap->cmdchar == 'P'
7314 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7315 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7316 prep_redo_cmd(cap);
7317 if (cap->cmdchar == 'g')
7318 flags |= PUT_CURSEND;
7319 else if (cap->cmdchar == 'z')
7320 flags |= PUT_BLOCK_INNER;
7321
7322 if (VIsual_active)
7323 {
7324 // Putting in Visual mode: The put text replaces the selected
7325 // text. First delete the selected text, then put the new text.
7326 // Need to save and restore the registers that the delete
7327 // overwrites if the old contents is being put.
7328 was_visual = TRUE;
7329 regname = cap->oap->regname;
7330 keep_registers = cap->cmdchar == 'P';
7331#ifdef FEAT_CLIPBOARD
7332 adjust_clip_reg(&regname);
7333#endif
7334 if (regname == 0 || regname == '"'
7335 || VIM_ISDIGIT(regname) || regname == '-'
7336#ifdef FEAT_CLIPBOARD
7337 || (clip_unnamed && (regname == '*' || regname == '+'))
7338#endif
7339
7340 )
7341 {
7342 // The delete is going to overwrite the register we want to
7343 // put, save it first.
7344 reg1 = get_register(regname, TRUE);
7345 }
7346
7347 // Now delete the selected text. Avoid messages here.
7348 cap->cmdchar = 'd';
7349 cap->nchar = NUL;
7350 cap->oap->regname = keep_registers ? '_' : NUL;
7351 ++msg_silent;
7352 nv_operator(cap);
7353 do_pending_operator(cap, 0, FALSE);
7354 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7355 --msg_silent;
7356
7357 // delete PUT_LINE_BACKWARD;
7358 cap->oap->regname = regname;
7359
7360 if (reg1 != NULL)
7361 {
7362 // Delete probably changed the register we want to put, save
7363 // it first. Then put back what was there before the delete.
7364 reg2 = get_register(regname, FALSE);
7365 put_register(regname, reg1);
7366 }
7367
7368 // When deleted a linewise Visual area, put the register as
7369 // lines to avoid it joined with the next line. When deletion was
7370 // characterwise, split a line when putting lines.
7371 if (VIsual_mode == 'V')
7372 flags |= PUT_LINE;
7373 else if (VIsual_mode == 'v')
7374 flags |= PUT_LINE_SPLIT;
7375 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7376 flags |= PUT_LINE_FORWARD;
7377 dir = BACKWARD;
7378 if ((VIsual_mode != 'V'
7379 && curwin->w_cursor.col < curbuf->b_op_start.col)
7380 || (VIsual_mode == 'V'
7381 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7382 // cursor is at the end of the line or end of file, put
7383 // forward.
7384 dir = FORWARD;
7385 // May have been reset in do_put().
7386 VIsual_active = TRUE;
7387 }
7388 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7389
7390 // If a register was saved, put it back now.
7391 if (reg2 != NULL)
7392 put_register(regname, reg2);
7393
7394 // What to reselect with "gv"? Selecting the just put text seems to
7395 // be the most useful, since the original text was removed.
7396 if (was_visual)
7397 {
7398 curbuf->b_visual.vi_start = curbuf->b_op_start;
7399 curbuf->b_visual.vi_end = curbuf->b_op_end;
7400 // need to adjust cursor position
7401 if (*p_sel == 'e')
7402 inc(&curbuf->b_visual.vi_end);
7403 }
7404
7405 // When all lines were selected and deleted do_put() leaves an empty
7406 // line that needs to be deleted now.
7407 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7408 {
7409 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7410 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7411
7412 // If the cursor was in that line, move it to the end of the last
7413 // line.
7414 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7415 {
7416 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7417 coladvance((colnr_T)MAXCOL);
7418 }
7419 }
7420 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
7423/*
7424 * "o" and "O" commands.
7425 */
7426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007427nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007430 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7432 {
7433 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007434 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436 else
7437#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007438 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007440#ifdef FEAT_JOB_CHANNEL
7441 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007442 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 n_opencmd(cap);
7446}
7447
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448#ifdef FEAT_NETBEANS_INTG
7449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007450nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451{
7452 netbeans_keycommand(cap->nchar);
7453}
7454#endif
7455
7456#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007458nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007460 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461}
7462#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007463
Bram Moolenaar3918c952005-03-15 22:34:55 +00007464/*
7465 * Trigger CursorHold event.
7466 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7467 * input buffer. "did_cursorhold" is set to avoid retriggering.
7468 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007470nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007471{
7472 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7473 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007474 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007475}