blob: 0d1eba759e698462fdf443d45831e2138a652234 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
189 if (text_locked())
190 {
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000191 if (oap != NULL)
192 clearopbeep(oap);
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100193 text_locked_msg();
194 return TRUE;
195 }
196 return FALSE;
197}
198
199/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000200 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
201 * Give an error message, possibly beep and return TRUE.
202 * "oap" may be NULL.
203 */
204 int
205check_text_or_curbuf_locked(oparg_T *oap)
206{
207 if (check_text_locked(oap))
208 return TRUE;
209 if (curbuf_locked())
210 {
211 if (oap != NULL)
212 clearop(oap);
213 return TRUE;
214 }
215 return FALSE;
216}
217
218/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000219 * Handle the count before a normal command and set cap->count0.
220 */
221 static int
222normal_cmd_get_count(
223 cmdarg_T *cap,
224 int c,
225 int toplevel UNUSED,
226 int set_prevcount UNUSED,
227 int *ctrl_w,
228 int *need_flushbuf UNUSED)
229{
230getcount:
231 if (!(VIsual_active && VIsual_select))
232 {
233 // Handle a count before a command and compute ca.count0.
234 // Note that '0' is a command and not the start of a count, but it's
235 // part of a count after other digits.
236 while ((c >= '1' && c <= '9')
237 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
238 || c == '0')))
239 {
240 if (c == K_DEL || c == K_KDEL)
241 {
242 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000244 }
245 else if (cap->count0 > 99999999L)
246 {
247 cap->count0 = 999999999L;
248 }
249 else
250 {
251 cap->count0 = cap->count0 * 10 + (c - '0');
252 }
253#ifdef FEAT_EVAL
254 // Set v:count here, when called from main() and not a stuffed
255 // command, so that v:count can be used in an expression mapping
256 // right after the count. Do set it for redo.
257 if (toplevel && readbuf1_empty())
258 set_vcount_ca(cap, &set_prevcount);
259#endif
260 if (*ctrl_w)
261 {
262 ++no_mapping;
263 ++allow_keys; // no mapping for nchar, but keys
264 }
265 ++no_zero_mapping; // don't map zero here
266 c = plain_vgetc();
267 LANGMAP_ADJUST(c, TRUE);
268 --no_zero_mapping;
269 if (*ctrl_w)
270 {
271 --no_mapping;
272 --allow_keys;
273 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000275 }
276
277 // If we got CTRL-W there may be a/another count
278 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
279 {
280 *ctrl_w = TRUE;
281 cap->opcount = cap->count0; // remember first count
282 cap->count0 = 0;
283 ++no_mapping;
284 ++allow_keys; // no mapping for nchar, but keys
285 c = plain_vgetc(); // get next character
286 LANGMAP_ADJUST(c, TRUE);
287 --no_mapping;
288 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000290 goto getcount; // jump back
291 }
292 }
293
294 if (c == K_CURSORHOLD)
295 {
296 // Save the count values so that ca.opcount and ca.count0 are exactly
297 // the same when coming back here after handling K_CURSORHOLD.
298 cap->oap->prev_opcount = cap->opcount;
299 cap->oap->prev_count0 = cap->count0;
300 }
301 else if (cap->opcount != 0)
302 {
303 // If we're in the middle of an operator (including after entering a
304 // yank buffer with '"') AND we had a count before the operator, then
305 // that count overrides the current value of ca.count0.
306 // What this means effectively, is that commands like "3dw" get turned
307 // into "d3w" which makes things fall into place pretty neatly.
308 // If you give a count before AND after the operator, they are
309 // multiplied.
310 if (cap->count0)
311 {
312 if (cap->opcount >= 999999999L / cap->count0)
313 cap->count0 = 999999999L;
314 else
315 cap->count0 *= cap->opcount;
316 }
317 else
318 cap->count0 = cap->opcount;
319 }
320
321 // Always remember the count. It will be set to zero (on the next call,
322 // above) when there is no pending operator.
323 // When called from main(), save the count for use by the "count" built-in
324 // variable.
325 cap->opcount = cap->count0;
326 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
327
328#ifdef FEAT_EVAL
329 // Only set v:count when called from main() and not a stuffed command.
330 // Do set it for redo.
331 if (toplevel && readbuf1_empty())
332 set_vcount(cap->count0, cap->count1, set_prevcount);
333#endif
334
335 return c;
336}
337
338/*
339 * Returns TRUE if the normal command (cap) needs a second character.
340 */
341 static int
342normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
343{
344 return ((cmd_flags & NV_NCH)
345 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
346 && cap->oap->op_type == OP_NOP)
347 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
348 || (cap->cmdchar == 'q'
349 && cap->oap->op_type == OP_NOP
350 && reg_recording == 0
351 && reg_executing == 0)
352 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
353 && (cap->oap->op_type != OP_NOP || VIsual_active))));
354}
355
356/*
357 * Get one or more additional characters for a normal command.
358 * Return the updated command index (if changed).
359 */
360 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000361normal_cmd_get_more_chars(
362 int idx_arg,
363 cmdarg_T *cap,
364 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000365{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000366 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000367 int c;
368 int *cp;
369 int repl = FALSE; // get character for replace mode
370 int lit = FALSE; // get extra character literally
371 int langmap_active = FALSE; // using :lmap mappings
372 int lang; // getting a text character
373#ifdef HAVE_INPUT_METHOD
374 int save_smd; // saved value of p_smd
375#endif
376
377 ++no_mapping;
378 ++allow_keys; // no mapping for nchar, but allow key codes
379 // Don't generate a CursorHold event here, most commands can't handle
380 // it, e.g., nv_replace(), nv_csearch().
381 did_cursorhold = TRUE;
382 if (cap->cmdchar == 'g')
383 {
384 /*
385 * For 'g' get the next character now, so that we can check for
386 * "gr", "g'" and "g`".
387 */
388 cap->nchar = plain_vgetc();
389 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000391 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
392 || cap->nchar == Ctrl_BSL)
393 {
394 cp = &cap->extra_char; // need to get a third character
395 if (cap->nchar != 'r')
396 lit = TRUE; // get it literally
397 else
398 repl = TRUE; // get it in replace mode
399 }
400 else
401 cp = NULL; // no third character needed
402 }
403 else
404 {
405 if (cap->cmdchar == 'r') // get it in replace mode
406 repl = TRUE;
407 cp = &cap->nchar;
408 }
409 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
410
411 /*
412 * Get a second or third character.
413 */
414 if (cp != NULL)
415 {
416 if (repl)
417 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100418 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000419#ifdef CURSOR_SHAPE
420 ui_cursor_shape(); // show different cursor shape
421#endif
422 }
423 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
424 {
425 // Allow mappings defined with ":lmap".
426 --no_mapping;
427 --allow_keys;
428 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100429 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000430 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100431 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000432 langmap_active = TRUE;
433 }
434#ifdef HAVE_INPUT_METHOD
435 save_smd = p_smd;
436 p_smd = FALSE; // Don't let the IM code show the mode here
437 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
438 im_set_active(TRUE);
439#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100440 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000441 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100442 MAY_WANT_TO_LOG_THIS;
443
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000444 // Disable bracketed paste and modifyOtherKeys here, we won't
445 // recognize the escape sequences with 'esckeys' off.
446 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000447 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000448 }
449
450 *cp = plain_vgetc();
451
Bram Moolenaar24959102022-05-07 20:01:16 +0100452 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000453 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100454 MAY_WANT_TO_LOG_THIS;
455
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000456 // Re-enable bracketed paste mode and modifyOtherKeys
457 out_str(T_BE);
458 out_str(T_CTI);
459 }
460
461 if (langmap_active)
462 {
463 // Undo the decrement done above
464 ++no_mapping;
465 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100466 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000467 }
468#ifdef HAVE_INPUT_METHOD
469 if (lang)
470 {
471 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
472 im_save_status(&curbuf->b_p_iminsert);
473 im_set_active(FALSE);
474 }
475 p_smd = save_smd;
476#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100477 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000479
480 if (!lit)
481 {
482#ifdef FEAT_DIGRAPHS
483 // Typing CTRL-K gets a digraph.
484 if (*cp == Ctrl_K
485 && ((nv_cmds[idx].cmd_flags & NV_LANG)
486 || cp == &cap->extra_char)
487 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
488 {
489 c = get_digraph(FALSE);
490 if (c > 0)
491 {
492 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000493 // Guessing how to update showcmd here...
494 del_from_showcmd(3);
495 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000496 }
497 }
498#endif
499
500 // adjust chars > 127, except after "tTfFr" commands
501 LANGMAP_ADJUST(*cp, !lang);
502#ifdef FEAT_RIGHTLEFT
503 // adjust Hebrew mapped char
504 if (p_hkmap && lang && KeyTyped)
505 *cp = hkmap(*cp);
506#endif
507 }
508
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000509 // When the next character is CTRL-\ a following CTRL-N means the
510 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000511 if (cp == &cap->extra_char
512 && cap->nchar == Ctrl_BSL
513 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
514 {
515 cap->cmdchar = Ctrl_BSL;
516 cap->nchar = cap->extra_char;
517 idx = find_command(cap->cmdchar);
518 }
519 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
546 // When getting a text character and the next character is a
547 // multi-byte character, it could be a composing character.
548 // However, don't wait for it to arrive. Also, do enable mapping,
549 // because if it's put back with vungetc() it's too late to apply
550 // mapping.
551 --no_mapping;
552 while (enc_utf8 && lang && (c = vpeekc()) > 0
553 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
554 {
555 c = plain_vgetc();
556 if (!utf_iscomposing(c))
557 {
558 vungetc(c); // it wasn't, put it back
559 break;
560 }
561 else if (cap->ncharC1 == 0)
562 cap->ncharC1 = c;
563 else
564 cap->ncharC2 = c;
565 }
566 ++no_mapping;
567 }
568 --no_mapping;
569 --allow_keys;
570
571 return idx;
572}
573
574/*
575 * Returns TRUE if after processing a normal mode command, need to wait for a
576 * moment when a message is displayed that will be overwritten by the mode
577 * message.
578 */
579 static int
580normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
581{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000582 // In Visual mode and with "^O" in Insert mode, a short message will be
583 // overwritten by the mode message. Wait a bit, until a key is hit.
584 // In Visual mode, it's more important to keep the Visual area updated
585 // than keeping a message (e.g. from a /pat search).
586 // Only do this if the command was typed, not from a mapping.
587 // Don't wait when emsg_silent is non-zero.
588 // Also wait a bit after an error message, e.g. for "^O:".
589 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000590 return ( ((p_smd
591 && msg_silent == 0
592 && (restart_edit != 0
593 || (VIsual_active
594 && old_pos->lnum == curwin->w_cursor.lnum
595 && old_pos->col == curwin->w_cursor.col)
596 )
597 && (clear_cmdline
598 || redraw_cmdline)
599 && (msg_didout || (msg_didany && msg_scroll))
600 && !msg_nowait
601 && KeyTyped)
602 || (restart_edit != 0
603 && !VIsual_active
604 && (msg_scroll
605 || emsg_on_display)))
606 && cap->oap->regname == 0
607 && !(cap->retval & CA_COMMAND_BUSY)
608 && stuff_empty()
609 && typebuf_typed()
610 && emsg_silent == 0
611 && !in_assert_fails
612 && !did_wait_return
613 && cap->oap->op_type == OP_NOP);
614}
615
616/*
617 * After processing a normal mode command, wait for a moment when a message is
618 * displayed that will be overwritten by the mode message.
619 */
620 static void
621normal_cmd_wait_for_msg(void)
622{
623 int save_State = State;
624
625 // Draw the cursor with the right shape here
626 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100627 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000628
629 // If need to redraw, and there is a "keep_msg", redraw before the
630 // delay
631 if (must_redraw && keep_msg != NULL && !emsg_on_display)
632 {
633 char_u *kmsg;
634
635 kmsg = keep_msg;
636 keep_msg = NULL;
637 // Showmode() will clear keep_msg, but we want to use it anyway.
638 // First update w_topline.
639 setcursor();
640 update_screen(0);
641 // now reset it, otherwise it's put in the history again
642 keep_msg = kmsg;
643
644 kmsg = vim_strsave(keep_msg);
645 if (kmsg != NULL)
646 {
647 msg_attr((char *)kmsg, keep_msg_attr);
648 vim_free(kmsg);
649 }
650 }
651 setcursor();
652#ifdef CURSOR_SHAPE
653 ui_cursor_shape(); // may show different cursor shape
654#endif
655 cursor_on();
656 out_flush();
657 if (msg_scroll || emsg_on_display)
658 ui_delay(1003L, TRUE); // wait at least one second
659 ui_delay(3003L, FALSE); // wait up to three seconds
660 State = save_State;
661
662 msg_scroll = FALSE;
663 emsg_on_display = FALSE;
664}
665
666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * Execute a command in Normal mode.
668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670normal_cmd(
671 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000678 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 static int old_mapped_len = 0;
682 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000683 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200684 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaara80faa82020-04-12 19:37:17 +0200686 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // Use a count remembered from before entering an operator. After typing
690 // "3d" we return from normal_cmd() and come back here, the "3" is
691 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ca.opcount = opcount;
693
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000694 // If there is an operator pending, then the command we take this time
695 // will terminate it. Finish_op tells us to finish the operation before
696 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef CURSOR_SHAPE
698 c = finish_op;
699#endif
700 finish_op = (oap->op_type != OP_NOP);
701#ifdef CURSOR_SHAPE
702 if (finish_op != c)
703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705# ifdef FEAT_MOUSESHAPE
706 update_mouseshape(-1);
707# endif
708 }
709#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100710 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // When not finishing an operator and no register name typed, reset the
713 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000717#ifdef FEAT_EVAL
718 set_prevcount = TRUE;
719#endif
720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // Restore counts from before receiving K_CURSORHOLD. This means after
723 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
724 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000725 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
726 {
727 ca.opcount = oap->prev_opcount;
728 ca.count0 = oap->prev_count0;
729 oap->prev_opcount = 0;
730 oap->prev_count0 = 0;
731 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar24959102022-05-07 20:01:16 +0100735 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#endif
739
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100740#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100741 // Set v:count here, when called from main() and not a stuffed
742 // command, so that v:count can be used in an expression mapping
743 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100744 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100745 set_vcount_ca(&ca, &set_prevcount);
746#endif
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
749 * Get the command character from the user.
750 */
751 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100752 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000754 // If a mapping was started in Visual or Select mode, remember the length
755 // of the mapping. This is used below to not return to Insert mode for as
756 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (restart_edit == 0)
758 old_mapped_len = 0;
759 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000760 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 old_mapped_len = typebuf_maplen();
762
763 if (c == NUL)
764 c = K_ZERO;
765
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000766 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (VIsual_active
768 && VIsual_select
769 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
770 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000771 int len;
772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100773 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
774 // 'insertmode' is set) fake a "d"elete command, Insert mode will
775 // restart automatically.
776 // Insert the typed character in the typeahead buffer, so that it can
777 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000778 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
779
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000780 // When recording and gotchars() was called the character will be
781 // recorded again, remove the previous recording.
782 if (KeyTyped)
783 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000784
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000785 if (restart_edit != 0)
786 c = 'd';
787 else
788 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 msg_nowait = TRUE; // don't delay going to insert mode
790 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000793 // If the window was made so small that nothing shows, make it at least one
794 // line and one column when typing a command.
795 if (KeyTyped && !KeyStuffed)
796 win_ensure_size();
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000800 // Get the command count
801 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
802 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000804 // Find the command character in the table of commands.
805 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (ctrl_w)
807 {
808 ca.nchar = c;
809 ca.cmdchar = Ctrl_W;
810 }
811 else
812 ca.cmdchar = c;
813 idx = find_command(ca.cmdchar);
814 if (idx < 0)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
818 goto normal_end;
819 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000820
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100822 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000823 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000825 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (VIsual_active)
827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (km_stopsel
830 && (nv_cmds[idx].cmd_flags & NV_STS)
831 && !(mod_mask & MOD_MASK_SHIFT))
832 {
833 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100834 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (km_startsel)
839 {
840 if (nv_cmds[idx].cmd_flags & NV_SS)
841 {
842 unshift_special(&ca);
843 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 if (idx < 0)
845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000847 clearopbeep(oap);
848 goto normal_end;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
852 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857#ifdef FEAT_RIGHTLEFT
858 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
859 && (nv_cmds[idx].cmd_flags & NV_RL))
860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 // Invert horizontal movements and operations. Only when typed by the
862 // user directly, not when the result of a mapping or "x" translated
863 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 switch (ca.cmdchar)
865 {
866 case 'l': ca.cmdchar = 'h'; break;
867 case K_RIGHT: ca.cmdchar = K_LEFT; break;
868 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
869 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
870 case 'h': ca.cmdchar = 'l'; break;
871 case K_LEFT: ca.cmdchar = K_RIGHT; break;
872 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
873 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
874 case '>': ca.cmdchar = '<'; break;
875 case '<': ca.cmdchar = '>'; break;
876 }
877 idx = find_command(ca.cmdchar);
878 }
879#endif
880
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000881 // Get additional characters if we need them.
882 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
883 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000885 // Flush the showcmd characters onto the screen so we can see them while
886 // the command is being executed. Only do this when the shown command was
887 // actually displayed, otherwise this will slow down a lot when executing
888 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (need_flushbuf)
890 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100891
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000892 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200893 {
894 if (ex_normal_busy)
895 did_cursorhold = save_did_cursorhold;
896 else
897 did_cursorhold = FALSE;
898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 if (ca.nchar == ESC)
903 {
904 clearop(oap);
905 if (restart_edit == 0 && goto_im())
906 restart_edit = 'a';
907 goto normal_end;
908 }
909
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000910 if (ca.cmdchar != K_IGNORE)
911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000913 msg_col = 0;
914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When 'keymodel' contains "startsel" some keys start Select/Visual
919 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (!VIsual_active && km_startsel)
921 {
922 if (nv_cmds[idx].cmd_flags & NV_SS)
923 {
924 start_selection();
925 unshift_special(&ca);
926 idx = find_command(ca.cmdchar);
927 }
928 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
929 && (mod_mask & MOD_MASK_SHIFT))
930 {
931 start_selection();
932 mod_mask &= ~MOD_MASK_SHIFT;
933 }
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000936 // Execute the command!
937 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ca.arg = nv_cmds[idx].cmd_arg;
939 (nv_cmds[idx].cmd_func)(&ca);
940
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000941 // If we didn't start or finish an operator, reset oap->regname, unless we
942 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (!finish_op
944 && !oap->op_type
945 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
946 {
947 clearop(oap);
948#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200949 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951 }
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // Get the length of mapped chars again after typing a count, second
954 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000955 if (old_mapped_len > 0)
956 old_mapped_len = typebuf_maplen();
957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000958 // If an operation is pending, handle it. But not for K_IGNORE or
959 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200960 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100961 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000963 // Wait for a moment when a message is displayed that will be overwritten
964 // by the mode message.
965 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
966 normal_cmd_wait_for_msg();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969normal_end:
970
971 msg_nowait = FALSE;
972
Bram Moolenaarcc613032020-06-07 21:31:18 +0200973#ifdef FEAT_EVAL
974 if (finish_op)
975 reset_reg_var();
976#endif
977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100978 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef CURSOR_SHAPE
980 c = finish_op;
981#endif
982 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100983 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100985 // Redraw the cursor with another shape, if we were in Operator-pending
986 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (c || ca.cmdchar == 'r')
988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990# ifdef FEAT_MOUSESHAPE
991 update_mouseshape(-1);
992# endif
993 }
994#endif
995
Bram Moolenaara983fe92008-07-31 20:04:27 +0000996 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100997 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 vim_free(ca.searchbuf);
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (has_mbyte)
1004 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (curwin->w_p_scb && toplevel)
1007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001008 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 do_check_scrollbind(TRUE);
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar860cae12010-06-05 23:22:07 +02001012 if (curwin->w_p_crb && toplevel)
1013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001015 do_check_cursorbind();
1016 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001018#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001020 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001021 restart_edit = 0;
1022#endif
1023
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001024 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1025 // if still inside a mapping that started in Visual mode).
1026 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1029 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !(ca.retval & CA_COMMAND_BUSY)
1031 && stuff_empty()
1032 && oap->regname == 0)
1033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_VIsual_select == 1)
1035 {
1036 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001037 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 showmode();
1039 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001040 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001042 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)edit(restart_edit, FALSE, 1L);
1044 }
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (restart_VIsual_select == 2)
1047 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 opcount = ca.opcount;
1051}
1052
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001053#ifdef FEAT_EVAL
1054/*
1055 * Set v:count and v:count1 according to "cap".
1056 * Set v:prevcount only when "set_prevcount" is TRUE.
1057 */
1058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001060{
1061 long count = cap->count0;
1062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001064 if (cap->opcount != 0)
1065 count = cap->opcount * (count == 0 ? 1 : count);
1066 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001068}
1069#endif
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001072 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * if not.
1074 */
1075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
1078 static int did_check = FALSE;
1079
1080 if (full_screen)
1081 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001082 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 did_check = TRUE;
1085 }
1086}
1087
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001088#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1089/*
1090 * Call yank_do_autocmd() for "regname".
1091 */
1092 static void
1093call_yank_do_autocmd(int regname)
1094{
1095 oparg_T oa;
1096 yankreg_T *reg;
1097
1098 clear_oparg(&oa);
1099 oa.regname = regname;
1100 oa.op_type = OP_YANK;
1101 oa.is_VIsual = TRUE;
1102 reg = get_register(regname, TRUE);
1103 yank_do_autocmd(&oa, reg);
1104 free_register(reg);
1105}
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001109 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001110 * This function or the next should ALWAYS be called to end Visual mode, except
1111 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001114end_visual_mode()
1115{
1116 end_visual_mode_keep_button();
1117 reset_held_button();
1118}
1119
1120 void
1121end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001124 // If we are using the clipboard, then remember what was selected in case
1125 // we need to paste it somewhere while we still own the selection.
1126 // Only do this when the clipboard is already owned. Don't want to grab
1127 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (clip_star.available && clip_star.owned)
1129 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001130
1131# if defined(FEAT_EVAL)
1132 // Emit a TextYankPost for the automatic copy of the selection into the
1133 // star and/or plus register.
1134 if (has_textyankpost())
1135 {
1136 if (clip_isautosel_star())
1137 call_yank_do_autocmd('*');
1138 if (clip_isautosel_plus())
1139 call_yank_do_autocmd('+');
1140 }
1141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 setmouse();
1146 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001149 curbuf->b_visual.vi_mode = VIsual_mode;
1150 curbuf->b_visual.vi_start = VIsual;
1151 curbuf->b_visual.vi_end = curwin->w_cursor;
1152 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153#ifdef FEAT_EVAL
1154 curbuf->b_visual_mode_eval = VIsual_mode;
1155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!virtual_active())
1157 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001158 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001160 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001161 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162}
1163
1164/*
1165 * Reset VIsual_active and VIsual_reselect.
1166 */
1167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001168reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 if (VIsual_active)
1171 {
1172 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001173 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 VIsual_reselect = FALSE;
1176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect if it's set.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 VIsual_reselect = FALSE;
1189 }
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001192 void
1193restore_visual_mode(void)
1194{
1195 if (VIsual_mode_orig != NUL)
1196 {
1197 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1198 VIsual_mode_orig = NUL;
1199 }
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
1203 * Check for a balloon-eval special item to include when searching for an
1204 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1205 * Returns TRUE if the character at "*ptr" should be included.
1206 * "dir" is FORWARD or BACKWARD, the direction of searching.
1207 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1208 * "bnp" points to a counter for square brackets.
1209 */
1210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001211find_is_eval_item(
1212 char_u *ptr,
1213 int *colp,
1214 int *bnp,
1215 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1219 ++*bnp;
1220 if (*bnp > 0)
1221 {
1222 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1223 --*bnp;
1224 return TRUE;
1225 }
1226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (*ptr == '.')
1229 return TRUE;
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1233 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1234 {
1235 *colp += dir;
1236 return TRUE;
1237 }
1238 return FALSE;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241/*
1242 * Find the identifier under or to the right of the cursor.
1243 * "find_type" can have one of three values:
1244 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001245 * FIND_STRING: find any non-white text
1246 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001247 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *
1249 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001250 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * This doesn't match the real Vi but I like it a little better and it
1254 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001255 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * When FIND_IDENT isn't defined, we backup until a blank.
1257 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001258 * Returns the length of the text, or zero if no text is found.
1259 * If text is found, a pointer to the text is put in "*text". This
1260 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
1262 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001263find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267}
1268
1269/*
1270 * Like find_ident_under_cursor(), but for any window and any position.
1271 * However: Uses 'iskeyword' from the current window!.
1272 */
1273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274find_ident_at_pos(
1275 win_T *wp,
1276 linenr_T lnum,
1277 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001278 char_u **text,
1279 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001280 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001283 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int this_class = 0;
1286 int prev_class;
1287 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001288 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001290 // if i == 0: try to find an identifier
1291 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1293 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1294 {
1295 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (has_mbyte)
1300 {
1301 while (ptr[col] != NUL)
1302 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001303 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 this_class = mb_get_class(ptr + col);
1307 if (this_class != 0 && (i == 1 || this_class != 1))
1308 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 )
1317 ++col;
1318
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001319 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001323 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (has_mbyte)
1326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1329 this_class = mb_get_class((char_u *)"a");
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001332 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1335 prev_class = mb_get_class(ptr + prevcol);
1336 if (this_class != prev_class
1337 && (i == 0
1338 || prev_class == 0
1339 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && (!(find_type & FIND_EVAL)
1341 || prevcol == 0
1342 || !find_is_eval_item(ptr + prevcol, &prevcol,
1343 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 )
1345 break;
1346 col = prevcol;
1347 }
1348
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001349 // If we don't want just any old text, or we've found an
1350 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (this_class > 2)
1352 this_class = 2;
1353 if (!(find_type & FIND_STRING) || this_class == 2)
1354 break;
1355 }
1356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 while (col > 0
1359 && ((i == 0
1360 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001361 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 && (!(find_type & FIND_IDENT)
1363 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 || ((find_type & FIND_EVAL)
1365 && col > 1
1366 && find_is_eval_item(ptr + col - 1, &col,
1367 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 ))
1369 --col;
1370
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001371 // If we don't want just any old text, or we've found an
1372 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1374 break;
1375 }
1376 }
1377
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001378 if (ptr[col] == NUL || (i == 0
1379 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001381 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001382 if ((find_type & FIND_NOERROR) == 0)
1383 {
1384 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001385 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001386 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return 0;
1390 }
1391 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001392 *text = ptr;
1393 if (textcol != NULL)
1394 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001397 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 bn = 0;
1400 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (has_mbyte)
1403 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001404 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 this_class = mb_get_class(ptr);
1406 while (ptr[col] != NUL
1407 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1408 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || ((find_type & FIND_EVAL)
1410 && col <= (int)startcol
1411 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001413 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001417 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 || ((find_type & FIND_EVAL)
1419 && col <= (int)startcol
1420 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 return col;
1425}
1426
1427/*
1428 * Prepare for redo of a normal command.
1429 */
1430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001431prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 prep_redo(cap->oap->regname, cap->count0,
1434 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1435}
1436
1437/*
1438 * Prepare for redo of any command.
1439 * Note that only the last argument can be a multi-byte char.
1440 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001442prep_redo(
1443 int regname,
1444 long num,
1445 int cmd1,
1446 int cmd2,
1447 int cmd3,
1448 int cmd4,
1449 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001451 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1452}
1453
1454/*
1455 * Prepare for redo of any command with extra count after "cmd2".
1456 */
1457 void
1458prep_redo_num2(
1459 int regname,
1460 long num1,
1461 int cmd1,
1462 int cmd2,
1463 long num2,
1464 int cmd3,
1465 int cmd4,
1466 int cmd5)
1467{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001469
1470#ifdef FEAT_EVAL
1471 // Put info about a mapping in the redo buffer, so that "." will use the
1472 // same script context.
1473 may_add_last_used_map_to_redobuff();
1474#endif
1475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
1478 AppendCharToRedobuff('"');
1479 AppendCharToRedobuff(regname);
1480 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001481 if (num1 != 0)
1482 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (cmd1 != NUL)
1484 AppendCharToRedobuff(cmd1);
1485 if (cmd2 != NUL)
1486 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001487 if (num2 != 0)
1488 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (cmd3 != NUL)
1490 AppendCharToRedobuff(cmd3);
1491 if (cmd4 != NUL)
1492 AppendCharToRedobuff(cmd4);
1493 if (cmd5 != NUL)
1494 AppendCharToRedobuff(cmd5);
1495}
1496
1497/*
1498 * check for operator active and clear it
1499 *
1500 * return TRUE if operator was active
1501 */
1502 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 if (oap->op_type == OP_NOP)
1506 return FALSE;
1507 clearopbeep(oap);
1508 return TRUE;
1509}
1510
1511/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001512 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001514 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001519 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001526clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 oap->op_type = OP_NOP;
1529 oap->regname = 0;
1530 oap->motion_force = NUL;
1531 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001532 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 clearop(oap);
1539 beep_flush();
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
1543 * Remove the shift modifier from a special key.
1544 */
1545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 switch (cap->cmdchar)
1549 {
1550 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1551 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1552 case K_S_UP: cap->cmdchar = K_UP; break;
1553 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1554 case K_S_HOME: cap->cmdchar = K_HOME; break;
1555 case K_S_END: cap->cmdchar = K_END; break;
1556 }
1557 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001560/*
1561 * If the mode is currently displayed clear the command line or update the
1562 * command displayed.
1563 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001566{
1567 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001569 else
1570 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001571}
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/*
1574 * Routines for displaying a partly typed command
1575 */
1576
kylo252ae6f1d82022-02-16 19:24:07 +00001577#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001579static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580static int showcmd_is_clear = TRUE;
1581static int showcmd_visual = FALSE;
1582
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001583static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001586clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 if (!p_sc)
1589 return;
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (VIsual_active && !char_avail())
1592 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001593 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 long lines;
1595 colnr_T leftcol, rightcol;
1596 linenr_T top, bot;
1597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001598 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001599 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 top = VIsual.lnum;
1602 bot = curwin->w_cursor.lnum;
1603 }
1604 else
1605 {
1606 top = curwin->w_cursor.lnum;
1607 bot = VIsual.lnum;
1608 }
1609# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001611 (void)hasFolding(top, &top, NULL);
1612 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613# endif
1614 lines = bot - top + 1;
1615
1616 if (VIsual_mode == Ctrl_V)
1617 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001618# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001620 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001623 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001624 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001627# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001628 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001629 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1632 (long)(rightcol - leftcol + 1));
1633 }
1634 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1635 sprintf((char *)showcmd_buf, "%ld", lines);
1636 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001637 {
1638 char_u *s, *e;
1639 int l;
1640 int bytes = 0;
1641 int chars = 0;
1642
1643 if (cursor_bot)
1644 {
1645 s = ml_get_pos(&VIsual);
1646 e = ml_get_cursor();
1647 }
1648 else
1649 {
1650 s = ml_get_cursor();
1651 e = ml_get_pos(&VIsual);
1652 }
1653 while ((*p_sel != 'e') ? s <= e : s < e)
1654 {
1655 l = (*mb_ptr2len)(s);
1656 if (l == 0)
1657 {
1658 ++bytes;
1659 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001661 }
1662 bytes += l;
1663 ++chars;
1664 s += l;
1665 }
1666 if (bytes == chars)
1667 sprintf((char *)showcmd_buf, "%d", chars);
1668 else
1669 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1670 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001671 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 showcmd_visual = TRUE;
1673 }
1674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 showcmd_buf[0] = NUL;
1677 showcmd_visual = FALSE;
1678
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001679 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (showcmd_is_clear)
1681 return;
1682 }
1683
1684 display_showcmd();
1685}
1686
1687/*
1688 * Add 'c' to string of shown command chars.
1689 * Return TRUE if output has been written (and setcursor() has been called).
1690 */
1691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001692add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 char_u *p;
1695 int old_len;
1696 int extra_len;
1697 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 int i;
1699 static int ignore[] =
1700 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001701#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1703 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001704#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001705 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001706 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1708 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001709 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001711 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 0
1713 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar09df3122006-01-23 22:23:09 +00001715 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return FALSE;
1717
1718 if (showcmd_visual)
1719 {
1720 showcmd_buf[0] = NUL;
1721 showcmd_visual = FALSE;
1722 }
1723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001724 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (IS_SPECIAL(c))
1726 for (i = 0; ignore[i] != 0; ++i)
1727 if (ignore[i] == c)
1728 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001731 if (*p == ' ')
1732 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 old_len = (int)STRLEN(showcmd_buf);
1734 extra_len = (int)STRLEN(p);
1735 overflow = old_len + extra_len - SHOWCMD_COLS;
1736 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001737 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1738 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 STRCAT(showcmd_buf, p);
1740
1741 if (char_avail())
1742 return FALSE;
1743
1744 display_showcmd();
1745
1746 return TRUE;
1747}
1748
1749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 if (!add_to_showcmd(c))
1753 setcursor();
1754}
1755
1756/*
1757 * Delete 'len' characters from the end of the shown command.
1758 */
1759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 int old_len;
1763
1764 if (!p_sc)
1765 return;
1766
1767 old_len = (int)STRLEN(showcmd_buf);
1768 if (len > old_len)
1769 len = old_len;
1770 showcmd_buf[old_len - len] = NUL;
1771
1772 if (!char_avail())
1773 display_showcmd();
1774}
1775
1776/*
1777 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1778 * something and there is a partial mapping.
1779 */
1780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001781push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 if (p_sc)
1784 STRCPY(old_showcmd_buf, showcmd_buf);
1785}
1786
1787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 if (!p_sc)
1791 return;
1792
1793 STRCPY(showcmd_buf, old_showcmd_buf);
1794
1795 display_showcmd();
1796}
1797
1798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int len;
1802
1803 cursor_off();
1804
1805 len = (int)STRLEN(showcmd_buf);
1806 if (len == 0)
1807 showcmd_is_clear = TRUE;
1808 else
1809 {
1810 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1811 showcmd_is_clear = FALSE;
1812 }
1813
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001814 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1815 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1817
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{
2032 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002033 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002034 if (mod_mask & MOD_MASK_CTRL)
2035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 if (cap->arg == BACKWARD)
2038 goto_tabpage(-(int)cap->count1);
2039 else
2040 goto_tabpage((int)cap->count0);
2041 }
2042 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002043 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 }
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);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002064 }
2065 else
2066 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002067#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002068 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2069 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002070#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002071 // clear any search statistics
2072 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2073 clear_cmdline = TRUE;
2074 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002075}
2076
2077/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002078 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2079 * otherwise.
2080 */
2081 static int
2082is_ident(char_u *line, int offset)
2083{
2084 int i;
2085 int incomment = FALSE;
2086 int instring = 0;
2087 int prev = 0;
2088
2089 for (i = 0; i < offset && line[i] != NUL; i++)
2090 {
2091 if (instring != 0)
2092 {
2093 if (prev != '\\' && line[i] == instring)
2094 instring = 0;
2095 }
2096 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2097 {
2098 instring = line[i];
2099 }
2100 else
2101 {
2102 if (incomment)
2103 {
2104 if (prev == '*' && line[i] == '/')
2105 incomment = FALSE;
2106 }
2107 else if (prev == '/' && line[i] == '*')
2108 {
2109 incomment = TRUE;
2110 }
2111 else if (prev == '/' && line[i] == '/')
2112 {
2113 return FALSE;
2114 }
2115 }
2116
2117 prev = line[i];
2118 }
2119
2120 return incomment == FALSE && instring == 0;
2121}
2122
2123/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002124 * Search for variable declaration of "ptr[len]".
2125 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2126 * current file ("gD").
2127 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002128 * Return FAIL when not found.
2129 */
2130 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131find_decl(
2132 char_u *ptr,
2133 int len,
2134 int locally,
2135 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002136 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002137{
2138 char_u *pat;
2139 pos_T old_pos;
2140 pos_T par_pos;
2141 pos_T found_pos;
2142 int t;
2143 int save_p_ws;
2144 int save_p_scs;
2145 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002146 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002147 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002148 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002149
2150 if ((pat = alloc(len + 7)) == NULL)
2151 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002153 // Put "\V" before the pattern to avoid that the special meaning of "."
2154 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002155 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2156 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 old_pos = curwin->w_cursor;
2158 save_p_ws = p_ws;
2159 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002160 p_ws = FALSE; // don't wrap around end of file now
2161 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002163 // With "gD" go to line 1.
2164 // With "gd" Search back for the start of the current function, then go
2165 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002166 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002168 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002170 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 else
2173 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002174 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2176 --curwin->w_cursor.lnum;
2177 }
2178 curwin->w_cursor.col = 0;
2179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002181 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002182 for (;;)
2183 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002184 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002185 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002186 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002188
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002189 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002190 {
2191 pos_T *pos;
2192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // Check that the block the match is in doesn't end before the
2194 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002195 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2196 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2197 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002198 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 // There can't be a useful match before the end of this block.
2200 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002201 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002202 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002203 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002204 }
2205
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002206 if (t == FAIL)
2207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002208 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002209 if (found_pos.lnum != 0)
2210 {
2211 curwin->w_cursor = found_pos;
2212 t = OK;
2213 }
2214 break;
2215 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002216 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002219 ++curwin->w_cursor.lnum;
2220 curwin->w_cursor.col = 0;
2221 continue;
2222 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002223 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 // If the current position is not a valid identifier and a previous
2226 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002227 if (!valid && found_pos.lnum != 0)
2228 {
2229 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002231 }
2232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002233 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 if (valid && !locally)
2235 break;
2236 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 if (found_pos.lnum != 0)
2240 curwin->w_cursor = found_pos;
2241 break;
2242 }
2243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // For finding a local variable and the match is before the "{" or
2245 // inside a comment, continue searching. For K&R style function
2246 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002247 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002248 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002249 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002250 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // Remove SEARCH_START from flags to avoid getting stuck at one
2252 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002253 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002255
2256 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002258 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 curwin->w_cursor = old_pos;
2260 }
2261 else
2262 {
2263 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002264 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 reset_search_dir();
2266 }
2267
2268 vim_free(pat);
2269 p_ws = save_p_ws;
2270 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002271
2272 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273}
2274
2275/*
2276 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2277 * lines rather than lines in the file.
2278 * 'dist' must be positive.
2279 *
2280 * Return OK if able to move cursor, FAIL otherwise.
2281 */
2282 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002285 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int retval = OK;
2287 int atend = FALSE;
2288 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 int col_off1; // margin offset for first screen line
2290 int col_off2; // margin offset for wrapped screen line
2291 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002292 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002295 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 col_off1 = curwin_col_off();
2298 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002299 width1 = curwin->w_width - col_off1;
2300 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002301 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002305 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002306 // Instead of sticking at the last character of the buffer line we
2307 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (curwin->w_curswant == MAXCOL)
2309 {
2310 atend = TRUE;
2311 validate_virtcol();
2312 if (width1 <= 0)
2313 curwin->w_curswant = 0;
2314 else
2315 {
2316 curwin->w_curswant = width1 - 1;
2317 if (curwin->w_virtcol > curwin->w_curswant)
2318 curwin->w_curswant += ((curwin->w_virtcol
2319 - curwin->w_curswant - 1) / width2 + 1) * width2;
2320 }
2321 }
2322 else
2323 {
2324 if (linelen > width1)
2325 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2326 else
2327 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002328 if (curwin->w_curswant >= (colnr_T)n)
2329 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
2332 while (dist--)
2333 {
2334 if (dir == BACKWARD)
2335 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002336 if ((long)curwin->w_curswant >= width1
2337#ifdef FEAT_FOLDING
2338 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2339#endif
2340 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002341 // Move back within the line. This can give a negative value
2342 // for w_curswant if width1 < width2 (with cpoptions+=n),
2343 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 curwin->w_curswant -= width2;
2345 else
2346 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // Move to the start of a closed fold. Don't do that when
2350 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (!(fdo_flags & FDO_ALL))
2352 (void)hasFolding(curwin->w_cursor.lnum,
2353 &curwin->w_cursor.lnum, NULL);
2354#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002355 if (curwin->w_cursor.lnum == 1)
2356 {
2357 retval = FAIL;
2358 break;
2359 }
2360 --curwin->w_cursor.lnum;
2361
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002362 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (linelen > width1)
2364 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2365 + 1) * width2;
2366 }
2367 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
2370 if (linelen > width1)
2371 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2372 else
2373 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002374 if (curwin->w_curswant + width2 < (colnr_T)n
2375#ifdef FEAT_FOLDING
2376 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2377#endif
2378 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002379 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 curwin->w_curswant += width2;
2381 else
2382 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2387 &curwin->w_cursor.lnum);
2388#endif
2389 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2390 {
2391 retval = FAIL;
2392 break;
2393 }
2394 curwin->w_cursor.lnum++;
2395 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002396 // Check if the cursor has moved below the number display
2397 // when width1 < width2 (with cpoptions+=n). Subtract width2
2398 // to get a negative value for w_curswant, which will get
2399 // clipped to column 0.
2400 if (curwin->w_curswant >= width1)
2401 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002402 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 }
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002408 if (virtual_active() && atend)
2409 coladvance(MAXCOL);
2410 else
2411 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2414 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002415 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002416 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002417
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002418 // Check for landing on a character that got split at the end of the
2419 // last line. We want to advance a screenline, not end up in the same
2420 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002422 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002423#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002424 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2425 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002426#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002427
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002428 c = (*mb_ptr2char)(ml_get_cursor());
2429 if (dir == FORWARD && virtcol < curwin->w_curswant
2430 && (curwin->w_curswant <= (colnr_T)width1)
2431 && !vim_isprintc(c) && c > 255)
2432 oneright();
2433
Bram Moolenaar773b1582014-08-29 14:20:51 +02002434 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 && (curwin->w_curswant < (colnr_T)width1
2436 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2437 : ((curwin->w_curswant - width1) % width2
2438 > (colnr_T)width2 / 2)))
2439 --curwin->w_cursor.col;
2440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002443 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002444 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 return retval;
2447}
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449/*
2450 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2451 * cap->arg must be TRUE for CTRL-E.
2452 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002453 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002454nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456 if (!checkclearop(cap->oap))
2457 scroll_redraw(cap->arg, cap->count1);
2458}
2459
2460/*
2461 * Scroll "count" lines up or down, and redraw.
2462 */
2463 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002464scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002467 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#ifdef FEAT_DIFF
2469 int prev_topfill = curwin->w_topfill;
2470#endif
2471 linenr_T prev_lnum = curwin->w_cursor.lnum;
2472
2473 if (up)
2474 scrollup(count, TRUE);
2475 else
2476 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002477 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002479 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2480 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 cursor_correct();
2482 check_cursor_moved(curwin);
2483 curwin->w_valid |= VALID_TOPLINE;
2484
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002485 // If moved back to where we were, at least move the cursor, otherwise
2486 // we get stuck at one position. Don't move the cursor up if the
2487 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002489 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490#ifdef FEAT_DIFF
2491 && curwin->w_topfill == prev_topfill
2492#endif
2493 )
2494 {
2495 if (up)
2496 {
2497 if (curwin->w_cursor.lnum > prev_lnum
2498 || cursor_down(1L, FALSE) == FAIL)
2499 break;
2500 }
2501 else
2502 {
2503 if (curwin->w_cursor.lnum < prev_lnum
2504 || prev_topline == 1L
2505 || cursor_up(1L, FALSE) == FAIL)
2506 break;
2507 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002508 // Mark w_topline as valid, otherwise the screen jumps back at the
2509 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 check_cursor_moved(curwin);
2511 curwin->w_valid |= VALID_TOPLINE;
2512 }
2513 }
2514 if (curwin->w_cursor.lnum != prev_lnum)
2515 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002516 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517}
2518
2519/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002520 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2521 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2522 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002523 */
2524 static int
2525nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2526{
2527 int nchar = *nchar_arg;
2528 long n;
2529
2530 // "z123{nchar}": edit the count before obtaining {nchar}
2531 if (checkclearop(cap->oap))
2532 return FALSE;
2533 n = nchar - '0';
2534
2535 for (;;)
2536 {
2537#ifdef USE_ON_FLY_SCROLL
2538 dont_scroll = TRUE; // disallow scrolling here
2539#endif
2540 ++no_mapping;
2541 ++allow_keys; // no mapping for nchar, but allow key codes
2542 nchar = plain_vgetc();
2543 LANGMAP_ADJUST(nchar, TRUE);
2544 --no_mapping;
2545 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002546 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002547
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002548 if (nchar == K_DEL || nchar == K_KDEL)
2549 n /= 10;
2550 else if (VIM_ISDIGIT(nchar))
2551 n = n * 10 + (nchar - '0');
2552 else if (nchar == CAR)
2553 {
2554#ifdef FEAT_GUI
2555 need_mouse_correct = TRUE;
2556#endif
2557 win_setheight((int)n);
2558 break;
2559 }
2560 else if (nchar == 'l'
2561 || nchar == 'h'
2562 || nchar == K_LEFT
2563 || nchar == K_RIGHT)
2564 {
2565 cap->count1 = n ? n * cap->count1 : cap->count1;
2566 *nchar_arg = nchar;
2567 return TRUE;
2568 }
2569 else
2570 {
2571 clearopbeep(cap->oap);
2572 break;
2573 }
2574 }
2575 cap->oap->op_type = OP_NOP;
2576 return FALSE;
2577}
2578
2579#ifdef FEAT_SPELL
2580/*
2581 * "zug" and "zuw": undo "zg" and "zw"
2582 * "zg": add good word to word list
2583 * "zw": add wrong word to word list
2584 * "zG": add good word to temp word list
2585 * "zW": add wrong word to temp word list
2586 */
2587 static int
2588nv_zg_zw(cmdarg_T *cap, int nchar)
2589{
2590 char_u *ptr = NULL;
2591 int len;
2592 int undo = FALSE;
2593
2594 if (nchar == 'u')
2595 {
2596 ++no_mapping;
2597 ++allow_keys; // no mapping for nchar, but allow key codes
2598 nchar = plain_vgetc();
2599 LANGMAP_ADJUST(nchar, TRUE);
2600 --no_mapping;
2601 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002602 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002603
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002604 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2605 {
2606 clearopbeep(cap->oap);
2607 return OK;
2608 }
2609 undo = TRUE;
2610 }
2611
2612 if (checkclearop(cap->oap))
2613 return OK;
2614 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2615 return FAIL;
2616 if (ptr == NULL)
2617 {
2618 pos_T pos = curwin->w_cursor;
2619
2620 // Find bad word under the cursor. When 'spell' is
2621 // off this fails and find_ident_under_cursor() is
2622 // used below.
2623 emsg_off++;
2624 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2625 emsg_off--;
2626 if (len != 0 && curwin->w_cursor.col <= pos.col)
2627 ptr = ml_get_pos(&curwin->w_cursor);
2628 curwin->w_cursor = pos;
2629 }
2630
2631 if (ptr == NULL
2632 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2633 return FAIL;
2634 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2635 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2636 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2637
2638 return OK;
2639}
2640#endif
2641
2642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 * Commands that start with "z".
2644 */
2645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002646nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
2648 long n;
2649 colnr_T col;
2650 int nchar = cap->nchar;
2651#ifdef FEAT_FOLDING
2652 long old_fdl = curwin->w_p_fdl;
2653 int old_fen = curwin->w_p_fen;
2654#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002655 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002657 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (
2661#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002662 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2663 // and "zC" only in Visual mode. "zj" and "zk" are motion
2664 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 cap->nchar != 'f' && cap->nchar != 'F'
2666 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2667 && cap->nchar != 'j' && cap->nchar != 'k'
2668 &&
2669#endif
2670 checkclearop(cap->oap))
2671 return;
2672
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002673 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2674 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2676 && cap->count0
2677 && cap->count0 != curwin->w_cursor.lnum)
2678 {
2679 setpcmark();
2680 if (cap->count0 > curbuf->b_ml.ml_line_count)
2681 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2682 else
2683 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002684 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686
2687 switch (nchar)
2688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 case '+':
2691 if (cap->count0 == 0)
2692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // No count given: put cursor at the line below screen
2694 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2696 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2697 else
2698 curwin->w_cursor.lnum = curwin->w_botline;
2699 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002700 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 case NL:
2702 case CAR:
2703 case K_KENTER:
2704 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
2707 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002708 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002709 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 break;
2711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002712 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 case 'z': scroll_cursor_halfway(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002717 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002718 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 break;
2720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // "z^", "z-" and "zb": put cursor at bottom of screen
2722 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2723 // when <count> is at bottom of window, and puts that one at
2724 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (cap->count0 != 0)
2726 {
2727 scroll_cursor_bot(0, TRUE);
2728 curwin->w_cursor.lnum = curwin->w_topline;
2729 }
2730 else if (curwin->w_topline == 1)
2731 curwin->w_cursor.lnum = 1;
2732 else
2733 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 case '-':
2736 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002740 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002741 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 break;
2743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002746 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 case 'h':
2751 case K_LEFT:
2752 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002753 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2754 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 break;
2756
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002757 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002758 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002761 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 case 'l':
2763 case K_RIGHT:
2764 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002765 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 break;
2767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 case 's': if (!curwin->w_p_wrap)
2770 {
2771#ifdef FEAT_FOLDING
2772 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
2775#endif
2776 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002777 if ((long)col > siso)
2778 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
2780 col = 0;
2781 if (curwin->w_leftcol != col)
2782 {
2783 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002784 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 }
2787 break;
2788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002789 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 case 'e': if (!curwin->w_p_wrap)
2791 {
2792#ifdef FEAT_FOLDING
2793 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
2796#endif
2797 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002798 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002799 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 col = 0;
2801 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002802 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (curwin->w_leftcol != col)
2804 {
2805 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002806 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808 }
2809 break;
2810
Christian Brabandt2fa93842021-05-30 22:17:25 +02002811 // "zp", "zP" in block mode put without addind trailing spaces
2812 case 'P':
2813 case 'p': nv_put(cap);
2814 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002815 // "zy" Yank without trailing spaces
2816 case 'y': nv_operator(cap);
2817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002819 // "zF": create fold command
2820 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 case 'F':
2822 case 'f': if (foldManualAllowed(TRUE))
2823 {
2824 cap->nchar = 'f';
2825 nv_operator(cap);
2826 curwin->w_p_fen = TRUE;
2827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2830 {
2831 nv_operator(cap);
2832 finish_op = TRUE;
2833 }
2834 }
2835 else
2836 clearopbeep(cap->oap);
2837 break;
2838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // "zd": delete fold at cursor
2840 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 case 'd':
2842 case 'D': if (foldManualAllowed(FALSE))
2843 {
2844 if (VIsual_active)
2845 nv_operator(cap);
2846 else
2847 deleteFold(curwin->w_cursor.lnum,
2848 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2849 }
2850 break;
2851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 case 'E': if (foldmethodIsManual(curwin))
2854 {
2855 clearFolding(curwin);
2856 changed_window_setting();
2857 }
2858 else if (foldmethodIsMarker(curwin))
2859 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2860 TRUE, FALSE);
2861 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002862 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 break;
2864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 case 'n': curwin->w_p_fen = FALSE;
2867 break;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case 'N': curwin->w_p_fen = TRUE;
2871 break;
2872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2879 openFold(curwin->w_cursor.lnum, cap->count1);
2880 else
2881 {
2882 closeFold(curwin->w_cursor.lnum, cap->count1);
2883 curwin->w_p_fen = TRUE;
2884 }
2885 break;
2886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2889 openFoldRecurse(curwin->w_cursor.lnum);
2890 else
2891 {
2892 closeFoldRecurse(curwin->w_cursor.lnum);
2893 curwin->w_p_fen = TRUE;
2894 }
2895 break;
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case 'o': if (VIsual_active)
2899 nv_operator(cap);
2900 else
2901 openFold(curwin->w_cursor.lnum, cap->count1);
2902 break;
2903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 case 'O': if (VIsual_active)
2906 nv_operator(cap);
2907 else
2908 openFoldRecurse(curwin->w_cursor.lnum);
2909 break;
2910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 case 'c': if (VIsual_active)
2913 nv_operator(cap);
2914 else
2915 closeFold(curwin->w_cursor.lnum, cap->count1);
2916 curwin->w_p_fen = TRUE;
2917 break;
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 case 'C': if (VIsual_active)
2921 nv_operator(cap);
2922 else
2923 closeFoldRecurse(curwin->w_cursor.lnum);
2924 curwin->w_p_fen = TRUE;
2925 break;
2926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 case 'v': foldOpenCursor();
2929 break;
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 curwin->w_foldinvalid = TRUE; // recompute folds
2934 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 foldOpenCursor();
2936 break;
2937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 curwin->w_foldinvalid = TRUE; // recompute folds
2941 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 break;
2943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002946 {
2947 curwin->w_p_fdl -= cap->count1;
2948 if (curwin->w_p_fdl < 0)
2949 curwin->w_p_fdl = 0;
2950 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 curwin->w_p_fen = TRUE;
2953 break;
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 curwin->w_p_fen = TRUE;
2959 break;
2960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002962 case 'r': curwin->w_p_fdl += cap->count1;
2963 {
2964 int d = getDeepestNesting();
2965
2966 if (curwin->w_p_fdl >= d)
2967 curwin->w_p_fdl = d;
2968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 break;
2970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 break;
2975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 case 'j': // "zj" move to next fold downwards
2977 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2979 cap->count1) == FAIL)
2980 clearopbeep(cap->oap);
2981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002985#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 case 'g': // "zg": add good word to word list
2988 case 'w': // "zw": add wrong word to word list
2989 case 'G': // "zG": add good word to temp word list
2990 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002991 if (nv_zg_zw(cap, nchar) == FAIL)
2992 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002993 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002996 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002997 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002999#endif
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 default: clearopbeep(cap->oap);
3002 }
3003
3004#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (old_fen != curwin->w_p_fen)
3007 {
3008# ifdef FEAT_DIFF
3009 win_T *wp;
3010
3011 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3012 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 FOR_ALL_WINDOWS(wp)
3015 {
3016 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3017 {
3018 wp->w_p_fen = curwin->w_p_fen;
3019 changed_window_setting_win(wp);
3020 }
3021 }
3022 }
3023# endif
3024 changed_window_setting();
3025 }
3026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (old_fdl != curwin->w_p_fdl)
3029 newFoldLevel();
3030#endif
3031}
3032
3033#ifdef FEAT_GUI
3034/*
3035 * Vertical scrollbar movement.
3036 */
3037 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003038nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 if (cap->oap->op_type != OP_NOP)
3041 clearopbeep(cap->oap);
3042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 gui_do_scroll();
3045}
3046
3047/*
3048 * Horizontal scrollbar movement.
3049 */
3050 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 if (cap->oap->op_type != OP_NOP)
3054 clearopbeep(cap->oap);
3055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003057 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059#endif
3060
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003061#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003062/*
3063 * Click in GUI tab.
3064 */
3065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003066nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003067{
3068 if (cap->oap->op_type != OP_NOP)
3069 clearopbeep(cap->oap);
3070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003072 goto_tabpage(current_tab);
3073}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003074
3075/*
3076 * Selected item in tab line menu.
3077 */
3078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003079nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003080{
3081 if (cap->oap->op_type != OP_NOP)
3082 clearopbeep(cap->oap);
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003085 handle_tabmenu();
3086}
3087
3088/*
3089 * Handle selecting an item of the GUI tab line menu.
3090 * Used in Normal and Insert mode.
3091 */
3092 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003093handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003094{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003095 switch (current_tabmenu)
3096 {
3097 case TABLINE_MENU_CLOSE:
3098 if (current_tab == 0)
3099 do_cmdline_cmd((char_u *)"tabclose");
3100 else
3101 {
3102 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3103 current_tab);
3104 do_cmdline_cmd(IObuff);
3105 }
3106 break;
3107
3108 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003109 if (current_tab == 0)
3110 do_cmdline_cmd((char_u *)"$tabnew");
3111 else
3112 {
3113 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3114 current_tab - 1);
3115 do_cmdline_cmd(IObuff);
3116 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003117 break;
3118
3119 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003120 if (current_tab == 0)
3121 do_cmdline_cmd((char_u *)"browse $tabnew");
3122 else
3123 {
3124 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3125 current_tab - 1);
3126 do_cmdline_cmd(IObuff);
3127 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003128 break;
3129 }
3130}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003131#endif
3132
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133/*
3134 * "Q" command.
3135 */
3136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003137nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003139 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003141 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003142 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 do_exmode(FALSE);
3144}
3145
3146/*
3147 * Handle a ":" command.
3148 */
3149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003150nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003152 int old_p_im;
3153 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003154 int is_cmdkey = cap->cmdchar == K_COMMAND
3155 || cap->cmdchar == K_SCRIPT_COMMAND;
3156 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
Bram Moolenaar957cf672020-11-12 14:21:06 +01003158 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 nv_operator(cap);
3160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
3162 if (cap->oap->op_type != OP_NOP)
3163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 cap->oap->motion_type = MCHAR;
3166 cap->oap->inclusive = FALSE;
3167 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003168 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003170 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 stuffcharReadbuff('.');
3172 if (cap->count0 > 1)
3173 {
3174 stuffReadbuff((char_u *)",.+");
3175 stuffnumReadbuff((long)cap->count0 - 1L);
3176 }
3177 }
3178
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003179 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (KeyTyped)
3181 compute_cmdrow();
3182
3183 old_p_im = p_im;
3184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003186 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3187 if (is_cmdkey)
3188 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3189 else
3190 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (p_im != old_p_im)
3194 {
3195 if (p_im)
3196 restart_edit = 'i';
3197 else
3198 restart_edit = 0;
3199 }
3200
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003201 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003202 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003203 clearop(cap->oap);
3204 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3206 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003207 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3208 || did_emsg
3209 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 clearopbeep(cap->oap);
3212 }
3213}
3214
3215/*
3216 * Handle CTRL-G command.
3217 */
3218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003219nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
3223 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003224 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 showmode();
3226 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003227 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003228 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 fileinfo((int)cap->count0, FALSE, TRUE);
3230}
3231
3232/*
3233 * Handle CTRL-H <Backspace> command.
3234 */
3235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (VIsual_active && VIsual_select)
3239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 v_visop(cap);
3242 }
3243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 nv_left(cap);
3245}
3246
3247/*
3248 * CTRL-L: clear screen and redraw.
3249 */
3250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003251nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 if (!checkclearop(cap->oap))
3254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003256 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003257 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003258# ifdef FEAT_RELTIME
3259 {
3260 win_T *wp;
3261
3262 FOR_ALL_WINDOWS(wp)
3263 wp->w_s->b_syn_slow = FALSE;
3264 }
3265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003267 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003268#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3269# ifdef VIMDLL
3270 if (!gui.in_use)
3271# endif
3272 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275}
3276
3277/*
3278 * CTRL-O: In Select mode: switch to Visual mode for one command.
3279 * Otherwise: Go to older pcmark.
3280 */
3281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003282nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (VIsual_active && VIsual_select)
3285 {
3286 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003287 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003289 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 cap->count1 = -cap->count1;
3294 nv_pcmark(cap);
3295 }
3296}
3297
3298/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003299 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3300 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
3302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003303nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 if (!checkclearopq(cap->oap))
3306 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3307 GETF_SETMARK|GETF_ALT, FALSE);
3308}
3309
3310/*
3311 * "Z" commands.
3312 */
3313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 if (!checkclearopq(cap->oap))
3317 {
3318 switch (cap->nchar)
3319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003320 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 case 'Z': do_cmdline_cmd((char_u *)"x");
3322 break;
3323
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003324 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 case 'Q': do_cmdline_cmd((char_u *)"q!");
3326 break;
3327
3328 default: clearopbeep(cap->oap);
3329 }
3330 }
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
3334 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3335 */
3336 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003337do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 oparg_T oa;
3340 cmdarg_T ca;
3341
3342 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003343 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 ca.oap = &oa;
3345 ca.cmdchar = c1;
3346 ca.nchar = c2;
3347 nv_ident(&ca);
3348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003351 * 'K' normal-mode command. Get the command to lookup the keyword under the
3352 * cursor.
3353 */
3354 static int
3355nv_K_getcmd(
3356 cmdarg_T *cap,
3357 char_u *kp,
3358 int kp_help,
3359 int kp_ex,
3360 char_u **ptr_arg,
3361 int n,
3362 char_u *buf,
3363 unsigned buflen)
3364{
3365 char_u *ptr = *ptr_arg;
3366 int isman;
3367 int isman_s;
3368
3369 if (kp_help)
3370 {
3371 // in the help buffer
3372 STRCPY(buf, "he! ");
3373 return n;
3374 }
3375
3376 if (kp_ex)
3377 {
3378 // 'keywordprog' is an ex command
3379 if (cap->count0 != 0)
3380 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3381 else
3382 STRCPY(buf, kp);
3383 STRCAT(buf, " ");
3384 return n;
3385 }
3386
3387 // An external command will probably use an argument starting
3388 // with "-" as an option. To avoid trouble we skip the "-".
3389 while (*ptr == '-' && n > 0)
3390 {
3391 ++ptr;
3392 --n;
3393 }
3394 if (n == 0)
3395 {
3396 // found dashes only
3397 emsg(_(e_no_identifier_under_cursor));
3398 vim_free(buf);
3399 *ptr_arg = ptr;
3400 return 0;
3401 }
3402
3403 // When a count is given, turn it into a range. Is this
3404 // really what we want?
3405 isman = (STRCMP(kp, "man") == 0);
3406 isman_s = (STRCMP(kp, "man -s") == 0);
3407 if (cap->count0 != 0 && !(isman || isman_s))
3408 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3409
3410 STRCAT(buf, "! ");
3411 if (cap->count0 == 0 && isman_s)
3412 STRCAT(buf, "man");
3413 else
3414 STRCAT(buf, kp);
3415 STRCAT(buf, " ");
3416 if (cap->count0 != 0 && (isman || isman_s))
3417 {
3418 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3419 STRCAT(buf, " ");
3420 }
3421
3422 *ptr_arg = ptr;
3423 return n;
3424}
3425
3426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * Handle the commands that use the word under the cursor.
3428 * [g] CTRL-] :ta to current identifier
3429 * [g] 'K' run program for current identifier
3430 * [g] '*' / to current identifier or string
3431 * [g] '#' ? to current identifier or string
3432 * g ']' :tselect for current identifier
3433 */
3434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003435nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 char_u *ptr = NULL;
3438 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003439 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003440 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 char_u *kp; // value of 'keywordprg'
3443 int kp_help; // 'keywordprg' is ":he"
3444 int kp_ex; // 'keywordprg' starts with ":"
3445 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003448 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003451 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 cmdchar = cap->nchar;
3454 g_cmd = TRUE;
3455 }
3456 else
3457 {
3458 cmdchar = cap->cmdchar;
3459 g_cmd = FALSE;
3460 }
3461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 cmdchar = '#';
3464
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003465 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3469 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (checkclearopq(cap->oap))
3471 return;
3472 }
3473
3474 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3475 (cmdchar == '*' || cmdchar == '#')
3476 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3477 {
3478 clearop(cap->oap);
3479 return;
3480 }
3481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // Allocate buffer to put the command in. Inserting backslashes can
3483 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3484 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3486 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3487 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003488 if (kp_help && *skipwhite(ptr) == NUL)
3489 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003490 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003491 return;
3492 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003493 kp_ex = (*kp == ':');
3494 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3495 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (buf == NULL)
3497 return;
3498 buf[0] = NUL;
3499
3500 switch (cmdchar)
3501 {
3502 case '*':
3503 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003504 // Put cursor at start of word, makes search skip the word
3505 // under the cursor.
3506 // Call setpcmark() first, so "*``" puts the cursor back where
3507 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 setpcmark();
3509 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3510
3511 if (!g_cmd && vim_iswordp(ptr))
3512 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003513 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
3515
3516 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003517 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3518 if (n == 0)
3519 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 break;
3521
3522 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003523 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#ifdef FEAT_CSCOPE
3525 if (p_cst)
3526 STRCPY(buf, "cstag ");
3527 else
3528#endif
3529 STRCPY(buf, "ts ");
3530 break;
3531
3532 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003533 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (curbuf->b_help)
3535 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003537 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 if (g_cmd)
3539 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003540 else if (cap->count0 == 0)
3541 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003542 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003543 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003547 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003548 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003550 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003551 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003552 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003553 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003554 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003556 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003557 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003560 vim_free(buf);
3561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003563 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003564 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003565 {
3566 vim_free(buf);
3567 vim_free(p);
3568 return;
3569 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003570 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 STRCAT(buf, p);
3572 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003574 else
3575 {
3576 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003577 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003579 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003580 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003581 {
3582 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003584 aux_ptr = (char_u *)"";
3585 else
3586 aux_ptr = (char_u *)"\\|\"\n[";
3587 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003588 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 aux_ptr = (char_u *)"\\|\"\n*?[";
3590
3591 p = buf + STRLEN(buf);
3592 while (n-- > 0)
3593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003595 if (vim_strchr(aux_ptr, *ptr) != NULL)
3596 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // When current byte is a part of multibyte character, copy all
3598 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003599 if (has_mbyte)
3600 {
3601 int i;
3602 int len = (*mb_ptr2len)(ptr) - 1;
3603
3604 for (i = 0; i < len && n >= 1; ++i, --n)
3605 *p++ = *ptr++;
3606 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 *p++ = *ptr++;
3608 }
3609 *p = NUL;
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003612 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (cmdchar == '*' || cmdchar == '#')
3614 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003615 if (!g_cmd && (has_mbyte
3616 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3617 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003619
3620 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003621 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003623
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003624 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003627 {
3628 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003630 g_tag_at_cursor = FALSE;
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 vim_free(buf);
3634}
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636/*
3637 * Get visually selected text, within one line only.
3638 * Returns FAIL if more than one line selected.
3639 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003641get_visual_text(
3642 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 char_u **pp, // return: start of selected text
3644 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 if (VIsual_mode != 'V')
3647 unadjust_for_sel();
3648 if (VIsual.lnum != curwin->w_cursor.lnum)
3649 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003650 if (cap != NULL)
3651 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return FAIL;
3653 }
3654 if (VIsual_mode == 'V')
3655 {
3656 *pp = ml_get_curline();
3657 *lenp = (int)STRLEN(*pp);
3658 }
3659 else
3660 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003661 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 *pp = ml_get_pos(&curwin->w_cursor);
3664 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3665 }
3666 else
3667 {
3668 *pp = ml_get_pos(&VIsual);
3669 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3670 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003671 if (**pp == NUL)
3672 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003673 if (*lenp > 0)
3674 {
3675 if (has_mbyte)
3676 // Correct the length to include all bytes of the last
3677 // character.
3678 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3679 else if ((*pp)[*lenp - 1] == NUL)
3680 // Do not include a trailing NUL.
3681 *lenp -= 1;
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 reset_VIsual_and_resel();
3685 return OK;
3686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
3688/*
3689 * CTRL-T: backwards in tag stack
3690 */
3691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003692nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 if (!checkclearopq(cap->oap))
3695 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3696}
3697
3698/*
3699 * Handle scrolling command 'H', 'L' and 'M'.
3700 */
3701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003702nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 int used = 0;
3705 long n;
3706#ifdef FEAT_FOLDING
3707 linenr_T lnum;
3708#endif
3709 int half;
3710
3711 cap->oap->motion_type = MLINE;
3712 setpcmark();
3713
3714 if (cap->cmdchar == 'L')
3715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 curwin->w_cursor.lnum = curwin->w_botline - 1;
3718 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3719 curwin->w_cursor.lnum = 1;
3720 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003721 {
3722#ifdef FEAT_FOLDING
3723 if (hasAnyFolding(curwin))
3724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003725 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003726 for (n = cap->count1 - 1; n > 0
3727 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3728 {
3729 (void)hasFolding(curwin->w_cursor.lnum,
3730 &curwin->w_cursor.lnum, NULL);
3731 --curwin->w_cursor.lnum;
3732 }
3733 }
3734 else
3735#endif
3736 curwin->w_cursor.lnum -= cap->count1 - 1;
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739 else
3740 {
3741 if (cap->cmdchar == 'M')
3742 {
3743#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003744 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 used -= diff_check_fill(curwin, curwin->w_topline)
3746 - curwin->w_topfill;
3747#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003748 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3750 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3751 {
3752#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003753 // Count half he number of filler lines to be "below this
3754 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3756 + n) / 2 >= half)
3757 {
3758 --n;
3759 break;
3760 }
3761#endif
3762 used += plines(curwin->w_topline + n);
3763 if (used >= half)
3764 break;
3765#ifdef FEAT_FOLDING
3766 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3767 n = lnum - curwin->w_topline;
3768#endif
3769 }
3770 if (n > 0 && used > curwin->w_height)
3771 --n;
3772 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003776#ifdef FEAT_FOLDING
3777 if (hasAnyFolding(curwin))
3778 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003780 lnum = curwin->w_topline;
3781 while (n-- > 0 && lnum < curwin->w_botline - 1)
3782 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003783 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003784 ++lnum;
3785 }
3786 n = lnum - curwin->w_topline;
3787 }
3788#endif
3789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 curwin->w_cursor.lnum = curwin->w_topline + n;
3791 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3792 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3793 }
3794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003795 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003796 if (cap->oap->op_type == OP_NOP)
3797 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 beginline(BL_SOL | BL_FIX);
3799}
3800
3801/*
3802 * Cursor right commands.
3803 */
3804 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003805nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003808 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003810 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3811 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003812 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003813 if (mod_mask & MOD_MASK_CTRL)
3814 cap->arg = TRUE;
3815 nv_wordcmd(cap);
3816 return;
3817 }
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 cap->oap->motion_type = MCHAR;
3820 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003821 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003823 // In virtual edit mode, there's no such thing as "past_line", as lines
3824 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003826 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 for (n = cap->count1; n > 0; --n)
3829 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003830 if ((!past_line && oneright() == FAIL)
3831 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003832 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003834 // <Space> wraps to next line if 'whichwrap' has 's'.
3835 // 'l' wraps to next line if 'whichwrap' has 'l'.
3836 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if ( ((cap->cmdchar == ' '
3838 && vim_strchr(p_ww, 's') != NULL)
3839 || (cap->cmdchar == 'l'
3840 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003841 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 && vim_strchr(p_ww, '>') != NULL))
3843 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003845 // When deleting we also count the NL as a character.
3846 // Set cap->oap->inclusive when last char in the line is
3847 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003848 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003850 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 cap->oap->inclusive = TRUE;
3852 else
3853 {
3854 ++curwin->w_cursor.lnum;
3855 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 curwin->w_set_curswant = TRUE;
3858 cap->oap->inclusive = FALSE;
3859 }
3860 continue;
3861 }
3862 if (cap->oap->op_type == OP_NOP)
3863 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003864 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (n == cap->count1)
3866 beep_flush();
3867 }
3868 else
3869 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003870 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 cap->oap->inclusive = TRUE;
3872 }
3873 break;
3874 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003875 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (virtual_active())
3879 oneright();
3880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003883 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 ++curwin->w_cursor.col;
3886 }
3887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889#ifdef FEAT_FOLDING
3890 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3891 && cap->oap->op_type == OP_NOP)
3892 foldOpenCursor();
3893#endif
3894}
3895
3896/*
3897 * Cursor left commands.
3898 *
3899 * Returns TRUE when operator end should not be adjusted.
3900 */
3901 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003902nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 long n;
3905
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003906 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003908 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003909 if (mod_mask & MOD_MASK_CTRL)
3910 cap->arg = 1;
3911 nv_bck_word(cap);
3912 return;
3913 }
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 cap->oap->motion_type = MCHAR;
3916 cap->oap->inclusive = FALSE;
3917 for (n = cap->count1; n > 0; --n)
3918 {
3919 if (oneleft() == FAIL)
3920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003921 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3922 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3923 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if ( (((cap->cmdchar == K_BS
3925 || cap->cmdchar == Ctrl_H)
3926 && vim_strchr(p_ww, 'b') != NULL)
3927 || (cap->cmdchar == 'h'
3928 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003929 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 && vim_strchr(p_ww, '<') != NULL))
3931 && curwin->w_cursor.lnum > 1)
3932 {
3933 --(curwin->w_cursor.lnum);
3934 coladvance((colnr_T)MAXCOL);
3935 curwin->w_set_curswant = TRUE;
3936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 // When the NL before the first char has to be deleted we
3938 // put the cursor on the NUL after the previous line.
3939 // This is a very special case, be careful!
3940 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if ( (cap->oap->op_type == OP_DELETE
3942 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003943 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003945 char_u *cp = ml_get_cursor();
3946
3947 if (*cp != NUL)
3948 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003949 if (has_mbyte)
3950 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3951 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003952 ++curwin->w_cursor.col;
3953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 cap->retval |= CA_NO_ADJ_OP_END;
3955 }
3956 continue;
3957 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003958 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3960 beep_flush();
3961 break;
3962 }
3963 }
3964#ifdef FEAT_FOLDING
3965 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3966 && cap->oap->op_type == OP_NOP)
3967 foldOpenCursor();
3968#endif
3969}
3970
3971/*
3972 * Cursor up commands.
3973 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3974 */
3975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003976nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003978 if (mod_mask & MOD_MASK_SHIFT)
3979 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003980 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 cap->arg = BACKWARD;
3982 nv_page(cap);
3983 }
3984 else
3985 {
3986 cap->oap->motion_type = MLINE;
3987 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3988 clearopbeep(cap->oap);
3989 else if (cap->arg)
3990 beginline(BL_WHITE | BL_FIX);
3991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993
3994/*
3995 * Cursor down commands.
3996 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3997 */
3998 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003999nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 if (mod_mask & MOD_MASK_SHIFT)
4002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004 cap->arg = FORWARD;
4005 nv_page(cap);
4006 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004007#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004008 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004009 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4010 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004015 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 cmdwin_result = CAR;
4017 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004018#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004020 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4021 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4022 {
4023 invoke_prompt_callback();
4024 if (restart_edit == 0)
4025 restart_edit = 'a';
4026 }
4027 else
4028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 cap->oap->motion_type = MLINE;
4031 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4032 clearopbeep(cap->oap);
4033 else if (cap->arg)
4034 beginline(BL_WHITE | BL_FIX);
4035 }
4036 }
4037}
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039/*
4040 * Grab the file name under the cursor and edit it.
4041 */
4042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004043nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004046 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004048 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004050
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004051#ifdef FEAT_PROP_POPUP
4052 if (ERROR_IF_TERM_POPUP_WINDOW)
4053 return;
4054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004056 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 if (ptr != NULL)
4059 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004060 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004061 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004062 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004064 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004065 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004066 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004067 {
4068 curwin->w_cursor.lnum = lnum;
4069 check_cursor_lnum();
4070 beginline(BL_SOL | BL_FIX);
4071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 vim_free(ptr);
4073 }
4074 else
4075 clearop(cap->oap);
4076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078/*
4079 * <End> command: to end of current line or last line.
4080 */
4081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004084 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004086 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004088 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090 nv_dollar(cap);
4091}
4092
4093/*
4094 * Handle the "$" command.
4095 */
4096 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004097nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 cap->oap->motion_type = MCHAR;
4100 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // In virtual mode when off the edge of a line and an operator
4102 // is pending (whew!) keep the cursor where it is.
4103 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (!virtual_active() || gchar_cursor() != NUL
4105 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (cursor_down((long)(cap->count1 - 1),
4108 cap->oap->op_type == OP_NOP) == FAIL)
4109 clearopbeep(cap->oap);
4110#ifdef FEAT_FOLDING
4111 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4112 foldOpenCursor();
4113#endif
4114}
4115
4116/*
4117 * Implementation of '?' and '/' commands.
4118 * If cap->arg is TRUE don't set PC mark.
4119 */
4120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004124 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004128 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 cap->cmdchar = 'g';
4130 cap->nchar = '?';
4131 nv_operator(cap);
4132 return;
4133 }
4134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004135 // When using 'incsearch' the cursor may be moved to set a different search
4136 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004137 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 if (cap->searchbuf == NULL)
4140 {
4141 clearop(oap);
4142 return;
4143 }
4144
Bram Moolenaar46539112015-02-17 15:43:57 +01004145 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004146 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004147 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
LemonBoya4399382022-04-09 21:04:08 +01004150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151/*
4152 * Handle "N" and "n" commands.
4153 * cap->arg is SEARCH_REV for "N", 0 for "n".
4154 */
4155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004156nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004158 pos_T old = curwin->w_cursor;
4159 int wrapped = FALSE;
4160 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004161
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004162 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004164 // Avoid getting stuck on the current cursor position, which can
4165 // happen when an offset is given and the cursor is on the last char
4166 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004167 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004168 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004169 cap->count1 -= 1;
4170 }
LemonBoya4399382022-04-09 21:04:08 +01004171
4172#ifdef FEAT_SEARCH_EXTRA
4173 // Redraw the window to refresh the highlighted matches.
4174 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004175 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177}
4178
4179/*
4180 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4181 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004182 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004184 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185normal_search(
4186 cmdarg_T *cap,
4187 int dir,
4188 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004189 int opt, // extra flags for do_search()
4190 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004193 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004194#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004195 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 cap->oap->motion_type = MCHAR;
4199 cap->oap->inclusive = FALSE;
4200 cap->oap->use_reg_one = TRUE;
4201 curwin->w_set_curswant = TRUE;
4202
Bram Moolenaara80faa82020-04-12 19:37:17 +02004203 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004204 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004205 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4206 if (wrapped != NULL)
4207 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (i == 0)
4209 clearop(cap->oap);
4210 else
4211 {
4212 if (i == 2)
4213 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#ifdef FEAT_FOLDING
4216 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4217 foldOpenCursor();
4218#endif
4219 }
LemonBoya4399382022-04-09 21:04:08 +01004220#ifdef FEAT_SEARCH_EXTRA
4221 // Redraw the window to refresh the highlighted matches.
4222 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004223 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // "/$" will put the cursor after the end of the line, may need to
4227 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004229 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
4232/*
4233 * Character search commands.
4234 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4235 * ',' and FALSE for ';'.
4236 * cap->nchar is NUL for ',' and ';' (repeat the search)
4237 */
4238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004239nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
4241 int t_cmd;
4242
4243 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4244 t_cmd = TRUE;
4245 else
4246 t_cmd = FALSE;
4247
4248 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4250 clearopbeep(cap->oap);
4251 else
4252 {
4253 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004254 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4256 && (t_cmd || cap->oap->op_type != OP_NOP))
4257 {
4258 colnr_T scol, ecol;
4259
4260 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4261 curwin->w_cursor.coladd = ecol - scol;
4262 }
4263 else
4264 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266#ifdef FEAT_FOLDING
4267 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4268 foldOpenCursor();
4269#endif
4270 }
4271}
4272
4273/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004274 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4275 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4276 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4277 * "[m" or "]m" search for prev/next start of (Java) method.
4278 * "[M" or "]M" search for prev/next end of (Java) method.
4279 */
4280 static void
4281nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4282{
4283 pos_T new_pos = {0, 0, 0};
4284 pos_T *pos = NULL; // init for GCC
4285 pos_T prev_pos;
4286 long n;
4287 int findc;
4288 int c;
4289
4290 if (cap->nchar == '*')
4291 cap->nchar = '/';
4292 prev_pos.lnum = 0;
4293 if (cap->nchar == 'm' || cap->nchar == 'M')
4294 {
4295 if (cap->cmdchar == '[')
4296 findc = '{';
4297 else
4298 findc = '}';
4299 n = 9999;
4300 }
4301 else
4302 {
4303 findc = cap->nchar;
4304 n = cap->count1;
4305 }
4306 for ( ; n > 0; --n)
4307 {
4308 if ((pos = findmatchlimit(cap->oap, findc,
4309 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4310 {
4311 if (new_pos.lnum == 0) // nothing found
4312 {
4313 if (cap->nchar != 'm' && cap->nchar != 'M')
4314 clearopbeep(cap->oap);
4315 }
4316 else
4317 pos = &new_pos; // use last one found
4318 break;
4319 }
4320 prev_pos = new_pos;
4321 curwin->w_cursor = *pos;
4322 new_pos = *pos;
4323 }
4324 curwin->w_cursor = *old_pos;
4325
4326 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4327 // brought us to the match for "[m" and "]M" when inside a method.
4328 // Try finding the '{' or '}' we want to be at.
4329 // Also repeat for the given count.
4330 if (cap->nchar == 'm' || cap->nchar == 'M')
4331 {
4332 // norm is TRUE for "]M" and "[m"
4333 int norm = ((findc == '{') == (cap->nchar == 'm'));
4334
4335 n = cap->count1;
4336 // found a match: we were inside a method
4337 if (prev_pos.lnum != 0)
4338 {
4339 pos = &prev_pos;
4340 curwin->w_cursor = prev_pos;
4341 if (norm)
4342 --n;
4343 }
4344 else
4345 pos = NULL;
4346 while (n > 0)
4347 {
4348 for (;;)
4349 {
4350 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4351 {
4352 // if not found anything, that's an error
4353 if (pos == NULL)
4354 clearopbeep(cap->oap);
4355 n = 0;
4356 break;
4357 }
4358 c = gchar_cursor();
4359 if (c == '{' || c == '}')
4360 {
4361 // Must have found end/start of class: use it.
4362 // Or found the place to be at.
4363 if ((c == findc && norm) || (n == 1 && !norm))
4364 {
4365 new_pos = curwin->w_cursor;
4366 pos = &new_pos;
4367 n = 0;
4368 }
4369 // if no match found at all, we started outside of the
4370 // class and we're inside now. Just go on.
4371 else if (new_pos.lnum == 0)
4372 {
4373 new_pos = curwin->w_cursor;
4374 pos = &new_pos;
4375 }
4376 // found start/end of other method: go to match
4377 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004378 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4379 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004380 n = 0;
4381 else
4382 curwin->w_cursor = *pos;
4383 break;
4384 }
4385 }
4386 --n;
4387 }
4388 curwin->w_cursor = *old_pos;
4389 if (pos == NULL && new_pos.lnum != 0)
4390 clearopbeep(cap->oap);
4391 }
4392 if (pos != NULL)
4393 {
4394 setpcmark();
4395 curwin->w_cursor = *pos;
4396 curwin->w_set_curswant = TRUE;
4397#ifdef FEAT_FOLDING
4398 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4399 && cap->oap->op_type == OP_NOP)
4400 foldOpenCursor();
4401#endif
4402 }
4403}
4404
4405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * "[" and "]" commands.
4407 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4408 */
4409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004410nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004413 pos_T *pos = NULL; // init for GCC
4414 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 int flag;
4416 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 cap->oap->motion_type = MCHAR;
4419 cap->oap->inclusive = FALSE;
4420 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004421 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004423 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (cap->nchar == 'f')
4425 nv_gotofile(cap);
4426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004429 // Find the occurrence(s) of the identifier or define under cursor
4430 // in current and included files or jump to the first occurrence.
4431 //
4432 // search list jump
4433 // fwd bwd fwd bwd fwd bwd
4434 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4435 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004436 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 char_u *ptr;
4439 int len;
4440
4441 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4442 clearop(cap->oap);
4443 else
4444 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004445 // Make a copy, if the line was changed it will be freed.
4446 ptr = vim_strnsave(ptr, len);
4447 if (ptr == NULL)
4448 return;
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 find_pattern_in_path(ptr, 0, len, TRUE,
4451 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4452 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4453 cap->count1,
4454 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4455 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4456 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4457 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004458 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 curwin->w_set_curswant = TRUE;
4460 }
4461 }
4462 else
4463#endif
4464
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004465 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4466 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4467 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4468 // "[m" or "]m" search for prev/next start of (Java) method.
4469 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if ( (cap->cmdchar == '['
4471 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4472 || (cap->cmdchar == ']'
4473 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004474 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004476 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 else if (cap->nchar == '[' || cap->nchar == ']')
4478 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004479 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 flag = '{';
4481 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004482 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
4484 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004485 // Imitate strange Vi behaviour: When using "]]" with an operator
4486 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004487 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 (cap->oap->op_type != OP_NOP
4489 && cap->arg == FORWARD && flag == '{')))
4490 clearopbeep(cap->oap);
4491 else
4492 {
4493 if (cap->oap->op_type == OP_NOP)
4494 beginline(BL_WHITE | BL_FIX);
4495#ifdef FEAT_FOLDING
4496 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4497 foldOpenCursor();
4498#endif
4499 }
4500 }
4501
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004502 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 else if (cap->nchar == 'p' || cap->nchar == 'P')
4504 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004505 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004508 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else if (cap->nchar == '\'' || cap->nchar == '`')
4510 {
4511 pos = &curwin->w_cursor;
4512 for (n = cap->count1; n > 0; --n)
4513 {
4514 prev_pos = *pos;
4515 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4516 cap->nchar == '\'');
4517 if (pos == NULL)
4518 break;
4519 }
4520 if (pos == NULL)
4521 pos = &prev_pos;
4522 nv_cursormark(cap, cap->nchar == '\'', pos);
4523 }
4524
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004525 // [ or ] followed by a middle mouse click: put selected text with
4526 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004527 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 (void)do_mouse(cap->oap, cap->nchar,
4530 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4531 cap->count1, PUT_FIXINDENT);
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004535 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 else if (cap->nchar == 'z')
4537 {
4538 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4539 cap->count1) == FAIL)
4540 clearopbeep(cap->oap);
4541 }
4542#endif
4543
4544#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004545 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 else if (cap->nchar == 'c')
4547 {
4548 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4549 cap->count1) == FAIL)
4550 clearopbeep(cap->oap);
4551 }
4552#endif
4553
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004554#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004555 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004556 else if (cap->nchar == 's' || cap->nchar == 'S')
4557 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004558 setpcmark();
4559 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004560 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4561 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004562 {
4563 clearopbeep(cap->oap);
4564 break;
4565 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004566 else
4567 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004568# ifdef FEAT_FOLDING
4569 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4570 foldOpenCursor();
4571# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004572 }
4573#endif
4574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004575 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
4577 clearopbeep(cap->oap);
4578}
4579
4580/*
4581 * Handle Normal mode "%" command.
4582 */
4583 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004584nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
4586 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004587#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 linenr_T lnum = curwin->w_cursor.lnum;
4589#endif
4590
4591 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004592 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 if (cap->count0 > 100)
4595 clearopbeep(cap->oap);
4596 else
4597 {
4598 cap->oap->motion_type = MLINE;
4599 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004600 // Round up, so 'normal 100%' always jumps at the line line.
4601 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4602 // overflow on 32-bits, so use a formula with less accuracy
4603 // to avoid overflows.
4604 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4606 / 100L * cap->count0;
4607 else
4608 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4609 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004610 if (curwin->w_cursor.lnum < 1)
4611 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4613 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4614 beginline(BL_SOL | BL_FIX);
4615 }
4616 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619 cap->oap->motion_type = MCHAR;
4620 cap->oap->use_reg_one = TRUE;
4621 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4622 clearopbeep(cap->oap);
4623 else
4624 {
4625 setpcmark();
4626 curwin->w_cursor = *pos;
4627 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 }
4632#ifdef FEAT_FOLDING
4633 if (cap->oap->op_type == OP_NOP
4634 && lnum != curwin->w_cursor.lnum
4635 && (fdo_flags & FDO_PERCENT)
4636 && KeyTyped)
4637 foldOpenCursor();
4638#endif
4639}
4640
4641/*
4642 * Handle "(" and ")" commands.
4643 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4644 */
4645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004646nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647{
4648 cap->oap->motion_type = MCHAR;
4649 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004650 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004651 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 curwin->w_set_curswant = TRUE;
4653
4654 if (findsent(cap->arg, cap->count1) == FAIL)
4655 clearopbeep(cap->oap);
4656 else
4657 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004658 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004659 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#ifdef FEAT_FOLDING
4662 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4663 foldOpenCursor();
4664#endif
4665 }
4666}
4667
4668/*
4669 * "m" command: Mark a position.
4670 */
4671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004672nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 if (!checkclearop(cap->oap))
4675 {
4676 if (setmark(cap->nchar) == FAIL)
4677 clearopbeep(cap->oap);
4678 }
4679}
4680
4681/*
4682 * "{" and "}" commands.
4683 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4684 */
4685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004686nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 cap->oap->motion_type = MCHAR;
4689 cap->oap->inclusive = FALSE;
4690 cap->oap->use_reg_one = TRUE;
4691 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004692 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 clearopbeep(cap->oap);
4694 else
4695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#ifdef FEAT_FOLDING
4698 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4699 foldOpenCursor();
4700#endif
4701 }
4702}
4703
4704/*
4705 * "u" command: Undo or make lower case.
4706 */
4707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004710 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004712 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 cap->cmdchar = 'g';
4714 cap->nchar = 'u';
4715 nv_operator(cap);
4716 }
4717 else
4718 nv_kundo(cap);
4719}
4720
4721/*
4722 * <Undo> command.
4723 */
4724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004725nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 if (!checkclearopq(cap->oap))
4728 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004729#ifdef FEAT_JOB_CHANNEL
4730 if (bt_prompt(curbuf))
4731 {
4732 clearopbeep(cap->oap);
4733 return;
4734 }
4735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 u_undo((int)cap->count1);
4737 curwin->w_set_curswant = TRUE;
4738 }
4739}
4740
4741/*
4742 * Handle the "r" command.
4743 */
4744 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004745nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 char_u *ptr;
4748 int had_ctrl_v;
4749 long n;
4750
4751 if (checkclearop(cap->oap))
4752 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004753#ifdef FEAT_JOB_CHANNEL
4754 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4755 {
4756 clearopbeep(cap->oap);
4757 return;
4758 }
4759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (cap->nchar == Ctrl_V)
4763 {
4764 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004765 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004766 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (cap->nchar > DEL)
4768 had_ctrl_v = NUL;
4769 }
4770 else
4771 had_ctrl_v = NUL;
4772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004774 if (IS_SPECIAL(cap->nchar))
4775 {
4776 clearopbeep(cap->oap);
4777 return;
4778 }
4779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (VIsual_active)
4782 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004783 if (got_int)
4784 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004785 if (had_ctrl_v)
4786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Use a special (negative) number to make a difference between a
4788 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004789 if (cap->nchar == CAR)
4790 cap->nchar = REPLACE_CR_NCHAR;
4791 else if (cap->nchar == NL)
4792 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 nv_operator(cap);
4795 return;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (virtual_active())
4800 {
4801 if (u_save_cursor() == FAIL)
4802 return;
4803 if (gchar_cursor() == NUL)
4804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004805 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 coladvance_force((colnr_T)(getviscol() + cap->count1));
4807 curwin->w_cursor.col -= cap->count1;
4808 }
4809 else if (gchar_cursor() == TAB)
4810 coladvance_force(getviscol());
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004815 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004816 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 clearopbeep(cap->oap);
4819 return;
4820 }
4821
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004822 // Replacing with a TAB is done by edit() when it is complicated because
4823 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4824 // Other characters are done below to avoid problems with things like
4825 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4827 {
4828 stuffnumReadbuff(cap->count1);
4829 stuffcharReadbuff('R');
4830 stuffcharReadbuff('\t');
4831 stuffcharReadbuff(ESC);
4832 return;
4833 }
4834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 if (u_save_cursor() == FAIL)
4837 return;
4838
4839 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4840 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004841 // Replace character(s) by a single newline.
4842 // Strange vi behaviour: Only one newline is inserted.
4843 // Delete the characters here.
4844 // Insert the newline with an insert command, takes care of
4845 // autoindent. The insert command depends on being on the last
4846 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004847 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 stuffcharReadbuff('\r');
4849 stuffcharReadbuff(ESC);
4850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 invoke_edit(cap, TRUE, 'r', FALSE);
4853 }
4854 else
4855 {
4856 prep_redo(cap->oap->regname, cap->count1,
4857 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4858
4859 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (has_mbyte)
4861 {
4862 int old_State = State;
4863
4864 if (cap->ncharC1 != 0)
4865 AppendCharToRedobuff(cap->ncharC1);
4866 if (cap->ncharC2 != 0)
4867 AppendCharToRedobuff(cap->ncharC2);
4868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 // This is slow, but it handles replacing a single-byte with a
4870 // multi-byte and the other way around. Also handles adding
4871 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 for (n = cap->count1; n > 0; --n)
4873 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004874 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004875 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4876 {
4877 int c = ins_copychar(curwin->w_cursor.lnum
4878 + (cap->nchar == Ctrl_Y ? -1 : 1));
4879 if (c != NUL)
4880 ins_char(c);
4881 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004882 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004883 ++curwin->w_cursor.col;
4884 }
4885 else
4886 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 State = old_State;
4888 if (cap->ncharC1 != 0)
4889 ins_char(cap->ncharC1);
4890 if (cap->ncharC2 != 0)
4891 ins_char(cap->ncharC2);
4892 }
4893 }
4894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004896 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 for (n = cap->count1; n > 0; --n)
4898 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004899 // Get ptr again, because u_save and/or showmatch() will have
4900 // released the line. This may also happen in ins_copychar().
4901 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004902 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4903 {
4904 int c = ins_copychar(curwin->w_cursor.lnum
4905 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004906
4907 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004908 if (c != NUL)
4909 ptr[curwin->w_cursor.col] = c;
4910 }
4911 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004912 {
4913 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004914 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (p_sm && msg_silent == 0)
4917 showmatch(cap->nchar);
4918 ++curwin->w_cursor.col;
4919 }
4920#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004921 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004923 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
Bram Moolenaar009b2592004-10-24 19:18:58 +00004925 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004926 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004928 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930#endif
4931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004932 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 changed_bytes(curwin->w_cursor.lnum,
4934 (colnr_T)(curwin->w_cursor.col - cap->count1));
4935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 --curwin->w_cursor.col; // cursor on the last replaced char
4937 // if the character on the left of the current cursor is a multi-byte
4938 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (has_mbyte)
4940 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 curbuf->b_op_end = curwin->w_cursor;
4942 curwin->w_set_curswant = TRUE;
4943 set_last_insert(cap->nchar);
4944 }
4945}
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947/*
4948 * 'o': Exchange start and end of Visual area.
4949 * 'O': same, but in block mode exchange left and right corners.
4950 */
4951 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004952v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 pos_T old_cursor;
4955 colnr_T left, right;
4956
4957 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4958 {
4959 old_cursor = curwin->w_cursor;
4960 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4961 curwin->w_cursor.lnum = VIsual.lnum;
4962 coladvance(left);
4963 VIsual = curwin->w_cursor;
4964
4965 curwin->w_cursor.lnum = old_cursor.lnum;
4966 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004967 // 'selection "exclusive" and cursor at right-bottom corner: move it
4968 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4970 ++curwin->w_curswant;
4971 coladvance(curwin->w_curswant);
4972 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004974 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 curwin->w_cursor.lnum = VIsual.lnum;
4977 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4978 ++right;
4979 coladvance(right);
4980 VIsual = curwin->w_cursor;
4981
4982 curwin->w_cursor.lnum = old_cursor.lnum;
4983 coladvance(left);
4984 curwin->w_curswant = left;
4985 }
4986 }
4987 else
4988 {
4989 old_cursor = curwin->w_cursor;
4990 curwin->w_cursor = VIsual;
4991 VIsual = old_cursor;
4992 curwin->w_set_curswant = TRUE;
4993 }
4994}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996/*
4997 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4998 */
4999 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005000nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005002 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 cap->cmdchar = 'c';
5005 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005006 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 VIsual_mode = 'V';
5008 nv_operator(cap);
5009 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005010 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
5012 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005013 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 else
5015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (virtual_active())
5017 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5019 }
5020 }
5021}
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * "gr".
5025 */
5026 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005027nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (VIsual_active)
5030 {
5031 cap->cmdchar = 'r';
5032 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005033 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005035 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005038 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 else
5040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005041 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005042 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 stuffcharReadbuff(cap->extra_char);
5044 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (virtual_active())
5046 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 invoke_edit(cap, TRUE, 'v', FALSE);
5048 }
5049 }
5050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
5052/*
5053 * Swap case for "~" command, when it does not work like an operator.
5054 */
5055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005056n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 long n;
5059 pos_T startpos;
5060 int did_change = 0;
5061#ifdef FEAT_NETBEANS_INTG
5062 pos_T pos;
5063 char_u *ptr;
5064 int count;
5065#endif
5066
5067 if (checkclearopq(cap->oap))
5068 return;
5069
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005070 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
5072 clearopbeep(cap->oap);
5073 return;
5074 }
5075
5076 prep_redo_cmd(cap);
5077
5078 if (u_save_cursor() == FAIL)
5079 return;
5080
5081 startpos = curwin->w_cursor;
5082#ifdef FEAT_NETBEANS_INTG
5083 pos = startpos;
5084#endif
5085 for (n = cap->count1; n > 0; --n)
5086 {
5087 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5088 inc_cursor();
5089 if (gchar_cursor() == NUL)
5090 {
5091 if (vim_strchr(p_ww, '~') != NULL
5092 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5093 {
5094#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005095 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 if (did_change)
5098 {
5099 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005100 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005101 netbeans_removed(curbuf, pos.lnum, pos.col,
5102 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005103 // line may have been flushed, get it again
5104 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005106 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 pos.col = 0;
5109 pos.lnum++;
5110 }
5111#endif
5112 ++curwin->w_cursor.lnum;
5113 curwin->w_cursor.col = 0;
5114 if (n > 1)
5115 {
5116 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5117 break;
5118 u_clearline();
5119 }
5120 }
5121 else
5122 break;
5123 }
5124 }
5125#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005126 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 ptr = ml_get(pos.lnum);
5129 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005130 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5131 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133#endif
5134
5135
5136 check_cursor();
5137 curwin->w_set_curswant = TRUE;
5138 if (did_change)
5139 {
5140 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5141 0L);
5142 curbuf->b_op_start = startpos;
5143 curbuf->b_op_end = curwin->w_cursor;
5144 if (curbuf->b_op_end.col > 0)
5145 --curbuf->b_op_end.col;
5146 }
5147}
5148
5149/*
5150 * Move cursor to mark.
5151 */
5152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154{
5155 if (check_mark(pos) == FAIL)
5156 clearop(cap->oap);
5157 else
5158 {
5159 if (cap->cmdchar == '\''
5160 || cap->cmdchar == '`'
5161 || cap->cmdchar == '['
5162 || cap->cmdchar == ']')
5163 setpcmark();
5164 curwin->w_cursor = *pos;
5165 if (flag)
5166 beginline(BL_WHITE | BL_FIX);
5167 else
5168 check_cursor();
5169 }
5170 cap->oap->motion_type = flag ? MLINE : MCHAR;
5171 if (cap->cmdchar == '`')
5172 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 curwin->w_set_curswant = TRUE;
5175}
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177/*
5178 * Handle commands that are operators in Visual mode.
5179 */
5180 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005185 // Uppercase means linewise, except in block mode, then "D" deletes till
5186 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (isupper(cap->cmdchar))
5188 {
5189 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005190 {
5191 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5195 curwin->w_curswant = MAXCOL;
5196 }
5197 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5198 nv_operator(cap);
5199}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
5201/*
5202 * "s" and "S" commands.
5203 */
5204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005205nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005207#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005208 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005209 if (term_swap_diff() == OK)
5210 return;
5211#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005212#ifdef FEAT_JOB_CHANNEL
5213 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5214 {
5215 clearopbeep(cap->oap);
5216 return;
5217 }
5218#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005219 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005222 {
5223 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 cap->cmdchar = 'c';
5227 nv_operator(cap);
5228 }
5229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 nv_optrans(cap);
5231}
5232
5233/*
5234 * Abbreviated commands.
5235 */
5236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005237nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005242 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (VIsual_active)
5244 v_visop(cap);
5245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 nv_optrans(cap);
5247}
5248
5249/*
5250 * Translate a command into another command.
5251 */
5252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005253nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5256 (char_u *)"d$", (char_u *)"c$",
5257 (char_u *)"cl", (char_u *)"cc",
5258 (char_u *)"yy", (char_u *)":s\r"};
5259 static char_u *str = (char_u *)"xXDCsSY&";
5260
5261 if (!checkclearopq(cap->oap))
5262 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005263 // In Vi "2D" doesn't delete the next line. Can't translate it
5264 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005265 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5266 {
5267 cap->oap->start = curwin->w_cursor;
5268 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005269#ifdef FEAT_EVAL
5270 set_op_var(OP_DELETE);
5271#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005272 cap->count1 = 1;
5273 nv_dollar(cap);
5274 finish_op = TRUE;
5275 ResetRedobuff();
5276 AppendCharToRedobuff('D');
5277 }
5278 else
5279 {
5280 if (cap->count0)
5281 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005282 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285 cap->opcount = 0;
5286}
5287
5288/*
5289 * "'" and "`" commands. Also for "g'" and "g`".
5290 * cap->arg is TRUE for "'" and "g'".
5291 */
5292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005293nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
5295 pos_T *pos;
5296 int c;
5297#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005298 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005299 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300#endif
5301
5302 if (cap->cmdchar == 'g')
5303 c = cap->extra_char;
5304 else
5305 c = cap->nchar;
5306 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
5309 if (cap->arg)
5310 {
5311 check_cursor_lnum();
5312 beginline(BL_WHITE | BL_FIX);
5313 }
5314 else
5315 check_cursor();
5316 }
5317 else
5318 nv_cursormark(cap, cap->arg, pos);
5319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (!virtual_active())
5322 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005323 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#ifdef FEAT_FOLDING
5325 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005326 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005327 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 && (fdo_flags & FDO_MARK)
5329 && old_KeyTyped)
5330 foldOpenCursor();
5331#endif
5332}
5333
5334/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005335 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 */
5337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005338nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005341#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 if (!checkclearopq(cap->oap))
5347 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005348 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5349 {
5350 if (goto_tabpage_lastused() == FAIL)
5351 clearopbeep(cap->oap);
5352 return;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (cap->cmdchar == 'g')
5355 pos = movechangelist((int)cap->count1);
5356 else
5357 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005358 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 curwin->w_set_curswant = TRUE;
5361 check_cursor();
5362 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005363 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 nv_cursormark(cap, FALSE, pos);
5365 else if (cap->cmdchar == 'g')
5366 {
5367 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005368 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005370 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005372 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 else
5375 clearopbeep(cap->oap);
5376# ifdef FEAT_FOLDING
5377 if (cap->oap->op_type == OP_NOP
5378 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5379 && (fdo_flags & FDO_MARK)
5380 && old_KeyTyped)
5381 foldOpenCursor();
5382# endif
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384}
5385
5386/*
5387 * Handle '"' command.
5388 */
5389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005390nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 if (checkclearop(cap->oap))
5393 return;
5394#ifdef FEAT_EVAL
5395 if (cap->nchar == '=')
5396 cap->nchar = get_expr_register();
5397#endif
5398 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5399 {
5400 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005401 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#ifdef FEAT_EVAL
5403 set_reg_var(cap->oap->regname);
5404#endif
5405 }
5406 else
5407 clearopbeep(cap->oap);
5408}
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
5411 * Handle "v", "V" and "CTRL-V" commands.
5412 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5413 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005414 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 */
5416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005417nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005419 if (cap->cmdchar == Ctrl_Q)
5420 cap->cmdchar = Ctrl_V;
5421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005422 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5423 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (cap->oap->op_type != OP_NOP)
5425 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005426 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005427 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return;
5429 }
5430
5431 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005432 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 else // toggle char/block mode
5437 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 VIsual_mode = cap->cmdchar;
5439 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005440 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005442 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
5446 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005447 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005449 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 VIsual = curwin->w_cursor;
5451
5452 VIsual_active = TRUE;
5453 VIsual_reselect = TRUE;
5454 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005458 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005460 // For V and ^V, we multiply the number of lines even if there
5461 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5463 {
5464 curwin->w_cursor.lnum +=
5465 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005466 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 VIsual_mode = resel_VIsual_mode;
5469 if (VIsual_mode == 'v')
5470 {
5471 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005472 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005473 update_curswant_force();
5474 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005477 curwin->w_curswant = resel_VIsual_vcol;
5478 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005480 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 curwin->w_curswant = MAXCOL;
5483 coladvance((colnr_T)MAXCOL);
5484 }
5485 else if (VIsual_mode == Ctrl_V)
5486 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005487 update_curswant_force();
5488 curwin->w_curswant += + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 coladvance(curwin->w_curswant);
5490 }
5491 else
5492 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005493 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 else
5496 {
5497 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005498 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 may_start_select('c');
5500 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005501 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005502 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005503 if (cap->count0 > 0 && --cap->count1 > 0)
5504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005505 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005506 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5507 nv_right(cap);
5508 else if (VIsual_mode == 'V')
5509 nv_down(cap);
5510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
5512 }
5513}
5514
5515/*
5516 * Start selection for Shift-movement keys.
5517 */
5518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005519start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 may_start_select('k');
5523 n_start_visual_mode('v');
5524}
5525
5526/*
5527 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005528 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 */
5530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005531may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005533 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5534 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * Start Visual mode "c".
5539 * Should set VIsual_select before calling this.
5540 */
5541 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005542n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005544#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005545 int cursor_line_was_concealed = curwin->w_p_cole > 0
5546 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005547#endif
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 VIsual_mode = c;
5550 VIsual_active = TRUE;
5551 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005552
5553 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005554 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005555 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005556 {
5557 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 VIsual = curwin->w_cursor;
5561
5562#ifdef FEAT_FOLDING
5563 foldAdjustVisual();
5564#endif
5565
LemonBoy2bf52dd2022-04-09 18:17:34 +01005566 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005568#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005569 // Check if redraw is needed after changing the state.
5570 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005571#endif
5572
Bram Moolenaar09df3122006-01-23 22:23:09 +00005573 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005574 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005576 // Make sure the clipboard gets updated. Needed because start and
5577 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 clip_star.vmode = NUL;
5579#endif
5580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005581 // Only need to redraw this line, unless still need to redraw an old
5582 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005583 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5586 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5587 }
5588}
5589
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
5591/*
5592 * CTRL-W: Window commands
5593 */
5594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005595nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005597 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005600 cap->cmdchar = ':';
5601 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005602 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005603 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005604 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005605 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606}
5607
5608/*
5609 * CTRL-Z: Suspend
5610 */
5611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005612nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005616 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005617 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619
5620/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005621 * "gv": Reselect the previous Visual area. If Visual already active,
5622 * exchange previous and current Visual area.
5623 */
5624 static void
5625nv_gv_cmd(cmdarg_T *cap)
5626{
5627 pos_T tpos;
5628 int i;
5629
5630 if (checkclearop(cap->oap))
5631 return;
5632
5633 if (curbuf->b_visual.vi_start.lnum == 0
5634 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5635 || curbuf->b_visual.vi_end.lnum == 0)
5636 {
5637 beep_flush();
5638 return;
5639 }
5640
5641 // set w_cursor to the start of the Visual area, tpos to the end
5642 if (VIsual_active)
5643 {
5644 i = VIsual_mode;
5645 VIsual_mode = curbuf->b_visual.vi_mode;
5646 curbuf->b_visual.vi_mode = i;
5647# ifdef FEAT_EVAL
5648 curbuf->b_visual_mode_eval = i;
5649# endif
5650 i = curwin->w_curswant;
5651 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5652 curbuf->b_visual.vi_curswant = i;
5653
5654 tpos = curbuf->b_visual.vi_end;
5655 curbuf->b_visual.vi_end = curwin->w_cursor;
5656 curwin->w_cursor = curbuf->b_visual.vi_start;
5657 curbuf->b_visual.vi_start = VIsual;
5658 }
5659 else
5660 {
5661 VIsual_mode = curbuf->b_visual.vi_mode;
5662 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5663 tpos = curbuf->b_visual.vi_end;
5664 curwin->w_cursor = curbuf->b_visual.vi_start;
5665 }
5666
5667 VIsual_active = TRUE;
5668 VIsual_reselect = TRUE;
5669
5670 // Set Visual to the start and w_cursor to the end of the Visual
5671 // area. Make sure they are on an existing character.
5672 check_cursor();
5673 VIsual = curwin->w_cursor;
5674 curwin->w_cursor = tpos;
5675 check_cursor();
5676 update_topline();
5677
5678 // When called from normal "g" command: start Select mode when
5679 // 'selectmode' contains "cmd". When called for K_SELECT, always
5680 // start Select mode.
5681 if (cap->arg)
5682 {
5683 VIsual_select = TRUE;
5684 VIsual_select_reg = 0;
5685 }
5686 else
5687 may_start_select('c');
5688 setmouse();
5689#ifdef FEAT_CLIPBOARD
5690 // Make sure the clipboard gets updated. Needed because start and
5691 // end are still the same, and the selection needs to be owned
5692 clip_star.vmode = NUL;
5693#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005694 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005695 showmode();
5696}
5697
5698/*
5699 * "g0", "g^" : Like "0" and "^" but for screen lines.
5700 * "gm": middle of "g0" and "g$".
5701 */
5702 static void
5703nv_g_home_m_cmd(cmdarg_T *cap)
5704{
5705 int i;
5706 int flag = FALSE;
5707
5708 if (cap->nchar == '^')
5709 flag = TRUE;
5710
5711 cap->oap->motion_type = MCHAR;
5712 cap->oap->inclusive = FALSE;
5713 if (curwin->w_p_wrap && curwin->w_width != 0)
5714 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005715 int width1 = curwin->w_width - curwin_col_off();
5716 int width2 = width1 + curwin_col_off2();
5717 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005718
5719 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005720 virtcol = curwin->w_virtcol
5721#ifdef FEAT_PROP_POPUP
5722 - curwin->w_virtcol_first_char
5723#endif
5724 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005725 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005726 if (virtcol >= (colnr_T)width1 && width2 > 0)
5727 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005728 }
5729 else
5730 i = curwin->w_leftcol;
5731 // Go to the middle of the screen line. When 'number' or
5732 // 'relativenumber' is on and lines are wrapping the middle can be more
5733 // to the left.
5734 if (cap->nchar == 'm')
5735 i += (curwin->w_width - curwin_col_off()
5736 + ((curwin->w_p_wrap && i > 0)
5737 ? curwin_col_off2() : 0)) / 2;
5738 coladvance((colnr_T)i);
5739 if (flag)
5740 {
5741 do
5742 i = gchar_cursor();
5743 while (VIM_ISWHITE(i) && oneright() == OK);
5744 curwin->w_valid &= ~VALID_WCOL;
5745 }
5746 curwin->w_set_curswant = TRUE;
5747}
5748
5749/*
5750 * "g_": to the last non-blank character in the line or <count> lines
5751 * downward.
5752 */
5753 static void
5754nv_g_underscore_cmd(cmdarg_T *cap)
5755{
5756 char_u *ptr;
5757
5758 cap->oap->motion_type = MCHAR;
5759 cap->oap->inclusive = TRUE;
5760 curwin->w_curswant = MAXCOL;
5761 if (cursor_down((long)(cap->count1 - 1),
5762 cap->oap->op_type == OP_NOP) == FAIL)
5763 {
5764 clearopbeep(cap->oap);
5765 return;
5766 }
5767
5768 ptr = ml_get_curline();
5769
5770 // In Visual mode we may end up after the line.
5771 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5772 --curwin->w_cursor.col;
5773
5774 // Decrease the cursor column until it's on a non-blank.
5775 while (curwin->w_cursor.col > 0
5776 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5777 --curwin->w_cursor.col;
5778 curwin->w_set_curswant = TRUE;
5779 adjust_for_sel(cap);
5780}
5781
5782/*
5783 * "g$" : Like "$" but for screen lines.
5784 */
5785 static void
5786nv_g_dollar_cmd(cmdarg_T *cap)
5787{
5788 oparg_T *oap = cap->oap;
5789 int i;
5790 int col_off = curwin_col_off();
5791
5792 oap->motion_type = MCHAR;
5793 oap->inclusive = TRUE;
5794 if (curwin->w_p_wrap && curwin->w_width != 0)
5795 {
5796 curwin->w_curswant = MAXCOL; // so we stay at the end
5797 if (cap->count1 == 1)
5798 {
5799 int width1 = curwin->w_width - col_off;
5800 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005801 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005802
5803 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005804 virtcol = curwin->w_virtcol
5805#ifdef FEAT_PROP_POPUP
5806 - curwin->w_virtcol_first_char
5807#endif
5808 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005809 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005810 if (virtcol >= (colnr_T)width1)
5811 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005812 * width2;
5813 coladvance((colnr_T)i);
5814
5815 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005816 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005817 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5818 {
5819 // Check for landing on a character that got split at
5820 // the end of the line. We do not want to advance to
5821 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005822 if (curwin->w_virtcol
5823#ifdef FEAT_PROP_POPUP
5824 - curwin->w_virtcol_first_char
5825#endif
5826 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005827 --curwin->w_cursor.col;
5828 }
5829 }
5830 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5831 clearopbeep(oap);
5832 }
5833 else
5834 {
5835 if (cap->count1 > 1)
5836 // if it fails, let the cursor still move to the last char
5837 (void)cursor_down(cap->count1 - 1, FALSE);
5838
5839 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5840 coladvance((colnr_T)i);
5841
5842 // if the character doesn't fit move one back
5843 if (curwin->w_cursor.col > 0
5844 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5845 {
5846 colnr_T vcol;
5847
5848 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5849 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5850 --curwin->w_cursor.col;
5851 }
5852
5853 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005854 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005855 }
5856}
5857
5858/*
5859 * "gi": start Insert at the last position.
5860 */
5861 static void
5862nv_gi_cmd(cmdarg_T *cap)
5863{
5864 int i;
5865
5866 if (curbuf->b_last_insert.lnum != 0)
5867 {
5868 curwin->w_cursor = curbuf->b_last_insert;
5869 check_cursor_lnum();
5870 i = (int)STRLEN(ml_get_curline());
5871 if (curwin->w_cursor.col > (colnr_T)i)
5872 {
5873 if (virtual_active())
5874 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5875 curwin->w_cursor.col = i;
5876 }
5877 }
5878 cap->cmdchar = 'i';
5879 nv_edit(cap);
5880}
5881
5882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 * Commands starting with "g".
5884 */
5885 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005886nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 switch (cap->nchar)
5892 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005893 case Ctrl_A:
5894 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005896 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005897 if (!VIsual_active && cap->nchar == Ctrl_A)
5898 vim_mem_profile_dump();
5899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005901 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005902 if (VIsual_active)
5903 {
5904 cap->arg = TRUE;
5905 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005906 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005907 nv_addsub(cap);
5908 }
5909 else
5910 clearopbeep(oap);
5911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005913 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 case 'R':
5915 cap->arg = TRUE;
5916 nv_Replace(cap);
5917 break;
5918
5919 case 'r':
5920 nv_vreplace(cap);
5921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
5923 case '&':
5924 do_cmdline_cmd((char_u *)"%s//~/&");
5925 break;
5926
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005927 // "gv": Reselect the previous Visual area. If Visual already active,
5928 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005930 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005932
5933 // "gV": Don't reselect the previous Visual area after a Select mode
5934 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 case 'V':
5936 VIsual_reselect = FALSE;
5937 break;
5938
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005939 // "gh": start Select mode.
5940 // "gH": start Select line mode.
5941 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 case K_BS:
5943 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005944 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 case 'h':
5946 case 'H':
5947 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 cap->cmdchar = cap->nchar + ('v' - 'h');
5949 cap->arg = TRUE;
5950 nv_visual(cap);
5951 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005953 // "gn", "gN" visually select next/previous search match
5954 // "gn" selects next match
5955 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005956 case 'N':
5957 case 'n':
5958 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005959 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005960 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005962 // "gj" and "gk" two new funny movement keys -- up and down
5963 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 case 'j':
5965 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005966 // with 'nowrap' it works just like the normal "j" command.
5967 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
5969 oap->motion_type = MLINE;
5970 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5971 }
5972 else
5973 i = nv_screengo(oap, FORWARD, cap->count1);
5974 if (i == FAIL)
5975 clearopbeep(oap);
5976 break;
5977
5978 case 'k':
5979 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005980 // with 'nowrap' it works just like the normal "k" command.
5981 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 oap->motion_type = MLINE;
5984 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5985 }
5986 else
5987 i = nv_screengo(oap, BACKWARD, cap->count1);
5988 if (i == FAIL)
5989 clearopbeep(oap);
5990 break;
5991
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005992 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 case 'J':
5994 nv_join(cap);
5995 break;
5996
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005997 // "g0", "g^" : Like "0" and "^" but for screen lines.
5998 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 case '0':
6001 case 'm':
6002 case K_HOME:
6003 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006004 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 break;
6006
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006007 case 'M':
6008 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006009 oap->motion_type = MCHAR;
6010 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006011 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006012 if (cap->count0 > 0 && cap->count0 <= 100)
6013 coladvance((colnr_T)(i * cap->count0 / 100));
6014 else
6015 coladvance((colnr_T)(i / 2));
6016 curwin->w_set_curswant = TRUE;
6017 }
6018 break;
6019
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006020 // "g_": to the last non-blank character in the line or <count> lines
6021 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006023 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 break;
6025
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006026 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 case '$':
6028 case K_END:
6029 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 break;
6032
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 case '*':
6035 case '#':
6036#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006037 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006039 case Ctrl_RSB: // :tag or :tselect for current identifier
6040 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 nv_ident(cap);
6042 break;
6043
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006044 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 case 'e':
6046 case 'E':
6047 oap->motion_type = MCHAR;
6048 curwin->w_set_curswant = TRUE;
6049 oap->inclusive = TRUE;
6050 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6051 clearopbeep(oap);
6052 break;
6053
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006054 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006056 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 break;
6058
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006059 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006061 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 break;
6063
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006064 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 case 'I':
6066 beginline(0);
6067 if (!checkclearopq(oap))
6068 invoke_edit(cap, FALSE, 'g', FALSE);
6069 break;
6070
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006071 // "gf": goto file, edit file under cursor
6072 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006074 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 nv_gotofile(cap);
6076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 case '\'':
6080 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006081 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 case '`':
6083 nv_gomark(cap);
6084 break;
6085
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006086 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006088 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 break;
6090
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006091 // "ga": Display the ascii value of the character under the
6092 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 case 'a':
6094 do_ascii(NULL);
6095 break;
6096
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006097 // "g8": Display the bytes used for the UTF-8 character under the
6098 // cursor. It is displayed in hex.
6099 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006101 if (cap->count0 == 8)
6102 utf_find_illegal();
6103 else
6104 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006107 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006108 case '<':
6109 show_sb_text();
6110 break;
6111
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006112 // "gg": Goto the first line in file. With a count it goes to
6113 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 case 'g':
6115 cap->arg = FALSE;
6116 nv_goto(cap);
6117 break;
6118
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006119 // Two-character operators:
6120 // "gq" Format text
6121 // "gw" Format text and keep cursor position
6122 // "g~" Toggle the case of the text.
6123 // "gu" Change text to lower case.
6124 // "gU" Change text to upper case.
6125 // "g?" rot13 encoding
6126 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 case 'q':
6128 case 'w':
6129 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006130 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 case '~':
6132 case 'u':
6133 case 'U':
6134 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006135 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 nv_operator(cap);
6137 break;
6138
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006139 // "gd": Find first occurrence of pattern under the cursor in the
6140 // current function
6141 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 case 'd':
6143 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006144 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 break;
6146
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006147 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 case K_MIDDLEMOUSE:
6149 case K_MIDDLEDRAG:
6150 case K_MIDDLERELEASE:
6151 case K_LEFTMOUSE:
6152 case K_LEFTDRAG:
6153 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006154 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 case K_RIGHTMOUSE:
6156 case K_RIGHTDRAG:
6157 case K_RIGHTRELEASE:
6158 case K_X1MOUSE:
6159 case K_X1DRAG:
6160 case K_X1RELEASE:
6161 case K_X2MOUSE:
6162 case K_X2DRAG:
6163 case K_X2RELEASE:
6164 mod_mask = MOD_MASK_CTRL;
6165 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
6168 case K_IGNORE:
6169 break;
6170
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006171 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 case 'p':
6173 case 'P':
6174 nv_put(cap);
6175 break;
6176
6177#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006178 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 case 'o':
6180 goto_byte(cap->count0);
6181 break;
6182#endif
6183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006184 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006186 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 do_exmode(TRUE);
6188 break;
6189
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 case ',':
6191 nv_pcmark(cap);
6192 break;
6193
6194 case ';':
6195 cap->count1 = -cap->count1;
6196 nv_pcmark(cap);
6197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006200 if (!checkclearop(oap))
6201 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006203 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006204 if (!checkclearop(oap))
6205 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006206 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207
Bram Moolenaar62a23252020-08-09 14:04:42 +02006208 case TAB:
6209 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6210 clearopbeep(oap);
6211 break;
6212
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006213 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006214 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006215 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006216 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006217 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006218 break;
6219
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 default:
6221 clearopbeep(oap);
6222 break;
6223 }
6224}
6225
6226/*
6227 * Handle "o" and "O" commands.
6228 */
6229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006230n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006232#ifdef FEAT_CONCEAL
6233 linenr_T oldline = curwin->w_cursor.lnum;
6234#endif
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 if (!checkclearopq(cap->oap))
6237 {
6238#ifdef FEAT_FOLDING
6239 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 (void)hasFolding(curwin->w_cursor.lnum,
6242 &curwin->w_cursor.lnum, NULL);
6243 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 (void)hasFolding(curwin->w_cursor.lnum,
6246 NULL, &curwin->w_cursor.lnum);
6247#endif
6248 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,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006254 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006255 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006257#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006258 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006259 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006260#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006261#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006262 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006264 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006265#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006266 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006267 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6268 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6270 }
6271 }
6272}
6273
6274/*
6275 * "." command: redo last change.
6276 */
6277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006278nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 if (!checkclearopq(cap->oap))
6281 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006282 // 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.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6286 clearopbeep(cap->oap);
6287 }
6288}
6289
6290/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006291 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 */
6293 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006294nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006296 if (VIsual_select && VIsual_active)
6297 {
6298 int reg;
6299 // Get register name
6300 ++no_mapping;
6301 ++allow_keys;
6302 reg = plain_vgetc();
6303 LANGMAP_ADJUST(reg, TRUE);
6304 --no_mapping;
6305 --allow_keys;
6306
6307 if (reg == '"')
6308 // the unnamed register is 0
6309 reg = 0;
6310
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006311 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006312 return;
6313 }
6314
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (!checkclearopq(cap->oap))
6316 {
6317 u_redo((int)cap->count1);
6318 curwin->w_set_curswant = TRUE;
6319 }
6320}
6321
6322/*
6323 * Handle "U" command.
6324 */
6325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006326nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006328 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006329 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006331 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 cap->cmdchar = 'g';
6333 cap->nchar = 'U';
6334 nv_operator(cap);
6335 }
6336 else if (!checkclearopq(cap->oap))
6337 {
6338 u_undoline();
6339 curwin->w_set_curswant = TRUE;
6340 }
6341}
6342
6343/*
6344 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6345 * single character.
6346 */
6347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006348nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006350 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006351 {
6352#ifdef FEAT_JOB_CHANNEL
6353 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6354 {
6355 clearopbeep(cap->oap);
6356 return;
6357 }
6358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 else
6362 nv_operator(cap);
6363}
6364
6365/*
6366 * Handle an operator command.
6367 * The actual work is done by do_pending_operator().
6368 */
6369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006370nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 int op_type;
6373
6374 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006375#ifdef FEAT_JOB_CHANNEL
6376 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6377 {
6378 clearopbeep(cap->oap);
6379 return;
6380 }
6381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006383 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 nv_lineop(cap);
6385 else if (!checkclearop(cap->oap))
6386 {
6387 cap->oap->start = curwin->w_cursor;
6388 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006389#ifdef FEAT_EVAL
6390 set_op_var(op_type);
6391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 }
6393}
6394
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006395#ifdef FEAT_EVAL
6396/*
6397 * Set v:operator to the characters for "optype".
6398 */
6399 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006400set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006401{
6402 char_u opchars[3];
6403
6404 if (optype == OP_NOP)
6405 set_vim_var_string(VV_OP, NULL, 0);
6406 else
6407 {
6408 opchars[0] = get_op_char(optype);
6409 opchars[1] = get_extra_op_char(optype);
6410 opchars[2] = NUL;
6411 set_vim_var_string(VV_OP, opchars, -1);
6412 }
6413}
6414#endif
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416/*
6417 * Handle linewise operator "dd", "yy", etc.
6418 *
6419 * "_" is is a strange motion command that helps make operators more logical.
6420 * It is actually implemented, but not documented in the real Vi. This motion
6421 * command actually refers to "the current line". Commands like "dd" and "yy"
6422 * are really an alternate form of "d_" and "y_". It does accept a count, so
6423 * "d3_" works to delete 3 lines.
6424 */
6425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006426nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 cap->oap->motion_type = MLINE;
6429 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6430 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006431 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006432 && cap->oap->motion_force != 'v'
6433 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 || cap->oap->op_type == OP_LSHIFT
6435 || cap->oap->op_type == OP_RSHIFT)
6436 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006437 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 beginline(BL_WHITE | BL_FIX);
6439}
6440
6441/*
6442 * <Home> command.
6443 */
6444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006445nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006447 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006448 if (mod_mask & MOD_MASK_CTRL)
6449 nv_goto(cap);
6450 else
6451 {
6452 cap->count0 = 1;
6453 nv_pipe(cap);
6454 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006455 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6456 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
6459/*
6460 * "|" command.
6461 */
6462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006463nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 cap->oap->motion_type = MCHAR;
6466 cap->oap->inclusive = FALSE;
6467 beginline(0);
6468 if (cap->count0 > 0)
6469 {
6470 coladvance((colnr_T)(cap->count0 - 1));
6471 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6472 }
6473 else
6474 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006475 // keep curswant at the column where we wanted to go, not where
6476 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 curwin->w_set_curswant = FALSE;
6478}
6479
6480/*
6481 * Handle back-word command "b" and "B".
6482 * cap->arg is 1 for "B"
6483 */
6484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006485nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 cap->oap->motion_type = MCHAR;
6488 cap->oap->inclusive = FALSE;
6489 curwin->w_set_curswant = TRUE;
6490 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6491 clearopbeep(cap->oap);
6492#ifdef FEAT_FOLDING
6493 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6494 foldOpenCursor();
6495#endif
6496}
6497
6498/*
6499 * Handle word motion commands "e", "E", "w" and "W".
6500 * cap->arg is TRUE for "E" and "W".
6501 */
6502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006503nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 int n;
6506 int word_end;
6507 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006508 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006510 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6512 word_end = TRUE;
6513 else
6514 word_end = FALSE;
6515 cap->oap->inclusive = word_end;
6516
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006517 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (!word_end && cap->oap->op_type == OP_CHANGE)
6519 {
6520 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006521 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006523 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006525 // Reproduce a funny Vi behaviour: "cw" on a blank only
6526 // changes one character, not all blanks until the start of
6527 // the next word. Only do this when the 'w' flag is included
6528 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6530 {
6531 cap->oap->inclusive = TRUE;
6532 cap->oap->motion_type = MCHAR;
6533 return;
6534 }
6535 }
6536 else
6537 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006538 // This is a little strange. To match what the real Vi does,
6539 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6540 // that we are not on a space or a TAB. This seems impolite
6541 // at first, but it's really more what we mean when we say
6542 // 'cw'.
6543 // Another strangeness: When standing on the end of a word
6544 // "ce" will change until the end of the next word, but "cw"
6545 // will change only one character! This is done by setting
6546 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 cap->oap->inclusive = TRUE;
6548 word_end = TRUE;
6549 flag = TRUE;
6550 }
6551 }
6552 }
6553
6554 cap->oap->motion_type = MCHAR;
6555 curwin->w_set_curswant = TRUE;
6556 if (word_end)
6557 n = end_word(cap->count1, cap->arg, flag, FALSE);
6558 else
6559 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006561 // Don't leave the cursor on the NUL past the end of line. Unless we
6562 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006563 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006564 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566 if (n == FAIL && cap->oap->op_type == OP_NOP)
6567 clearopbeep(cap->oap);
6568 else
6569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571#ifdef FEAT_FOLDING
6572 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6573 foldOpenCursor();
6574#endif
6575 }
6576}
6577
6578/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006579 * Used after a movement command: If the cursor ends up on the NUL after the
6580 * end of the line, may move it back to the last character and make the motion
6581 * inclusive.
6582 */
6583 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006585{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006586 // The cursor cannot remain on the NUL when:
6587 // - the column is > 0
6588 // - not in Visual mode or 'selection' is "o"
6589 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006590 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006592 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006593 {
6594 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006595 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006596 if (has_mbyte)
6597 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006598 oap->inclusive = TRUE;
6599 }
6600}
6601
6602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 * "0" and "^" commands.
6604 * cap->arg is the argument for beginline().
6605 */
6606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006607nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 cap->oap->motion_type = MCHAR;
6610 cap->oap->inclusive = FALSE;
6611 beginline(cap->arg);
6612#ifdef FEAT_FOLDING
6613 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6614 foldOpenCursor();
6615#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006616 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6617 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618}
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
6621 * In exclusive Visual mode, may include the last character.
6622 */
6623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006624adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006627 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 if (has_mbyte)
6630 inc_cursor();
6631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 ++curwin->w_cursor.col;
6633 cap->oap->inclusive = FALSE;
6634 }
6635}
6636
6637/*
6638 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6639 * Should check VIsual_mode before calling this.
6640 * Returns TRUE when backed up to the previous line.
6641 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006643unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 pos_T *pp;
6646
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006647 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006649 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 pp = &curwin->w_cursor;
6651 else
6652 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (pp->coladd > 0)
6654 --pp->coladd;
6655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (pp->col > 0)
6657 {
6658 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006659 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 }
6661 else if (pp->lnum > 1)
6662 {
6663 --pp->lnum;
6664 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6665 return TRUE;
6666 }
6667 }
6668 return FALSE;
6669}
6670
6671/*
6672 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6673 */
6674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006675nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006680 VIsual_select_reg = 0;
6681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 else if (VIsual_reselect)
6683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006684 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 cap->arg = TRUE;
6686 nv_g_cmd(cap);
6687 }
6688}
6689
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691/*
6692 * "G", "gg", CTRL-END, CTRL-HOME.
6693 * cap->arg is TRUE for "G".
6694 */
6695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006696nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 linenr_T lnum;
6699
6700 if (cap->arg)
6701 lnum = curbuf->b_ml.ml_line_count;
6702 else
6703 lnum = 1L;
6704 cap->oap->motion_type = MLINE;
6705 setpcmark();
6706
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006707 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (cap->count0 != 0)
6709 lnum = cap->count0;
6710 if (lnum < 1L)
6711 lnum = 1L;
6712 else if (lnum > curbuf->b_ml.ml_line_count)
6713 lnum = curbuf->b_ml.ml_line_count;
6714 curwin->w_cursor.lnum = lnum;
6715 beginline(BL_SOL | BL_FIX);
6716#ifdef FEAT_FOLDING
6717 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6718 foldOpenCursor();
6719#endif
6720}
6721
6722/*
6723 * CTRL-\ in Normal mode.
6724 */
6725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006726nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6729 {
6730 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006731 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006732 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (cmdwin_type != 0)
6735 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (VIsual_active)
6737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006738 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006739 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006741 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (cap->nchar == Ctrl_G && p_im)
6743 restart_edit = 'a';
6744 }
6745 else
6746 clearopbeep(cap->oap);
6747}
6748
6749/*
6750 * ESC in Normal mode: beep, but don't flush buffers.
6751 * Don't even beep if we are canceling a command.
6752 */
6753 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006754nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 int no_reason;
6757
6758 no_reason = (cap->oap->op_type == OP_NOP
6759 && cap->opcount == 0
6760 && cap->count0 == 0
6761 && cap->oap->regname == 0
6762 && !p_im);
6763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006764 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006766 if (restart_edit == 0 && cmdwin_type == 0
6767 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006768 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006769 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6770
Bram Moolenaar5939c352022-09-24 12:50:45 +01006771 // The user may accidentally do "vim file | grep word" and then
6772 // CTRL-C doesn't show anything. With a changed buffer give the
6773 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006774 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006775 {
6776 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6777
6778 if (out_redir)
6779 mch_errmsg(ms);
6780 else
6781 msg(ms);
6782 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006783 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006784 {
6785 if (out_redir)
6786 {
6787 got_int = FALSE;
6788 do_cmdline_cmd((char_u *)"qa");
6789 }
6790 else
6791 msg(_("Type :qa and press <Enter> to exit Vim"));
6792 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006795 if (restart_edit != 0)
6796 redraw_mode = TRUE; // remove "-- (insert) --"
6797
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006798 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6799 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (!p_im)
6801 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 if (cmdwin_type != 0)
6803 {
6804 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 return;
6807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006809 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006810 {
6811 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006812 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6813 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006814 cmdwin_result = K_IGNORE;
6815 return;
6816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (VIsual_active)
6819 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006820 end_visual_mode(); // stop Visual
6821 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006823 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006825 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006826 {
6827#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006828 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006829 popup_hide_message_win();
6830 else
6831#endif
6832 vim_beep(BO_ESC);
6833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 clearop(cap->oap);
6835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006836 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6837 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006838 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 restart_edit = 'a';
6840}
6841
6842/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006843 * Move the cursor for the "A" command.
6844 */
6845 void
6846set_cursor_for_append_to_line(void)
6847{
6848 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006849 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006850 {
6851 int save_State = State;
6852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006853 // Pretend Insert mode here to allow the cursor on the
6854 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006855 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006856 coladvance((colnr_T)MAXCOL);
6857 State = save_State;
6858 }
6859 else
6860 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6861}
6862
6863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006865 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 */
6867 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006868nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006870 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6872 cap->cmdchar = 'i';
6873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006876 {
6877#ifdef FEAT_TERMINAL
6878 if (term_in_normal_mode())
6879 {
6880 end_visual_mode();
6881 clearop(cap->oap);
6882 term_enter_job_mode();
6883 return;
6884 }
6885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006889 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006890 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6891 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006895#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006896 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006897 {
6898 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006899 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006900 return;
6901 }
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 else if (!curbuf->b_p_ma && !p_im)
6904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006906 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006908 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006909 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006910 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006912 else if (cap->cmdchar == K_PS && VIsual_active)
6913 {
6914 pos_T old_pos = curwin->w_cursor;
6915 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006916 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006918 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006919 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6920 {
6921 shift_delete_registers();
6922 cap->oap->regname = '1';
6923 }
6924 else
6925 cap->oap->regname = '-';
6926 cap->cmdchar = 'd';
6927 cap->nchar = NUL;
6928 nv_operator(cap);
6929 do_pending_operator(cap, 0, FALSE);
6930 cap->cmdchar = K_PS;
6931
zeertzjq7a732522022-03-14 20:46:41 +00006932 if (*ml_get_cursor() != NUL)
6933 {
6934 if (old_visual_mode == 'V')
6935 {
6936 // In linewise Visual mode insert before the beginning of the
6937 // next line.
6938 // When the last line in the buffer was deleted then create a
6939 // new line, otherwise there is not need to move cursor.
6940 // Detect this by checking if cursor moved above Visual area.
6941 if (curwin->w_cursor.lnum < old_pos.lnum
6942 && curwin->w_cursor.lnum < old_visual.lnum)
6943 {
6944 if (u_save_cursor() == OK)
6945 {
6946 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6947 FALSE);
6948 appended_lines(curwin->w_cursor.lnum++, 1L);
6949 }
6950 }
6951 }
6952 // When the last char in the line was deleted then append.
6953 // Detect this by checking if cursor moved before Visual area.
6954 else if (curwin->w_cursor.col < old_pos.col
6955 && curwin->w_cursor.col < old_visual.col)
6956 inc_cursor();
6957 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006959 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006960 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 else if (!checkclearopq(cap->oap))
6963 {
6964 switch (cap->cmdchar)
6965 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006967 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 break;
6969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006970 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006971 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6972 beginline(BL_WHITE);
6973 else
6974 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 break;
6976
Bram Moolenaara1891842017-02-04 21:34:31 +01006977 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // Bracketed paste works like "a"ppend, unless the cursor is in
6979 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006980 if (curwin->w_cursor.col == 0)
6981 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006982 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006984 case 'a': // "a"ppend is like "i"nsert on the next character.
6985 // increment coladd when in virtual space, increment the
6986 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (virtual_active()
6988 && (curwin->w_cursor.coladd > 0
6989 || *ml_get_cursor() == NUL
6990 || *ml_get_cursor() == TAB))
6991 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006992 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 inc_cursor();
6994 break;
6995 }
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6998 {
6999 int save_State = State;
7000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // Pretend Insert mode here to allow the cursor on the
7002 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007003 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 coladvance(getviscol());
7005 State = save_State;
7006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7009 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007010 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007012 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013}
7014
7015/*
7016 * Invoke edit() and take care of "restart_edit" and the return value.
7017 */
7018 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007019invoke_edit(
7020 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007021 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007022 int cmd,
7023 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 int restart_edit_save = 0;
7026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007027 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7028 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7029 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (repl || !stuff_empty())
7031 restart_edit_save = restart_edit;
7032 else
7033 restart_edit_save = 0;
7034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007035 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 restart_edit = 0;
7037
7038 if (edit(cmd, startln, cap->count1))
7039 cap->retval |= CA_COMMAND_BUSY;
7040
7041 if (restart_edit == 0)
7042 restart_edit = restart_edit_save;
7043}
7044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045/*
7046 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7047 */
7048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007049nv_object(
7050 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int flag;
7053 int include;
7054 char_u *mps_save;
7055
7056 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007059 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007061 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 mps_save = curbuf->b_p_mps;
7063 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7064
7065 switch (cap->nchar)
7066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 flag = current_word(cap->oap, cap->count1, include, FALSE);
7069 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 flag = current_word(cap->oap, cap->count1, include, TRUE);
7072 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 case '(':
7075 case ')':
7076 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7077 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 case '{':
7080 case '}':
7081 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7082 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 case ']':
7085 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7086 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007087 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 case '>':
7089 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7090 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007091#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 case 't': // "at" = a tag block (xml and html)
7093 // Do not adjust oap->end in do_pending_operator()
7094 // otherwise there are different results for 'dit'
7095 // (note leading whitespace in last line):
7096 // 1) <b> 2) <b>
7097 // foobar foobar
7098 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007099 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007100 flag = current_tagblock(cap->oap, cap->count1, include);
7101 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007102#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 flag = current_par(cap->oap, cap->count1, include, 'p');
7105 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 flag = current_sent(cap->oap, cap->count1, include);
7108 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 case '"': // "a"" = a double quoted string
7110 case '\'': // "a'" = a single quoted string
7111 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007112 flag = current_quote(cap->oap, cap->count1, include,
7113 cap->nchar);
7114 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007115#if 0 // TODO
7116 case 'S': // "aS" = a section
7117 case 'f': // "af" = a filename
7118 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119#endif
7120 default:
7121 flag = FAIL;
7122 break;
7123 }
7124
7125 curbuf->b_p_mps = mps_save;
7126 if (flag == FAIL)
7127 clearopbeep(cap->oap);
7128 adjust_cursor_col();
7129 curwin->w_set_curswant = TRUE;
7130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
7132/*
7133 * "q" command: Start/stop recording.
7134 * "q:", "q/", "q?": edit command-line in command-line window.
7135 */
7136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007137nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 if (cap->oap->op_type == OP_FORMAT)
7140 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007141 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 cap->cmdchar = 'g';
7143 cap->nchar = 'q';
7144 nv_operator(cap);
7145 }
7146 else if (!checkclearop(cap->oap))
7147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7149 {
Bram Moolenaarc963ec32022-07-24 20:08:01 +01007150 if (cmdwin_type != 0)
7151 {
7152 emsg(_(e_cmdline_window_already_open));
7153 return;
7154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 stuffcharReadbuff(cap->nchar);
7156 stuffcharReadbuff(K_CMDWIN);
7157 }
7158 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007159 // (stop) recording into a named register, unless executing a
7160 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007161 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 clearopbeep(cap->oap);
7163 }
7164}
7165
7166/*
7167 * Handle the "@r" command.
7168 */
7169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007170nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
7172 if (checkclearop(cap->oap))
7173 return;
7174#ifdef FEAT_EVAL
7175 if (cap->nchar == '=')
7176 {
7177 if (get_expr_register() == NUL)
7178 return;
7179 }
7180#endif
7181 while (cap->count1-- && !got_int)
7182 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007183 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
7185 clearopbeep(cap->oap);
7186 break;
7187 }
7188 line_breakcheck();
7189 }
7190}
7191
7192/*
7193 * Handle the CTRL-U and CTRL-D commands.
7194 */
7195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007196nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7199 || (cap->cmdchar == Ctrl_D
7200 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7201 clearopbeep(cap->oap);
7202 else if (!checkclearop(cap->oap))
7203 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7204}
7205
7206/*
7207 * Handle "J" or "gJ" command.
7208 */
7209 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007210nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007212 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007214 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
7216 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7219 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007222 if (cap->count0 <= 2)
7223 {
7224 clearopbeep(cap->oap);
7225 return;
7226 }
7227 cap->count0 = curbuf->b_ml.ml_line_count
7228 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007230
7231 prep_redo(cap->oap->regname, cap->count0,
7232 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7233 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235}
7236
7237/*
7238 * "P", "gP", "p" and "gp" commands.
7239 */
7240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007241nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007243 nv_put_opt(cap, FALSE);
7244}
7245
7246/*
7247 * "P", "gP", "p" and "gp" commands.
7248 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7249 */
7250 static void
7251nv_put_opt(cmdarg_T *cap, int fix_indent)
7252{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 int regname = 0;
7254 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007255 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007256 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 int dir;
7258 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007259 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 if (cap->oap->op_type != OP_NOP)
7262 {
7263#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007264 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7266 {
7267 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007268 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 else
7271#endif
7272 clearopbeep(cap->oap);
7273 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007274#ifdef FEAT_JOB_CHANNEL
7275 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7276 {
7277 clearopbeep(cap->oap);
7278 }
7279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 else
7281 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007282 if (fix_indent)
7283 {
7284 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7285 ? FORWARD : BACKWARD;
7286 flags |= PUT_FIXINDENT;
7287 }
7288 else
7289 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007290 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7291 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 prep_redo_cmd(cap);
7293 if (cap->cmdchar == 'g')
7294 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007295 else if (cap->cmdchar == 'z')
7296 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (VIsual_active)
7299 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007300 // Putting in Visual mode: The put text replaces the selected
7301 // text. First delete the selected text, then put the new text.
7302 // Need to save and restore the registers that the delete
7303 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007304 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007306 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007307#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007309#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007310 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007311 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007312#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
7316 )
7317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007318 // The delete is going to overwrite the register we want to
7319 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 reg1 = get_register(regname, TRUE);
7321 }
7322
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007323 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 cap->cmdchar = 'd';
7325 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007326 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007327 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 nv_operator(cap);
7329 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007330 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007331 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007333 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 cap->oap->regname = regname;
7335
7336 if (reg1 != NULL)
7337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 // Delete probably changed the register we want to put, save
7339 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 reg2 = get_register(regname, FALSE);
7341 put_register(regname, reg1);
7342 }
7343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007344 // When deleted a linewise Visual area, put the register as
7345 // lines to avoid it joined with the next line. When deletion was
7346 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (VIsual_mode == 'V')
7348 flags |= PUT_LINE;
7349 else if (VIsual_mode == 'v')
7350 flags |= PUT_LINE_SPLIT;
7351 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7352 flags |= PUT_LINE_FORWARD;
7353 dir = BACKWARD;
7354 if ((VIsual_mode != 'V'
7355 && curwin->w_cursor.col < curbuf->b_op_start.col)
7356 || (VIsual_mode == 'V'
7357 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 // cursor is at the end of the line or end of file, put
7359 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007362 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007364 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (reg2 != NULL)
7368 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007369
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007370 // What to reselect with "gv"? Selecting the just put text seems to
7371 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007372 if (was_visual)
7373 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007374 curbuf->b_visual.vi_start = curbuf->b_op_start;
7375 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007376 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007377 if (*p_sel == 'e')
7378 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007379 }
7380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007381 // When all lines were selected and deleted do_put() leaves an empty
7382 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007383 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007384 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007385 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007386 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007388 // If the cursor was in that line, move it to the end of the last
7389 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007390 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7391 {
7392 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7393 coladvance((colnr_T)MAXCOL);
7394 }
7395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 auto_format(FALSE, TRUE);
7397 }
7398}
7399
7400/*
7401 * "o" and "O" commands.
7402 */
7403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007404nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007407 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7409 {
7410 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007411 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413 else
7414#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007415 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007417#ifdef FEAT_JOB_CHANNEL
7418 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007419 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 n_opencmd(cap);
7423}
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#ifdef FEAT_NETBEANS_INTG
7426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007427nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 netbeans_keycommand(cap->nchar);
7430}
7431#endif
7432
7433#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007435nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007437 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438}
7439#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007440
Bram Moolenaar3918c952005-03-15 22:34:55 +00007441/*
7442 * Trigger CursorHold event.
7443 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7444 * input buffer. "did_cursorhold" is set to avoid retriggering.
7445 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007447nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007448{
7449 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7450 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007451 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007452}