blob: 1ab159922cb73ec8978c593efca1a71316e56621 [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
Luuk van Baal441a7a92023-02-18 20:15:44 +00002347 if (!cursor_up_inner(curwin, 1))
Bram Moolenaare71996b2021-01-21 17:03:07 +01002348 {
2349 retval = FAIL;
2350 break;
2351 }
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002352 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (linelen > width1)
2354 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2355 + 1) * width2;
2356 }
2357 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002358 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 if (linelen > width1)
2361 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2362 else
2363 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002364 if (curwin->w_curswant + width2 < (colnr_T)n
2365#ifdef FEAT_FOLDING
2366 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2367#endif
2368 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002369 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 curwin->w_curswant += width2;
2371 else
2372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002373 // to next line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002374 if (!cursor_down_inner(curwin, 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 retval = FAIL;
2377 break;
2378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002380 // Check if the cursor has moved below the number display
2381 // when width1 < width2 (with cpoptions+=n). Subtract width2
2382 // to get a negative value for w_curswant, which will get
2383 // clipped to column 0.
2384 if (curwin->w_curswant >= width1)
2385 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002386 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388 }
2389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002392 if (virtual_active() && atend)
2393 coladvance(MAXCOL);
2394 else
2395 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2398 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002399 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002400 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002401
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002402 // Check for landing on a character that got split at the end of the
2403 // last line. We want to advance a screenline, not end up in the same
2404 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002406 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002407#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002408 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2409 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002410#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002411
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002412 c = (*mb_ptr2char)(ml_get_cursor());
2413 if (dir == FORWARD && virtcol < curwin->w_curswant
2414 && (curwin->w_curswant <= (colnr_T)width1)
2415 && !vim_isprintc(c) && c > 255)
2416 oneright();
2417
Bram Moolenaar773b1582014-08-29 14:20:51 +02002418 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 && (curwin->w_curswant < (colnr_T)width1
2420 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2421 : ((curwin->w_curswant - width1) % width2
2422 > (colnr_T)width2 / 2)))
2423 --curwin->w_cursor.col;
2424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002428 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 return retval;
2431}
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2435 * cap->arg must be TRUE for CTRL-E.
2436 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 if (!checkclearop(cap->oap))
2441 scroll_redraw(cap->arg, cap->count1);
2442}
2443
2444/*
2445 * Scroll "count" lines up or down, and redraw.
2446 */
2447 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002448scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
2450 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002451 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452#ifdef FEAT_DIFF
2453 int prev_topfill = curwin->w_topfill;
2454#endif
2455 linenr_T prev_lnum = curwin->w_cursor.lnum;
2456
2457 if (up)
2458 scrollup(count, TRUE);
2459 else
2460 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002461 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002463 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2464 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 cursor_correct();
2466 check_cursor_moved(curwin);
2467 curwin->w_valid |= VALID_TOPLINE;
2468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002469 // If moved back to where we were, at least move the cursor, otherwise
2470 // we get stuck at one position. Don't move the cursor up if the
2471 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002473 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#ifdef FEAT_DIFF
2475 && curwin->w_topfill == prev_topfill
2476#endif
2477 )
2478 {
2479 if (up)
2480 {
2481 if (curwin->w_cursor.lnum > prev_lnum
2482 || cursor_down(1L, FALSE) == FAIL)
2483 break;
2484 }
2485 else
2486 {
2487 if (curwin->w_cursor.lnum < prev_lnum
2488 || prev_topline == 1L
2489 || cursor_up(1L, FALSE) == FAIL)
2490 break;
2491 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002492 // Mark w_topline as valid, otherwise the screen jumps back at the
2493 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 check_cursor_moved(curwin);
2495 curwin->w_valid |= VALID_TOPLINE;
2496 }
2497 }
2498 if (curwin->w_cursor.lnum != prev_lnum)
2499 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002500 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501}
2502
2503/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002504 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2505 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2506 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002507 */
2508 static int
2509nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2510{
2511 int nchar = *nchar_arg;
2512 long n;
2513
2514 // "z123{nchar}": edit the count before obtaining {nchar}
2515 if (checkclearop(cap->oap))
2516 return FALSE;
2517 n = nchar - '0';
2518
2519 for (;;)
2520 {
2521#ifdef USE_ON_FLY_SCROLL
2522 dont_scroll = TRUE; // disallow scrolling here
2523#endif
2524 ++no_mapping;
2525 ++allow_keys; // no mapping for nchar, but allow key codes
2526 nchar = plain_vgetc();
2527 LANGMAP_ADJUST(nchar, TRUE);
2528 --no_mapping;
2529 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002530 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002531
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002532 if (nchar == K_DEL || nchar == K_KDEL)
2533 n /= 10;
2534 else if (VIM_ISDIGIT(nchar))
2535 n = n * 10 + (nchar - '0');
2536 else if (nchar == CAR)
2537 {
2538#ifdef FEAT_GUI
2539 need_mouse_correct = TRUE;
2540#endif
2541 win_setheight((int)n);
2542 break;
2543 }
2544 else if (nchar == 'l'
2545 || nchar == 'h'
2546 || nchar == K_LEFT
2547 || nchar == K_RIGHT)
2548 {
2549 cap->count1 = n ? n * cap->count1 : cap->count1;
2550 *nchar_arg = nchar;
2551 return TRUE;
2552 }
2553 else
2554 {
2555 clearopbeep(cap->oap);
2556 break;
2557 }
2558 }
2559 cap->oap->op_type = OP_NOP;
2560 return FALSE;
2561}
2562
2563#ifdef FEAT_SPELL
2564/*
2565 * "zug" and "zuw": undo "zg" and "zw"
2566 * "zg": add good word to word list
2567 * "zw": add wrong word to word list
2568 * "zG": add good word to temp word list
2569 * "zW": add wrong word to temp word list
2570 */
2571 static int
2572nv_zg_zw(cmdarg_T *cap, int nchar)
2573{
2574 char_u *ptr = NULL;
2575 int len;
2576 int undo = FALSE;
2577
2578 if (nchar == 'u')
2579 {
2580 ++no_mapping;
2581 ++allow_keys; // no mapping for nchar, but allow key codes
2582 nchar = plain_vgetc();
2583 LANGMAP_ADJUST(nchar, TRUE);
2584 --no_mapping;
2585 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002586 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002587
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002588 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2589 {
2590 clearopbeep(cap->oap);
2591 return OK;
2592 }
2593 undo = TRUE;
2594 }
2595
2596 if (checkclearop(cap->oap))
2597 return OK;
2598 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2599 return FAIL;
2600 if (ptr == NULL)
2601 {
2602 pos_T pos = curwin->w_cursor;
2603
2604 // Find bad word under the cursor. When 'spell' is
2605 // off this fails and find_ident_under_cursor() is
2606 // used below.
2607 emsg_off++;
2608 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2609 emsg_off--;
2610 if (len != 0 && curwin->w_cursor.col <= pos.col)
2611 ptr = ml_get_pos(&curwin->w_cursor);
2612 curwin->w_cursor = pos;
2613 }
2614
2615 if (ptr == NULL
2616 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2617 return FAIL;
2618 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2619 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2620 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2621
2622 return OK;
2623}
2624#endif
2625
2626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * Commands that start with "z".
2628 */
2629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002630nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 long n;
2633 colnr_T col;
2634 int nchar = cap->nchar;
2635#ifdef FEAT_FOLDING
2636 long old_fdl = curwin->w_p_fdl;
2637 int old_fen = curwin->w_p_fen;
2638#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002639 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002641 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (
2645#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002646 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2647 // and "zC" only in Visual mode. "zj" and "zk" are motion
2648 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 cap->nchar != 'f' && cap->nchar != 'F'
2650 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2651 && cap->nchar != 'j' && cap->nchar != 'k'
2652 &&
2653#endif
2654 checkclearop(cap->oap))
2655 return;
2656
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002657 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2658 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2660 && cap->count0
2661 && cap->count0 != curwin->w_cursor.lnum)
2662 {
2663 setpcmark();
2664 if (cap->count0 > curbuf->b_ml.ml_line_count)
2665 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2666 else
2667 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002668 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670
2671 switch (nchar)
2672 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 case '+':
2675 if (cap->count0 == 0)
2676 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 // No count given: put cursor at the line below screen
2678 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2680 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2681 else
2682 curwin->w_cursor.lnum = curwin->w_botline;
2683 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002684 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 case NL:
2686 case CAR:
2687 case K_KENTER:
2688 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002692 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002693 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 break;
2695
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002700 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002701 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002702 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 break;
2704
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // "z^", "z-" and "zb": put cursor at bottom of screen
2706 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2707 // when <count> is at bottom of window, and puts that one at
2708 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (cap->count0 != 0)
2710 {
2711 scroll_cursor_bot(0, TRUE);
2712 curwin->w_cursor.lnum = curwin->w_topline;
2713 }
2714 else if (curwin->w_topline == 1)
2715 curwin->w_cursor.lnum = 1;
2716 else
2717 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 case '-':
2720 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002724 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002725 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 break;
2727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002730 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 case 'h':
2735 case K_LEFT:
2736 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002737 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2738 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 break;
2740
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002741 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002742 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002745 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 case 'l':
2747 case K_RIGHT:
2748 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002749 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 break;
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 case 's': if (!curwin->w_p_wrap)
2754 {
2755#ifdef FEAT_FOLDING
2756 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002757 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
2759#endif
2760 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002761 if ((long)col > siso)
2762 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
2764 col = 0;
2765 if (curwin->w_leftcol != col)
2766 {
2767 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002768 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 }
2771 break;
2772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 case 'e': if (!curwin->w_p_wrap)
2775 {
2776#ifdef FEAT_FOLDING
2777 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
2780#endif
2781 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002782 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002783 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 col = 0;
2785 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002786 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (curwin->w_leftcol != col)
2788 {
2789 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002790 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 }
2793 break;
2794
Christian Brabandt2fa93842021-05-30 22:17:25 +02002795 // "zp", "zP" in block mode put without addind trailing spaces
2796 case 'P':
2797 case 'p': nv_put(cap);
2798 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002799 // "zy" Yank without trailing spaces
2800 case 'y': nv_operator(cap);
2801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // "zF": create fold command
2804 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 case 'F':
2806 case 'f': if (foldManualAllowed(TRUE))
2807 {
2808 cap->nchar = 'f';
2809 nv_operator(cap);
2810 curwin->w_p_fen = TRUE;
2811
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002812 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2814 {
2815 nv_operator(cap);
2816 finish_op = TRUE;
2817 }
2818 }
2819 else
2820 clearopbeep(cap->oap);
2821 break;
2822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // "zd": delete fold at cursor
2824 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 case 'd':
2826 case 'D': if (foldManualAllowed(FALSE))
2827 {
2828 if (VIsual_active)
2829 nv_operator(cap);
2830 else
2831 deleteFold(curwin->w_cursor.lnum,
2832 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2833 }
2834 break;
2835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002836 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 case 'E': if (foldmethodIsManual(curwin))
2838 {
2839 clearFolding(curwin);
2840 changed_window_setting();
2841 }
2842 else if (foldmethodIsMarker(curwin))
2843 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2844 TRUE, FALSE);
2845 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002846 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 break;
2848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 case 'n': curwin->w_p_fen = FALSE;
2851 break;
2852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case 'N': curwin->w_p_fen = TRUE;
2855 break;
2856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2859 break;
2860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2863 openFold(curwin->w_cursor.lnum, cap->count1);
2864 else
2865 {
2866 closeFold(curwin->w_cursor.lnum, cap->count1);
2867 curwin->w_p_fen = TRUE;
2868 }
2869 break;
2870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2873 openFoldRecurse(curwin->w_cursor.lnum);
2874 else
2875 {
2876 closeFoldRecurse(curwin->w_cursor.lnum);
2877 curwin->w_p_fen = TRUE;
2878 }
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'o': if (VIsual_active)
2883 nv_operator(cap);
2884 else
2885 openFold(curwin->w_cursor.lnum, cap->count1);
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'O': if (VIsual_active)
2890 nv_operator(cap);
2891 else
2892 openFoldRecurse(curwin->w_cursor.lnum);
2893 break;
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 case 'c': if (VIsual_active)
2897 nv_operator(cap);
2898 else
2899 closeFold(curwin->w_cursor.lnum, cap->count1);
2900 curwin->w_p_fen = TRUE;
2901 break;
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 case 'C': if (VIsual_active)
2905 nv_operator(cap);
2906 else
2907 closeFoldRecurse(curwin->w_cursor.lnum);
2908 curwin->w_p_fen = TRUE;
2909 break;
2910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 case 'v': foldOpenCursor();
2913 break;
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 curwin->w_foldinvalid = TRUE; // recompute folds
2918 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 foldOpenCursor();
2920 break;
2921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 curwin->w_foldinvalid = TRUE; // recompute folds
2925 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 break;
2927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002930 {
2931 curwin->w_p_fdl -= cap->count1;
2932 if (curwin->w_p_fdl < 0)
2933 curwin->w_p_fdl = 0;
2934 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 curwin->w_p_fen = TRUE;
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 curwin->w_p_fen = TRUE;
2943 break;
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002946 case 'r': curwin->w_p_fdl += cap->count1;
2947 {
2948 int d = getDeepestNesting();
2949
2950 if (curwin->w_p_fdl >= d)
2951 curwin->w_p_fdl = d;
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 break;
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 break;
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 case 'j': // "zj" move to next fold downwards
2961 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2963 cap->count1) == FAIL)
2964 clearopbeep(cap->oap);
2965 break;
2966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002969#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 case 'g': // "zg": add good word to word list
2972 case 'w': // "zw": add wrong word to word list
2973 case 'G': // "zG": add good word to temp word list
2974 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002975 if (nv_zg_zw(cap, nchar) == FAIL)
2976 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002977 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002980 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002981 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002982 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002983#endif
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 default: clearopbeep(cap->oap);
2986 }
2987
2988#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (old_fen != curwin->w_p_fen)
2991 {
2992# ifdef FEAT_DIFF
2993 win_T *wp;
2994
2995 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 FOR_ALL_WINDOWS(wp)
2999 {
3000 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3001 {
3002 wp->w_p_fen = curwin->w_p_fen;
3003 changed_window_setting_win(wp);
3004 }
3005 }
3006 }
3007# endif
3008 changed_window_setting();
3009 }
3010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003011 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (old_fdl != curwin->w_p_fdl)
3013 newFoldLevel();
3014#endif
3015}
3016
3017#ifdef FEAT_GUI
3018/*
3019 * Vertical scrollbar movement.
3020 */
3021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003022nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 if (cap->oap->op_type != OP_NOP)
3025 clearopbeep(cap->oap);
3026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 gui_do_scroll();
3029}
3030
3031/*
3032 * Horizontal scrollbar movement.
3033 */
3034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003035nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 if (cap->oap->op_type != OP_NOP)
3038 clearopbeep(cap->oap);
3039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003041 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043#endif
3044
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003045#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003046/*
3047 * Click in GUI tab.
3048 */
3049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +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 jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003056 goto_tabpage(current_tab);
3057}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003058
3059/*
3060 * Selected item in tab line menu.
3061 */
3062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003063nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003064{
3065 if (cap->oap->op_type != OP_NOP)
3066 clearopbeep(cap->oap);
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003069 handle_tabmenu();
3070}
3071
3072/*
3073 * Handle selecting an item of the GUI tab line menu.
3074 * Used in Normal and Insert mode.
3075 */
3076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003077handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003078{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003079 switch (current_tabmenu)
3080 {
3081 case TABLINE_MENU_CLOSE:
3082 if (current_tab == 0)
3083 do_cmdline_cmd((char_u *)"tabclose");
3084 else
3085 {
3086 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3087 current_tab);
3088 do_cmdline_cmd(IObuff);
3089 }
3090 break;
3091
3092 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003093 if (current_tab == 0)
3094 do_cmdline_cmd((char_u *)"$tabnew");
3095 else
3096 {
3097 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3098 current_tab - 1);
3099 do_cmdline_cmd(IObuff);
3100 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003101 break;
3102
3103 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003104 if (current_tab == 0)
3105 do_cmdline_cmd((char_u *)"browse $tabnew");
3106 else
3107 {
3108 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3109 current_tab - 1);
3110 do_cmdline_cmd(IObuff);
3111 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003112 break;
3113 }
3114}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003115#endif
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117/*
3118 * "Q" command.
3119 */
3120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003121nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003123 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003125 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003126 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 do_exmode(FALSE);
3128}
3129
3130/*
3131 * Handle a ":" command.
3132 */
3133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003134nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003136 int old_p_im;
3137 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003138 int is_cmdkey = cap->cmdchar == K_COMMAND
3139 || cap->cmdchar == K_SCRIPT_COMMAND;
3140 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaar957cf672020-11-12 14:21:06 +01003142 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003144 nv_operator(cap);
3145 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003147
3148 if (cap->oap->op_type != OP_NOP)
3149 {
3150 // Using ":" as a movement is characterwise exclusive.
3151 cap->oap->motion_type = MCHAR;
3152 cap->oap->inclusive = FALSE;
3153 }
3154 else if (cap->count0 && !is_cmdkey)
3155 {
3156 // translate "count:" into ":.,.+(count - 1)"
3157 stuffcharReadbuff('.');
3158 if (cap->count0 > 1)
3159 {
3160 stuffReadbuff((char_u *)",.+");
3161 stuffnumReadbuff((long)cap->count0 - 1L);
3162 }
3163 }
3164
3165 // When typing, don't type below an old message
3166 if (KeyTyped)
3167 compute_cmdrow();
3168
3169 old_p_im = p_im;
3170
3171 // get a command line and execute it
3172 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3173 if (is_cmdkey)
3174 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3175 else
3176 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3177
3178 // If 'insertmode' changed, enter or exit Insert mode
3179 if (p_im != old_p_im)
3180 {
3181 if (p_im)
3182 restart_edit = 'i';
3183 else
3184 restart_edit = 0;
3185 }
3186
3187 if (cmd_result == FAIL)
3188 // The Ex command failed, do not execute the operator.
3189 clearop(cap->oap);
3190 else if (cap->oap->op_type != OP_NOP
3191 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3192 || cap->oap->start.col >
3193 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3194 || did_emsg
3195 ))
3196 // The start of the operator has become invalid by the Ex command.
3197 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198}
3199
3200/*
3201 * Handle CTRL-G command.
3202 */
3203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003204nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003206 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003209 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 showmode();
3211 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003212 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 fileinfo((int)cap->count0, FALSE, TRUE);
3215}
3216
3217/*
3218 * Handle CTRL-H <Backspace> command.
3219 */
3220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003221nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (VIsual_active && VIsual_select)
3224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 v_visop(cap);
3227 }
3228 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 nv_left(cap);
3230}
3231
3232/*
3233 * CTRL-L: clear screen and redraw.
3234 */
3235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003238 if (checkclearop(cap->oap))
3239 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003240
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003241#ifdef FEAT_SYN_HL
3242 // Clear all syntax states to force resyncing.
3243 syn_stack_free_all(curwin->w_s);
3244# ifdef FEAT_RELTIME
3245 {
3246 win_T *wp;
3247
3248 FOR_ALL_WINDOWS(wp)
3249 wp->w_s->b_syn_slow = FALSE;
3250 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003253 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003254#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3255# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003256 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003257# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003258 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261
3262/*
3263 * CTRL-O: In Select mode: switch to Visual mode for one command.
3264 * Otherwise: Go to older pcmark.
3265 */
3266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003267nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (VIsual_active && VIsual_select)
3270 {
3271 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003272 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
3278 cap->count1 = -cap->count1;
3279 nv_pcmark(cap);
3280 }
3281}
3282
3283/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003284 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3285 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 */
3287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003288nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 if (!checkclearopq(cap->oap))
3291 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3292 GETF_SETMARK|GETF_ALT, FALSE);
3293}
3294
3295/*
3296 * "Z" commands.
3297 */
3298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003301 if (checkclearopq(cap->oap))
3302 return;
3303
3304 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003306 // "ZZ": equivalent to ":x".
3307 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 break;
3309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003310 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003311 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 break;
3313
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003314 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316}
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318/*
3319 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3320 */
3321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003322do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 oparg_T oa;
3325 cmdarg_T ca;
3326
3327 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003328 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 ca.oap = &oa;
3330 ca.cmdchar = c1;
3331 ca.nchar = c2;
3332 nv_ident(&ca);
3333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003336 * 'K' normal-mode command. Get the command to lookup the keyword under the
3337 * cursor.
3338 */
3339 static int
3340nv_K_getcmd(
3341 cmdarg_T *cap,
3342 char_u *kp,
3343 int kp_help,
3344 int kp_ex,
3345 char_u **ptr_arg,
3346 int n,
3347 char_u *buf,
3348 unsigned buflen)
3349{
3350 char_u *ptr = *ptr_arg;
3351 int isman;
3352 int isman_s;
3353
3354 if (kp_help)
3355 {
3356 // in the help buffer
3357 STRCPY(buf, "he! ");
3358 return n;
3359 }
3360
3361 if (kp_ex)
3362 {
3363 // 'keywordprog' is an ex command
3364 if (cap->count0 != 0)
3365 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3366 else
3367 STRCPY(buf, kp);
3368 STRCAT(buf, " ");
3369 return n;
3370 }
3371
3372 // An external command will probably use an argument starting
3373 // with "-" as an option. To avoid trouble we skip the "-".
3374 while (*ptr == '-' && n > 0)
3375 {
3376 ++ptr;
3377 --n;
3378 }
3379 if (n == 0)
3380 {
3381 // found dashes only
3382 emsg(_(e_no_identifier_under_cursor));
3383 vim_free(buf);
3384 *ptr_arg = ptr;
3385 return 0;
3386 }
3387
3388 // When a count is given, turn it into a range. Is this
3389 // really what we want?
3390 isman = (STRCMP(kp, "man") == 0);
3391 isman_s = (STRCMP(kp, "man -s") == 0);
3392 if (cap->count0 != 0 && !(isman || isman_s))
3393 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3394
3395 STRCAT(buf, "! ");
3396 if (cap->count0 == 0 && isman_s)
3397 STRCAT(buf, "man");
3398 else
3399 STRCAT(buf, kp);
3400 STRCAT(buf, " ");
3401 if (cap->count0 != 0 && (isman || isman_s))
3402 {
3403 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3404 STRCAT(buf, " ");
3405 }
3406
3407 *ptr_arg = ptr;
3408 return n;
3409}
3410
3411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * Handle the commands that use the word under the cursor.
3413 * [g] CTRL-] :ta to current identifier
3414 * [g] 'K' run program for current identifier
3415 * [g] '*' / to current identifier or string
3416 * [g] '#' ? to current identifier or string
3417 * g ']' :tselect for current identifier
3418 */
3419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003420nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 char_u *ptr = NULL;
3423 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003424 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003425 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 char_u *kp; // value of 'keywordprg'
3428 int kp_help; // 'keywordprg' is ":he"
3429 int kp_ex; // 'keywordprg' starts with ":"
3430 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003432 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003433 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003436 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 cmdchar = cap->nchar;
3439 g_cmd = TRUE;
3440 }
3441 else
3442 {
3443 cmdchar = cap->cmdchar;
3444 g_cmd = FALSE;
3445 }
3446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 cmdchar = '#';
3449
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003450 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3454 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (checkclearopq(cap->oap))
3456 return;
3457 }
3458
3459 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3460 (cmdchar == '*' || cmdchar == '#')
3461 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3462 {
3463 clearop(cap->oap);
3464 return;
3465 }
3466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003467 // Allocate buffer to put the command in. Inserting backslashes can
3468 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3469 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3471 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3472 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003473 if (kp_help && *skipwhite(ptr) == NUL)
3474 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003475 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003476 return;
3477 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003478 kp_ex = (*kp == ':');
3479 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3480 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (buf == NULL)
3482 return;
3483 buf[0] = NUL;
3484
3485 switch (cmdchar)
3486 {
3487 case '*':
3488 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003489 // Put cursor at start of word, makes search skip the word
3490 // under the cursor.
3491 // Call setpcmark() first, so "*``" puts the cursor back where
3492 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 setpcmark();
3494 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3495
3496 if (!g_cmd && vim_iswordp(ptr))
3497 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003498 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
3501 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003502 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3503 if (n == 0)
3504 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
3506
3507 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003508 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#ifdef FEAT_CSCOPE
3510 if (p_cst)
3511 STRCPY(buf, "cstag ");
3512 else
3513#endif
3514 STRCPY(buf, "ts ");
3515 break;
3516
3517 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003518 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (curbuf->b_help)
3520 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003522 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003523 if (g_cmd)
3524 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003525 else if (cap->count0 == 0)
3526 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003527 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003528 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003532 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003533 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003535 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003536 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003537 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003538 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003539 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003540 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003541 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003542 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003543 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003545 vim_free(buf);
3546 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003548 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003549 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003550 {
3551 vim_free(buf);
3552 vim_free(p);
3553 return;
3554 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003555 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003556 STRCAT(buf, p);
3557 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003559 else
3560 {
3561 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003562 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003563 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003564 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003565 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003566 {
3567 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003569 aux_ptr = (char_u *)"";
3570 else
3571 aux_ptr = (char_u *)"\\|\"\n[";
3572 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003573 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003574 aux_ptr = (char_u *)"\\|\"\n*?[";
3575
3576 p = buf + STRLEN(buf);
3577 while (n-- > 0)
3578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003579 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003580 if (vim_strchr(aux_ptr, *ptr) != NULL)
3581 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003582 // When current byte is a part of multibyte character, copy all
3583 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003584 if (has_mbyte)
3585 {
3586 int i;
3587 int len = (*mb_ptr2len)(ptr) - 1;
3588
3589 for (i = 0; i < len && n >= 1; ++i, --n)
3590 *p++ = *ptr++;
3591 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003592 *p++ = *ptr++;
3593 }
3594 *p = NUL;
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003597 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (cmdchar == '*' || cmdchar == '#')
3599 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003600 if (!g_cmd && (has_mbyte
3601 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3602 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003604
3605 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003606 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003608
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003609 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003612 {
3613 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003615 g_tag_at_cursor = FALSE;
3616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 vim_free(buf);
3619}
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621/*
3622 * Get visually selected text, within one line only.
3623 * Returns FAIL if more than one line selected.
3624 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003625 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003626get_visual_text(
3627 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003628 char_u **pp, // return: start of selected text
3629 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 if (VIsual_mode != 'V')
3632 unadjust_for_sel();
3633 if (VIsual.lnum != curwin->w_cursor.lnum)
3634 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003635 if (cap != NULL)
3636 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 return FAIL;
3638 }
3639 if (VIsual_mode == 'V')
3640 {
3641 *pp = ml_get_curline();
3642 *lenp = (int)STRLEN(*pp);
3643 }
3644 else
3645 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003646 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 *pp = ml_get_pos(&curwin->w_cursor);
3649 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3650 }
3651 else
3652 {
3653 *pp = ml_get_pos(&VIsual);
3654 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3655 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003656 if (**pp == NUL)
3657 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003658 if (*lenp > 0)
3659 {
3660 if (has_mbyte)
3661 // Correct the length to include all bytes of the last
3662 // character.
3663 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3664 else if ((*pp)[*lenp - 1] == NUL)
3665 // Do not include a trailing NUL.
3666 *lenp -= 1;
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669 reset_VIsual_and_resel();
3670 return OK;
3671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673/*
3674 * CTRL-T: backwards in tag stack
3675 */
3676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003677nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 if (!checkclearopq(cap->oap))
3680 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3681}
3682
3683/*
3684 * Handle scrolling command 'H', 'L' and 'M'.
3685 */
3686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003687nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 int used = 0;
3690 long n;
3691#ifdef FEAT_FOLDING
3692 linenr_T lnum;
3693#endif
3694 int half;
3695
3696 cap->oap->motion_type = MLINE;
3697 setpcmark();
3698
3699 if (cap->cmdchar == 'L')
3700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003701 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 curwin->w_cursor.lnum = curwin->w_botline - 1;
3703 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3704 curwin->w_cursor.lnum = 1;
3705 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003706 {
3707#ifdef FEAT_FOLDING
3708 if (hasAnyFolding(curwin))
3709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003710 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003711 for (n = cap->count1 - 1; n > 0
3712 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3713 {
3714 (void)hasFolding(curwin->w_cursor.lnum,
3715 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003716 if (curwin->w_cursor.lnum > curwin->w_topline)
3717 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003718 }
3719 }
3720 else
3721#endif
3722 curwin->w_cursor.lnum -= cap->count1 - 1;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 else
3726 {
3727 if (cap->cmdchar == 'M')
3728 {
3729#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 used -= diff_check_fill(curwin, curwin->w_topline)
3732 - curwin->w_topfill;
3733#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003734 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3736 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3737 {
3738#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003739 // Count half he number of filler lines to be "below this
3740 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3742 + n) / 2 >= half)
3743 {
3744 --n;
3745 break;
3746 }
3747#endif
3748 used += plines(curwin->w_topline + n);
3749 if (used >= half)
3750 break;
3751#ifdef FEAT_FOLDING
3752 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3753 n = lnum - curwin->w_topline;
3754#endif
3755 }
3756 if (n > 0 && used > curwin->w_height)
3757 --n;
3758 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003762#ifdef FEAT_FOLDING
3763 if (hasAnyFolding(curwin))
3764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003765 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003766 lnum = curwin->w_topline;
3767 while (n-- > 0 && lnum < curwin->w_botline - 1)
3768 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003769 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003770 ++lnum;
3771 }
3772 n = lnum - curwin->w_topline;
3773 }
3774#endif
3775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 curwin->w_cursor.lnum = curwin->w_topline + n;
3777 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3778 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3779 }
3780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003781 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003782 if (cap->oap->op_type == OP_NOP)
3783 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 beginline(BL_SOL | BL_FIX);
3785}
3786
3787/*
3788 * Cursor right commands.
3789 */
3790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003791nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
3793 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003794 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003796 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003798 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003799 if (mod_mask & MOD_MASK_CTRL)
3800 cap->arg = TRUE;
3801 nv_wordcmd(cap);
3802 return;
3803 }
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 cap->oap->motion_type = MCHAR;
3806 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003807 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003809 // In virtual edit mode, there's no such thing as "past_line", as lines
3810 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003812 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 for (n = cap->count1; n > 0; --n)
3815 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003816 if ((!past_line && oneright() == FAIL)
3817 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003818 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003820 // <Space> wraps to next line if 'whichwrap' has 's'.
3821 // 'l' wraps to next line if 'whichwrap' has 'l'.
3822 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if ( ((cap->cmdchar == ' '
3824 && vim_strchr(p_ww, 's') != NULL)
3825 || (cap->cmdchar == 'l'
3826 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003827 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 && vim_strchr(p_ww, '>') != NULL))
3829 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3830 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003831 // When deleting we also count the NL as a character.
3832 // Set cap->oap->inclusive when last char in the line is
3833 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003834 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003836 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 cap->oap->inclusive = TRUE;
3838 else
3839 {
3840 ++curwin->w_cursor.lnum;
3841 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 curwin->w_set_curswant = TRUE;
3844 cap->oap->inclusive = FALSE;
3845 }
3846 continue;
3847 }
3848 if (cap->oap->op_type == OP_NOP)
3849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003850 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (n == cap->count1)
3852 beep_flush();
3853 }
3854 else
3855 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003856 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 cap->oap->inclusive = TRUE;
3858 }
3859 break;
3860 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003861 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
3863 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (virtual_active())
3865 oneright();
3866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003869 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 ++curwin->w_cursor.col;
3872 }
3873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875#ifdef FEAT_FOLDING
3876 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3877 && cap->oap->op_type == OP_NOP)
3878 foldOpenCursor();
3879#endif
3880}
3881
3882/*
3883 * Cursor left commands.
3884 *
3885 * Returns TRUE when operator end should not be adjusted.
3886 */
3887 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003888nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 long n;
3891
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003892 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3893 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003894 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003895 if (mod_mask & MOD_MASK_CTRL)
3896 cap->arg = 1;
3897 nv_bck_word(cap);
3898 return;
3899 }
3900
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 cap->oap->motion_type = MCHAR;
3902 cap->oap->inclusive = FALSE;
3903 for (n = cap->count1; n > 0; --n)
3904 {
3905 if (oneleft() == FAIL)
3906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003907 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3908 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3909 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if ( (((cap->cmdchar == K_BS
3911 || cap->cmdchar == Ctrl_H)
3912 && vim_strchr(p_ww, 'b') != NULL)
3913 || (cap->cmdchar == 'h'
3914 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003915 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 && vim_strchr(p_ww, '<') != NULL))
3917 && curwin->w_cursor.lnum > 1)
3918 {
3919 --(curwin->w_cursor.lnum);
3920 coladvance((colnr_T)MAXCOL);
3921 curwin->w_set_curswant = TRUE;
3922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003923 // When the NL before the first char has to be deleted we
3924 // put the cursor on the NUL after the previous line.
3925 // This is a very special case, be careful!
3926 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if ( (cap->oap->op_type == OP_DELETE
3928 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003929 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003931 char_u *cp = ml_get_cursor();
3932
3933 if (*cp != NUL)
3934 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003935 if (has_mbyte)
3936 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3937 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003938 ++curwin->w_cursor.col;
3939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 cap->retval |= CA_NO_ADJ_OP_END;
3941 }
3942 continue;
3943 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003944 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3946 beep_flush();
3947 break;
3948 }
3949 }
3950#ifdef FEAT_FOLDING
3951 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3952 && cap->oap->op_type == OP_NOP)
3953 foldOpenCursor();
3954#endif
3955}
3956
3957/*
3958 * Cursor up commands.
3959 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3960 */
3961 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003962nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003964 if (mod_mask & MOD_MASK_SHIFT)
3965 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 cap->arg = BACKWARD;
3968 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003969 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003970 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003971
3972 cap->oap->motion_type = MLINE;
3973 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3974 clearopbeep(cap->oap);
3975 else if (cap->arg)
3976 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977}
3978
3979/*
3980 * Cursor down commands.
3981 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3982 */
3983 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003984nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003986 if (mod_mask & MOD_MASK_SHIFT)
3987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003988 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003989 cap->arg = FORWARD;
3990 nv_page(cap);
3991 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003992#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003993 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003994 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
3995 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003997 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004000 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 cmdwin_result = CAR;
4002 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004003#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004005 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4006 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4007 {
4008 invoke_prompt_callback();
4009 if (restart_edit == 0)
4010 restart_edit = 'a';
4011 }
4012 else
4013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
4015 cap->oap->motion_type = MLINE;
4016 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4017 clearopbeep(cap->oap);
4018 else if (cap->arg)
4019 beginline(BL_WHITE | BL_FIX);
4020 }
4021 }
4022}
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024/*
4025 * Grab the file name under the cursor and edit it.
4026 */
4027 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004028nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
4030 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004031 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004033 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004035
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004036#ifdef FEAT_PROP_POPUP
4037 if (ERROR_IF_TERM_POPUP_WINDOW)
4038 return;
4039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004041 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
4043 if (ptr != NULL)
4044 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004045 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004046 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004047 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004049 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004050 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004051 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004052 {
4053 curwin->w_cursor.lnum = lnum;
4054 check_cursor_lnum();
4055 beginline(BL_SOL | BL_FIX);
4056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 vim_free(ptr);
4058 }
4059 else
4060 clearop(cap->oap);
4061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063/*
4064 * <End> command: to end of current line or last line.
4065 */
4066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004069 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004073 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 nv_dollar(cap);
4076}
4077
4078/*
4079 * Handle the "$" command.
4080 */
4081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
4084 cap->oap->motion_type = MCHAR;
4085 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 // In virtual mode when off the edge of a line and an operator
4087 // is pending (whew!) keep the cursor where it is.
4088 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 if (!virtual_active() || gchar_cursor() != NUL
4090 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004091 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (cursor_down((long)(cap->count1 - 1),
4093 cap->oap->op_type == OP_NOP) == FAIL)
4094 clearopbeep(cap->oap);
4095#ifdef FEAT_FOLDING
4096 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4097 foldOpenCursor();
4098#endif
4099}
4100
4101/*
4102 * Implementation of '?' and '/' commands.
4103 * If cap->arg is TRUE don't set PC mark.
4104 */
4105 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004106nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107{
4108 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004109 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004113 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 cap->cmdchar = 'g';
4115 cap->nchar = '?';
4116 nv_operator(cap);
4117 return;
4118 }
4119
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004120 // When using 'incsearch' the cursor may be moved to set a different search
4121 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004122 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 if (cap->searchbuf == NULL)
4125 {
4126 clearop(oap);
4127 return;
4128 }
4129
Bram Moolenaar46539112015-02-17 15:43:57 +01004130 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004131 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004132 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133}
4134
LemonBoya4399382022-04-09 21:04:08 +01004135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136/*
4137 * Handle "N" and "n" commands.
4138 * cap->arg is SEARCH_REV for "N", 0 for "n".
4139 */
4140 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004141nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004143 pos_T old = curwin->w_cursor;
4144 int wrapped = FALSE;
4145 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004146
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004147 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 // Avoid getting stuck on the current cursor position, which can
4150 // happen when an offset is given and the cursor is on the last char
4151 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004152 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004153 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004154 cap->count1 -= 1;
4155 }
LemonBoya4399382022-04-09 21:04:08 +01004156
4157#ifdef FEAT_SEARCH_EXTRA
4158 // Redraw the window to refresh the highlighted matches.
4159 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004160 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
4164/*
4165 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4166 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004167 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004170normal_search(
4171 cmdarg_T *cap,
4172 int dir,
4173 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004174 int opt, // extra flags for do_search()
4175 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004178 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004179#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004180 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 cap->oap->motion_type = MCHAR;
4184 cap->oap->inclusive = FALSE;
4185 cap->oap->use_reg_one = TRUE;
4186 curwin->w_set_curswant = TRUE;
4187
Bram Moolenaara80faa82020-04-12 19:37:17 +02004188 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004189 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004190 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4191 if (wrapped != NULL)
4192 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (i == 0)
4194 clearop(cap->oap);
4195 else
4196 {
4197 if (i == 2)
4198 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200#ifdef FEAT_FOLDING
4201 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4202 foldOpenCursor();
4203#endif
4204 }
LemonBoya4399382022-04-09 21:04:08 +01004205#ifdef FEAT_SEARCH_EXTRA
4206 // Redraw the window to refresh the highlighted matches.
4207 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004208 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004211 // "/$" will put the cursor after the end of the line, may need to
4212 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004214 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215}
4216
4217/*
4218 * Character search commands.
4219 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4220 * ',' and FALSE for ';'.
4221 * cap->nchar is NUL for ',' and ';' (repeat the search)
4222 */
4223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004224nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int t_cmd;
4227
4228 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4229 t_cmd = TRUE;
4230 else
4231 t_cmd = FALSE;
4232
4233 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004236 clearopbeep(cap->oap);
4237 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004239
4240 curwin->w_set_curswant = TRUE;
4241 // Include a Tab for "tx" and for "dfx".
4242 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4243 && (t_cmd || cap->oap->op_type != OP_NOP))
4244 {
4245 colnr_T scol, ecol;
4246
4247 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4248 curwin->w_cursor.coladd = ecol - scol;
4249 }
4250 else
4251 curwin->w_cursor.coladd = 0;
4252 adjust_for_sel(cap);
4253#ifdef FEAT_FOLDING
4254 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4255 foldOpenCursor();
4256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257}
4258
4259/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004260 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4261 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4262 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4263 * "[m" or "]m" search for prev/next start of (Java) method.
4264 * "[M" or "]M" search for prev/next end of (Java) method.
4265 */
4266 static void
4267nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4268{
4269 pos_T new_pos = {0, 0, 0};
4270 pos_T *pos = NULL; // init for GCC
4271 pos_T prev_pos;
4272 long n;
4273 int findc;
4274 int c;
4275
4276 if (cap->nchar == '*')
4277 cap->nchar = '/';
4278 prev_pos.lnum = 0;
4279 if (cap->nchar == 'm' || cap->nchar == 'M')
4280 {
4281 if (cap->cmdchar == '[')
4282 findc = '{';
4283 else
4284 findc = '}';
4285 n = 9999;
4286 }
4287 else
4288 {
4289 findc = cap->nchar;
4290 n = cap->count1;
4291 }
4292 for ( ; n > 0; --n)
4293 {
4294 if ((pos = findmatchlimit(cap->oap, findc,
4295 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4296 {
4297 if (new_pos.lnum == 0) // nothing found
4298 {
4299 if (cap->nchar != 'm' && cap->nchar != 'M')
4300 clearopbeep(cap->oap);
4301 }
4302 else
4303 pos = &new_pos; // use last one found
4304 break;
4305 }
4306 prev_pos = new_pos;
4307 curwin->w_cursor = *pos;
4308 new_pos = *pos;
4309 }
4310 curwin->w_cursor = *old_pos;
4311
4312 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4313 // brought us to the match for "[m" and "]M" when inside a method.
4314 // Try finding the '{' or '}' we want to be at.
4315 // Also repeat for the given count.
4316 if (cap->nchar == 'm' || cap->nchar == 'M')
4317 {
4318 // norm is TRUE for "]M" and "[m"
4319 int norm = ((findc == '{') == (cap->nchar == 'm'));
4320
4321 n = cap->count1;
4322 // found a match: we were inside a method
4323 if (prev_pos.lnum != 0)
4324 {
4325 pos = &prev_pos;
4326 curwin->w_cursor = prev_pos;
4327 if (norm)
4328 --n;
4329 }
4330 else
4331 pos = NULL;
4332 while (n > 0)
4333 {
4334 for (;;)
4335 {
4336 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4337 {
4338 // if not found anything, that's an error
4339 if (pos == NULL)
4340 clearopbeep(cap->oap);
4341 n = 0;
4342 break;
4343 }
4344 c = gchar_cursor();
4345 if (c == '{' || c == '}')
4346 {
4347 // Must have found end/start of class: use it.
4348 // Or found the place to be at.
4349 if ((c == findc && norm) || (n == 1 && !norm))
4350 {
4351 new_pos = curwin->w_cursor;
4352 pos = &new_pos;
4353 n = 0;
4354 }
4355 // if no match found at all, we started outside of the
4356 // class and we're inside now. Just go on.
4357 else if (new_pos.lnum == 0)
4358 {
4359 new_pos = curwin->w_cursor;
4360 pos = &new_pos;
4361 }
4362 // found start/end of other method: go to match
4363 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004364 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4365 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004366 n = 0;
4367 else
4368 curwin->w_cursor = *pos;
4369 break;
4370 }
4371 }
4372 --n;
4373 }
4374 curwin->w_cursor = *old_pos;
4375 if (pos == NULL && new_pos.lnum != 0)
4376 clearopbeep(cap->oap);
4377 }
4378 if (pos != NULL)
4379 {
4380 setpcmark();
4381 curwin->w_cursor = *pos;
4382 curwin->w_set_curswant = TRUE;
4383#ifdef FEAT_FOLDING
4384 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4385 && cap->oap->op_type == OP_NOP)
4386 foldOpenCursor();
4387#endif
4388 }
4389}
4390
4391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 * "[" and "]" commands.
4393 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4394 */
4395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004396nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004399 pos_T *pos = NULL; // init for GCC
4400 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 int flag;
4402 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 cap->oap->motion_type = MCHAR;
4405 cap->oap->inclusive = FALSE;
4406 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004407 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004409 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (cap->nchar == 'f')
4411 nv_gotofile(cap);
4412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004415 // Find the occurrence(s) of the identifier or define under cursor
4416 // in current and included files or jump to the first occurrence.
4417 //
4418 // search list jump
4419 // fwd bwd fwd bwd fwd bwd
4420 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4421 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004422 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 char_u *ptr;
4425 int len;
4426
4427 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4428 clearop(cap->oap);
4429 else
4430 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004431 // Make a copy, if the line was changed it will be freed.
4432 ptr = vim_strnsave(ptr, len);
4433 if (ptr == NULL)
4434 return;
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 find_pattern_in_path(ptr, 0, len, TRUE,
4437 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4438 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4439 cap->count1,
4440 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4441 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4442 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4443 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004444 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 curwin->w_set_curswant = TRUE;
4446 }
4447 }
4448 else
4449#endif
4450
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004451 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4452 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4453 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4454 // "[m" or "]m" search for prev/next start of (Java) method.
4455 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if ( (cap->cmdchar == '['
4457 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4458 || (cap->cmdchar == ']'
4459 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004460 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004462 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 else if (cap->nchar == '[' || cap->nchar == ']')
4464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004465 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 flag = '{';
4467 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004468 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004471 // Imitate strange Vi behaviour: When using "]]" with an operator
4472 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004473 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 (cap->oap->op_type != OP_NOP
4475 && cap->arg == FORWARD && flag == '{')))
4476 clearopbeep(cap->oap);
4477 else
4478 {
4479 if (cap->oap->op_type == OP_NOP)
4480 beginline(BL_WHITE | BL_FIX);
4481#ifdef FEAT_FOLDING
4482 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4483 foldOpenCursor();
4484#endif
4485 }
4486 }
4487
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004488 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 else if (cap->nchar == 'p' || cap->nchar == 'P')
4490 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004491 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004494 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 else if (cap->nchar == '\'' || cap->nchar == '`')
4496 {
4497 pos = &curwin->w_cursor;
4498 for (n = cap->count1; n > 0; --n)
4499 {
4500 prev_pos = *pos;
4501 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4502 cap->nchar == '\'');
4503 if (pos == NULL)
4504 break;
4505 }
4506 if (pos == NULL)
4507 pos = &prev_pos;
4508 nv_cursormark(cap, cap->nchar == '\'', pos);
4509 }
4510
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004511 // [ or ] followed by a middle mouse click: put selected text with
4512 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004513 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
4515 (void)do_mouse(cap->oap, cap->nchar,
4516 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4517 cap->count1, PUT_FIXINDENT);
4518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
4520#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004521 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 else if (cap->nchar == 'z')
4523 {
4524 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4525 cap->count1) == FAIL)
4526 clearopbeep(cap->oap);
4527 }
4528#endif
4529
4530#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004531 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else if (cap->nchar == 'c')
4533 {
4534 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4535 cap->count1) == FAIL)
4536 clearopbeep(cap->oap);
4537 }
4538#endif
4539
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004540#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004541 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004542 else if (cap->nchar == 's' || cap->nchar == 'S')
4543 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004544 setpcmark();
4545 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004546 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4547 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004548 {
4549 clearopbeep(cap->oap);
4550 break;
4551 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004552 else
4553 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004554# ifdef FEAT_FOLDING
4555 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4556 foldOpenCursor();
4557# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004558 }
4559#endif
4560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004561 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
4563 clearopbeep(cap->oap);
4564}
4565
4566/*
4567 * Handle Normal mode "%" command.
4568 */
4569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004570nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
4572 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004573#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 linenr_T lnum = curwin->w_cursor.lnum;
4575#endif
4576
4577 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004578 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
4580 if (cap->count0 > 100)
4581 clearopbeep(cap->oap);
4582 else
4583 {
4584 cap->oap->motion_type = MLINE;
4585 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004586 // Round up, so 'normal 100%' always jumps at the line line.
4587 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4588 // overflow on 32-bits, so use a formula with less accuracy
4589 // to avoid overflows.
4590 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4592 / 100L * cap->count0;
4593 else
4594 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4595 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004596 if (curwin->w_cursor.lnum < 1)
4597 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4599 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4600 beginline(BL_SOL | BL_FIX);
4601 }
4602 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004603 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
4605 cap->oap->motion_type = MCHAR;
4606 cap->oap->use_reg_one = TRUE;
4607 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4608 clearopbeep(cap->oap);
4609 else
4610 {
4611 setpcmark();
4612 curwin->w_cursor = *pos;
4613 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617 }
4618#ifdef FEAT_FOLDING
4619 if (cap->oap->op_type == OP_NOP
4620 && lnum != curwin->w_cursor.lnum
4621 && (fdo_flags & FDO_PERCENT)
4622 && KeyTyped)
4623 foldOpenCursor();
4624#endif
4625}
4626
4627/*
4628 * Handle "(" and ")" commands.
4629 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4630 */
4631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004632nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 cap->oap->motion_type = MCHAR;
4635 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004636 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004637 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 curwin->w_set_curswant = TRUE;
4639
4640 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004642 clearopbeep(cap->oap);
4643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004645
4646 // Don't leave the cursor on the NUL past end of line.
4647 adjust_cursor(cap->oap);
4648 curwin->w_cursor.coladd = 0;
4649#ifdef FEAT_FOLDING
4650 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4651 foldOpenCursor();
4652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653}
4654
4655/*
4656 * "m" command: Mark a position.
4657 */
4658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004659nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004661 if (checkclearop(cap->oap))
4662 return;
4663
4664 if (setmark(cap->nchar) == FAIL)
4665 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666}
4667
4668/*
4669 * "{" and "}" commands.
4670 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4671 */
4672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004673nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 cap->oap->motion_type = MCHAR;
4676 cap->oap->inclusive = FALSE;
4677 cap->oap->use_reg_one = TRUE;
4678 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004679 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004681 clearopbeep(cap->oap);
4682 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004684
4685 curwin->w_cursor.coladd = 0;
4686#ifdef FEAT_FOLDING
4687 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4688 foldOpenCursor();
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691
4692/*
4693 * "u" command: Undo or make lower case.
4694 */
4695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004696nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004698 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004700 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 cap->cmdchar = 'g';
4702 cap->nchar = 'u';
4703 nv_operator(cap);
4704 }
4705 else
4706 nv_kundo(cap);
4707}
4708
4709/*
4710 * <Undo> command.
4711 */
4712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004713nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004715 if (checkclearopq(cap->oap))
4716 return;
4717
Bram Moolenaarf2732452018-06-03 14:47:35 +02004718#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004719 if (bt_prompt(curbuf))
4720 {
4721 clearopbeep(cap->oap);
4722 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004724#endif
4725 u_undo((int)cap->count1);
4726 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727}
4728
4729/*
4730 * Handle the "r" command.
4731 */
4732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004733nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 char_u *ptr;
4736 int had_ctrl_v;
4737 long n;
4738
4739 if (checkclearop(cap->oap))
4740 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004741#ifdef FEAT_JOB_CHANNEL
4742 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4743 {
4744 clearopbeep(cap->oap);
4745 return;
4746 }
4747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004749 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (cap->nchar == Ctrl_V)
4751 {
4752 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004753 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004754 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (cap->nchar > DEL)
4756 had_ctrl_v = NUL;
4757 }
4758 else
4759 had_ctrl_v = NUL;
4760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004762 if (IS_SPECIAL(cap->nchar))
4763 {
4764 clearopbeep(cap->oap);
4765 return;
4766 }
4767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004768 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (VIsual_active)
4770 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004771 if (got_int)
4772 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004773 if (had_ctrl_v)
4774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 // Use a special (negative) number to make a difference between a
4776 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004777 if (cap->nchar == CAR)
4778 cap->nchar = REPLACE_CR_NCHAR;
4779 else if (cap->nchar == NL)
4780 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 nv_operator(cap);
4783 return;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004786 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (virtual_active())
4788 {
4789 if (u_save_cursor() == FAIL)
4790 return;
4791 if (gchar_cursor() == NUL)
4792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 coladvance_force((colnr_T)(getviscol() + cap->count1));
4795 curwin->w_cursor.col -= cap->count1;
4796 }
4797 else if (gchar_cursor() == TAB)
4798 coladvance_force(getviscol());
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004803 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004804 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 clearopbeep(cap->oap);
4807 return;
4808 }
4809
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004810 // Replacing with a TAB is done by edit() when it is complicated because
4811 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4812 // Other characters are done below to avoid problems with things like
4813 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4815 {
4816 stuffnumReadbuff(cap->count1);
4817 stuffcharReadbuff('R');
4818 stuffcharReadbuff('\t');
4819 stuffcharReadbuff(ESC);
4820 return;
4821 }
4822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004823 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (u_save_cursor() == FAIL)
4825 return;
4826
4827 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4828 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004829 // Replace character(s) by a single newline.
4830 // Strange vi behaviour: Only one newline is inserted.
4831 // Delete the characters here.
4832 // Insert the newline with an insert command, takes care of
4833 // autoindent. The insert command depends on being on the last
4834 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 stuffcharReadbuff('\r');
4837 stuffcharReadbuff(ESC);
4838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004839 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 invoke_edit(cap, TRUE, 'r', FALSE);
4841 }
4842 else
4843 {
4844 prep_redo(cap->oap->regname, cap->count1,
4845 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4846
4847 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (has_mbyte)
4849 {
4850 int old_State = State;
4851
4852 if (cap->ncharC1 != 0)
4853 AppendCharToRedobuff(cap->ncharC1);
4854 if (cap->ncharC2 != 0)
4855 AppendCharToRedobuff(cap->ncharC2);
4856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004857 // This is slow, but it handles replacing a single-byte with a
4858 // multi-byte and the other way around. Also handles adding
4859 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 for (n = cap->count1; n > 0; --n)
4861 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004862 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004863 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4864 {
4865 int c = ins_copychar(curwin->w_cursor.lnum
4866 + (cap->nchar == Ctrl_Y ? -1 : 1));
4867 if (c != NUL)
4868 ins_char(c);
4869 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004871 ++curwin->w_cursor.col;
4872 }
4873 else
4874 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 State = old_State;
4876 if (cap->ncharC1 != 0)
4877 ins_char(cap->ncharC1);
4878 if (cap->ncharC2 != 0)
4879 ins_char(cap->ncharC2);
4880 }
4881 }
4882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004884 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 for (n = cap->count1; n > 0; --n)
4886 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004887 // Get ptr again, because u_save and/or showmatch() will have
4888 // released the line. This may also happen in ins_copychar().
4889 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004890 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4891 {
4892 int c = ins_copychar(curwin->w_cursor.lnum
4893 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004894
4895 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004896 if (c != NUL)
4897 ptr[curwin->w_cursor.col] = c;
4898 }
4899 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004900 {
4901 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004902 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (p_sm && msg_silent == 0)
4905 showmatch(cap->nchar);
4906 ++curwin->w_cursor.col;
4907 }
4908#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004909 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004911 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
Bram Moolenaar009b2592004-10-24 19:18:58 +00004913 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004914 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004916 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918#endif
4919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004920 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 changed_bytes(curwin->w_cursor.lnum,
4922 (colnr_T)(curwin->w_cursor.col - cap->count1));
4923 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004924 --curwin->w_cursor.col; // cursor on the last replaced char
4925 // if the character on the left of the current cursor is a multi-byte
4926 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (has_mbyte)
4928 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 curbuf->b_op_end = curwin->w_cursor;
4930 curwin->w_set_curswant = TRUE;
4931 set_last_insert(cap->nchar);
4932 }
4933}
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935/*
4936 * 'o': Exchange start and end of Visual area.
4937 * 'O': same, but in block mode exchange left and right corners.
4938 */
4939 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004940v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 pos_T old_cursor;
4943 colnr_T left, right;
4944
4945 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4946 {
4947 old_cursor = curwin->w_cursor;
4948 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4949 curwin->w_cursor.lnum = VIsual.lnum;
4950 coladvance(left);
4951 VIsual = curwin->w_cursor;
4952
4953 curwin->w_cursor.lnum = old_cursor.lnum;
4954 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004955 // 'selection "exclusive" and cursor at right-bottom corner: move it
4956 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4958 ++curwin->w_curswant;
4959 coladvance(curwin->w_curswant);
4960 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004962 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 curwin->w_cursor.lnum = VIsual.lnum;
4965 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4966 ++right;
4967 coladvance(right);
4968 VIsual = curwin->w_cursor;
4969
4970 curwin->w_cursor.lnum = old_cursor.lnum;
4971 coladvance(left);
4972 curwin->w_curswant = left;
4973 }
4974 }
4975 else
4976 {
4977 old_cursor = curwin->w_cursor;
4978 curwin->w_cursor = VIsual;
4979 VIsual = old_cursor;
4980 curwin->w_set_curswant = TRUE;
4981 }
4982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
4984/*
4985 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4986 */
4987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004988nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004990 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 cap->cmdchar = 'c';
4993 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004994 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 VIsual_mode = 'V';
4996 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004997 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004999
5000 if (checkclearopq(cap->oap))
5001 return;
5002
5003 if (!curbuf->b_p_ma)
5004 emsg(_(e_cannot_make_changes_modifiable_is_off));
5005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005007 if (virtual_active())
5008 coladvance(getviscol());
5009 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011}
5012
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013/*
5014 * "gr".
5015 */
5016 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005017nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (VIsual_active)
5020 {
5021 cap->cmdchar = 'r';
5022 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005023 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005024 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005026
5027 if (checkclearopq(cap->oap))
5028 return;
5029
5030 if (!curbuf->b_p_ma)
5031 emsg(_(e_cannot_make_changes_modifiable_is_off));
5032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005034 if (cap->extra_char == Ctrl_V) // get another character
5035 cap->extra_char = get_literal(FALSE);
5036 stuffcharReadbuff(cap->extra_char);
5037 stuffcharReadbuff(ESC);
5038 if (virtual_active())
5039 coladvance(getviscol());
5040 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044/*
5045 * Swap case for "~" command, when it does not work like an operator.
5046 */
5047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005048n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
5050 long n;
5051 pos_T startpos;
5052 int did_change = 0;
5053#ifdef FEAT_NETBEANS_INTG
5054 pos_T pos;
5055 char_u *ptr;
5056 int count;
5057#endif
5058
5059 if (checkclearopq(cap->oap))
5060 return;
5061
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005062 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 clearopbeep(cap->oap);
5065 return;
5066 }
5067
5068 prep_redo_cmd(cap);
5069
5070 if (u_save_cursor() == FAIL)
5071 return;
5072
5073 startpos = curwin->w_cursor;
5074#ifdef FEAT_NETBEANS_INTG
5075 pos = startpos;
5076#endif
5077 for (n = cap->count1; n > 0; --n)
5078 {
5079 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5080 inc_cursor();
5081 if (gchar_cursor() == NUL)
5082 {
5083 if (vim_strchr(p_ww, '~') != NULL
5084 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5085 {
5086#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005087 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 if (did_change)
5090 {
5091 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005092 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005093 netbeans_removed(curbuf, pos.lnum, pos.col,
5094 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005095 // line may have been flushed, get it again
5096 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005098 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 pos.col = 0;
5101 pos.lnum++;
5102 }
5103#endif
5104 ++curwin->w_cursor.lnum;
5105 curwin->w_cursor.col = 0;
5106 if (n > 1)
5107 {
5108 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5109 break;
5110 u_clearline();
5111 }
5112 }
5113 else
5114 break;
5115 }
5116 }
5117#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005118 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 ptr = ml_get(pos.lnum);
5121 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005122 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5123 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125#endif
5126
5127
5128 check_cursor();
5129 curwin->w_set_curswant = TRUE;
5130 if (did_change)
5131 {
5132 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5133 0L);
5134 curbuf->b_op_start = startpos;
5135 curbuf->b_op_end = curwin->w_cursor;
5136 if (curbuf->b_op_end.col > 0)
5137 --curbuf->b_op_end.col;
5138 }
5139}
5140
5141/*
5142 * Move cursor to mark.
5143 */
5144 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005145nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
5147 if (check_mark(pos) == FAIL)
5148 clearop(cap->oap);
5149 else
5150 {
5151 if (cap->cmdchar == '\''
5152 || cap->cmdchar == '`'
5153 || cap->cmdchar == '['
5154 || cap->cmdchar == ']')
5155 setpcmark();
5156 curwin->w_cursor = *pos;
5157 if (flag)
5158 beginline(BL_WHITE | BL_FIX);
5159 else
5160 check_cursor();
5161 }
5162 cap->oap->motion_type = flag ? MLINE : MCHAR;
5163 if (cap->cmdchar == '`')
5164 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005165 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 curwin->w_set_curswant = TRUE;
5167}
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169/*
5170 * Handle commands that are operators in Visual mode.
5171 */
5172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005173v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5176
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005177 // Uppercase means linewise, except in block mode, then "D" deletes till
5178 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (isupper(cap->cmdchar))
5180 {
5181 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005182 {
5183 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5187 curwin->w_curswant = MAXCOL;
5188 }
5189 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5190 nv_operator(cap);
5191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
5193/*
5194 * "s" and "S" commands.
5195 */
5196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005197nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005199#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005200 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005201 if (term_swap_diff() == OK)
5202 return;
5203#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005204#ifdef FEAT_JOB_CHANNEL
5205 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5206 {
5207 clearopbeep(cap->oap);
5208 return;
5209 }
5210#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005211 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
5213 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005214 {
5215 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 cap->cmdchar = 'c';
5219 nv_operator(cap);
5220 }
5221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 nv_optrans(cap);
5223}
5224
5225/*
5226 * Abbreviated commands.
5227 */
5228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005229nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005232 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005234 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (VIsual_active)
5236 v_visop(cap);
5237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 nv_optrans(cap);
5239}
5240
5241/*
5242 * Translate a command into another command.
5243 */
5244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005245nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246{
5247 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5248 (char_u *)"d$", (char_u *)"c$",
5249 (char_u *)"cl", (char_u *)"cc",
5250 (char_u *)"yy", (char_u *)":s\r"};
5251 static char_u *str = (char_u *)"xXDCsSY&";
5252
5253 if (!checkclearopq(cap->oap))
5254 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005255 // In Vi "2D" doesn't delete the next line. Can't translate it
5256 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005257 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5258 {
5259 cap->oap->start = curwin->w_cursor;
5260 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005261#ifdef FEAT_EVAL
5262 set_op_var(OP_DELETE);
5263#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005264 cap->count1 = 1;
5265 nv_dollar(cap);
5266 finish_op = TRUE;
5267 ResetRedobuff();
5268 AppendCharToRedobuff('D');
5269 }
5270 else
5271 {
5272 if (cap->count0)
5273 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005274 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277 cap->opcount = 0;
5278}
5279
5280/*
5281 * "'" and "`" commands. Also for "g'" and "g`".
5282 * cap->arg is TRUE for "'" and "g'".
5283 */
5284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005285nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287 pos_T *pos;
5288 int c;
5289#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005290 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005291 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
5293
5294 if (cap->cmdchar == 'g')
5295 c = cap->extra_char;
5296 else
5297 c = cap->nchar;
5298 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005299 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
5301 if (cap->arg)
5302 {
5303 check_cursor_lnum();
5304 beginline(BL_WHITE | BL_FIX);
5305 }
5306 else
5307 check_cursor();
5308 }
5309 else
5310 nv_cursormark(cap, cap->arg, pos);
5311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005312 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (!virtual_active())
5314 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005315 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#ifdef FEAT_FOLDING
5317 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005318 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005319 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 && (fdo_flags & FDO_MARK)
5321 && old_KeyTyped)
5322 foldOpenCursor();
5323#endif
5324}
5325
5326/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005327 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
5329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005330nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005333#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005335 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005338 if (checkclearopq(cap->oap))
5339 return;
5340
5341 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005343 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005345 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005347 if (cap->cmdchar == 'g')
5348 pos = movechangelist((int)cap->count1);
5349 else
5350 pos = movemark((int)cap->count1);
5351 if (pos == (pos_T *)-1) // jump to other file
5352 {
5353 curwin->w_set_curswant = TRUE;
5354 check_cursor();
5355 }
5356 else if (pos != NULL) // can jump
5357 nv_cursormark(cap, FALSE, pos);
5358 else if (cap->cmdchar == 'g')
5359 {
5360 if (curbuf->b_changelistlen == 0)
5361 emsg(_(e_changelist_is_empty));
5362 else if (cap->count1 < 0)
5363 emsg(_(e_at_start_of_changelist));
5364 else
5365 emsg(_(e_at_end_of_changelist));
5366 }
5367 else
5368 clearopbeep(cap->oap);
5369# ifdef FEAT_FOLDING
5370 if (cap->oap->op_type == OP_NOP
5371 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5372 && (fdo_flags & FDO_MARK)
5373 && old_KeyTyped)
5374 foldOpenCursor();
5375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376}
5377
5378/*
5379 * Handle '"' command.
5380 */
5381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005382nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 if (checkclearop(cap->oap))
5385 return;
5386#ifdef FEAT_EVAL
5387 if (cap->nchar == '=')
5388 cap->nchar = get_expr_register();
5389#endif
5390 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5391 {
5392 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005393 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#ifdef FEAT_EVAL
5395 set_reg_var(cap->oap->regname);
5396#endif
5397 }
5398 else
5399 clearopbeep(cap->oap);
5400}
5401
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402/*
5403 * Handle "v", "V" and "CTRL-V" commands.
5404 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5405 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005406 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 */
5408 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005409nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005411 if (cap->cmdchar == Ctrl_Q)
5412 cap->cmdchar = Ctrl_V;
5413
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005414 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5415 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 if (cap->oap->op_type != OP_NOP)
5417 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005418 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 return;
5421 }
5422
5423 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005424 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005426 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005428 else // toggle char/block mode
5429 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 VIsual_mode = cap->cmdchar;
5431 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005432 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005434 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
5438 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005439 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 VIsual = curwin->w_cursor;
5443
5444 VIsual_active = TRUE;
5445 VIsual_reselect = TRUE;
5446 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005450 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005451 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005452 // For V and ^V, we multiply the number of lines even if there
5453 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5455 {
5456 curwin->w_cursor.lnum +=
5457 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005458 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460 VIsual_mode = resel_VIsual_mode;
5461 if (VIsual_mode == 'v')
5462 {
5463 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005464 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005465 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005466 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5467 if (*p_sel != 'e')
5468 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005471 curwin->w_curswant = resel_VIsual_vcol;
5472 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005474 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 curwin->w_curswant = MAXCOL;
5477 coladvance((colnr_T)MAXCOL);
5478 }
5479 else if (VIsual_mode == Ctrl_V)
5480 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005481 // Update curswant on the original line, that is where "col" is
5482 // valid.
5483 linenr_T lnum = curwin->w_cursor.lnum;
5484 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005485 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005486 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5487 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 coladvance(curwin->w_curswant);
5489 }
5490 else
5491 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005492 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
5494 else
5495 {
5496 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 may_start_select('c');
5499 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005500 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005501 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005502 if (cap->count0 > 0 && --cap->count1 > 0)
5503 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005504 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005505 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5506 nv_right(cap);
5507 else if (VIsual_mode == 'V')
5508 nv_down(cap);
5509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 }
5512}
5513
5514/*
5515 * Start selection for Shift-movement keys.
5516 */
5517 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005518start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 may_start_select('k');
5522 n_start_visual_mode('v');
5523}
5524
5525/*
5526 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005527 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 */
5529 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005530may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005532 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5533 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534}
5535
5536/*
5537 * Start Visual mode "c".
5538 * Should set VIsual_select before calling this.
5539 */
5540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005541n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005543#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005544 int cursor_line_was_concealed = curwin->w_p_cole > 0
5545 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005546#endif
5547
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 VIsual_mode = c;
5549 VIsual_active = TRUE;
5550 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005551
5552 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005553 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005554 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005555 {
5556 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 VIsual = curwin->w_cursor;
5560
5561#ifdef FEAT_FOLDING
5562 foldAdjustVisual();
5563#endif
5564
LemonBoy2bf52dd2022-04-09 18:17:34 +01005565 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005567#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005568 // Check if redraw is needed after changing the state.
5569 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005570#endif
5571
Bram Moolenaar09df3122006-01-23 22:23:09 +00005572 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 // Make sure the clipboard gets updated. Needed because start and
5576 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 clip_star.vmode = NUL;
5578#endif
5579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005580 // Only need to redraw this line, unless still need to redraw an old
5581 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005582 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
5584 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5585 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5586 }
5587}
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590/*
5591 * CTRL-W: Window commands
5592 */
5593 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005594nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005596 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005597 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005599 cap->cmdchar = ':';
5600 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005601 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005602 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005603 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605}
5606
5607/*
5608 * CTRL-Z: Suspend
5609 */
5610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005611nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005616 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617}
5618
5619/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005620 * "gv": Reselect the previous Visual area. If Visual already active,
5621 * exchange previous and current Visual area.
5622 */
5623 static void
5624nv_gv_cmd(cmdarg_T *cap)
5625{
5626 pos_T tpos;
5627 int i;
5628
5629 if (checkclearop(cap->oap))
5630 return;
5631
5632 if (curbuf->b_visual.vi_start.lnum == 0
5633 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5634 || curbuf->b_visual.vi_end.lnum == 0)
5635 {
5636 beep_flush();
5637 return;
5638 }
5639
5640 // set w_cursor to the start of the Visual area, tpos to the end
5641 if (VIsual_active)
5642 {
5643 i = VIsual_mode;
5644 VIsual_mode = curbuf->b_visual.vi_mode;
5645 curbuf->b_visual.vi_mode = i;
5646# ifdef FEAT_EVAL
5647 curbuf->b_visual_mode_eval = i;
5648# endif
5649 i = curwin->w_curswant;
5650 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5651 curbuf->b_visual.vi_curswant = i;
5652
5653 tpos = curbuf->b_visual.vi_end;
5654 curbuf->b_visual.vi_end = curwin->w_cursor;
5655 curwin->w_cursor = curbuf->b_visual.vi_start;
5656 curbuf->b_visual.vi_start = VIsual;
5657 }
5658 else
5659 {
5660 VIsual_mode = curbuf->b_visual.vi_mode;
5661 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5662 tpos = curbuf->b_visual.vi_end;
5663 curwin->w_cursor = curbuf->b_visual.vi_start;
5664 }
5665
5666 VIsual_active = TRUE;
5667 VIsual_reselect = TRUE;
5668
5669 // Set Visual to the start and w_cursor to the end of the Visual
5670 // area. Make sure they are on an existing character.
5671 check_cursor();
5672 VIsual = curwin->w_cursor;
5673 curwin->w_cursor = tpos;
5674 check_cursor();
5675 update_topline();
5676
5677 // When called from normal "g" command: start Select mode when
5678 // 'selectmode' contains "cmd". When called for K_SELECT, always
5679 // start Select mode.
5680 if (cap->arg)
5681 {
5682 VIsual_select = TRUE;
5683 VIsual_select_reg = 0;
5684 }
5685 else
5686 may_start_select('c');
5687 setmouse();
5688#ifdef FEAT_CLIPBOARD
5689 // Make sure the clipboard gets updated. Needed because start and
5690 // end are still the same, and the selection needs to be owned
5691 clip_star.vmode = NUL;
5692#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005693 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005694 showmode();
5695}
5696
5697/*
5698 * "g0", "g^" : Like "0" and "^" but for screen lines.
5699 * "gm": middle of "g0" and "g$".
5700 */
5701 static void
5702nv_g_home_m_cmd(cmdarg_T *cap)
5703{
5704 int i;
5705 int flag = FALSE;
5706
5707 if (cap->nchar == '^')
5708 flag = TRUE;
5709
5710 cap->oap->motion_type = MCHAR;
5711 cap->oap->inclusive = FALSE;
5712 if (curwin->w_p_wrap && curwin->w_width != 0)
5713 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005714 int width1 = curwin->w_width - curwin_col_off();
5715 int width2 = width1 + curwin_col_off2();
5716 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005717
5718 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005719 virtcol = curwin->w_virtcol
5720#ifdef FEAT_PROP_POPUP
5721 - curwin->w_virtcol_first_char
5722#endif
5723 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005724 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005725 if (virtcol >= (colnr_T)width1 && width2 > 0)
5726 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005727 }
5728 else
5729 i = curwin->w_leftcol;
5730 // Go to the middle of the screen line. When 'number' or
5731 // 'relativenumber' is on and lines are wrapping the middle can be more
5732 // to the left.
5733 if (cap->nchar == 'm')
5734 i += (curwin->w_width - curwin_col_off()
5735 + ((curwin->w_p_wrap && i > 0)
5736 ? curwin_col_off2() : 0)) / 2;
5737 coladvance((colnr_T)i);
5738 if (flag)
5739 {
5740 do
5741 i = gchar_cursor();
5742 while (VIM_ISWHITE(i) && oneright() == OK);
5743 curwin->w_valid &= ~VALID_WCOL;
5744 }
5745 curwin->w_set_curswant = TRUE;
5746}
5747
5748/*
5749 * "g_": to the last non-blank character in the line or <count> lines
5750 * downward.
5751 */
5752 static void
5753nv_g_underscore_cmd(cmdarg_T *cap)
5754{
5755 char_u *ptr;
5756
5757 cap->oap->motion_type = MCHAR;
5758 cap->oap->inclusive = TRUE;
5759 curwin->w_curswant = MAXCOL;
5760 if (cursor_down((long)(cap->count1 - 1),
5761 cap->oap->op_type == OP_NOP) == FAIL)
5762 {
5763 clearopbeep(cap->oap);
5764 return;
5765 }
5766
5767 ptr = ml_get_curline();
5768
5769 // In Visual mode we may end up after the line.
5770 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5771 --curwin->w_cursor.col;
5772
5773 // Decrease the cursor column until it's on a non-blank.
5774 while (curwin->w_cursor.col > 0
5775 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5776 --curwin->w_cursor.col;
5777 curwin->w_set_curswant = TRUE;
5778 adjust_for_sel(cap);
5779}
5780
5781/*
5782 * "g$" : Like "$" but for screen lines.
5783 */
5784 static void
5785nv_g_dollar_cmd(cmdarg_T *cap)
5786{
5787 oparg_T *oap = cap->oap;
5788 int i;
5789 int col_off = curwin_col_off();
5790
5791 oap->motion_type = MCHAR;
5792 oap->inclusive = TRUE;
5793 if (curwin->w_p_wrap && curwin->w_width != 0)
5794 {
5795 curwin->w_curswant = MAXCOL; // so we stay at the end
5796 if (cap->count1 == 1)
5797 {
5798 int width1 = curwin->w_width - col_off;
5799 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005800 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005801
5802 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005803 virtcol = curwin->w_virtcol
5804#ifdef FEAT_PROP_POPUP
5805 - curwin->w_virtcol_first_char
5806#endif
5807 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005808 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005809 if (virtcol >= (colnr_T)width1)
5810 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005811 * width2;
5812 coladvance((colnr_T)i);
5813
5814 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005815 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005816 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5817 {
5818 // Check for landing on a character that got split at
5819 // the end of the line. We do not want to advance to
5820 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005821 if (curwin->w_virtcol
5822#ifdef FEAT_PROP_POPUP
5823 - curwin->w_virtcol_first_char
5824#endif
5825 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005826 --curwin->w_cursor.col;
5827 }
5828 }
5829 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5830 clearopbeep(oap);
5831 }
5832 else
5833 {
5834 if (cap->count1 > 1)
5835 // if it fails, let the cursor still move to the last char
5836 (void)cursor_down(cap->count1 - 1, FALSE);
5837
5838 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5839 coladvance((colnr_T)i);
5840
5841 // if the character doesn't fit move one back
5842 if (curwin->w_cursor.col > 0
5843 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5844 {
5845 colnr_T vcol;
5846
5847 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5848 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5849 --curwin->w_cursor.col;
5850 }
5851
5852 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005853 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005854 }
5855}
5856
5857/*
5858 * "gi": start Insert at the last position.
5859 */
5860 static void
5861nv_gi_cmd(cmdarg_T *cap)
5862{
5863 int i;
5864
5865 if (curbuf->b_last_insert.lnum != 0)
5866 {
5867 curwin->w_cursor = curbuf->b_last_insert;
5868 check_cursor_lnum();
5869 i = (int)STRLEN(ml_get_curline());
5870 if (curwin->w_cursor.col > (colnr_T)i)
5871 {
5872 if (virtual_active())
5873 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5874 curwin->w_cursor.col = i;
5875 }
5876 }
5877 cap->cmdchar = 'i';
5878 nv_edit(cap);
5879}
5880
5881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 * Commands starting with "g".
5883 */
5884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005885nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 switch (cap->nchar)
5891 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005892 case Ctrl_A:
5893 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005895 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005896 if (!VIsual_active && cap->nchar == Ctrl_A)
5897 vim_mem_profile_dump();
5898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005900 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005901 if (VIsual_active)
5902 {
5903 cap->arg = TRUE;
5904 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005905 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005906 nv_addsub(cap);
5907 }
5908 else
5909 clearopbeep(oap);
5910 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005912 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 case 'R':
5914 cap->arg = TRUE;
5915 nv_Replace(cap);
5916 break;
5917
5918 case 'r':
5919 nv_vreplace(cap);
5920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922 case '&':
5923 do_cmdline_cmd((char_u *)"%s//~/&");
5924 break;
5925
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005926 // "gv": Reselect the previous Visual area. If Visual already active,
5927 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005929 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005931
5932 // "gV": Don't reselect the previous Visual area after a Select mode
5933 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 case 'V':
5935 VIsual_reselect = FALSE;
5936 break;
5937
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005938 // "gh": start Select mode.
5939 // "gH": start Select line mode.
5940 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 case K_BS:
5942 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005943 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 case 'h':
5945 case 'H':
5946 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 cap->cmdchar = cap->nchar + ('v' - 'h');
5948 cap->arg = TRUE;
5949 nv_visual(cap);
5950 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005952 // "gn", "gN" visually select next/previous search match
5953 // "gn" selects next match
5954 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005955 case 'N':
5956 case 'n':
5957 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005958 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005959 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005961 // "gj" and "gk" two new funny movement keys -- up and down
5962 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 case 'j':
5964 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005965 // with 'nowrap' it works just like the normal "j" command.
5966 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 oap->motion_type = MLINE;
5969 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5970 }
5971 else
5972 i = nv_screengo(oap, FORWARD, cap->count1);
5973 if (i == FAIL)
5974 clearopbeep(oap);
5975 break;
5976
5977 case 'k':
5978 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005979 // with 'nowrap' it works just like the normal "k" command.
5980 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
5982 oap->motion_type = MLINE;
5983 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5984 }
5985 else
5986 i = nv_screengo(oap, BACKWARD, cap->count1);
5987 if (i == FAIL)
5988 clearopbeep(oap);
5989 break;
5990
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005991 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 case 'J':
5993 nv_join(cap);
5994 break;
5995
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005996 // "g0", "g^" : Like "0" and "^" but for screen lines.
5997 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 case '0':
6000 case 'm':
6001 case K_HOME:
6002 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006003 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 break;
6005
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006006 case 'M':
6007 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006008 oap->motion_type = MCHAR;
6009 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006010 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006011 if (cap->count0 > 0 && cap->count0 <= 100)
6012 coladvance((colnr_T)(i * cap->count0 / 100));
6013 else
6014 coladvance((colnr_T)(i / 2));
6015 curwin->w_set_curswant = TRUE;
6016 }
6017 break;
6018
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006019 // "g_": to the last non-blank character in the line or <count> lines
6020 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006022 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 break;
6024
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006025 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 case '$':
6027 case K_END:
6028 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006029 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 break;
6031
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006032 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 case '*':
6034 case '#':
6035#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006036 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006038 case Ctrl_RSB: // :tag or :tselect for current identifier
6039 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 nv_ident(cap);
6041 break;
6042
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006043 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 case 'e':
6045 case 'E':
6046 oap->motion_type = MCHAR;
6047 curwin->w_set_curswant = TRUE;
6048 oap->inclusive = TRUE;
6049 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6050 clearopbeep(oap);
6051 break;
6052
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006053 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006055 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 break;
6057
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006058 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006060 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 break;
6062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'I':
6065 beginline(0);
6066 if (!checkclearopq(oap))
6067 invoke_edit(cap, FALSE, 'g', FALSE);
6068 break;
6069
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006070 // "gf": goto file, edit file under cursor
6071 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006073 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 nv_gotofile(cap);
6075 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006077 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case '\'':
6079 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006080 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 case '`':
6082 nv_gomark(cap);
6083 break;
6084
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006085 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006087 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 break;
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "ga": Display the ascii value of the character under the
6091 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case 'a':
6093 do_ascii(NULL);
6094 break;
6095
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006096 // "g8": Display the bytes used for the UTF-8 character under the
6097 // cursor. It is displayed in hex.
6098 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006100 if (cap->count0 == 8)
6101 utf_find_illegal();
6102 else
6103 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006107 case '<':
6108 show_sb_text();
6109 break;
6110
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006111 // "gg": Goto the first line in file. With a count it goes to
6112 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 case 'g':
6114 cap->arg = FALSE;
6115 nv_goto(cap);
6116 break;
6117
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006118 // Two-character operators:
6119 // "gq" Format text
6120 // "gw" Format text and keep cursor position
6121 // "g~" Toggle the case of the text.
6122 // "gu" Change text to lower case.
6123 // "gU" Change text to upper case.
6124 // "g?" rot13 encoding
6125 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 case 'q':
6127 case 'w':
6128 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006129 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 case '~':
6131 case 'u':
6132 case 'U':
6133 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006134 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 nv_operator(cap);
6136 break;
6137
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006138 // "gd": Find first occurrence of pattern under the cursor in the
6139 // current function
6140 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 case 'd':
6142 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006143 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 break;
6145
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006146 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 case K_MIDDLEMOUSE:
6148 case K_MIDDLEDRAG:
6149 case K_MIDDLERELEASE:
6150 case K_LEFTMOUSE:
6151 case K_LEFTDRAG:
6152 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006153 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 case K_RIGHTMOUSE:
6155 case K_RIGHTDRAG:
6156 case K_RIGHTRELEASE:
6157 case K_X1MOUSE:
6158 case K_X1DRAG:
6159 case K_X1RELEASE:
6160 case K_X2MOUSE:
6161 case K_X2DRAG:
6162 case K_X2RELEASE:
6163 mod_mask = MOD_MASK_CTRL;
6164 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6165 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
6167 case K_IGNORE:
6168 break;
6169
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006170 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 case 'p':
6172 case 'P':
6173 nv_put(cap);
6174 break;
6175
6176#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006177 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 case 'o':
6179 goto_byte(cap->count0);
6180 break;
6181#endif
6182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006183 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006185 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 do_exmode(TRUE);
6187 break;
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case ',':
6190 nv_pcmark(cap);
6191 break;
6192
6193 case ';':
6194 cap->count1 = -cap->count1;
6195 nv_pcmark(cap);
6196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006199 if (!checkclearop(oap))
6200 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006202 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006203 if (!checkclearop(oap))
6204 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006205 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206
Bram Moolenaar62a23252020-08-09 14:04:42 +02006207 case TAB:
6208 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6209 clearopbeep(oap);
6210 break;
6211
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006212 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006213 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006214 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006215 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006216 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006217 break;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 default:
6220 clearopbeep(oap);
6221 break;
6222 }
6223}
6224
6225/*
6226 * Handle "o" and "O" commands.
6227 */
6228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006229n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006231#ifdef FEAT_CONCEAL
6232 linenr_T oldline = curwin->w_cursor.lnum;
6233#endif
6234
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006235 if (checkclearopq(cap->oap))
6236 return;
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006239 if (cap->cmdchar == 'O')
6240 // Open above the first line of a folded sequence of lines
6241 (void)hasFolding(curwin->w_cursor.lnum,
6242 &curwin->w_cursor.lnum, NULL);
6243 else
6244 // Open below the last line of a folded sequence of lines
6245 (void)hasFolding(curwin->w_cursor.lnum,
6246 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006248 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6249 (cap->cmdchar == 'O' ? 1 : 0)),
6250 (linenr_T)(curwin->w_cursor.lnum +
6251 (cap->cmdchar == 'o' ? 1 : 0))
6252 ) == OK
6253 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6254 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6255 0, NULL) == OK)
6256 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006257#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006258 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6259 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006260#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006261#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006262 if (curwin->w_p_cul)
6263 // force redraw of cursorline
6264 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006265#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006266 // When '#' is in 'cpoptions' ignore the count.
6267 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6268 cap->count1 = 1;
6269 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271}
6272
6273/*
6274 * "." command: redo last change.
6275 */
6276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006277nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006279 if (checkclearopq(cap->oap))
6280 return;
6281
6282 // If "restart_edit" is TRUE, the last but one command is repeated
6283 // instead of the last command (inserting text). This is used for
6284 // CTRL-O <.> in insert mode.
6285 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6286 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287}
6288
6289/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006290 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 */
6292 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006293nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006295 if (VIsual_select && VIsual_active)
6296 {
6297 int reg;
6298 // Get register name
6299 ++no_mapping;
6300 ++allow_keys;
6301 reg = plain_vgetc();
6302 LANGMAP_ADJUST(reg, TRUE);
6303 --no_mapping;
6304 --allow_keys;
6305
6306 if (reg == '"')
6307 // the unnamed register is 0
6308 reg = 0;
6309
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006310 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006311 return;
6312 }
6313
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006314 if (checkclearopq(cap->oap))
6315 return;
6316
6317 u_redo((int)cap->count1);
6318 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319}
6320
6321/*
6322 * Handle "U" command.
6323 */
6324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006325nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006327 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006328 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006330 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 cap->cmdchar = 'g';
6332 cap->nchar = 'U';
6333 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006334 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006336
6337 if (checkclearopq(cap->oap))
6338 return;
6339
6340 u_undoline();
6341 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
6343
6344/*
6345 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6346 * single character.
6347 */
6348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006349nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006351 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006352 {
6353#ifdef FEAT_JOB_CHANNEL
6354 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6355 {
6356 clearopbeep(cap->oap);
6357 return;
6358 }
6359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 else
6363 nv_operator(cap);
6364}
6365
6366/*
6367 * Handle an operator command.
6368 * The actual work is done by do_pending_operator().
6369 */
6370 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 int op_type;
6374
6375 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006376#ifdef FEAT_JOB_CHANNEL
6377 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6378 {
6379 clearopbeep(cap->oap);
6380 return;
6381 }
6382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006384 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 nv_lineop(cap);
6386 else if (!checkclearop(cap->oap))
6387 {
6388 cap->oap->start = curwin->w_cursor;
6389 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006390#ifdef FEAT_EVAL
6391 set_op_var(op_type);
6392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394}
6395
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006396#ifdef FEAT_EVAL
6397/*
6398 * Set v:operator to the characters for "optype".
6399 */
6400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006401set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006402{
6403 char_u opchars[3];
6404
6405 if (optype == OP_NOP)
6406 set_vim_var_string(VV_OP, NULL, 0);
6407 else
6408 {
6409 opchars[0] = get_op_char(optype);
6410 opchars[1] = get_extra_op_char(optype);
6411 opchars[2] = NUL;
6412 set_vim_var_string(VV_OP, opchars, -1);
6413 }
6414}
6415#endif
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417/*
6418 * Handle linewise operator "dd", "yy", etc.
6419 *
6420 * "_" is is a strange motion command that helps make operators more logical.
6421 * It is actually implemented, but not documented in the real Vi. This motion
6422 * command actually refers to "the current line". Commands like "dd" and "yy"
6423 * are really an alternate form of "d_" and "y_". It does accept a count, so
6424 * "d3_" works to delete 3 lines.
6425 */
6426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006427nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 cap->oap->motion_type = MLINE;
6430 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6431 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006432 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006433 && cap->oap->motion_force != 'v'
6434 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 || cap->oap->op_type == OP_LSHIFT
6436 || cap->oap->op_type == OP_RSHIFT)
6437 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 beginline(BL_WHITE | BL_FIX);
6440}
6441
6442/*
6443 * <Home> command.
6444 */
6445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006446nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006448 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006449 if (mod_mask & MOD_MASK_CTRL)
6450 nv_goto(cap);
6451 else
6452 {
6453 cap->count0 = 1;
6454 nv_pipe(cap);
6455 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006456 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6457 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458}
6459
6460/*
6461 * "|" command.
6462 */
6463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006464nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 cap->oap->motion_type = MCHAR;
6467 cap->oap->inclusive = FALSE;
6468 beginline(0);
6469 if (cap->count0 > 0)
6470 {
6471 coladvance((colnr_T)(cap->count0 - 1));
6472 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6473 }
6474 else
6475 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006476 // keep curswant at the column where we wanted to go, not where
6477 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 curwin->w_set_curswant = FALSE;
6479}
6480
6481/*
6482 * Handle back-word command "b" and "B".
6483 * cap->arg is 1 for "B"
6484 */
6485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006486nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 cap->oap->motion_type = MCHAR;
6489 cap->oap->inclusive = FALSE;
6490 curwin->w_set_curswant = TRUE;
6491 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6492 clearopbeep(cap->oap);
6493#ifdef FEAT_FOLDING
6494 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6495 foldOpenCursor();
6496#endif
6497}
6498
6499/*
6500 * Handle word motion commands "e", "E", "w" and "W".
6501 * cap->arg is TRUE for "E" and "W".
6502 */
6503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006504nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 int n;
6507 int word_end;
6508 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006509 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006511 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6513 word_end = TRUE;
6514 else
6515 word_end = FALSE;
6516 cap->oap->inclusive = word_end;
6517
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006518 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 if (!word_end && cap->oap->op_type == OP_CHANGE)
6520 {
6521 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006522 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006524 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006526 // Reproduce a funny Vi behaviour: "cw" on a blank only
6527 // changes one character, not all blanks until the start of
6528 // the next word. Only do this when the 'w' flag is included
6529 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6531 {
6532 cap->oap->inclusive = TRUE;
6533 cap->oap->motion_type = MCHAR;
6534 return;
6535 }
6536 }
6537 else
6538 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006539 // This is a little strange. To match what the real Vi does,
6540 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6541 // that we are not on a space or a TAB. This seems impolite
6542 // at first, but it's really more what we mean when we say
6543 // 'cw'.
6544 // Another strangeness: When standing on the end of a word
6545 // "ce" will change until the end of the next word, but "cw"
6546 // will change only one character! This is done by setting
6547 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 cap->oap->inclusive = TRUE;
6549 word_end = TRUE;
6550 flag = TRUE;
6551 }
6552 }
6553 }
6554
6555 cap->oap->motion_type = MCHAR;
6556 curwin->w_set_curswant = TRUE;
6557 if (word_end)
6558 n = end_word(cap->count1, cap->arg, flag, FALSE);
6559 else
6560 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006562 // Don't leave the cursor on the NUL past the end of line. Unless we
6563 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006564 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006565 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567 if (n == FAIL && cap->oap->op_type == OP_NOP)
6568 clearopbeep(cap->oap);
6569 else
6570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572#ifdef FEAT_FOLDING
6573 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6574 foldOpenCursor();
6575#endif
6576 }
6577}
6578
6579/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006580 * Used after a movement command: If the cursor ends up on the NUL after the
6581 * end of the line, may move it back to the last character and make the motion
6582 * inclusive.
6583 */
6584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006585adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006586{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006587 // The cursor cannot remain on the NUL when:
6588 // - the column is > 0
6589 // - not in Visual mode or 'selection' is "o"
6590 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006592 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006593 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006594 {
6595 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006596 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597 if (has_mbyte)
6598 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006599 oap->inclusive = TRUE;
6600 }
6601}
6602
6603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 * "0" and "^" commands.
6605 * cap->arg is the argument for beginline().
6606 */
6607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006608nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610 cap->oap->motion_type = MCHAR;
6611 cap->oap->inclusive = FALSE;
6612 beginline(cap->arg);
6613#ifdef FEAT_FOLDING
6614 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6615 foldOpenCursor();
6616#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6618 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619}
6620
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621/*
6622 * In exclusive Visual mode, may include the last character.
6623 */
6624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006625adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006628 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 if (has_mbyte)
6631 inc_cursor();
6632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 ++curwin->w_cursor.col;
6634 cap->oap->inclusive = FALSE;
6635 }
6636}
6637
6638/*
6639 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6640 * Should check VIsual_mode before calling this.
6641 * Returns TRUE when backed up to the previous line.
6642 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006644unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 pos_T *pp;
6647
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006648 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006650 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 pp = &curwin->w_cursor;
6652 else
6653 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (pp->coladd > 0)
6655 --pp->coladd;
6656 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (pp->col > 0)
6658 {
6659 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006660 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662 else if (pp->lnum > 1)
6663 {
6664 --pp->lnum;
6665 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6666 return TRUE;
6667 }
6668 }
6669 return FALSE;
6670}
6671
6672/*
6673 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6674 */
6675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006681 VIsual_select_reg = 0;
6682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 else if (VIsual_reselect)
6684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006685 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 cap->arg = TRUE;
6687 nv_g_cmd(cap);
6688 }
6689}
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692/*
6693 * "G", "gg", CTRL-END, CTRL-HOME.
6694 * cap->arg is TRUE for "G".
6695 */
6696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006697nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 linenr_T lnum;
6700
6701 if (cap->arg)
6702 lnum = curbuf->b_ml.ml_line_count;
6703 else
6704 lnum = 1L;
6705 cap->oap->motion_type = MLINE;
6706 setpcmark();
6707
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006708 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (cap->count0 != 0)
6710 lnum = cap->count0;
6711 if (lnum < 1L)
6712 lnum = 1L;
6713 else if (lnum > curbuf->b_ml.ml_line_count)
6714 lnum = curbuf->b_ml.ml_line_count;
6715 curwin->w_cursor.lnum = lnum;
6716 beginline(BL_SOL | BL_FIX);
6717#ifdef FEAT_FOLDING
6718 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6719 foldOpenCursor();
6720#endif
6721}
6722
6723/*
6724 * CTRL-\ in Normal mode.
6725 */
6726 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006727nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6730 {
6731 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006732 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006733 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (cmdwin_type != 0)
6736 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (VIsual_active)
6738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006740 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006742 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (cap->nchar == Ctrl_G && p_im)
6744 restart_edit = 'a';
6745 }
6746 else
6747 clearopbeep(cap->oap);
6748}
6749
6750/*
6751 * ESC in Normal mode: beep, but don't flush buffers.
6752 * Don't even beep if we are canceling a command.
6753 */
6754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006755nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 int no_reason;
6758
6759 no_reason = (cap->oap->op_type == OP_NOP
6760 && cap->opcount == 0
6761 && cap->count0 == 0
6762 && cap->oap->regname == 0
6763 && !p_im);
6764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006765 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006767 if (restart_edit == 0 && cmdwin_type == 0
6768 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006769 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006770 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6771
Bram Moolenaar5939c352022-09-24 12:50:45 +01006772 // The user may accidentally do "vim file | grep word" and then
6773 // CTRL-C doesn't show anything. With a changed buffer give the
6774 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006775 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006776 {
6777 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6778
6779 if (out_redir)
6780 mch_errmsg(ms);
6781 else
6782 msg(ms);
6783 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006784 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006785 {
6786 if (out_redir)
6787 {
6788 got_int = FALSE;
6789 do_cmdline_cmd((char_u *)"qa");
6790 }
6791 else
6792 msg(_("Type :qa and press <Enter> to exit Vim"));
6793 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006796 if (restart_edit != 0)
6797 redraw_mode = TRUE; // remove "-- (insert) --"
6798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6800 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (!p_im)
6802 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (cmdwin_type != 0)
6804 {
6805 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006806 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 return;
6808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006810 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006811 {
6812 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006813 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6814 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006815 cmdwin_result = K_IGNORE;
6816 return;
6817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (VIsual_active)
6820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 end_visual_mode(); // stop Visual
6822 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006824 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006826 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006827 {
6828#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006829 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006830 popup_hide_message_win();
6831 else
6832#endif
6833 vim_beep(BO_ESC);
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 clearop(cap->oap);
6836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6838 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006839 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 restart_edit = 'a';
6841}
6842
6843/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006844 * Move the cursor for the "A" command.
6845 */
6846 void
6847set_cursor_for_append_to_line(void)
6848{
6849 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006850 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006851 {
6852 int save_State = State;
6853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 // Pretend Insert mode here to allow the cursor on the
6855 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006856 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006857 coladvance((colnr_T)MAXCOL);
6858 State = save_State;
6859 }
6860 else
6861 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6862}
6863
6864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006866 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 */
6868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6873 cap->cmdchar = 'i';
6874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006875 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006877 {
6878#ifdef FEAT_TERMINAL
6879 if (term_in_normal_mode())
6880 {
6881 end_visual_mode();
6882 clearop(cap->oap);
6883 term_enter_job_mode();
6884 return;
6885 }
6886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006891 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6892 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006896#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006897 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006898 {
6899 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006900 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006901 return;
6902 }
6903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 else if (!curbuf->b_p_ma && !p_im)
6905 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006906 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006907 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006909 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006911 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006913 else if (cap->cmdchar == K_PS && VIsual_active)
6914 {
6915 pos_T old_pos = curwin->w_cursor;
6916 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006917 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006920 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6921 {
6922 shift_delete_registers();
6923 cap->oap->regname = '1';
6924 }
6925 else
6926 cap->oap->regname = '-';
6927 cap->cmdchar = 'd';
6928 cap->nchar = NUL;
6929 nv_operator(cap);
6930 do_pending_operator(cap, 0, FALSE);
6931 cap->cmdchar = K_PS;
6932
zeertzjq7a732522022-03-14 20:46:41 +00006933 if (*ml_get_cursor() != NUL)
6934 {
6935 if (old_visual_mode == 'V')
6936 {
6937 // In linewise Visual mode insert before the beginning of the
6938 // next line.
6939 // When the last line in the buffer was deleted then create a
6940 // new line, otherwise there is not need to move cursor.
6941 // Detect this by checking if cursor moved above Visual area.
6942 if (curwin->w_cursor.lnum < old_pos.lnum
6943 && curwin->w_cursor.lnum < old_visual.lnum)
6944 {
6945 if (u_save_cursor() == OK)
6946 {
6947 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6948 FALSE);
6949 appended_lines(curwin->w_cursor.lnum++, 1L);
6950 }
6951 }
6952 }
6953 // When the last char in the line was deleted then append.
6954 // Detect this by checking if cursor moved before Visual area.
6955 else if (curwin->w_cursor.col < old_pos.col
6956 && curwin->w_cursor.col < old_visual.col)
6957 inc_cursor();
6958 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006961 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 else if (!checkclearopq(cap->oap))
6964 {
6965 switch (cap->cmdchar)
6966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006968 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 break;
6970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006971 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006972 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6973 beginline(BL_WHITE);
6974 else
6975 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 break;
6977
Bram Moolenaara1891842017-02-04 21:34:31 +01006978 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 // Bracketed paste works like "a"ppend, unless the cursor is in
6980 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006981 if (curwin->w_cursor.col == 0)
6982 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006983 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 case 'a': // "a"ppend is like "i"nsert on the next character.
6986 // increment coladd when in virtual space, increment the
6987 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (virtual_active()
6989 && (curwin->w_cursor.coladd > 0
6990 || *ml_get_cursor() == NUL
6991 || *ml_get_cursor() == TAB))
6992 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006993 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 inc_cursor();
6995 break;
6996 }
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6999 {
7000 int save_State = State;
7001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007002 // Pretend Insert mode here to allow the cursor on the
7003 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007004 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 coladvance(getviscol());
7006 State = save_State;
7007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7010 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007011 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007012 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007013 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
7016/*
7017 * Invoke edit() and take care of "restart_edit" and the return value.
7018 */
7019 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007020invoke_edit(
7021 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007022 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007023 int cmd,
7024 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 int restart_edit_save = 0;
7027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7029 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7030 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (repl || !stuff_empty())
7032 restart_edit_save = restart_edit;
7033 else
7034 restart_edit_save = 0;
7035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007036 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 restart_edit = 0;
7038
7039 if (edit(cmd, startln, cap->count1))
7040 cap->retval |= CA_COMMAND_BUSY;
7041
7042 if (restart_edit == 0)
7043 restart_edit = restart_edit_save;
7044}
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046/*
7047 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7048 */
7049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007050nv_object(
7051 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 int flag;
7054 int include;
7055 char_u *mps_save;
7056
7057 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 mps_save = curbuf->b_p_mps;
7064 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7065
7066 switch (cap->nchar)
7067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 flag = current_word(cap->oap, cap->count1, include, FALSE);
7070 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007071 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 flag = current_word(cap->oap, cap->count1, include, TRUE);
7073 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 case '(':
7076 case ')':
7077 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7078 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 case '{':
7081 case '}':
7082 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7083 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 case ']':
7086 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7087 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 case '>':
7090 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7091 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007092#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 case 't': // "at" = a tag block (xml and html)
7094 // Do not adjust oap->end in do_pending_operator()
7095 // otherwise there are different results for 'dit'
7096 // (note leading whitespace in last line):
7097 // 1) <b> 2) <b>
7098 // foobar foobar
7099 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007100 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007101 flag = current_tagblock(cap->oap, cap->count1, include);
7102 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007103#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 flag = current_par(cap->oap, cap->count1, include, 'p');
7106 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007107 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 flag = current_sent(cap->oap, cap->count1, include);
7109 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 case '"': // "a"" = a double quoted string
7111 case '\'': // "a'" = a single quoted string
7112 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007113 flag = current_quote(cap->oap, cap->count1, include,
7114 cap->nchar);
7115 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007116#if 0 // TODO
7117 case 'S': // "aS" = a section
7118 case 'f': // "af" = a filename
7119 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120#endif
7121 default:
7122 flag = FAIL;
7123 break;
7124 }
7125
7126 curbuf->b_p_mps = mps_save;
7127 if (flag == FAIL)
7128 clearopbeep(cap->oap);
7129 adjust_cursor_col();
7130 curwin->w_set_curswant = TRUE;
7131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
7133/*
7134 * "q" command: Start/stop recording.
7135 * "q:", "q/", "q?": edit command-line in command-line window.
7136 */
7137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007138nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 if (cap->oap->op_type == OP_FORMAT)
7141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 cap->cmdchar = 'g';
7144 cap->nchar = 'q';
7145 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007146 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007148
7149 if (checkclearop(cap->oap))
7150 return;
7151
7152 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007154 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007156 emsg(_(e_cmdline_window_already_open));
7157 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007159 stuffcharReadbuff(cap->nchar);
7160 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007162 else
7163 // (stop) recording into a named register, unless executing a
7164 // register
7165 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7166 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169/*
7170 * Handle the "@r" command.
7171 */
7172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007173nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 if (checkclearop(cap->oap))
7176 return;
7177#ifdef FEAT_EVAL
7178 if (cap->nchar == '=')
7179 {
7180 if (get_expr_register() == NUL)
7181 return;
7182 }
7183#endif
7184 while (cap->count1-- && !got_int)
7185 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007186 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 clearopbeep(cap->oap);
7189 break;
7190 }
7191 line_breakcheck();
7192 }
7193}
7194
7195/*
7196 * Handle the CTRL-U and CTRL-D commands.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7202 || (cap->cmdchar == Ctrl_D
7203 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7204 clearopbeep(cap->oap);
7205 else if (!checkclearop(cap->oap))
7206 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7207}
7208
7209/*
7210 * Handle "J" or "gJ" command.
7211 */
7212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007213nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007217 nv_operator(cap);
7218 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007220
7221 if (checkclearop(cap->oap))
7222 return;
7223
7224 if (cap->count0 <= 1)
7225 cap->count0 = 2; // default for join is two lines!
7226 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7227 curbuf->b_ml.ml_line_count)
7228 {
7229 // can't join when on the last line
7230 if (cap->count0 <= 2)
7231 {
7232 clearopbeep(cap->oap);
7233 return;
7234 }
7235 cap->count0 = curbuf->b_ml.ml_line_count
7236 - curwin->w_cursor.lnum + 1;
7237 }
7238
7239 prep_redo(cap->oap->regname, cap->count0,
7240 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7241 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242}
7243
7244/*
7245 * "P", "gP", "p" and "gp" commands.
7246 */
7247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007248nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007250 nv_put_opt(cap, FALSE);
7251}
7252
7253/*
7254 * "P", "gP", "p" and "gp" commands.
7255 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7256 */
7257 static void
7258nv_put_opt(cmdarg_T *cap, int fix_indent)
7259{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 int regname = 0;
7261 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007262 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007263 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 int dir;
7265 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007266 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
7268 if (cap->oap->op_type != OP_NOP)
7269 {
7270#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007271 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7273 {
7274 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007275 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 else
7278#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007279 clearopbeep(cap->oap);
7280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007282
Bram Moolenaarf2732452018-06-03 14:47:35 +02007283#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007284 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007285 {
7286 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007287 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007288 }
7289#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007290
7291 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007293 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7294 ? FORWARD : BACKWARD;
7295 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007297 else
7298 dir = (cap->cmdchar == 'P'
7299 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7300 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7301 prep_redo_cmd(cap);
7302 if (cap->cmdchar == 'g')
7303 flags |= PUT_CURSEND;
7304 else if (cap->cmdchar == 'z')
7305 flags |= PUT_BLOCK_INNER;
7306
7307 if (VIsual_active)
7308 {
7309 // Putting in Visual mode: The put text replaces the selected
7310 // text. First delete the selected text, then put the new text.
7311 // Need to save and restore the registers that the delete
7312 // overwrites if the old contents is being put.
7313 was_visual = TRUE;
7314 regname = cap->oap->regname;
7315 keep_registers = cap->cmdchar == 'P';
7316#ifdef FEAT_CLIPBOARD
7317 adjust_clip_reg(&regname);
7318#endif
7319 if (regname == 0 || regname == '"'
7320 || VIM_ISDIGIT(regname) || regname == '-'
7321#ifdef FEAT_CLIPBOARD
7322 || (clip_unnamed && (regname == '*' || regname == '+'))
7323#endif
7324
7325 )
7326 {
7327 // The delete is going to overwrite the register we want to
7328 // put, save it first.
7329 reg1 = get_register(regname, TRUE);
7330 }
7331
7332 // Now delete the selected text. Avoid messages here.
7333 cap->cmdchar = 'd';
7334 cap->nchar = NUL;
7335 cap->oap->regname = keep_registers ? '_' : NUL;
7336 ++msg_silent;
7337 nv_operator(cap);
7338 do_pending_operator(cap, 0, FALSE);
7339 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7340 --msg_silent;
7341
7342 // delete PUT_LINE_BACKWARD;
7343 cap->oap->regname = regname;
7344
7345 if (reg1 != NULL)
7346 {
7347 // Delete probably changed the register we want to put, save
7348 // it first. Then put back what was there before the delete.
7349 reg2 = get_register(regname, FALSE);
7350 put_register(regname, reg1);
7351 }
7352
7353 // When deleted a linewise Visual area, put the register as
7354 // lines to avoid it joined with the next line. When deletion was
7355 // characterwise, split a line when putting lines.
7356 if (VIsual_mode == 'V')
7357 flags |= PUT_LINE;
7358 else if (VIsual_mode == 'v')
7359 flags |= PUT_LINE_SPLIT;
7360 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7361 flags |= PUT_LINE_FORWARD;
7362 dir = BACKWARD;
7363 if ((VIsual_mode != 'V'
7364 && curwin->w_cursor.col < curbuf->b_op_start.col)
7365 || (VIsual_mode == 'V'
7366 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7367 // cursor is at the end of the line or end of file, put
7368 // forward.
7369 dir = FORWARD;
7370 // May have been reset in do_put().
7371 VIsual_active = TRUE;
7372 }
7373 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7374
7375 // If a register was saved, put it back now.
7376 if (reg2 != NULL)
7377 put_register(regname, reg2);
7378
7379 // What to reselect with "gv"? Selecting the just put text seems to
7380 // be the most useful, since the original text was removed.
7381 if (was_visual)
7382 {
7383 curbuf->b_visual.vi_start = curbuf->b_op_start;
7384 curbuf->b_visual.vi_end = curbuf->b_op_end;
7385 // need to adjust cursor position
7386 if (*p_sel == 'e')
7387 inc(&curbuf->b_visual.vi_end);
7388 }
7389
7390 // When all lines were selected and deleted do_put() leaves an empty
7391 // line that needs to be deleted now.
7392 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7393 {
7394 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7395 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7396
7397 // If the cursor was in that line, move it to the end of the last
7398 // line.
7399 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7400 {
7401 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7402 coladvance((colnr_T)MAXCOL);
7403 }
7404 }
7405 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406}
7407
7408/*
7409 * "o" and "O" commands.
7410 */
7411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007412nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007415 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7417 {
7418 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007419 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 else
7422#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007423 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007425#ifdef FEAT_JOB_CHANNEL
7426 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007427 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 n_opencmd(cap);
7431}
7432
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433#ifdef FEAT_NETBEANS_INTG
7434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007435nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 netbeans_keycommand(cap->nchar);
7438}
7439#endif
7440
7441#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007443nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007445 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007448
Bram Moolenaar3918c952005-03-15 22:34:55 +00007449/*
7450 * Trigger CursorHold event.
7451 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7452 * input buffer. "did_cursorhold" is set to avoid retriggering.
7453 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007455nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007456{
7457 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7458 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007459 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007460}