blob: 932d72320540b68afa5cf783d80bcd395888e37a [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 Moolenaar071d4272004-06-13 20:20:40 +000023#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
26
27/*
28 * nv_*(): functions called to handle Normal and Visual mode commands.
29 * n_*(): functions called to handle Normal mode commands.
30 * v_*(): functions called to handle Visual mode commands.
31 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static void nv_ignore(cmdarg_T *cap);
33static void nv_nop(cmdarg_T *cap);
34static void nv_error(cmdarg_T *cap);
35static void nv_help(cmdarg_T *cap);
36static void nv_addsub(cmdarg_T *cap);
37static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_ver_scrollbar(cmdarg_T *cap);
41static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000043#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void nv_tabline(cmdarg_T *cap);
45static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000046#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010047static void nv_exmode(cmdarg_T *cap);
48static void nv_colon(cmdarg_T *cap);
49static void nv_ctrlg(cmdarg_T *cap);
50static void nv_ctrlh(cmdarg_T *cap);
51static void nv_clear(cmdarg_T *cap);
52static void nv_ctrlo(cmdarg_T *cap);
53static void nv_hat(cmdarg_T *cap);
54static void nv_Zet(cmdarg_T *cap);
55static void nv_ident(cmdarg_T *cap);
56static void nv_tagpop(cmdarg_T *cap);
57static void nv_scroll(cmdarg_T *cap);
58static void nv_right(cmdarg_T *cap);
59static void nv_left(cmdarg_T *cap);
60static void nv_up(cmdarg_T *cap);
61static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void nv_end(cmdarg_T *cap);
63static void nv_dollar(cmdarg_T *cap);
64static void nv_search(cmdarg_T *cap);
65static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020066static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010067static void nv_csearch(cmdarg_T *cap);
68static void nv_brackets(cmdarg_T *cap);
69static void nv_percent(cmdarg_T *cap);
70static void nv_brace(cmdarg_T *cap);
71static void nv_mark(cmdarg_T *cap);
72static void nv_findpar(cmdarg_T *cap);
73static void nv_undo(cmdarg_T *cap);
74static void nv_kundo(cmdarg_T *cap);
75static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010077static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
78static void v_visop(cmdarg_T *cap);
79static void nv_subst(cmdarg_T *cap);
80static void nv_abbrev(cmdarg_T *cap);
81static void nv_optrans(cmdarg_T *cap);
82static void nv_gomark(cmdarg_T *cap);
83static void nv_pcmark(cmdarg_T *cap);
84static void nv_regname(cmdarg_T *cap);
85static void nv_visual(cmdarg_T *cap);
86static void n_start_visual_mode(int c);
87static void nv_window(cmdarg_T *cap);
88static void nv_suspend(cmdarg_T *cap);
89static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000091static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_Undo(cmdarg_T *cap);
93static void nv_tilde(cmdarg_T *cap);
94static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_lineop(cmdarg_T *cap);
99static void nv_home(cmdarg_T *cap);
100static void nv_pipe(cmdarg_T *cap);
101static void nv_bck_word(cmdarg_T *cap);
102static void nv_wordcmd(cmdarg_T *cap);
103static void nv_beginline(cmdarg_T *cap);
104static void adjust_cursor(oparg_T *oap);
105static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_select(cmdarg_T *cap);
107static void nv_goto(cmdarg_T *cap);
108static void nv_normal(cmdarg_T *cap);
109static void nv_esc(cmdarg_T *oap);
110static void nv_edit(cmdarg_T *cap);
111static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_record(cmdarg_T *cap);
116static void nv_at(cmdarg_T *cap);
117static void nv_halfpage(cmdarg_T *cap);
118static void nv_join(cmdarg_T *cap);
119static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200120static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
ichizok672776d2022-01-31 12:27:18 +0000130// Declare nv_cmds[].
131#define DO_DECLARE_NVCMD
132#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000134// Include the lookuptable generated by create_nvcmdidx.vim.
135#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
138 * Search for a command in the commands table.
139 * Returns -1 for invalid command.
140 */
141 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100142find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
144 int i;
145 int idx;
146 int top, bot;
147 int c;
148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 if (cmdchar >= 0x100)
151 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100153 // We use the absolute value of the character. Special keys have a
154 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (cmdchar < 0)
156 cmdchar = -cmdchar;
157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100158 // If the character is in the first part: The character is the index into
159 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (cmdchar <= nv_max_linear)
161 return nv_cmd_idx[cmdchar];
162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 bot = nv_max_linear + 1;
165 top = NV_CMDS_SIZE - 1;
166 idx = -1;
167 while (bot <= top)
168 {
169 i = (top + bot) / 2;
170 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
171 if (c < 0)
172 c = -c;
173 if (cmdchar == c)
174 {
175 idx = nv_cmd_idx[i];
176 break;
177 }
178 if (cmdchar > c)
179 bot = i + 1;
180 else
181 top = i - 1;
182 }
183 return idx;
184}
185
186/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100187 * If currently editing a cmdline or text is locked: beep and give an error
188 * message, return TRUE.
189 */
190 static int
191check_text_locked(oparg_T *oap)
192{
193 if (text_locked())
194 {
195 clearopbeep(oap);
196 text_locked_msg();
197 return TRUE;
198 }
199 return FALSE;
200}
201
202/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000203 * Handle the count before a normal command and set cap->count0.
204 */
205 static int
206normal_cmd_get_count(
207 cmdarg_T *cap,
208 int c,
209 int toplevel UNUSED,
210 int set_prevcount UNUSED,
211 int *ctrl_w,
212 int *need_flushbuf UNUSED)
213{
214getcount:
215 if (!(VIsual_active && VIsual_select))
216 {
217 // Handle a count before a command and compute ca.count0.
218 // Note that '0' is a command and not the start of a count, but it's
219 // part of a count after other digits.
220 while ((c >= '1' && c <= '9')
221 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
222 || c == '0')))
223 {
224 if (c == K_DEL || c == K_KDEL)
225 {
226 cap->count0 /= 10;
227#ifdef FEAT_CMDL_INFO
228 del_from_showcmd(4); // delete the digit and ~@%
229#endif
230 }
231 else if (cap->count0 > 99999999L)
232 {
233 cap->count0 = 999999999L;
234 }
235 else
236 {
237 cap->count0 = cap->count0 * 10 + (c - '0');
238 }
239#ifdef FEAT_EVAL
240 // Set v:count here, when called from main() and not a stuffed
241 // command, so that v:count can be used in an expression mapping
242 // right after the count. Do set it for redo.
243 if (toplevel && readbuf1_empty())
244 set_vcount_ca(cap, &set_prevcount);
245#endif
246 if (*ctrl_w)
247 {
248 ++no_mapping;
249 ++allow_keys; // no mapping for nchar, but keys
250 }
251 ++no_zero_mapping; // don't map zero here
252 c = plain_vgetc();
253 LANGMAP_ADJUST(c, TRUE);
254 --no_zero_mapping;
255 if (*ctrl_w)
256 {
257 --no_mapping;
258 --allow_keys;
259 }
260#ifdef FEAT_CMDL_INFO
261 *need_flushbuf |= add_to_showcmd(c);
262#endif
263 }
264
265 // If we got CTRL-W there may be a/another count
266 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
267 {
268 *ctrl_w = TRUE;
269 cap->opcount = cap->count0; // remember first count
270 cap->count0 = 0;
271 ++no_mapping;
272 ++allow_keys; // no mapping for nchar, but keys
273 c = plain_vgetc(); // get next character
274 LANGMAP_ADJUST(c, TRUE);
275 --no_mapping;
276 --allow_keys;
277#ifdef FEAT_CMDL_INFO
278 *need_flushbuf |= add_to_showcmd(c);
279#endif
280 goto getcount; // jump back
281 }
282 }
283
284 if (c == K_CURSORHOLD)
285 {
286 // Save the count values so that ca.opcount and ca.count0 are exactly
287 // the same when coming back here after handling K_CURSORHOLD.
288 cap->oap->prev_opcount = cap->opcount;
289 cap->oap->prev_count0 = cap->count0;
290 }
291 else if (cap->opcount != 0)
292 {
293 // If we're in the middle of an operator (including after entering a
294 // yank buffer with '"') AND we had a count before the operator, then
295 // that count overrides the current value of ca.count0.
296 // What this means effectively, is that commands like "3dw" get turned
297 // into "d3w" which makes things fall into place pretty neatly.
298 // If you give a count before AND after the operator, they are
299 // multiplied.
300 if (cap->count0)
301 {
302 if (cap->opcount >= 999999999L / cap->count0)
303 cap->count0 = 999999999L;
304 else
305 cap->count0 *= cap->opcount;
306 }
307 else
308 cap->count0 = cap->opcount;
309 }
310
311 // Always remember the count. It will be set to zero (on the next call,
312 // above) when there is no pending operator.
313 // When called from main(), save the count for use by the "count" built-in
314 // variable.
315 cap->opcount = cap->count0;
316 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
317
318#ifdef FEAT_EVAL
319 // Only set v:count when called from main() and not a stuffed command.
320 // Do set it for redo.
321 if (toplevel && readbuf1_empty())
322 set_vcount(cap->count0, cap->count1, set_prevcount);
323#endif
324
325 return c;
326}
327
328/*
329 * Returns TRUE if the normal command (cap) needs a second character.
330 */
331 static int
332normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
333{
334 return ((cmd_flags & NV_NCH)
335 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
336 && cap->oap->op_type == OP_NOP)
337 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
338 || (cap->cmdchar == 'q'
339 && cap->oap->op_type == OP_NOP
340 && reg_recording == 0
341 && reg_executing == 0)
342 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
343 && (cap->oap->op_type != OP_NOP || VIsual_active))));
344}
345
346/*
347 * Get one or more additional characters for a normal command.
348 * Return the updated command index (if changed).
349 */
350 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000351normal_cmd_get_more_chars(
352 int idx_arg,
353 cmdarg_T *cap,
354 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000355{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000356 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000357 int c;
358 int *cp;
359 int repl = FALSE; // get character for replace mode
360 int lit = FALSE; // get extra character literally
361 int langmap_active = FALSE; // using :lmap mappings
362 int lang; // getting a text character
363#ifdef HAVE_INPUT_METHOD
364 int save_smd; // saved value of p_smd
365#endif
366
367 ++no_mapping;
368 ++allow_keys; // no mapping for nchar, but allow key codes
369 // Don't generate a CursorHold event here, most commands can't handle
370 // it, e.g., nv_replace(), nv_csearch().
371 did_cursorhold = TRUE;
372 if (cap->cmdchar == 'g')
373 {
374 /*
375 * For 'g' get the next character now, so that we can check for
376 * "gr", "g'" and "g`".
377 */
378 cap->nchar = plain_vgetc();
379 LANGMAP_ADJUST(cap->nchar, TRUE);
380#ifdef FEAT_CMDL_INFO
381 *need_flushbuf |= add_to_showcmd(cap->nchar);
382#endif
383 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
384 || cap->nchar == Ctrl_BSL)
385 {
386 cp = &cap->extra_char; // need to get a third character
387 if (cap->nchar != 'r')
388 lit = TRUE; // get it literally
389 else
390 repl = TRUE; // get it in replace mode
391 }
392 else
393 cp = NULL; // no third character needed
394 }
395 else
396 {
397 if (cap->cmdchar == 'r') // get it in replace mode
398 repl = TRUE;
399 cp = &cap->nchar;
400 }
401 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
402
403 /*
404 * Get a second or third character.
405 */
406 if (cp != NULL)
407 {
408 if (repl)
409 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100410 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000411#ifdef CURSOR_SHAPE
412 ui_cursor_shape(); // show different cursor shape
413#endif
414 }
415 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
416 {
417 // Allow mappings defined with ":lmap".
418 --no_mapping;
419 --allow_keys;
420 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100421 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000422 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100423 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000424 langmap_active = TRUE;
425 }
426#ifdef HAVE_INPUT_METHOD
427 save_smd = p_smd;
428 p_smd = FALSE; // Don't let the IM code show the mode here
429 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
430 im_set_active(TRUE);
431#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100432 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000433 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100434 MAY_WANT_TO_LOG_THIS;
435
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000436 // Disable bracketed paste and modifyOtherKeys here, we won't
437 // recognize the escape sequences with 'esckeys' off.
438 out_str(T_BD);
439 out_str(T_CTE);
440 }
441
442 *cp = plain_vgetc();
443
Bram Moolenaar24959102022-05-07 20:01:16 +0100444 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000445 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100446 MAY_WANT_TO_LOG_THIS;
447
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000448 // Re-enable bracketed paste mode and modifyOtherKeys
449 out_str(T_BE);
450 out_str(T_CTI);
451 }
452
453 if (langmap_active)
454 {
455 // Undo the decrement done above
456 ++no_mapping;
457 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100458 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000459 }
460#ifdef HAVE_INPUT_METHOD
461 if (lang)
462 {
463 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
464 im_save_status(&curbuf->b_p_iminsert);
465 im_set_active(FALSE);
466 }
467 p_smd = save_smd;
468#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100469 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000470#ifdef FEAT_CMDL_INFO
471 *need_flushbuf |= add_to_showcmd(*cp);
472#endif
473
474 if (!lit)
475 {
476#ifdef FEAT_DIGRAPHS
477 // Typing CTRL-K gets a digraph.
478 if (*cp == Ctrl_K
479 && ((nv_cmds[idx].cmd_flags & NV_LANG)
480 || cp == &cap->extra_char)
481 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
482 {
483 c = get_digraph(FALSE);
484 if (c > 0)
485 {
486 *cp = c;
487# ifdef FEAT_CMDL_INFO
488 // Guessing how to update showcmd here...
489 del_from_showcmd(3);
490 *need_flushbuf |= add_to_showcmd(*cp);
491# endif
492 }
493 }
494#endif
495
496 // adjust chars > 127, except after "tTfFr" commands
497 LANGMAP_ADJUST(*cp, !lang);
498#ifdef FEAT_RIGHTLEFT
499 // adjust Hebrew mapped char
500 if (p_hkmap && lang && KeyTyped)
501 *cp = hkmap(*cp);
502#endif
503 }
504
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000505 // When the next character is CTRL-\ a following CTRL-N means the
506 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000507 if (cp == &cap->extra_char
508 && cap->nchar == Ctrl_BSL
509 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
510 {
511 cap->cmdchar = Ctrl_BSL;
512 cap->nchar = cap->extra_char;
513 idx = find_command(cap->cmdchar);
514 }
515 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
516 cap->oap->op_type = get_op_type(*cp, NUL);
517 else if (*cp == Ctrl_BSL)
518 {
519 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
520
521 // There is a busy wait here when typing "f<C-\>" and then
522 // something different from CTRL-N. Can't be avoided.
523 while ((c = vpeekc()) <= 0 && towait > 0L)
524 {
525 do_sleep(towait > 50L ? 50L : towait, FALSE);
526 towait -= 50L;
527 }
528 if (c > 0)
529 {
530 c = plain_vgetc();
531 if (c != Ctrl_N && c != Ctrl_G)
532 vungetc(c);
533 else
534 {
535 cap->cmdchar = Ctrl_BSL;
536 cap->nchar = c;
537 idx = find_command(cap->cmdchar);
538 }
539 }
540 }
541
542 // When getting a text character and the next character is a
543 // multi-byte character, it could be a composing character.
544 // However, don't wait for it to arrive. Also, do enable mapping,
545 // because if it's put back with vungetc() it's too late to apply
546 // mapping.
547 --no_mapping;
548 while (enc_utf8 && lang && (c = vpeekc()) > 0
549 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
550 {
551 c = plain_vgetc();
552 if (!utf_iscomposing(c))
553 {
554 vungetc(c); // it wasn't, put it back
555 break;
556 }
557 else if (cap->ncharC1 == 0)
558 cap->ncharC1 = c;
559 else
560 cap->ncharC2 = c;
561 }
562 ++no_mapping;
563 }
564 --no_mapping;
565 --allow_keys;
566
567 return idx;
568}
569
570/*
571 * Returns TRUE if after processing a normal mode command, need to wait for a
572 * moment when a message is displayed that will be overwritten by the mode
573 * message.
574 */
575 static int
576normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
577{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000578 // In Visual mode and with "^O" in Insert mode, a short message will be
579 // overwritten by the mode message. Wait a bit, until a key is hit.
580 // In Visual mode, it's more important to keep the Visual area updated
581 // than keeping a message (e.g. from a /pat search).
582 // Only do this if the command was typed, not from a mapping.
583 // Don't wait when emsg_silent is non-zero.
584 // Also wait a bit after an error message, e.g. for "^O:".
585 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000586 return ( ((p_smd
587 && msg_silent == 0
588 && (restart_edit != 0
589 || (VIsual_active
590 && old_pos->lnum == curwin->w_cursor.lnum
591 && old_pos->col == curwin->w_cursor.col)
592 )
593 && (clear_cmdline
594 || redraw_cmdline)
595 && (msg_didout || (msg_didany && msg_scroll))
596 && !msg_nowait
597 && KeyTyped)
598 || (restart_edit != 0
599 && !VIsual_active
600 && (msg_scroll
601 || emsg_on_display)))
602 && cap->oap->regname == 0
603 && !(cap->retval & CA_COMMAND_BUSY)
604 && stuff_empty()
605 && typebuf_typed()
606 && emsg_silent == 0
607 && !in_assert_fails
608 && !did_wait_return
609 && cap->oap->op_type == OP_NOP);
610}
611
612/*
613 * After processing a normal mode command, wait for a moment when a message is
614 * displayed that will be overwritten by the mode message.
615 */
616 static void
617normal_cmd_wait_for_msg(void)
618{
619 int save_State = State;
620
621 // Draw the cursor with the right shape here
622 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100623 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000624
625 // If need to redraw, and there is a "keep_msg", redraw before the
626 // delay
627 if (must_redraw && keep_msg != NULL && !emsg_on_display)
628 {
629 char_u *kmsg;
630
631 kmsg = keep_msg;
632 keep_msg = NULL;
633 // Showmode() will clear keep_msg, but we want to use it anyway.
634 // First update w_topline.
635 setcursor();
636 update_screen(0);
637 // now reset it, otherwise it's put in the history again
638 keep_msg = kmsg;
639
640 kmsg = vim_strsave(keep_msg);
641 if (kmsg != NULL)
642 {
643 msg_attr((char *)kmsg, keep_msg_attr);
644 vim_free(kmsg);
645 }
646 }
647 setcursor();
648#ifdef CURSOR_SHAPE
649 ui_cursor_shape(); // may show different cursor shape
650#endif
651 cursor_on();
652 out_flush();
653 if (msg_scroll || emsg_on_display)
654 ui_delay(1003L, TRUE); // wait at least one second
655 ui_delay(3003L, FALSE); // wait up to three seconds
656 State = save_State;
657
658 msg_scroll = FALSE;
659 emsg_on_display = FALSE;
660}
661
662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 * Execute a command in Normal mode.
664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100666normal_cmd(
667 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000674 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100675 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 static int old_mapped_len = 0;
678 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000679 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200680 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
Bram Moolenaara80faa82020-04-12 19:37:17 +0200682 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000684
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 // Use a count remembered from before entering an operator. After typing
686 // "3d" we return from normal_cmd() and come back here, the "3" is
687 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ca.opcount = opcount;
689
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000690 // If there is an operator pending, then the command we take this time
691 // will terminate it. Finish_op tells us to finish the operation before
692 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef CURSOR_SHAPE
694 c = finish_op;
695#endif
696 finish_op = (oap->op_type != OP_NOP);
697#ifdef CURSOR_SHAPE
698 if (finish_op != c)
699 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100700 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701# ifdef FEAT_MOUSESHAPE
702 update_mouseshape(-1);
703# endif
704 }
705#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100706 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100708 // When not finishing an operator and no register name typed, reset the
709 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000713#ifdef FEAT_EVAL
714 set_prevcount = TRUE;
715#endif
716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 // Restore counts from before receiving K_CURSORHOLD. This means after
719 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
720 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000721 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
722 {
723 ca.opcount = oap->prev_opcount;
724 ca.count0 = oap->prev_count0;
725 oap->prev_opcount = 0;
726 oap->prev_count0 = 0;
727 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaar24959102022-05-07 20:01:16 +0100731 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100733 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
735
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100736#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 // Set v:count here, when called from main() and not a stuffed
738 // command, so that v:count can be used in an expression mapping
739 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100740 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100741 set_vcount_ca(&ca, &set_prevcount);
742#endif
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 /*
745 * Get the command character from the user.
746 */
747 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100748 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000750 // If a mapping was started in Visual or Select mode, remember the length
751 // of the mapping. This is used below to not return to Insert mode for as
752 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (restart_edit == 0)
754 old_mapped_len = 0;
755 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000756 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 old_mapped_len = typebuf_maplen();
758
759 if (c == NUL)
760 c = K_ZERO;
761
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000762 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (VIsual_active
764 && VIsual_select
765 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
766 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000767 int len;
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
770 // 'insertmode' is set) fake a "d"elete command, Insert mode will
771 // restart automatically.
772 // Insert the typed character in the typeahead buffer, so that it can
773 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000774 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
775
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000776 // When recording and gotchars() was called the character will be
777 // recorded again, remove the previous recording.
778 if (KeyTyped)
779 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000780
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000781 if (restart_edit != 0)
782 c = 'd';
783 else
784 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 msg_nowait = TRUE; // don't delay going to insert mode
786 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000789 // If the window was made so small that nothing shows, make it at least one
790 // line and one column when typing a command.
791 if (KeyTyped && !KeyStuffed)
792 win_ensure_size();
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794#ifdef FEAT_CMDL_INFO
795 need_flushbuf = add_to_showcmd(c);
796#endif
797
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000798 // Get the command count
799 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
800 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000802 // Find the command character in the table of commands.
803 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (ctrl_w)
805 {
806 ca.nchar = c;
807 ca.cmdchar = Ctrl_W;
808 }
809 else
810 ca.cmdchar = c;
811 idx = find_command(ca.cmdchar);
812 if (idx < 0)
813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100814 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 clearopbeep(oap);
816 goto normal_end;
817 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000818
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100819 if ((nv_cmds[idx].cmd_flags & NV_NCW)
820 && (check_text_locked(oap) || curbuf_locked()))
821 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000822 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000824 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (VIsual_active)
826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (km_stopsel
829 && (nv_cmds[idx].cmd_flags & NV_STS)
830 && !(mod_mask & MOD_MASK_SHIFT))
831 {
832 end_visual_mode();
833 redraw_curbuf_later(INVERTED);
834 }
835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (km_startsel)
838 {
839 if (nv_cmds[idx].cmd_flags & NV_SS)
840 {
841 unshift_special(&ca);
842 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000843 if (idx < 0)
844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000846 clearopbeep(oap);
847 goto normal_end;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
851 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856#ifdef FEAT_RIGHTLEFT
857 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
858 && (nv_cmds[idx].cmd_flags & NV_RL))
859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 // Invert horizontal movements and operations. Only when typed by the
861 // user directly, not when the result of a mapping or "x" translated
862 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 switch (ca.cmdchar)
864 {
865 case 'l': ca.cmdchar = 'h'; break;
866 case K_RIGHT: ca.cmdchar = K_LEFT; break;
867 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
868 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
869 case 'h': ca.cmdchar = 'l'; break;
870 case K_LEFT: ca.cmdchar = K_RIGHT; break;
871 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
872 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
873 case '>': ca.cmdchar = '<'; break;
874 case '<': ca.cmdchar = '>'; break;
875 }
876 idx = find_command(ca.cmdchar);
877 }
878#endif
879
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000880 // Get additional characters if we need them.
881 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
882 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884#ifdef FEAT_CMDL_INFO
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();
891#endif
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
996#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +0000997 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100998 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 clear_showcmd();
1000#endif
1001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001002 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 vim_free(ca.searchbuf);
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (has_mbyte)
1006 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (curwin->w_p_scb && toplevel)
1009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001010 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 do_check_scrollbind(TRUE);
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar860cae12010-06-05 23:22:07 +02001014 if (curwin->w_p_crb && toplevel)
1015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001016 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001017 do_check_cursorbind();
1018 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001019
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001020#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001021 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001022 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001023 restart_edit = 0;
1024#endif
1025
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001026 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1027 // if still inside a mapping that started in Visual mode).
1028 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1031 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && !(ca.retval & CA_COMMAND_BUSY)
1033 && stuff_empty()
1034 && oap->regname == 0)
1035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (restart_VIsual_select == 1)
1037 {
1038 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001039 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 showmode();
1041 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001042 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001044 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 (void)edit(restart_edit, FALSE, 1L);
1046 }
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (restart_VIsual_select == 2)
1049 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001051 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 opcount = ca.opcount;
1053}
1054
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001055#ifdef FEAT_EVAL
1056/*
1057 * Set v:count and v:count1 according to "cap".
1058 * Set v:prevcount only when "set_prevcount" is TRUE.
1059 */
1060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001062{
1063 long count = cap->count0;
1064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001065 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001066 if (cap->opcount != 0)
1067 count = cap->opcount * (count == 0 ? 1 : count);
1068 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001070}
1071#endif
1072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001074 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 * if not.
1076 */
1077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001078check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
1080 static int did_check = FALSE;
1081
1082 if (full_screen)
1083 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001084 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001085 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 did_check = TRUE;
1087 }
1088}
1089
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001090#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1091/*
1092 * Call yank_do_autocmd() for "regname".
1093 */
1094 static void
1095call_yank_do_autocmd(int regname)
1096{
1097 oparg_T oa;
1098 yankreg_T *reg;
1099
1100 clear_oparg(&oa);
1101 oa.regname = regname;
1102 oa.op_type = OP_YANK;
1103 oa.is_VIsual = TRUE;
1104 reg = get_register(regname, TRUE);
1105 yank_do_autocmd(&oa, reg);
1106 free_register(reg);
1107}
1108#endif
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001111 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001112 * This function or the next should ALWAYS be called to end Visual mode, except
1113 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
1115 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001116end_visual_mode()
1117{
1118 end_visual_mode_keep_button();
1119 reset_held_button();
1120}
1121
1122 void
1123end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001126 // If we are using the clipboard, then remember what was selected in case
1127 // we need to paste it somewhere while we still own the selection.
1128 // Only do this when the clipboard is already owned. Don't want to grab
1129 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (clip_star.available && clip_star.owned)
1131 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001132
1133# if defined(FEAT_EVAL)
1134 // Emit a TextYankPost for the automatic copy of the selection into the
1135 // star and/or plus register.
1136 if (has_textyankpost())
1137 {
1138 if (clip_isautosel_star())
1139 call_yank_do_autocmd('*');
1140 if (clip_isautosel_plus())
1141 call_yank_do_autocmd('+');
1142 }
1143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145
1146 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 setmouse();
1148 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001150 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001151 curbuf->b_visual.vi_mode = VIsual_mode;
1152 curbuf->b_visual.vi_start = VIsual;
1153 curbuf->b_visual.vi_end = curwin->w_cursor;
1154 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155#ifdef FEAT_EVAL
1156 curbuf->b_visual_mode_eval = VIsual_mode;
1157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (!virtual_active())
1159 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001160 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001162 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001163 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164}
1165
1166/*
1167 * Reset VIsual_active and VIsual_reselect.
1168 */
1169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001170reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 if (VIsual_active)
1173 {
1174 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001175 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177 VIsual_reselect = FALSE;
1178}
1179
1180/*
1181 * Reset VIsual_active and VIsual_reselect if it's set.
1182 */
1183 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001184reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 if (VIsual_active)
1187 {
1188 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 VIsual_reselect = FALSE;
1191 }
1192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001194 void
1195restore_visual_mode(void)
1196{
1197 if (VIsual_mode_orig != NUL)
1198 {
1199 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1200 VIsual_mode_orig = NUL;
1201 }
1202}
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204/*
1205 * Check for a balloon-eval special item to include when searching for an
1206 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1207 * Returns TRUE if the character at "*ptr" should be included.
1208 * "dir" is FORWARD or BACKWARD, the direction of searching.
1209 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1210 * "bnp" points to a counter for square brackets.
1211 */
1212 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001213find_is_eval_item(
1214 char_u *ptr,
1215 int *colp,
1216 int *bnp,
1217 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1221 ++*bnp;
1222 if (*bnp > 0)
1223 {
1224 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1225 --*bnp;
1226 return TRUE;
1227 }
1228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (*ptr == '.')
1231 return TRUE;
1232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001233 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1235 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1236 {
1237 *colp += dir;
1238 return TRUE;
1239 }
1240 return FALSE;
1241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243/*
1244 * Find the identifier under or to the right of the cursor.
1245 * "find_type" can have one of three values:
1246 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001247 * FIND_STRING: find any non-white text
1248 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001249 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 *
1251 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001252 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001254 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * This doesn't match the real Vi but I like it a little better and it
1256 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001257 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 * When FIND_IDENT isn't defined, we backup until a blank.
1259 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001260 * Returns the length of the text, or zero if no text is found.
1261 * If text is found, a pointer to the text is put in "*text". This
1262 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 */
1264 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001265find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001268 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269}
1270
1271/*
1272 * Like find_ident_under_cursor(), but for any window and any position.
1273 * However: Uses 'iskeyword' from the current window!.
1274 */
1275 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001276find_ident_at_pos(
1277 win_T *wp,
1278 linenr_T lnum,
1279 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001280 char_u **text,
1281 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001282 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001285 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 int this_class = 0;
1288 int prev_class;
1289 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001290 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001292 // if i == 0: try to find an identifier
1293 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1295 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1296 {
1297 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001298 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 */
1300 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (has_mbyte)
1302 {
1303 while (ptr[col] != NUL)
1304 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001305 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 this_class = mb_get_class(ptr + col);
1309 if (this_class != 0 && (i == 1 || this_class != 1))
1310 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001311 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
1314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001316 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 )
1319 ++col;
1320
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001321 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001325 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (has_mbyte)
1328 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1331 this_class = mb_get_class((char_u *)"a");
1332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001334 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1337 prev_class = mb_get_class(ptr + prevcol);
1338 if (this_class != prev_class
1339 && (i == 0
1340 || prev_class == 0
1341 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 && (!(find_type & FIND_EVAL)
1343 || prevcol == 0
1344 || !find_is_eval_item(ptr + prevcol, &prevcol,
1345 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 )
1347 break;
1348 col = prevcol;
1349 }
1350
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001351 // If we don't want just any old text, or we've found an
1352 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (this_class > 2)
1354 this_class = 2;
1355 if (!(find_type & FIND_STRING) || this_class == 2)
1356 break;
1357 }
1358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 while (col > 0
1361 && ((i == 0
1362 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001363 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 && (!(find_type & FIND_IDENT)
1365 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 || ((find_type & FIND_EVAL)
1367 && col > 1
1368 && find_is_eval_item(ptr + col - 1, &col,
1369 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ))
1371 --col;
1372
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001373 // If we don't want just any old text, or we've found an
1374 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1376 break;
1377 }
1378 }
1379
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001380 if (ptr[col] == NUL || (i == 0
1381 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001383 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001384 if ((find_type & FIND_NOERROR) == 0)
1385 {
1386 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001387 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001388 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001389 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 return 0;
1392 }
1393 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001394 *text = ptr;
1395 if (textcol != NULL)
1396 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
1398 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001399 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 bn = 0;
1402 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (has_mbyte)
1405 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001406 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 this_class = mb_get_class(ptr);
1408 while (ptr[col] != NUL
1409 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1410 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 || ((find_type & FIND_EVAL)
1412 && col <= (int)startcol
1413 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001415 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
1417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001419 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 || ((find_type & FIND_EVAL)
1421 && col <= (int)startcol
1422 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 return col;
1427}
1428
1429/*
1430 * Prepare for redo of a normal command.
1431 */
1432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001433prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 prep_redo(cap->oap->regname, cap->count0,
1436 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1437}
1438
1439/*
1440 * Prepare for redo of any command.
1441 * Note that only the last argument can be a multi-byte char.
1442 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001443 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001444prep_redo(
1445 int regname,
1446 long num,
1447 int cmd1,
1448 int cmd2,
1449 int cmd3,
1450 int cmd4,
1451 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001453 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1454}
1455
1456/*
1457 * Prepare for redo of any command with extra count after "cmd2".
1458 */
1459 void
1460prep_redo_num2(
1461 int regname,
1462 long num1,
1463 int cmd1,
1464 int cmd2,
1465 long num2,
1466 int cmd3,
1467 int cmd4,
1468 int cmd5)
1469{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 AppendCharToRedobuff('"');
1474 AppendCharToRedobuff(regname);
1475 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001476 if (num1 != 0)
1477 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (cmd1 != NUL)
1479 AppendCharToRedobuff(cmd1);
1480 if (cmd2 != NUL)
1481 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001482 if (num2 != 0)
1483 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (cmd3 != NUL)
1485 AppendCharToRedobuff(cmd3);
1486 if (cmd4 != NUL)
1487 AppendCharToRedobuff(cmd4);
1488 if (cmd5 != NUL)
1489 AppendCharToRedobuff(cmd5);
1490}
1491
1492/*
1493 * check for operator active and clear it
1494 *
1495 * return TRUE if operator was active
1496 */
1497 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001498checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 if (oap->op_type == OP_NOP)
1501 return FALSE;
1502 clearopbeep(oap);
1503 return TRUE;
1504}
1505
1506/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001507 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001509 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001514 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 return FALSE;
1516 clearopbeep(oap);
1517 return TRUE;
1518}
1519
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001521clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 oap->op_type = OP_NOP;
1524 oap->regname = 0;
1525 oap->motion_force = NUL;
1526 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001527 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528}
1529
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001531clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 clearop(oap);
1534 beep_flush();
1535}
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537/*
1538 * Remove the shift modifier from a special key.
1539 */
1540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001541unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 switch (cap->cmdchar)
1544 {
1545 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1546 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1547 case K_S_UP: cap->cmdchar = K_UP; break;
1548 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1549 case K_S_HOME: cap->cmdchar = K_HOME; break;
1550 case K_S_END: cap->cmdchar = K_END; break;
1551 }
1552 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001555/*
1556 * If the mode is currently displayed clear the command line or update the
1557 * command displayed.
1558 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001560may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001561{
1562 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001563 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001564#ifdef FEAT_CMDL_INFO
1565 else
1566 clear_showcmd();
1567#endif
1568}
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1571/*
1572 * Routines for displaying a partly typed command
1573 */
1574
kylo252ae6f1d82022-02-16 19:24:07 +00001575#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001577static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578static int showcmd_is_clear = TRUE;
1579static int showcmd_visual = FALSE;
1580
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001581static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001584clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 if (!p_sc)
1587 return;
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (VIsual_active && !char_avail())
1590 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001591 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 long lines;
1593 colnr_T leftcol, rightcol;
1594 linenr_T top, bot;
1595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001597 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 top = VIsual.lnum;
1600 bot = curwin->w_cursor.lnum;
1601 }
1602 else
1603 {
1604 top = curwin->w_cursor.lnum;
1605 bot = VIsual.lnum;
1606 }
1607# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001609 (void)hasFolding(top, &top, NULL);
1610 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611# endif
1612 lines = bot - top + 1;
1613
1614 if (VIsual_mode == Ctrl_V)
1615 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001616# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001617 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001618 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001621 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001622 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001625# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001626 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001627 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1630 (long)(rightcol - leftcol + 1));
1631 }
1632 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1633 sprintf((char *)showcmd_buf, "%ld", lines);
1634 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001635 {
1636 char_u *s, *e;
1637 int l;
1638 int bytes = 0;
1639 int chars = 0;
1640
1641 if (cursor_bot)
1642 {
1643 s = ml_get_pos(&VIsual);
1644 e = ml_get_cursor();
1645 }
1646 else
1647 {
1648 s = ml_get_cursor();
1649 e = ml_get_pos(&VIsual);
1650 }
1651 while ((*p_sel != 'e') ? s <= e : s < e)
1652 {
1653 l = (*mb_ptr2len)(s);
1654 if (l == 0)
1655 {
1656 ++bytes;
1657 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001658 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001659 }
1660 bytes += l;
1661 ++chars;
1662 s += l;
1663 }
1664 if (bytes == chars)
1665 sprintf((char *)showcmd_buf, "%d", chars);
1666 else
1667 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 showcmd_visual = TRUE;
1671 }
1672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 showcmd_buf[0] = NUL;
1675 showcmd_visual = FALSE;
1676
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001677 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (showcmd_is_clear)
1679 return;
1680 }
1681
1682 display_showcmd();
1683}
1684
1685/*
1686 * Add 'c' to string of shown command chars.
1687 * Return TRUE if output has been written (and setcursor() has been called).
1688 */
1689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001690add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 char_u *p;
1693 int old_len;
1694 int extra_len;
1695 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int i;
1697 static int ignore[] =
1698 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1701 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001702#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001703 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001704 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1706 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001707 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001709 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 0
1711 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar09df3122006-01-23 22:23:09 +00001713 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return FALSE;
1715
1716 if (showcmd_visual)
1717 {
1718 showcmd_buf[0] = NUL;
1719 showcmd_visual = FALSE;
1720 }
1721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (IS_SPECIAL(c))
1724 for (i = 0; ignore[i] != 0; ++i)
1725 if (ignore[i] == c)
1726 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001729 if (*p == ' ')
1730 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 old_len = (int)STRLEN(showcmd_buf);
1732 extra_len = (int)STRLEN(p);
1733 overflow = old_len + extra_len - SHOWCMD_COLS;
1734 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001735 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1736 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 STRCAT(showcmd_buf, p);
1738
1739 if (char_avail())
1740 return FALSE;
1741
1742 display_showcmd();
1743
1744 return TRUE;
1745}
1746
1747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001748add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 if (!add_to_showcmd(c))
1751 setcursor();
1752}
1753
1754/*
1755 * Delete 'len' characters from the end of the shown command.
1756 */
1757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 int old_len;
1761
1762 if (!p_sc)
1763 return;
1764
1765 old_len = (int)STRLEN(showcmd_buf);
1766 if (len > old_len)
1767 len = old_len;
1768 showcmd_buf[old_len - len] = NUL;
1769
1770 if (!char_avail())
1771 display_showcmd();
1772}
1773
1774/*
1775 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1776 * something and there is a partial mapping.
1777 */
1778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 if (p_sc)
1782 STRCPY(old_showcmd_buf, showcmd_buf);
1783}
1784
1785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 if (!p_sc)
1789 return;
1790
1791 STRCPY(showcmd_buf, old_showcmd_buf);
1792
1793 display_showcmd();
1794}
1795
1796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 int len;
1800
1801 cursor_off();
1802
1803 len = (int)STRLEN(showcmd_buf);
1804 if (len == 0)
1805 showcmd_is_clear = TRUE;
1806 else
1807 {
1808 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1809 showcmd_is_clear = FALSE;
1810 }
1811
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001812 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1813 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001816 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817}
1818#endif
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820/*
1821 * When "check" is FALSE, prepare for commands that scroll the window.
1822 * When "check" is TRUE, take care of scroll-binding after the window has
1823 * scrolled. Called from normal_cmd() and edit().
1824 */
1825 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 static win_T *old_curwin = NULL;
1829 static linenr_T old_topline = 0;
1830#ifdef FEAT_DIFF
1831 static int old_topfill = 0;
1832#endif
1833 static buf_T *old_buf = NULL;
1834 static colnr_T old_leftcol = 0;
1835
1836 if (check && curwin->w_p_scb)
1837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001838 // If a ":syncbind" command was just used, don't scroll, only reset
1839 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (did_syncbind)
1841 did_syncbind = FALSE;
1842 else if (curwin == old_curwin)
1843 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001844 // Synchronize other windows, as necessary according to
1845 // 'scrollbind'. Don't do this after an ":edit" command, except
1846 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if ((curwin->w_buffer == old_buf
1848#ifdef FEAT_DIFF
1849 || curwin->w_p_diff
1850#endif
1851 )
1852 && (curwin->w_topline != old_topline
1853#ifdef FEAT_DIFF
1854 || curwin->w_topfill != old_topfill
1855#endif
1856 || curwin->w_leftcol != old_leftcol))
1857 {
1858 check_scrollbind(curwin->w_topline - old_topline,
1859 (long)(curwin->w_leftcol - old_leftcol));
1860 }
1861 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001862 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001864 // When switching between windows, make sure that the relative
1865 // vertical offset is valid for the new window. The relative
1866 // offset is invalid whenever another 'scrollbind' window has
1867 // scrolled to a point that would force the current window to
1868 // scroll past the beginning or end of its buffer. When the
1869 // resync is performed, some of the other 'scrollbind' windows may
1870 // need to jump so that the current window's relative position is
1871 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1873 }
1874 curwin->w_scbind_pos = curwin->w_topline;
1875 }
1876
1877 old_curwin = curwin;
1878 old_topline = curwin->w_topline;
1879#ifdef FEAT_DIFF
1880 old_topfill = curwin->w_topfill;
1881#endif
1882 old_buf = curwin->w_buffer;
1883 old_leftcol = curwin->w_leftcol;
1884}
1885
1886/*
1887 * Synchronize any windows that have "scrollbind" set, based on the
1888 * number of rows by which the current window has changed
1889 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1890 */
1891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001892check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 int want_ver;
1895 int want_hor;
1896 win_T *old_curwin = curwin;
1897 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int old_VIsual_select = VIsual_select;
1899 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 colnr_T tgt_leftcol = curwin->w_leftcol;
1901 long topline;
1902 long y;
1903
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001904 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1906#ifdef FEAT_DIFF
1907 want_ver |= old_curwin->w_p_diff;
1908#endif
1909 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1910
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001911 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001913 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001916 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (curwin != old_curwin && curwin->w_p_scb)
1918 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001919 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (want_ver)
1921 {
1922#ifdef FEAT_DIFF
1923 if (old_curwin->w_p_diff && curwin->w_p_diff)
1924 {
1925 diff_set_topline(old_curwin, curwin);
1926 }
1927 else
1928#endif
1929 {
1930 curwin->w_scbind_pos += topline_diff;
1931 topline = curwin->w_scbind_pos;
1932 if (topline > curbuf->b_ml.ml_line_count)
1933 topline = curbuf->b_ml.ml_line_count;
1934 if (topline < 1)
1935 topline = 1;
1936
1937 y = topline - curwin->w_topline;
1938 if (y > 0)
1939 scrollup(y, FALSE);
1940 else
1941 scrolldown(-y, FALSE);
1942 }
1943
1944 redraw_later(VALID);
1945 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001949 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (want_hor && curwin->w_leftcol != tgt_leftcol)
1951 {
1952 curwin->w_leftcol = tgt_leftcol;
1953 leftcol_changed();
1954 }
1955 }
1956 }
1957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001958 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 VIsual_select = old_VIsual_select;
1960 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 curwin = old_curwin;
1962 curbuf = old_curbuf;
1963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965/*
1966 * Command character that's ignored.
1967 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001968 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001973 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974}
1975
1976/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001977 * Command character that doesn't do anything, but unlike nv_ignore() does
1978 * start edit(). Used for "startinsert" executed while starting up.
1979 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001981nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001982{
1983}
1984
1985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 * Command character doesn't exist.
1987 */
1988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001989nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 clearopbeep(cap->oap);
1992}
1993
1994/*
1995 * <Help> and <F1> commands.
1996 */
1997 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001998nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 if (!checkclearopq(cap->oap))
2001 ex_help(NULL);
2002}
2003
2004/*
2005 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2006 */
2007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002010#ifdef FEAT_JOB_CHANNEL
2011 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2012 clearopbeep(cap->oap);
2013 else
2014#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002015 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002016 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002017 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002018 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2019 op_addsub(cap->oap, cap->count1, cap->arg);
2020 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002021 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002022 else if (VIsual_active)
2023 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002024 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002025 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026}
2027
2028/*
2029 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2030 */
2031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002032nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002035 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002036 if (mod_mask & MOD_MASK_CTRL)
2037 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002038 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 if (cap->arg == BACKWARD)
2040 goto_tabpage(-(int)cap->count1);
2041 else
2042 goto_tabpage((int)cap->count0);
2043 }
2044 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002045 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047}
2048
2049/*
2050 * Implementation of "gd" and "gD" command.
2051 */
2052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053nv_gd(
2054 oparg_T *oap,
2055 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002056 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 char_u *ptr;
2060
Bram Moolenaard9d30582005-05-18 22:10:28 +00002061 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002062 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002063 == FAIL)
2064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002066 }
2067 else
2068 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002069#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002070 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2071 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002072#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002073 // clear any search statistics
2074 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2075 clear_cmdline = TRUE;
2076 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002077}
2078
2079/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002080 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2081 * otherwise.
2082 */
2083 static int
2084is_ident(char_u *line, int offset)
2085{
2086 int i;
2087 int incomment = FALSE;
2088 int instring = 0;
2089 int prev = 0;
2090
2091 for (i = 0; i < offset && line[i] != NUL; i++)
2092 {
2093 if (instring != 0)
2094 {
2095 if (prev != '\\' && line[i] == instring)
2096 instring = 0;
2097 }
2098 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2099 {
2100 instring = line[i];
2101 }
2102 else
2103 {
2104 if (incomment)
2105 {
2106 if (prev == '*' && line[i] == '/')
2107 incomment = FALSE;
2108 }
2109 else if (prev == '/' && line[i] == '*')
2110 {
2111 incomment = TRUE;
2112 }
2113 else if (prev == '/' && line[i] == '/')
2114 {
2115 return FALSE;
2116 }
2117 }
2118
2119 prev = line[i];
2120 }
2121
2122 return incomment == FALSE && instring == 0;
2123}
2124
2125/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002126 * Search for variable declaration of "ptr[len]".
2127 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2128 * current file ("gD").
2129 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002130 * Return FAIL when not found.
2131 */
2132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133find_decl(
2134 char_u *ptr,
2135 int len,
2136 int locally,
2137 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002139{
2140 char_u *pat;
2141 pos_T old_pos;
2142 pos_T par_pos;
2143 pos_T found_pos;
2144 int t;
2145 int save_p_ws;
2146 int save_p_scs;
2147 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002148 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002149 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002150 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002151
2152 if ((pat = alloc(len + 7)) == NULL)
2153 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002155 // Put "\V" before the pattern to avoid that the special meaning of "."
2156 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002157 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2158 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 old_pos = curwin->w_cursor;
2160 save_p_ws = p_ws;
2161 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002162 p_ws = FALSE; // don't wrap around end of file now
2163 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002165 // With "gD" go to line 1.
2166 // With "gd" Search back for the start of the current function, then go
2167 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002168 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002170 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002172 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174 else
2175 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002176 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2178 --curwin->w_cursor.lnum;
2179 }
2180 curwin->w_cursor.col = 0;
2181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002182 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002183 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002184 for (;;)
2185 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002186 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002187 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002188 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002190
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002191 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002192 {
2193 pos_T *pos;
2194
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002195 // Check that the block the match is in doesn't end before the
2196 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002197 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2198 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2199 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // There can't be a useful match before the end of this block.
2202 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002203 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002204 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002205 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002206 }
2207
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002208 if (t == FAIL)
2209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002211 if (found_pos.lnum != 0)
2212 {
2213 curwin->w_cursor = found_pos;
2214 t = OK;
2215 }
2216 break;
2217 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002218 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002219 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002220 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 ++curwin->w_cursor.lnum;
2222 curwin->w_cursor.col = 0;
2223 continue;
2224 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002225 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002227 // If the current position is not a valid identifier and a previous
2228 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002229 if (!valid && found_pos.lnum != 0)
2230 {
2231 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002232 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002233 }
2234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002235 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002236 if (valid && !locally)
2237 break;
2238 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002240 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241 if (found_pos.lnum != 0)
2242 curwin->w_cursor = found_pos;
2243 break;
2244 }
2245
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002246 // For finding a local variable and the match is before the "{" or
2247 // inside a comment, continue searching. For K&R style function
2248 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002249 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002250 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002251 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002252 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002253 // Remove SEARCH_START from flags to avoid getting stuck at one
2254 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002255 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002257
2258 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002260 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 curwin->w_cursor = old_pos;
2262 }
2263 else
2264 {
2265 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002266 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 reset_search_dir();
2268 }
2269
2270 vim_free(pat);
2271 p_ws = save_p_ws;
2272 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002273
2274 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275}
2276
2277/*
2278 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2279 * lines rather than lines in the file.
2280 * 'dist' must be positive.
2281 *
2282 * Return OK if able to move cursor, FAIL otherwise.
2283 */
2284 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002285nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 int linelen = linetabsize(ml_get_curline());
2288 int retval = OK;
2289 int atend = FALSE;
2290 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002291 int col_off1; // margin offset for first screen line
2292 int col_off2; // margin offset for wrapped screen line
2293 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002294 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002297 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 col_off1 = curwin_col_off();
2300 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002301 width1 = curwin->w_width - col_off1;
2302 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002303 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002304 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002307 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002308 // Instead of sticking at the last character of the buffer line we
2309 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (curwin->w_curswant == MAXCOL)
2311 {
2312 atend = TRUE;
2313 validate_virtcol();
2314 if (width1 <= 0)
2315 curwin->w_curswant = 0;
2316 else
2317 {
2318 curwin->w_curswant = width1 - 1;
2319 if (curwin->w_virtcol > curwin->w_curswant)
2320 curwin->w_curswant += ((curwin->w_virtcol
2321 - curwin->w_curswant - 1) / width2 + 1) * width2;
2322 }
2323 }
2324 else
2325 {
2326 if (linelen > width1)
2327 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2328 else
2329 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002330 if (curwin->w_curswant >= (colnr_T)n)
2331 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333
2334 while (dist--)
2335 {
2336 if (dir == BACKWARD)
2337 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002338 if ((long)curwin->w_curswant >= width1
2339#ifdef FEAT_FOLDING
2340 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2341#endif
2342 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002343 // Move back within the line. This can give a negative value
2344 // for w_curswant if width1 < width2 (with cpoptions+=n),
2345 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 curwin->w_curswant -= width2;
2347 else
2348 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002351 // Move to the start of a closed fold. Don't do that when
2352 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (!(fdo_flags & FDO_ALL))
2354 (void)hasFolding(curwin->w_cursor.lnum,
2355 &curwin->w_cursor.lnum, NULL);
2356#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002357 if (curwin->w_cursor.lnum == 1)
2358 {
2359 retval = FAIL;
2360 break;
2361 }
2362 --curwin->w_cursor.lnum;
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 linelen = linetabsize(ml_get_curline());
2365 if (linelen > width1)
2366 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2367 + 1) * width2;
2368 }
2369 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 if (linelen > width1)
2373 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2374 else
2375 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002376 if (curwin->w_curswant + width2 < (colnr_T)n
2377#ifdef FEAT_FOLDING
2378 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2379#endif
2380 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002381 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 curwin->w_curswant += width2;
2383 else
2384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2389 &curwin->w_cursor.lnum);
2390#endif
2391 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2392 {
2393 retval = FAIL;
2394 break;
2395 }
2396 curwin->w_cursor.lnum++;
2397 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002398 // Check if the cursor has moved below the number display
2399 // when width1 < width2 (with cpoptions+=n). Subtract width2
2400 // to get a negative value for w_curswant, which will get
2401 // clipped to column 0.
2402 if (curwin->w_curswant >= width1)
2403 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002404 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406 }
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002410 if (virtual_active() && atend)
2411 coladvance(MAXCOL);
2412 else
2413 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2416 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002417 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002418 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002419
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002420 // Check for landing on a character that got split at the end of the
2421 // last line. We want to advance a screenline, not end up in the same
2422 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002424 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002425#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002426 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2427 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002428#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002429
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002430 c = (*mb_ptr2char)(ml_get_cursor());
2431 if (dir == FORWARD && virtcol < curwin->w_curswant
2432 && (curwin->w_curswant <= (colnr_T)width1)
2433 && !vim_isprintc(c) && c > 255)
2434 oneright();
2435
Bram Moolenaar773b1582014-08-29 14:20:51 +02002436 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 && (curwin->w_curswant < (colnr_T)width1
2438 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2439 : ((curwin->w_curswant - width1) % width2
2440 > (colnr_T)width2 / 2)))
2441 --curwin->w_cursor.col;
2442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 return retval;
2448}
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450/*
2451 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2452 * cap->arg must be TRUE for CTRL-E.
2453 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002455nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 if (!checkclearop(cap->oap))
2458 scroll_redraw(cap->arg, cap->count1);
2459}
2460
2461/*
2462 * Scroll "count" lines up or down, and redraw.
2463 */
2464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 linenr_T prev_topline = curwin->w_topline;
2468#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 Moolenaar375e3392019-01-31 18:26:10 +01002477 if (get_scrolloff_value())
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
2489#ifdef FEAT_DIFF
2490 && curwin->w_topfill == prev_topfill
2491#endif
2492 )
2493 {
2494 if (up)
2495 {
2496 if (curwin->w_cursor.lnum > prev_lnum
2497 || cursor_down(1L, FALSE) == FAIL)
2498 break;
2499 }
2500 else
2501 {
2502 if (curwin->w_cursor.lnum < prev_lnum
2503 || prev_topline == 1L
2504 || cursor_up(1L, FALSE) == FAIL)
2505 break;
2506 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002507 // Mark w_topline as valid, otherwise the screen jumps back at the
2508 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 check_cursor_moved(curwin);
2510 curwin->w_valid |= VALID_TOPLINE;
2511 }
2512 }
2513 if (curwin->w_cursor.lnum != prev_lnum)
2514 coladvance(curwin->w_curswant);
2515 redraw_later(VALID);
2516}
2517
2518/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002519 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2520 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2521 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002522 */
2523 static int
2524nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2525{
2526 int nchar = *nchar_arg;
2527 long n;
2528
2529 // "z123{nchar}": edit the count before obtaining {nchar}
2530 if (checkclearop(cap->oap))
2531 return FALSE;
2532 n = nchar - '0';
2533
2534 for (;;)
2535 {
2536#ifdef USE_ON_FLY_SCROLL
2537 dont_scroll = TRUE; // disallow scrolling here
2538#endif
2539 ++no_mapping;
2540 ++allow_keys; // no mapping for nchar, but allow key codes
2541 nchar = plain_vgetc();
2542 LANGMAP_ADJUST(nchar, TRUE);
2543 --no_mapping;
2544 --allow_keys;
2545#ifdef FEAT_CMDL_INFO
2546 (void)add_to_showcmd(nchar);
2547#endif
2548 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;
2602#ifdef FEAT_CMDL_INFO
2603 (void)add_to_showcmd(nchar);
2604#endif
2605 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2606 {
2607 clearopbeep(cap->oap);
2608 return OK;
2609 }
2610 undo = TRUE;
2611 }
2612
2613 if (checkclearop(cap->oap))
2614 return OK;
2615 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2616 return FAIL;
2617 if (ptr == NULL)
2618 {
2619 pos_T pos = curwin->w_cursor;
2620
2621 // Find bad word under the cursor. When 'spell' is
2622 // off this fails and find_ident_under_cursor() is
2623 // used below.
2624 emsg_off++;
2625 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2626 emsg_off--;
2627 if (len != 0 && curwin->w_cursor.col <= pos.col)
2628 ptr = ml_get_pos(&curwin->w_cursor);
2629 curwin->w_cursor = pos;
2630 }
2631
2632 if (ptr == NULL
2633 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2634 return FAIL;
2635 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2636 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2637 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2638
2639 return OK;
2640}
2641#endif
2642
2643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * Commands that start with "z".
2645 */
2646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002647nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 long n;
2650 colnr_T col;
2651 int nchar = cap->nchar;
2652#ifdef FEAT_FOLDING
2653 long old_fdl = curwin->w_p_fdl;
2654 int old_fen = curwin->w_p_fen;
2655#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002656 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002658 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (
2662#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002663 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2664 // and "zC" only in Visual mode. "zj" and "zk" are motion
2665 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 cap->nchar != 'f' && cap->nchar != 'F'
2667 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2668 && cap->nchar != 'j' && cap->nchar != 'k'
2669 &&
2670#endif
2671 checkclearop(cap->oap))
2672 return;
2673
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002674 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2675 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2677 && cap->count0
2678 && cap->count0 != curwin->w_cursor.lnum)
2679 {
2680 setpcmark();
2681 if (cap->count0 > curbuf->b_ml.ml_line_count)
2682 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2683 else
2684 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002685 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687
2688 switch (nchar)
2689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002690 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 case '+':
2692 if (cap->count0 == 0)
2693 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002694 // No count given: put cursor at the line below screen
2695 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2697 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2698 else
2699 curwin->w_cursor.lnum = curwin->w_botline;
2700 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 case NL:
2703 case CAR:
2704 case K_KENTER:
2705 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 case 't': scroll_cursor_top(0, TRUE);
2709 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002710 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 case 'z': scroll_cursor_halfway(TRUE);
2718 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002719 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 break;
2721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // "z^", "z-" and "zb": put cursor at bottom of screen
2723 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2724 // when <count> is at bottom of window, and puts that one at
2725 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if (cap->count0 != 0)
2727 {
2728 scroll_cursor_bot(0, TRUE);
2729 curwin->w_cursor.lnum = curwin->w_topline;
2730 }
2731 else if (curwin->w_topline == 1)
2732 curwin->w_cursor.lnum = 1;
2733 else
2734 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 case '-':
2737 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
2740 case 'b': scroll_cursor_bot(0, TRUE);
2741 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002742 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 break;
2744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 case 'h':
2752 case K_LEFT:
2753 if (!curwin->w_p_wrap)
2754 {
2755 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2756 curwin->w_leftcol = 0;
2757 else
2758 curwin->w_leftcol -= (colnr_T)cap->count1;
2759 leftcol_changed();
2760 }
2761 break;
2762
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002764 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 case 'l':
2769 case K_RIGHT:
2770 if (!curwin->w_p_wrap)
2771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 curwin->w_leftcol += (colnr_T)cap->count1;
2774 leftcol_changed();
2775 }
2776 break;
2777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 case 's': if (!curwin->w_p_wrap)
2780 {
2781#ifdef FEAT_FOLDING
2782 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
2785#endif
2786 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002787 if ((long)col > siso)
2788 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 else
2790 col = 0;
2791 if (curwin->w_leftcol != col)
2792 {
2793 curwin->w_leftcol = col;
2794 redraw_later(NOT_VALID);
2795 }
2796 }
2797 break;
2798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 case 'e': if (!curwin->w_p_wrap)
2801 {
2802#ifdef FEAT_FOLDING
2803 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 else
2806#endif
2807 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002808 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002809 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 col = 0;
2811 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002812 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (curwin->w_leftcol != col)
2814 {
2815 curwin->w_leftcol = col;
2816 redraw_later(NOT_VALID);
2817 }
2818 }
2819 break;
2820
Christian Brabandt2fa93842021-05-30 22:17:25 +02002821 // "zp", "zP" in block mode put without addind trailing spaces
2822 case 'P':
2823 case 'p': nv_put(cap);
2824 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002825 // "zy" Yank without trailing spaces
2826 case 'y': nv_operator(cap);
2827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // "zF": create fold command
2830 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case 'F':
2832 case 'f': if (foldManualAllowed(TRUE))
2833 {
2834 cap->nchar = 'f';
2835 nv_operator(cap);
2836 curwin->w_p_fen = TRUE;
2837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2840 {
2841 nv_operator(cap);
2842 finish_op = TRUE;
2843 }
2844 }
2845 else
2846 clearopbeep(cap->oap);
2847 break;
2848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // "zd": delete fold at cursor
2850 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case 'd':
2852 case 'D': if (foldManualAllowed(FALSE))
2853 {
2854 if (VIsual_active)
2855 nv_operator(cap);
2856 else
2857 deleteFold(curwin->w_cursor.lnum,
2858 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2859 }
2860 break;
2861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case 'E': if (foldmethodIsManual(curwin))
2864 {
2865 clearFolding(curwin);
2866 changed_window_setting();
2867 }
2868 else if (foldmethodIsMarker(curwin))
2869 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2870 TRUE, FALSE);
2871 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002872 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
2874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 case 'n': curwin->w_p_fen = FALSE;
2877 break;
2878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 case 'N': curwin->w_p_fen = TRUE;
2881 break;
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2885 break;
2886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2889 openFold(curwin->w_cursor.lnum, cap->count1);
2890 else
2891 {
2892 closeFold(curwin->w_cursor.lnum, cap->count1);
2893 curwin->w_p_fen = TRUE;
2894 }
2895 break;
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2899 openFoldRecurse(curwin->w_cursor.lnum);
2900 else
2901 {
2902 closeFoldRecurse(curwin->w_cursor.lnum);
2903 curwin->w_p_fen = TRUE;
2904 }
2905 break;
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 case 'o': if (VIsual_active)
2909 nv_operator(cap);
2910 else
2911 openFold(curwin->w_cursor.lnum, cap->count1);
2912 break;
2913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 case 'O': if (VIsual_active)
2916 nv_operator(cap);
2917 else
2918 openFoldRecurse(curwin->w_cursor.lnum);
2919 break;
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case 'c': if (VIsual_active)
2923 nv_operator(cap);
2924 else
2925 closeFold(curwin->w_cursor.lnum, cap->count1);
2926 curwin->w_p_fen = TRUE;
2927 break;
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 case 'C': if (VIsual_active)
2931 nv_operator(cap);
2932 else
2933 closeFoldRecurse(curwin->w_cursor.lnum);
2934 curwin->w_p_fen = TRUE;
2935 break;
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 case 'v': foldOpenCursor();
2939 break;
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 curwin->w_foldinvalid = TRUE; // recompute folds
2944 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 foldOpenCursor();
2946 break;
2947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 curwin->w_foldinvalid = TRUE; // recompute folds
2951 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002956 {
2957 curwin->w_p_fdl -= cap->count1;
2958 if (curwin->w_p_fdl < 0)
2959 curwin->w_p_fdl = 0;
2960 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 curwin->w_p_fen = TRUE;
2963 break;
2964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 curwin->w_p_fen = TRUE;
2969 break;
2970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002972 case 'r': curwin->w_p_fdl += cap->count1;
2973 {
2974 int d = getDeepestNesting();
2975
2976 if (curwin->w_p_fdl >= d)
2977 curwin->w_p_fdl = d;
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 break;
2980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002981 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 break;
2985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 case 'j': // "zj" move to next fold downwards
2987 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2989 cap->count1) == FAIL)
2990 clearopbeep(cap->oap);
2991 break;
2992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002995#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 case 'g': // "zg": add good word to word list
2998 case 'w': // "zw": add wrong word to word list
2999 case 'G': // "zG": add good word to temp word list
3000 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003001 if (nv_zg_zw(cap, nchar) == FAIL)
3002 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003003 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003006 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003007 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003008 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003009#endif
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 default: clearopbeep(cap->oap);
3012 }
3013
3014#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (old_fen != curwin->w_p_fen)
3017 {
3018# ifdef FEAT_DIFF
3019 win_T *wp;
3020
3021 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3022 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 FOR_ALL_WINDOWS(wp)
3025 {
3026 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3027 {
3028 wp->w_p_fen = curwin->w_p_fen;
3029 changed_window_setting_win(wp);
3030 }
3031 }
3032 }
3033# endif
3034 changed_window_setting();
3035 }
3036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003037 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (old_fdl != curwin->w_p_fdl)
3039 newFoldLevel();
3040#endif
3041}
3042
3043#ifdef FEAT_GUI
3044/*
3045 * Vertical scrollbar movement.
3046 */
3047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003048nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 if (cap->oap->op_type != OP_NOP)
3051 clearopbeep(cap->oap);
3052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 gui_do_scroll();
3055}
3056
3057/*
3058 * Horizontal scrollbar movement.
3059 */
3060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003061nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 if (cap->oap->op_type != OP_NOP)
3064 clearopbeep(cap->oap);
3065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003066 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003067 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068}
3069#endif
3070
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003071#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003072/*
3073 * Click in GUI tab.
3074 */
3075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003076nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003077{
3078 if (cap->oap->op_type != OP_NOP)
3079 clearopbeep(cap->oap);
3080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003082 goto_tabpage(current_tab);
3083}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003084
3085/*
3086 * Selected item in tab line menu.
3087 */
3088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003089nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003090{
3091 if (cap->oap->op_type != OP_NOP)
3092 clearopbeep(cap->oap);
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003095 handle_tabmenu();
3096}
3097
3098/*
3099 * Handle selecting an item of the GUI tab line menu.
3100 * Used in Normal and Insert mode.
3101 */
3102 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003103handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003104{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003105 switch (current_tabmenu)
3106 {
3107 case TABLINE_MENU_CLOSE:
3108 if (current_tab == 0)
3109 do_cmdline_cmd((char_u *)"tabclose");
3110 else
3111 {
3112 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3113 current_tab);
3114 do_cmdline_cmd(IObuff);
3115 }
3116 break;
3117
3118 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003119 if (current_tab == 0)
3120 do_cmdline_cmd((char_u *)"$tabnew");
3121 else
3122 {
3123 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3124 current_tab - 1);
3125 do_cmdline_cmd(IObuff);
3126 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003127 break;
3128
3129 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003130 if (current_tab == 0)
3131 do_cmdline_cmd((char_u *)"browse $tabnew");
3132 else
3133 {
3134 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3135 current_tab - 1);
3136 do_cmdline_cmd(IObuff);
3137 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003138 break;
3139 }
3140}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003141#endif
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143/*
3144 * "Q" command.
3145 */
3146 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003147nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003149 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003151 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003152 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 do_exmode(FALSE);
3154}
3155
3156/*
3157 * Handle a ":" command.
3158 */
3159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003160nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003162 int old_p_im;
3163 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003164 int is_cmdkey = cap->cmdchar == K_COMMAND
3165 || cap->cmdchar == K_SCRIPT_COMMAND;
3166 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaar957cf672020-11-12 14:21:06 +01003168 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 nv_operator(cap);
3170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172 if (cap->oap->op_type != OP_NOP)
3173 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 cap->oap->motion_type = MCHAR;
3176 cap->oap->inclusive = FALSE;
3177 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003178 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 stuffcharReadbuff('.');
3182 if (cap->count0 > 1)
3183 {
3184 stuffReadbuff((char_u *)",.+");
3185 stuffnumReadbuff((long)cap->count0 - 1L);
3186 }
3187 }
3188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003189 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (KeyTyped)
3191 compute_cmdrow();
3192
3193 old_p_im = p_im;
3194
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003195 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003196 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3197 if (is_cmdkey)
3198 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3199 else
3200 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003202 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (p_im != old_p_im)
3204 {
3205 if (p_im)
3206 restart_edit = 'i';
3207 else
3208 restart_edit = 0;
3209 }
3210
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003211 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003212 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003213 clearop(cap->oap);
3214 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3216 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003217 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3218 || did_emsg
3219 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 clearopbeep(cap->oap);
3222 }
3223}
3224
3225/*
3226 * Handle CTRL-G command.
3227 */
3228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003229nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003231 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
3233 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003234 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 showmode();
3236 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003237 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 fileinfo((int)cap->count0, FALSE, TRUE);
3240}
3241
3242/*
3243 * Handle CTRL-H <Backspace> command.
3244 */
3245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003246nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (VIsual_active && VIsual_select)
3249 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003250 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 v_visop(cap);
3252 }
3253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 nv_left(cap);
3255}
3256
3257/*
3258 * CTRL-L: clear screen and redraw.
3259 */
3260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003261nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
3263 if (!checkclearop(cap->oap))
3264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003266 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003267 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003268# ifdef FEAT_RELTIME
3269 {
3270 win_T *wp;
3271
3272 FOR_ALL_WINDOWS(wp)
3273 wp->w_s->b_syn_slow = FALSE;
3274 }
3275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#endif
3277 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003278#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3279# ifdef VIMDLL
3280 if (!gui.in_use)
3281# endif
3282 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285}
3286
3287/*
3288 * CTRL-O: In Select mode: switch to Visual mode for one command.
3289 * Otherwise: Go to older pcmark.
3290 */
3291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003292nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 if (VIsual_active && VIsual_select)
3295 {
3296 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003297 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003299 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 cap->count1 = -cap->count1;
3304 nv_pcmark(cap);
3305 }
3306}
3307
3308/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003309 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3310 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 */
3312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003313nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 if (!checkclearopq(cap->oap))
3316 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3317 GETF_SETMARK|GETF_ALT, FALSE);
3318}
3319
3320/*
3321 * "Z" commands.
3322 */
3323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003324nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 if (!checkclearopq(cap->oap))
3327 {
3328 switch (cap->nchar)
3329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 case 'Z': do_cmdline_cmd((char_u *)"x");
3332 break;
3333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003334 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 case 'Q': do_cmdline_cmd((char_u *)"q!");
3336 break;
3337
3338 default: clearopbeep(cap->oap);
3339 }
3340 }
3341}
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343/*
3344 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3345 */
3346 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003347do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 oparg_T oa;
3350 cmdarg_T ca;
3351
3352 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003353 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 ca.oap = &oa;
3355 ca.cmdchar = c1;
3356 ca.nchar = c2;
3357 nv_ident(&ca);
3358}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003361 * 'K' normal-mode command. Get the command to lookup the keyword under the
3362 * cursor.
3363 */
3364 static int
3365nv_K_getcmd(
3366 cmdarg_T *cap,
3367 char_u *kp,
3368 int kp_help,
3369 int kp_ex,
3370 char_u **ptr_arg,
3371 int n,
3372 char_u *buf,
3373 unsigned buflen)
3374{
3375 char_u *ptr = *ptr_arg;
3376 int isman;
3377 int isman_s;
3378
3379 if (kp_help)
3380 {
3381 // in the help buffer
3382 STRCPY(buf, "he! ");
3383 return n;
3384 }
3385
3386 if (kp_ex)
3387 {
3388 // 'keywordprog' is an ex command
3389 if (cap->count0 != 0)
3390 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3391 else
3392 STRCPY(buf, kp);
3393 STRCAT(buf, " ");
3394 return n;
3395 }
3396
3397 // An external command will probably use an argument starting
3398 // with "-" as an option. To avoid trouble we skip the "-".
3399 while (*ptr == '-' && n > 0)
3400 {
3401 ++ptr;
3402 --n;
3403 }
3404 if (n == 0)
3405 {
3406 // found dashes only
3407 emsg(_(e_no_identifier_under_cursor));
3408 vim_free(buf);
3409 *ptr_arg = ptr;
3410 return 0;
3411 }
3412
3413 // When a count is given, turn it into a range. Is this
3414 // really what we want?
3415 isman = (STRCMP(kp, "man") == 0);
3416 isman_s = (STRCMP(kp, "man -s") == 0);
3417 if (cap->count0 != 0 && !(isman || isman_s))
3418 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3419
3420 STRCAT(buf, "! ");
3421 if (cap->count0 == 0 && isman_s)
3422 STRCAT(buf, "man");
3423 else
3424 STRCAT(buf, kp);
3425 STRCAT(buf, " ");
3426 if (cap->count0 != 0 && (isman || isman_s))
3427 {
3428 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3429 STRCAT(buf, " ");
3430 }
3431
3432 *ptr_arg = ptr;
3433 return n;
3434}
3435
3436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * Handle the commands that use the word under the cursor.
3438 * [g] CTRL-] :ta to current identifier
3439 * [g] 'K' run program for current identifier
3440 * [g] '*' / to current identifier or string
3441 * [g] '#' ? to current identifier or string
3442 * g ']' :tselect for current identifier
3443 */
3444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003445nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 char_u *ptr = NULL;
3448 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003449 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003450 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 char_u *kp; // value of 'keywordprg'
3453 int kp_help; // 'keywordprg' is ":he"
3454 int kp_ex; // 'keywordprg' starts with ":"
3455 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003458 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003461 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 cmdchar = cap->nchar;
3464 g_cmd = TRUE;
3465 }
3466 else
3467 {
3468 cmdchar = cap->cmdchar;
3469 g_cmd = FALSE;
3470 }
3471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003472 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 cmdchar = '#';
3474
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003475 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3479 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (checkclearopq(cap->oap))
3481 return;
3482 }
3483
3484 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3485 (cmdchar == '*' || cmdchar == '#')
3486 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3487 {
3488 clearop(cap->oap);
3489 return;
3490 }
3491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003492 // Allocate buffer to put the command in. Inserting backslashes can
3493 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3494 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3496 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3497 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003498 if (kp_help && *skipwhite(ptr) == NUL)
3499 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003500 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003501 return;
3502 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003503 kp_ex = (*kp == ':');
3504 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3505 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (buf == NULL)
3507 return;
3508 buf[0] = NUL;
3509
3510 switch (cmdchar)
3511 {
3512 case '*':
3513 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003514 // Put cursor at start of word, makes search skip the word
3515 // under the cursor.
3516 // Call setpcmark() first, so "*``" puts the cursor back where
3517 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 setpcmark();
3519 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3520
3521 if (!g_cmd && vim_iswordp(ptr))
3522 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 break;
3525
3526 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003527 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3528 if (n == 0)
3529 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 break;
3531
3532 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003533 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef FEAT_CSCOPE
3535 if (p_cst)
3536 STRCPY(buf, "cstag ");
3537 else
3538#endif
3539 STRCPY(buf, "ts ");
3540 break;
3541
3542 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003543 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (curbuf->b_help)
3545 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003547 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003548 if (g_cmd)
3549 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003550 else if (cap->count0 == 0)
3551 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003552 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003553 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003557 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003560 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003561 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003562 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003563 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003564 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003566 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003567 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003568 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003570 vim_free(buf);
3571 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003573 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003574 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 {
3576 vim_free(buf);
3577 vim_free(p);
3578 return;
3579 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003580 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003581 STRCAT(buf, p);
3582 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003584 else
3585 {
3586 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003587 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003588 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003589 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003590 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003591 {
3592 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003593 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003594 aux_ptr = (char_u *)"";
3595 else
3596 aux_ptr = (char_u *)"\\|\"\n[";
3597 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003598 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003599 aux_ptr = (char_u *)"\\|\"\n*?[";
3600
3601 p = buf + STRLEN(buf);
3602 while (n-- > 0)
3603 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003604 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003605 if (vim_strchr(aux_ptr, *ptr) != NULL)
3606 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003607 // When current byte is a part of multibyte character, copy all
3608 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003609 if (has_mbyte)
3610 {
3611 int i;
3612 int len = (*mb_ptr2len)(ptr) - 1;
3613
3614 for (i = 0; i < len && n >= 1; ++i, --n)
3615 *p++ = *ptr++;
3616 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003617 *p++ = *ptr++;
3618 }
3619 *p = NUL;
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003622 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (cmdchar == '*' || cmdchar == '#')
3624 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003625 if (!g_cmd && (has_mbyte
3626 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3627 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003629
3630 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003631 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003633
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003634 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003637 {
3638 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003640 g_tag_at_cursor = FALSE;
3641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 vim_free(buf);
3644}
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646/*
3647 * Get visually selected text, within one line only.
3648 * Returns FAIL if more than one line selected.
3649 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003651get_visual_text(
3652 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003653 char_u **pp, // return: start of selected text
3654 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 if (VIsual_mode != 'V')
3657 unadjust_for_sel();
3658 if (VIsual.lnum != curwin->w_cursor.lnum)
3659 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003660 if (cap != NULL)
3661 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return FAIL;
3663 }
3664 if (VIsual_mode == 'V')
3665 {
3666 *pp = ml_get_curline();
3667 *lenp = (int)STRLEN(*pp);
3668 }
3669 else
3670 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003671 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 *pp = ml_get_pos(&curwin->w_cursor);
3674 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3675 }
3676 else
3677 {
3678 *pp = ml_get_pos(&VIsual);
3679 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3680 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003681 if (**pp == NUL)
3682 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003683 if (*lenp > 0)
3684 {
3685 if (has_mbyte)
3686 // Correct the length to include all bytes of the last
3687 // character.
3688 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3689 else if ((*pp)[*lenp - 1] == NUL)
3690 // Do not include a trailing NUL.
3691 *lenp -= 1;
3692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694 reset_VIsual_and_resel();
3695 return OK;
3696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698/*
3699 * CTRL-T: backwards in tag stack
3700 */
3701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003702nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 if (!checkclearopq(cap->oap))
3705 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3706}
3707
3708/*
3709 * Handle scrolling command 'H', 'L' and 'M'.
3710 */
3711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003712nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 int used = 0;
3715 long n;
3716#ifdef FEAT_FOLDING
3717 linenr_T lnum;
3718#endif
3719 int half;
3720
3721 cap->oap->motion_type = MLINE;
3722 setpcmark();
3723
3724 if (cap->cmdchar == 'L')
3725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 curwin->w_cursor.lnum = curwin->w_botline - 1;
3728 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3729 curwin->w_cursor.lnum = 1;
3730 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003731 {
3732#ifdef FEAT_FOLDING
3733 if (hasAnyFolding(curwin))
3734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003735 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003736 for (n = cap->count1 - 1; n > 0
3737 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3738 {
3739 (void)hasFolding(curwin->w_cursor.lnum,
3740 &curwin->w_cursor.lnum, NULL);
3741 --curwin->w_cursor.lnum;
3742 }
3743 }
3744 else
3745#endif
3746 curwin->w_cursor.lnum -= cap->count1 - 1;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749 else
3750 {
3751 if (cap->cmdchar == 'M')
3752 {
3753#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 used -= diff_check_fill(curwin, curwin->w_topline)
3756 - curwin->w_topfill;
3757#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3760 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3761 {
3762#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003763 // Count half he number of filler lines to be "below this
3764 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3766 + n) / 2 >= half)
3767 {
3768 --n;
3769 break;
3770 }
3771#endif
3772 used += plines(curwin->w_topline + n);
3773 if (used >= half)
3774 break;
3775#ifdef FEAT_FOLDING
3776 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3777 n = lnum - curwin->w_topline;
3778#endif
3779 }
3780 if (n > 0 && used > curwin->w_height)
3781 --n;
3782 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003783 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003786#ifdef FEAT_FOLDING
3787 if (hasAnyFolding(curwin))
3788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003789 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003790 lnum = curwin->w_topline;
3791 while (n-- > 0 && lnum < curwin->w_botline - 1)
3792 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003793 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003794 ++lnum;
3795 }
3796 n = lnum - curwin->w_topline;
3797 }
3798#endif
3799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 curwin->w_cursor.lnum = curwin->w_topline + n;
3801 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3802 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3803 }
3804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003805 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003806 if (cap->oap->op_type == OP_NOP)
3807 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 beginline(BL_SOL | BL_FIX);
3809}
3810
3811/*
3812 * Cursor right commands.
3813 */
3814 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003815nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003818 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003820 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3821 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003822 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003823 if (mod_mask & MOD_MASK_CTRL)
3824 cap->arg = TRUE;
3825 nv_wordcmd(cap);
3826 return;
3827 }
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 cap->oap->motion_type = MCHAR;
3830 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003831 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003833 // In virtual edit mode, there's no such thing as "past_line", as lines
3834 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003836 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 for (n = cap->count1; n > 0; --n)
3839 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003840 if ((!past_line && oneright() == FAIL)
3841 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003842 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003844 // <Space> wraps to next line if 'whichwrap' has 's'.
3845 // 'l' wraps to next line if 'whichwrap' has 'l'.
3846 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if ( ((cap->cmdchar == ' '
3848 && vim_strchr(p_ww, 's') != NULL)
3849 || (cap->cmdchar == 'l'
3850 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003851 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 && vim_strchr(p_ww, '>') != NULL))
3853 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3854 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003855 // When deleting we also count the NL as a character.
3856 // Set cap->oap->inclusive when last char in the line is
3857 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003858 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003860 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 cap->oap->inclusive = TRUE;
3862 else
3863 {
3864 ++curwin->w_cursor.lnum;
3865 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 curwin->w_set_curswant = TRUE;
3868 cap->oap->inclusive = FALSE;
3869 }
3870 continue;
3871 }
3872 if (cap->oap->op_type == OP_NOP)
3873 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003874 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (n == cap->count1)
3876 beep_flush();
3877 }
3878 else
3879 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003880 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 cap->oap->inclusive = TRUE;
3882 }
3883 break;
3884 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003885 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (virtual_active())
3889 oneright();
3890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003893 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 ++curwin->w_cursor.col;
3896 }
3897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899#ifdef FEAT_FOLDING
3900 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3901 && cap->oap->op_type == OP_NOP)
3902 foldOpenCursor();
3903#endif
3904}
3905
3906/*
3907 * Cursor left commands.
3908 *
3909 * Returns TRUE when operator end should not be adjusted.
3910 */
3911 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003912nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
3914 long n;
3915
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003916 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003919 if (mod_mask & MOD_MASK_CTRL)
3920 cap->arg = 1;
3921 nv_bck_word(cap);
3922 return;
3923 }
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 cap->oap->motion_type = MCHAR;
3926 cap->oap->inclusive = FALSE;
3927 for (n = cap->count1; n > 0; --n)
3928 {
3929 if (oneleft() == FAIL)
3930 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3932 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3933 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if ( (((cap->cmdchar == K_BS
3935 || cap->cmdchar == Ctrl_H)
3936 && vim_strchr(p_ww, 'b') != NULL)
3937 || (cap->cmdchar == 'h'
3938 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003939 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 && vim_strchr(p_ww, '<') != NULL))
3941 && curwin->w_cursor.lnum > 1)
3942 {
3943 --(curwin->w_cursor.lnum);
3944 coladvance((colnr_T)MAXCOL);
3945 curwin->w_set_curswant = TRUE;
3946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003947 // When the NL before the first char has to be deleted we
3948 // put the cursor on the NUL after the previous line.
3949 // This is a very special case, be careful!
3950 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if ( (cap->oap->op_type == OP_DELETE
3952 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003953 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003955 char_u *cp = ml_get_cursor();
3956
3957 if (*cp != NUL)
3958 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003959 if (has_mbyte)
3960 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3961 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003962 ++curwin->w_cursor.col;
3963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 cap->retval |= CA_NO_ADJ_OP_END;
3965 }
3966 continue;
3967 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003968 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3970 beep_flush();
3971 break;
3972 }
3973 }
3974#ifdef FEAT_FOLDING
3975 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3976 && cap->oap->op_type == OP_NOP)
3977 foldOpenCursor();
3978#endif
3979}
3980
3981/*
3982 * Cursor up commands.
3983 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3984 */
3985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003986nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988 if (mod_mask & MOD_MASK_SHIFT)
3989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003990 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003991 cap->arg = BACKWARD;
3992 nv_page(cap);
3993 }
3994 else
3995 {
3996 cap->oap->motion_type = MLINE;
3997 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3998 clearopbeep(cap->oap);
3999 else if (cap->arg)
4000 beginline(BL_WHITE | BL_FIX);
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002}
4003
4004/*
4005 * Cursor down commands.
4006 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4007 */
4008 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004009nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 if (mod_mask & MOD_MASK_SHIFT)
4012 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004013 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 cap->arg = FORWARD;
4015 nv_page(cap);
4016 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004017#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004019 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4020 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
4024#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004025 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004026 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 cmdwin_result = CAR;
4028 else
4029#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004031 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004032 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4033 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4034 {
4035 invoke_prompt_callback();
4036 if (restart_edit == 0)
4037 restart_edit = 'a';
4038 }
4039 else
4040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
4042 cap->oap->motion_type = MLINE;
4043 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4044 clearopbeep(cap->oap);
4045 else if (cap->arg)
4046 beginline(BL_WHITE | BL_FIX);
4047 }
4048 }
4049}
4050
4051#ifdef FEAT_SEARCHPATH
4052/*
4053 * Grab the file name under the cursor and edit it.
4054 */
4055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004059 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01004061 if (check_text_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063 if (curbuf_locked())
4064 {
4065 clearop(cap->oap);
4066 return;
4067 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004068#ifdef FEAT_PROP_POPUP
4069 if (ERROR_IF_TERM_POPUP_WINDOW)
4070 return;
4071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004073 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 if (ptr != NULL)
4076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004077 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004078 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004079 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004081 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004082 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004083 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004084 {
4085 curwin->w_cursor.lnum = lnum;
4086 check_cursor_lnum();
4087 beginline(BL_SOL | BL_FIX);
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 vim_free(ptr);
4090 }
4091 else
4092 clearop(cap->oap);
4093}
4094#endif
4095
4096/*
4097 * <End> command: to end of current line or last line.
4098 */
4099 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004100nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004104 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 nv_dollar(cap);
4109}
4110
4111/*
4112 * Handle the "$" command.
4113 */
4114 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004115nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
4117 cap->oap->motion_type = MCHAR;
4118 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 // In virtual mode when off the edge of a line and an operator
4120 // is pending (whew!) keep the cursor where it is.
4121 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (!virtual_active() || gchar_cursor() != NUL
4123 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004124 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (cursor_down((long)(cap->count1 - 1),
4126 cap->oap->op_type == OP_NOP) == FAIL)
4127 clearopbeep(cap->oap);
4128#ifdef FEAT_FOLDING
4129 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4130 foldOpenCursor();
4131#endif
4132}
4133
4134/*
4135 * Implementation of '?' and '/' commands.
4136 * If cap->arg is TRUE don't set PC mark.
4137 */
4138 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004139nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004142 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004146 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 cap->cmdchar = 'g';
4148 cap->nchar = '?';
4149 nv_operator(cap);
4150 return;
4151 }
4152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004153 // When using 'incsearch' the cursor may be moved to set a different search
4154 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004155 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 if (cap->searchbuf == NULL)
4158 {
4159 clearop(oap);
4160 return;
4161 }
4162
Bram Moolenaar46539112015-02-17 15:43:57 +01004163 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004164 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004165 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166}
4167
LemonBoya4399382022-04-09 21:04:08 +01004168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169/*
4170 * Handle "N" and "n" commands.
4171 * cap->arg is SEARCH_REV for "N", 0 for "n".
4172 */
4173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004174nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004176 pos_T old = curwin->w_cursor;
4177 int wrapped = FALSE;
4178 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004179
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004180 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004182 // Avoid getting stuck on the current cursor position, which can
4183 // happen when an offset is given and the cursor is on the last char
4184 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004185 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004186 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004187 cap->count1 -= 1;
4188 }
LemonBoya4399382022-04-09 21:04:08 +01004189
4190#ifdef FEAT_SEARCH_EXTRA
4191 // Redraw the window to refresh the highlighted matches.
4192 if (i > 0 && p_hls && !no_hlsearch)
4193 redraw_later(SOME_VALID);
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195}
4196
4197/*
4198 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4199 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004200 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004202 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004203normal_search(
4204 cmdarg_T *cap,
4205 int dir,
4206 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004207 int opt, // extra flags for do_search()
4208 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004211 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004212#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004213 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 cap->oap->motion_type = MCHAR;
4217 cap->oap->inclusive = FALSE;
4218 cap->oap->use_reg_one = TRUE;
4219 curwin->w_set_curswant = TRUE;
4220
Bram Moolenaara80faa82020-04-12 19:37:17 +02004221 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004222 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004223 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4224 if (wrapped != NULL)
4225 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (i == 0)
4227 clearop(cap->oap);
4228 else
4229 {
4230 if (i == 2)
4231 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#ifdef FEAT_FOLDING
4234 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4235 foldOpenCursor();
4236#endif
4237 }
LemonBoya4399382022-04-09 21:04:08 +01004238#ifdef FEAT_SEARCH_EXTRA
4239 // Redraw the window to refresh the highlighted matches.
4240 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
4241 redraw_later(SOME_VALID);
4242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004244 // "/$" will put the cursor after the end of the line, may need to
4245 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004247 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248}
4249
4250/*
4251 * Character search commands.
4252 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4253 * ',' and FALSE for ';'.
4254 * cap->nchar is NUL for ',' and ';' (repeat the search)
4255 */
4256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004257nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
4259 int t_cmd;
4260
4261 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4262 t_cmd = TRUE;
4263 else
4264 t_cmd = FALSE;
4265
4266 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4268 clearopbeep(cap->oap);
4269 else
4270 {
4271 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004272 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4274 && (t_cmd || cap->oap->op_type != OP_NOP))
4275 {
4276 colnr_T scol, ecol;
4277
4278 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4279 curwin->w_cursor.coladd = ecol - scol;
4280 }
4281 else
4282 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284#ifdef FEAT_FOLDING
4285 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4286 foldOpenCursor();
4287#endif
4288 }
4289}
4290
4291/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004292 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4293 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4294 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4295 * "[m" or "]m" search for prev/next start of (Java) method.
4296 * "[M" or "]M" search for prev/next end of (Java) method.
4297 */
4298 static void
4299nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4300{
4301 pos_T new_pos = {0, 0, 0};
4302 pos_T *pos = NULL; // init for GCC
4303 pos_T prev_pos;
4304 long n;
4305 int findc;
4306 int c;
4307
4308 if (cap->nchar == '*')
4309 cap->nchar = '/';
4310 prev_pos.lnum = 0;
4311 if (cap->nchar == 'm' || cap->nchar == 'M')
4312 {
4313 if (cap->cmdchar == '[')
4314 findc = '{';
4315 else
4316 findc = '}';
4317 n = 9999;
4318 }
4319 else
4320 {
4321 findc = cap->nchar;
4322 n = cap->count1;
4323 }
4324 for ( ; n > 0; --n)
4325 {
4326 if ((pos = findmatchlimit(cap->oap, findc,
4327 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4328 {
4329 if (new_pos.lnum == 0) // nothing found
4330 {
4331 if (cap->nchar != 'm' && cap->nchar != 'M')
4332 clearopbeep(cap->oap);
4333 }
4334 else
4335 pos = &new_pos; // use last one found
4336 break;
4337 }
4338 prev_pos = new_pos;
4339 curwin->w_cursor = *pos;
4340 new_pos = *pos;
4341 }
4342 curwin->w_cursor = *old_pos;
4343
4344 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4345 // brought us to the match for "[m" and "]M" when inside a method.
4346 // Try finding the '{' or '}' we want to be at.
4347 // Also repeat for the given count.
4348 if (cap->nchar == 'm' || cap->nchar == 'M')
4349 {
4350 // norm is TRUE for "]M" and "[m"
4351 int norm = ((findc == '{') == (cap->nchar == 'm'));
4352
4353 n = cap->count1;
4354 // found a match: we were inside a method
4355 if (prev_pos.lnum != 0)
4356 {
4357 pos = &prev_pos;
4358 curwin->w_cursor = prev_pos;
4359 if (norm)
4360 --n;
4361 }
4362 else
4363 pos = NULL;
4364 while (n > 0)
4365 {
4366 for (;;)
4367 {
4368 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4369 {
4370 // if not found anything, that's an error
4371 if (pos == NULL)
4372 clearopbeep(cap->oap);
4373 n = 0;
4374 break;
4375 }
4376 c = gchar_cursor();
4377 if (c == '{' || c == '}')
4378 {
4379 // Must have found end/start of class: use it.
4380 // Or found the place to be at.
4381 if ((c == findc && norm) || (n == 1 && !norm))
4382 {
4383 new_pos = curwin->w_cursor;
4384 pos = &new_pos;
4385 n = 0;
4386 }
4387 // if no match found at all, we started outside of the
4388 // class and we're inside now. Just go on.
4389 else if (new_pos.lnum == 0)
4390 {
4391 new_pos = curwin->w_cursor;
4392 pos = &new_pos;
4393 }
4394 // found start/end of other method: go to match
4395 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004396 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4397 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004398 n = 0;
4399 else
4400 curwin->w_cursor = *pos;
4401 break;
4402 }
4403 }
4404 --n;
4405 }
4406 curwin->w_cursor = *old_pos;
4407 if (pos == NULL && new_pos.lnum != 0)
4408 clearopbeep(cap->oap);
4409 }
4410 if (pos != NULL)
4411 {
4412 setpcmark();
4413 curwin->w_cursor = *pos;
4414 curwin->w_set_curswant = TRUE;
4415#ifdef FEAT_FOLDING
4416 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4417 && cap->oap->op_type == OP_NOP)
4418 foldOpenCursor();
4419#endif
4420 }
4421}
4422
4423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 * "[" and "]" commands.
4425 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4426 */
4427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004428nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004431 pos_T *pos = NULL; // init for GCC
4432 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 int flag;
4434 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 cap->oap->motion_type = MCHAR;
4437 cap->oap->inclusive = FALSE;
4438 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004439 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004442 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (cap->nchar == 'f')
4444 nv_gotofile(cap);
4445 else
4446#endif
4447
4448#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004449 // Find the occurrence(s) of the identifier or define under cursor
4450 // in current and included files or jump to the first occurrence.
4451 //
4452 // search list jump
4453 // fwd bwd fwd bwd fwd bwd
4454 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4455 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004456 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 char_u *ptr;
4459 int len;
4460
4461 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4462 clearop(cap->oap);
4463 else
4464 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004465 // Make a copy, if the line was changed it will be freed.
4466 ptr = vim_strnsave(ptr, len);
4467 if (ptr == NULL)
4468 return;
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 find_pattern_in_path(ptr, 0, len, TRUE,
4471 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4472 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4473 cap->count1,
4474 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4475 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4476 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4477 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004478 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 curwin->w_set_curswant = TRUE;
4480 }
4481 }
4482 else
4483#endif
4484
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004485 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4486 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4487 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4488 // "[m" or "]m" search for prev/next start of (Java) method.
4489 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if ( (cap->cmdchar == '['
4491 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4492 || (cap->cmdchar == ']'
4493 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004494 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004496 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 else if (cap->nchar == '[' || cap->nchar == ']')
4498 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 flag = '{';
4501 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004502 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004505 // Imitate strange Vi behaviour: When using "]]" with an operator
4506 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004507 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 (cap->oap->op_type != OP_NOP
4509 && cap->arg == FORWARD && flag == '{')))
4510 clearopbeep(cap->oap);
4511 else
4512 {
4513 if (cap->oap->op_type == OP_NOP)
4514 beginline(BL_WHITE | BL_FIX);
4515#ifdef FEAT_FOLDING
4516 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4517 foldOpenCursor();
4518#endif
4519 }
4520 }
4521
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else if (cap->nchar == 'p' || cap->nchar == 'P')
4524 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004525 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004528 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 else if (cap->nchar == '\'' || cap->nchar == '`')
4530 {
4531 pos = &curwin->w_cursor;
4532 for (n = cap->count1; n > 0; --n)
4533 {
4534 prev_pos = *pos;
4535 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4536 cap->nchar == '\'');
4537 if (pos == NULL)
4538 break;
4539 }
4540 if (pos == NULL)
4541 pos = &prev_pos;
4542 nv_cursormark(cap, cap->nchar == '\'', pos);
4543 }
4544
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004545 // [ or ] followed by a middle mouse click: put selected text with
4546 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004547 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 (void)do_mouse(cap->oap, cap->nchar,
4550 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4551 cap->count1, PUT_FIXINDENT);
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
4554#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004555 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 else if (cap->nchar == 'z')
4557 {
4558 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4559 cap->count1) == FAIL)
4560 clearopbeep(cap->oap);
4561 }
4562#endif
4563
4564#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004565 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 else if (cap->nchar == 'c')
4567 {
4568 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4569 cap->count1) == FAIL)
4570 clearopbeep(cap->oap);
4571 }
4572#endif
4573
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004574#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004575 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004576 else if (cap->nchar == 's' || cap->nchar == 'S')
4577 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004578 setpcmark();
4579 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004580 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4581 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004582 {
4583 clearopbeep(cap->oap);
4584 break;
4585 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004586 else
4587 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004588# ifdef FEAT_FOLDING
4589 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4590 foldOpenCursor();
4591# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004592 }
4593#endif
4594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004595 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 else
4597 clearopbeep(cap->oap);
4598}
4599
4600/*
4601 * Handle Normal mode "%" command.
4602 */
4603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004604nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004607#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 linenr_T lnum = curwin->w_cursor.lnum;
4609#endif
4610
4611 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 if (cap->count0 > 100)
4615 clearopbeep(cap->oap);
4616 else
4617 {
4618 cap->oap->motion_type = MLINE;
4619 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004620 // Round up, so 'normal 100%' always jumps at the line line.
4621 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4622 // overflow on 32-bits, so use a formula with less accuracy
4623 // to avoid overflows.
4624 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4626 / 100L * cap->count0;
4627 else
4628 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4629 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004630 if (curwin->w_cursor.lnum < 1)
4631 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4633 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4634 beginline(BL_SOL | BL_FIX);
4635 }
4636 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
4639 cap->oap->motion_type = MCHAR;
4640 cap->oap->use_reg_one = TRUE;
4641 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4642 clearopbeep(cap->oap);
4643 else
4644 {
4645 setpcmark();
4646 curwin->w_cursor = *pos;
4647 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 }
4652#ifdef FEAT_FOLDING
4653 if (cap->oap->op_type == OP_NOP
4654 && lnum != curwin->w_cursor.lnum
4655 && (fdo_flags & FDO_PERCENT)
4656 && KeyTyped)
4657 foldOpenCursor();
4658#endif
4659}
4660
4661/*
4662 * Handle "(" and ")" commands.
4663 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4664 */
4665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004666nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 cap->oap->motion_type = MCHAR;
4669 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004670 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004671 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 curwin->w_set_curswant = TRUE;
4673
4674 if (findsent(cap->arg, cap->count1) == FAIL)
4675 clearopbeep(cap->oap);
4676 else
4677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004679 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#ifdef FEAT_FOLDING
4682 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4683 foldOpenCursor();
4684#endif
4685 }
4686}
4687
4688/*
4689 * "m" command: Mark a position.
4690 */
4691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004692nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 if (!checkclearop(cap->oap))
4695 {
4696 if (setmark(cap->nchar) == FAIL)
4697 clearopbeep(cap->oap);
4698 }
4699}
4700
4701/*
4702 * "{" and "}" commands.
4703 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4704 */
4705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004706nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
4708 cap->oap->motion_type = MCHAR;
4709 cap->oap->inclusive = FALSE;
4710 cap->oap->use_reg_one = TRUE;
4711 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004712 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 clearopbeep(cap->oap);
4714 else
4715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_FOLDING
4718 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4719 foldOpenCursor();
4720#endif
4721 }
4722}
4723
4724/*
4725 * "u" command: Undo or make lower case.
4726 */
4727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004728nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004730 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004732 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 cap->cmdchar = 'g';
4734 cap->nchar = 'u';
4735 nv_operator(cap);
4736 }
4737 else
4738 nv_kundo(cap);
4739}
4740
4741/*
4742 * <Undo> command.
4743 */
4744 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004745nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 if (!checkclearopq(cap->oap))
4748 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004749#ifdef FEAT_JOB_CHANNEL
4750 if (bt_prompt(curbuf))
4751 {
4752 clearopbeep(cap->oap);
4753 return;
4754 }
4755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 u_undo((int)cap->count1);
4757 curwin->w_set_curswant = TRUE;
4758 }
4759}
4760
4761/*
4762 * Handle the "r" command.
4763 */
4764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004765nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
4767 char_u *ptr;
4768 int had_ctrl_v;
4769 long n;
4770
4771 if (checkclearop(cap->oap))
4772 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004773#ifdef FEAT_JOB_CHANNEL
4774 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4775 {
4776 clearopbeep(cap->oap);
4777 return;
4778 }
4779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004781 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (cap->nchar == Ctrl_V)
4783 {
4784 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004785 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004786 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (cap->nchar > DEL)
4788 had_ctrl_v = NUL;
4789 }
4790 else
4791 had_ctrl_v = NUL;
4792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004794 if (IS_SPECIAL(cap->nchar))
4795 {
4796 clearopbeep(cap->oap);
4797 return;
4798 }
4799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004800 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (VIsual_active)
4802 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004803 if (got_int)
4804 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004805 if (had_ctrl_v)
4806 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004807 // Use a special (negative) number to make a difference between a
4808 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004809 if (cap->nchar == CAR)
4810 cap->nchar = REPLACE_CR_NCHAR;
4811 else if (cap->nchar == NL)
4812 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 nv_operator(cap);
4815 return;
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004818 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (virtual_active())
4820 {
4821 if (u_save_cursor() == FAIL)
4822 return;
4823 if (gchar_cursor() == NUL)
4824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004825 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 coladvance_force((colnr_T)(getviscol() + cap->count1));
4827 curwin->w_cursor.col -= cap->count1;
4828 }
4829 else if (gchar_cursor() == TAB)
4830 coladvance_force(getviscol());
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004833 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004835 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004836 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
4838 clearopbeep(cap->oap);
4839 return;
4840 }
4841
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004842 // Replacing with a TAB is done by edit() when it is complicated because
4843 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4844 // Other characters are done below to avoid problems with things like
4845 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4847 {
4848 stuffnumReadbuff(cap->count1);
4849 stuffcharReadbuff('R');
4850 stuffcharReadbuff('\t');
4851 stuffcharReadbuff(ESC);
4852 return;
4853 }
4854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004855 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if (u_save_cursor() == FAIL)
4857 return;
4858
4859 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4860 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004861 // Replace character(s) by a single newline.
4862 // Strange vi behaviour: Only one newline is inserted.
4863 // Delete the characters here.
4864 // Insert the newline with an insert command, takes care of
4865 // autoindent. The insert command depends on being on the last
4866 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004867 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 stuffcharReadbuff('\r');
4869 stuffcharReadbuff(ESC);
4870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 invoke_edit(cap, TRUE, 'r', FALSE);
4873 }
4874 else
4875 {
4876 prep_redo(cap->oap->regname, cap->count1,
4877 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4878
4879 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (has_mbyte)
4881 {
4882 int old_State = State;
4883
4884 if (cap->ncharC1 != 0)
4885 AppendCharToRedobuff(cap->ncharC1);
4886 if (cap->ncharC2 != 0)
4887 AppendCharToRedobuff(cap->ncharC2);
4888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004889 // This is slow, but it handles replacing a single-byte with a
4890 // multi-byte and the other way around. Also handles adding
4891 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 for (n = cap->count1; n > 0; --n)
4893 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004894 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004895 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4896 {
4897 int c = ins_copychar(curwin->w_cursor.lnum
4898 + (cap->nchar == Ctrl_Y ? -1 : 1));
4899 if (c != NUL)
4900 ins_char(c);
4901 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004902 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004903 ++curwin->w_cursor.col;
4904 }
4905 else
4906 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 State = old_State;
4908 if (cap->ncharC1 != 0)
4909 ins_char(cap->ncharC1);
4910 if (cap->ncharC2 != 0)
4911 ins_char(cap->ncharC2);
4912 }
4913 }
4914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004916 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 for (n = cap->count1; n > 0; --n)
4918 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004919 // Get ptr again, because u_save and/or showmatch() will have
4920 // released the line. This may also happen in ins_copychar().
4921 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004922 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4923 {
4924 int c = ins_copychar(curwin->w_cursor.lnum
4925 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004926
4927 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004928 if (c != NUL)
4929 ptr[curwin->w_cursor.col] = c;
4930 }
4931 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004932 {
4933 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004934 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (p_sm && msg_silent == 0)
4937 showmatch(cap->nchar);
4938 ++curwin->w_cursor.col;
4939 }
4940#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004941 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004943 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
Bram Moolenaar009b2592004-10-24 19:18:58 +00004945 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004946 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004948 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950#endif
4951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 changed_bytes(curwin->w_cursor.lnum,
4954 (colnr_T)(curwin->w_cursor.col - cap->count1));
4955 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004956 --curwin->w_cursor.col; // cursor on the last replaced char
4957 // if the character on the left of the current cursor is a multi-byte
4958 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (has_mbyte)
4960 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 curbuf->b_op_end = curwin->w_cursor;
4962 curwin->w_set_curswant = TRUE;
4963 set_last_insert(cap->nchar);
4964 }
4965}
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967/*
4968 * 'o': Exchange start and end of Visual area.
4969 * 'O': same, but in block mode exchange left and right corners.
4970 */
4971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004972v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 pos_T old_cursor;
4975 colnr_T left, right;
4976
4977 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4978 {
4979 old_cursor = curwin->w_cursor;
4980 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4981 curwin->w_cursor.lnum = VIsual.lnum;
4982 coladvance(left);
4983 VIsual = curwin->w_cursor;
4984
4985 curwin->w_cursor.lnum = old_cursor.lnum;
4986 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004987 // 'selection "exclusive" and cursor at right-bottom corner: move it
4988 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4990 ++curwin->w_curswant;
4991 coladvance(curwin->w_curswant);
4992 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004994 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
4996 curwin->w_cursor.lnum = VIsual.lnum;
4997 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4998 ++right;
4999 coladvance(right);
5000 VIsual = curwin->w_cursor;
5001
5002 curwin->w_cursor.lnum = old_cursor.lnum;
5003 coladvance(left);
5004 curwin->w_curswant = left;
5005 }
5006 }
5007 else
5008 {
5009 old_cursor = curwin->w_cursor;
5010 curwin->w_cursor = VIsual;
5011 VIsual = old_cursor;
5012 curwin->w_set_curswant = TRUE;
5013 }
5014}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016/*
5017 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5018 */
5019 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005020nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 cap->cmdchar = 'c';
5025 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005026 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 VIsual_mode = 'V';
5028 nv_operator(cap);
5029 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005030 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005033 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
5035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (virtual_active())
5037 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5039 }
5040 }
5041}
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043/*
5044 * "gr".
5045 */
5046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005047nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (VIsual_active)
5050 {
5051 cap->cmdchar = 'r';
5052 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005053 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005055 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005058 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 else
5060 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005061 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005062 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 stuffcharReadbuff(cap->extra_char);
5064 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (virtual_active())
5066 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 invoke_edit(cap, TRUE, 'v', FALSE);
5068 }
5069 }
5070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072/*
5073 * Swap case for "~" command, when it does not work like an operator.
5074 */
5075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005076n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 long n;
5079 pos_T startpos;
5080 int did_change = 0;
5081#ifdef FEAT_NETBEANS_INTG
5082 pos_T pos;
5083 char_u *ptr;
5084 int count;
5085#endif
5086
5087 if (checkclearopq(cap->oap))
5088 return;
5089
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005090 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 clearopbeep(cap->oap);
5093 return;
5094 }
5095
5096 prep_redo_cmd(cap);
5097
5098 if (u_save_cursor() == FAIL)
5099 return;
5100
5101 startpos = curwin->w_cursor;
5102#ifdef FEAT_NETBEANS_INTG
5103 pos = startpos;
5104#endif
5105 for (n = cap->count1; n > 0; --n)
5106 {
5107 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5108 inc_cursor();
5109 if (gchar_cursor() == NUL)
5110 {
5111 if (vim_strchr(p_ww, '~') != NULL
5112 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5113 {
5114#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005115 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 if (did_change)
5118 {
5119 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005120 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005121 netbeans_removed(curbuf, pos.lnum, pos.col,
5122 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005123 // line may have been flushed, get it again
5124 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005126 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 pos.col = 0;
5129 pos.lnum++;
5130 }
5131#endif
5132 ++curwin->w_cursor.lnum;
5133 curwin->w_cursor.col = 0;
5134 if (n > 1)
5135 {
5136 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5137 break;
5138 u_clearline();
5139 }
5140 }
5141 else
5142 break;
5143 }
5144 }
5145#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005146 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 ptr = ml_get(pos.lnum);
5149 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005150 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5151 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153#endif
5154
5155
5156 check_cursor();
5157 curwin->w_set_curswant = TRUE;
5158 if (did_change)
5159 {
5160 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5161 0L);
5162 curbuf->b_op_start = startpos;
5163 curbuf->b_op_end = curwin->w_cursor;
5164 if (curbuf->b_op_end.col > 0)
5165 --curbuf->b_op_end.col;
5166 }
5167}
5168
5169/*
5170 * Move cursor to mark.
5171 */
5172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005173nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 if (check_mark(pos) == FAIL)
5176 clearop(cap->oap);
5177 else
5178 {
5179 if (cap->cmdchar == '\''
5180 || cap->cmdchar == '`'
5181 || cap->cmdchar == '['
5182 || cap->cmdchar == ']')
5183 setpcmark();
5184 curwin->w_cursor = *pos;
5185 if (flag)
5186 beginline(BL_WHITE | BL_FIX);
5187 else
5188 check_cursor();
5189 }
5190 cap->oap->motion_type = flag ? MLINE : MCHAR;
5191 if (cap->cmdchar == '`')
5192 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005193 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 curwin->w_set_curswant = TRUE;
5195}
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197/*
5198 * Handle commands that are operators in Visual mode.
5199 */
5200 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005201v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005205 // Uppercase means linewise, except in block mode, then "D" deletes till
5206 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (isupper(cap->cmdchar))
5208 {
5209 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005210 {
5211 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5215 curwin->w_curswant = MAXCOL;
5216 }
5217 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5218 nv_operator(cap);
5219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221/*
5222 * "s" and "S" commands.
5223 */
5224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005225nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005227#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005228 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005229 if (term_swap_diff() == OK)
5230 return;
5231#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005232#ifdef FEAT_JOB_CHANNEL
5233 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5234 {
5235 clearopbeep(cap->oap);
5236 return;
5237 }
5238#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005239 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005242 {
5243 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 cap->cmdchar = 'c';
5247 nv_operator(cap);
5248 }
5249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 nv_optrans(cap);
5251}
5252
5253/*
5254 * Abbreviated commands.
5255 */
5256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005260 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005262 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (VIsual_active)
5264 v_visop(cap);
5265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 nv_optrans(cap);
5267}
5268
5269/*
5270 * Translate a command into another command.
5271 */
5272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5276 (char_u *)"d$", (char_u *)"c$",
5277 (char_u *)"cl", (char_u *)"cc",
5278 (char_u *)"yy", (char_u *)":s\r"};
5279 static char_u *str = (char_u *)"xXDCsSY&";
5280
5281 if (!checkclearopq(cap->oap))
5282 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005283 // In Vi "2D" doesn't delete the next line. Can't translate it
5284 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005285 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5286 {
5287 cap->oap->start = curwin->w_cursor;
5288 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005289#ifdef FEAT_EVAL
5290 set_op_var(OP_DELETE);
5291#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005292 cap->count1 = 1;
5293 nv_dollar(cap);
5294 finish_op = TRUE;
5295 ResetRedobuff();
5296 AppendCharToRedobuff('D');
5297 }
5298 else
5299 {
5300 if (cap->count0)
5301 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005302 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 cap->opcount = 0;
5306}
5307
5308/*
5309 * "'" and "`" commands. Also for "g'" and "g`".
5310 * cap->arg is TRUE for "'" and "g'".
5311 */
5312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 pos_T *pos;
5316 int c;
5317#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005318 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005319 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#endif
5321
5322 if (cap->cmdchar == 'g')
5323 c = cap->extra_char;
5324 else
5325 c = cap->nchar;
5326 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005327 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
5329 if (cap->arg)
5330 {
5331 check_cursor_lnum();
5332 beginline(BL_WHITE | BL_FIX);
5333 }
5334 else
5335 check_cursor();
5336 }
5337 else
5338 nv_cursormark(cap, cap->arg, pos);
5339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (!virtual_active())
5342 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005343 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#ifdef FEAT_FOLDING
5345 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005346 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005347 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 && (fdo_flags & FDO_MARK)
5349 && old_KeyTyped)
5350 foldOpenCursor();
5351#endif
5352}
5353
5354/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005355 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 */
5357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005358nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005361#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005363 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 if (!checkclearopq(cap->oap))
5367 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005368 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5369 {
5370 if (goto_tabpage_lastused() == FAIL)
5371 clearopbeep(cap->oap);
5372 return;
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (cap->cmdchar == 'g')
5375 pos = movechangelist((int)cap->count1);
5376 else
5377 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005378 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 curwin->w_set_curswant = TRUE;
5381 check_cursor();
5382 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005383 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 nv_cursormark(cap, FALSE, pos);
5385 else if (cap->cmdchar == 'g')
5386 {
5387 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005388 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005390 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005392 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 else
5395 clearopbeep(cap->oap);
5396# ifdef FEAT_FOLDING
5397 if (cap->oap->op_type == OP_NOP
5398 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5399 && (fdo_flags & FDO_MARK)
5400 && old_KeyTyped)
5401 foldOpenCursor();
5402# endif
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404}
5405
5406/*
5407 * Handle '"' command.
5408 */
5409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005410nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 if (checkclearop(cap->oap))
5413 return;
5414#ifdef FEAT_EVAL
5415 if (cap->nchar == '=')
5416 cap->nchar = get_expr_register();
5417#endif
5418 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5419 {
5420 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005421 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#ifdef FEAT_EVAL
5423 set_reg_var(cap->oap->regname);
5424#endif
5425 }
5426 else
5427 clearopbeep(cap->oap);
5428}
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430/*
5431 * Handle "v", "V" and "CTRL-V" commands.
5432 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5433 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005434 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 */
5436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005437nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005439 if (cap->cmdchar == Ctrl_Q)
5440 cap->cmdchar = Ctrl_V;
5441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5443 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (cap->oap->op_type != OP_NOP)
5445 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005446 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 return;
5449 }
5450
5451 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005454 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 else // toggle char/block mode
5457 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 VIsual_mode = cap->cmdchar;
5459 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005460 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005462 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005464 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005467 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 VIsual = curwin->w_cursor;
5471
5472 VIsual_active = TRUE;
5473 VIsual_reselect = TRUE;
5474 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005478 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005479 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005480 // For V and ^V, we multiply the number of lines even if there
5481 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5483 {
5484 curwin->w_cursor.lnum +=
5485 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005486 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 VIsual_mode = resel_VIsual_mode;
5489 if (VIsual_mode == 'v')
5490 {
5491 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005492 {
5493 validate_virtcol();
5494 curwin->w_curswant = curwin->w_virtcol
5495 + resel_VIsual_vcol * cap->count0 - 1;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005498 curwin->w_curswant = resel_VIsual_vcol;
5499 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005501 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 curwin->w_curswant = MAXCOL;
5504 coladvance((colnr_T)MAXCOL);
5505 }
5506 else if (VIsual_mode == Ctrl_V)
5507 {
5508 validate_virtcol();
5509 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005510 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 coladvance(curwin->w_curswant);
5512 }
5513 else
5514 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005515 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517 else
5518 {
5519 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 may_start_select('c');
5522 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005523 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005525 if (cap->count0 > 0 && --cap->count1 > 0)
5526 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005528 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5529 nv_right(cap);
5530 else if (VIsual_mode == 'V')
5531 nv_down(cap);
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 }
5535}
5536
5537/*
5538 * Start selection for Shift-movement keys.
5539 */
5540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005541start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 may_start_select('k');
5545 n_start_visual_mode('v');
5546}
5547
5548/*
5549 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005550 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 */
5552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005553may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005555 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5556 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558
5559/*
5560 * Start Visual mode "c".
5561 * Should set VIsual_select before calling this.
5562 */
5563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005564n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005566#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005567 int cursor_line_was_concealed = curwin->w_p_cole > 0
5568 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005569#endif
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 VIsual_mode = c;
5572 VIsual_active = TRUE;
5573 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005574
5575 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005576 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005577 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005578 {
5579 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 VIsual = curwin->w_cursor;
5583
5584#ifdef FEAT_FOLDING
5585 foldAdjustVisual();
5586#endif
5587
LemonBoy2bf52dd2022-04-09 18:17:34 +01005588 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005590#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005591 // Check if redraw is needed after changing the state.
5592 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005593#endif
5594
Bram Moolenaar09df3122006-01-23 22:23:09 +00005595 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 // Make sure the clipboard gets updated. Needed because start and
5599 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 clip_star.vmode = NUL;
5601#endif
5602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 // Only need to redraw this line, unless still need to redraw an old
5604 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (curwin->w_redr_type < INVERTED)
5606 {
5607 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5608 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5609 }
5610}
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613/*
5614 * CTRL-W: Window commands
5615 */
5616 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005617nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005619 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005622 cap->cmdchar = ':';
5623 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005624 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005625 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005626 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630/*
5631 * CTRL-Z: Suspend
5632 */
5633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005634nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005638 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005639 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005643 * "gv": Reselect the previous Visual area. If Visual already active,
5644 * exchange previous and current Visual area.
5645 */
5646 static void
5647nv_gv_cmd(cmdarg_T *cap)
5648{
5649 pos_T tpos;
5650 int i;
5651
5652 if (checkclearop(cap->oap))
5653 return;
5654
5655 if (curbuf->b_visual.vi_start.lnum == 0
5656 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5657 || curbuf->b_visual.vi_end.lnum == 0)
5658 {
5659 beep_flush();
5660 return;
5661 }
5662
5663 // set w_cursor to the start of the Visual area, tpos to the end
5664 if (VIsual_active)
5665 {
5666 i = VIsual_mode;
5667 VIsual_mode = curbuf->b_visual.vi_mode;
5668 curbuf->b_visual.vi_mode = i;
5669# ifdef FEAT_EVAL
5670 curbuf->b_visual_mode_eval = i;
5671# endif
5672 i = curwin->w_curswant;
5673 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5674 curbuf->b_visual.vi_curswant = i;
5675
5676 tpos = curbuf->b_visual.vi_end;
5677 curbuf->b_visual.vi_end = curwin->w_cursor;
5678 curwin->w_cursor = curbuf->b_visual.vi_start;
5679 curbuf->b_visual.vi_start = VIsual;
5680 }
5681 else
5682 {
5683 VIsual_mode = curbuf->b_visual.vi_mode;
5684 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5685 tpos = curbuf->b_visual.vi_end;
5686 curwin->w_cursor = curbuf->b_visual.vi_start;
5687 }
5688
5689 VIsual_active = TRUE;
5690 VIsual_reselect = TRUE;
5691
5692 // Set Visual to the start and w_cursor to the end of the Visual
5693 // area. Make sure they are on an existing character.
5694 check_cursor();
5695 VIsual = curwin->w_cursor;
5696 curwin->w_cursor = tpos;
5697 check_cursor();
5698 update_topline();
5699
5700 // When called from normal "g" command: start Select mode when
5701 // 'selectmode' contains "cmd". When called for K_SELECT, always
5702 // start Select mode.
5703 if (cap->arg)
5704 {
5705 VIsual_select = TRUE;
5706 VIsual_select_reg = 0;
5707 }
5708 else
5709 may_start_select('c');
5710 setmouse();
5711#ifdef FEAT_CLIPBOARD
5712 // Make sure the clipboard gets updated. Needed because start and
5713 // end are still the same, and the selection needs to be owned
5714 clip_star.vmode = NUL;
5715#endif
5716 redraw_curbuf_later(INVERTED);
5717 showmode();
5718}
5719
5720/*
5721 * "g0", "g^" : Like "0" and "^" but for screen lines.
5722 * "gm": middle of "g0" and "g$".
5723 */
5724 static void
5725nv_g_home_m_cmd(cmdarg_T *cap)
5726{
5727 int i;
5728 int flag = FALSE;
5729
5730 if (cap->nchar == '^')
5731 flag = TRUE;
5732
5733 cap->oap->motion_type = MCHAR;
5734 cap->oap->inclusive = FALSE;
5735 if (curwin->w_p_wrap && curwin->w_width != 0)
5736 {
5737 int width1 = curwin->w_width - curwin_col_off();
5738 int width2 = width1 + curwin_col_off2();
5739
5740 validate_virtcol();
5741 i = 0;
5742 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5743 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5744 }
5745 else
5746 i = curwin->w_leftcol;
5747 // Go to the middle of the screen line. When 'number' or
5748 // 'relativenumber' is on and lines are wrapping the middle can be more
5749 // to the left.
5750 if (cap->nchar == 'm')
5751 i += (curwin->w_width - curwin_col_off()
5752 + ((curwin->w_p_wrap && i > 0)
5753 ? curwin_col_off2() : 0)) / 2;
5754 coladvance((colnr_T)i);
5755 if (flag)
5756 {
5757 do
5758 i = gchar_cursor();
5759 while (VIM_ISWHITE(i) && oneright() == OK);
5760 curwin->w_valid &= ~VALID_WCOL;
5761 }
5762 curwin->w_set_curswant = TRUE;
5763}
5764
5765/*
5766 * "g_": to the last non-blank character in the line or <count> lines
5767 * downward.
5768 */
5769 static void
5770nv_g_underscore_cmd(cmdarg_T *cap)
5771{
5772 char_u *ptr;
5773
5774 cap->oap->motion_type = MCHAR;
5775 cap->oap->inclusive = TRUE;
5776 curwin->w_curswant = MAXCOL;
5777 if (cursor_down((long)(cap->count1 - 1),
5778 cap->oap->op_type == OP_NOP) == FAIL)
5779 {
5780 clearopbeep(cap->oap);
5781 return;
5782 }
5783
5784 ptr = ml_get_curline();
5785
5786 // In Visual mode we may end up after the line.
5787 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5788 --curwin->w_cursor.col;
5789
5790 // Decrease the cursor column until it's on a non-blank.
5791 while (curwin->w_cursor.col > 0
5792 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5793 --curwin->w_cursor.col;
5794 curwin->w_set_curswant = TRUE;
5795 adjust_for_sel(cap);
5796}
5797
5798/*
5799 * "g$" : Like "$" but for screen lines.
5800 */
5801 static void
5802nv_g_dollar_cmd(cmdarg_T *cap)
5803{
5804 oparg_T *oap = cap->oap;
5805 int i;
5806 int col_off = curwin_col_off();
5807
5808 oap->motion_type = MCHAR;
5809 oap->inclusive = TRUE;
5810 if (curwin->w_p_wrap && curwin->w_width != 0)
5811 {
5812 curwin->w_curswant = MAXCOL; // so we stay at the end
5813 if (cap->count1 == 1)
5814 {
5815 int width1 = curwin->w_width - col_off;
5816 int width2 = width1 + curwin_col_off2();
5817
5818 validate_virtcol();
5819 i = width1 - 1;
5820 if (curwin->w_virtcol >= (colnr_T)width1)
5821 i += ((curwin->w_virtcol - width1) / width2 + 1)
5822 * width2;
5823 coladvance((colnr_T)i);
5824
5825 // Make sure we stick in this column.
5826 validate_virtcol();
5827 curwin->w_curswant = curwin->w_virtcol;
5828 curwin->w_set_curswant = FALSE;
5829 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5830 {
5831 // Check for landing on a character that got split at
5832 // the end of the line. We do not want to advance to
5833 // the next screen line.
5834 if (curwin->w_virtcol > (colnr_T)i)
5835 --curwin->w_cursor.col;
5836 }
5837 }
5838 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5839 clearopbeep(oap);
5840 }
5841 else
5842 {
5843 if (cap->count1 > 1)
5844 // if it fails, let the cursor still move to the last char
5845 (void)cursor_down(cap->count1 - 1, FALSE);
5846
5847 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5848 coladvance((colnr_T)i);
5849
5850 // if the character doesn't fit move one back
5851 if (curwin->w_cursor.col > 0
5852 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5853 {
5854 colnr_T vcol;
5855
5856 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5857 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5858 --curwin->w_cursor.col;
5859 }
5860
5861 // Make sure we stick in this column.
5862 validate_virtcol();
5863 curwin->w_curswant = curwin->w_virtcol;
5864 curwin->w_set_curswant = FALSE;
5865 }
5866}
5867
5868/*
5869 * "gi": start Insert at the last position.
5870 */
5871 static void
5872nv_gi_cmd(cmdarg_T *cap)
5873{
5874 int i;
5875
5876 if (curbuf->b_last_insert.lnum != 0)
5877 {
5878 curwin->w_cursor = curbuf->b_last_insert;
5879 check_cursor_lnum();
5880 i = (int)STRLEN(ml_get_curline());
5881 if (curwin->w_cursor.col > (colnr_T)i)
5882 {
5883 if (virtual_active())
5884 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5885 curwin->w_cursor.col = i;
5886 }
5887 }
5888 cap->cmdchar = 'i';
5889 nv_edit(cap);
5890}
5891
5892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 * Commands starting with "g".
5894 */
5895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005896nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 switch (cap->nchar)
5902 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005903 case Ctrl_A:
5904 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005906 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005907 if (!VIsual_active && cap->nchar == Ctrl_A)
5908 vim_mem_profile_dump();
5909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005911 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005912 if (VIsual_active)
5913 {
5914 cap->arg = TRUE;
5915 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005916 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005917 nv_addsub(cap);
5918 }
5919 else
5920 clearopbeep(oap);
5921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005923 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 case 'R':
5925 cap->arg = TRUE;
5926 nv_Replace(cap);
5927 break;
5928
5929 case 'r':
5930 nv_vreplace(cap);
5931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 case '&':
5934 do_cmdline_cmd((char_u *)"%s//~/&");
5935 break;
5936
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005937 // "gv": Reselect the previous Visual area. If Visual already active,
5938 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005940 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005942
5943 // "gV": Don't reselect the previous Visual area after a Select mode
5944 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 case 'V':
5946 VIsual_reselect = FALSE;
5947 break;
5948
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005949 // "gh": start Select mode.
5950 // "gH": start Select line mode.
5951 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 case K_BS:
5953 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005954 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 case 'h':
5956 case 'H':
5957 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 cap->cmdchar = cap->nchar + ('v' - 'h');
5959 cap->arg = TRUE;
5960 nv_visual(cap);
5961 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005963 // "gn", "gN" visually select next/previous search match
5964 // "gn" selects next match
5965 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005966 case 'N':
5967 case 'n':
5968 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005969 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005972 // "gj" and "gk" two new funny movement keys -- up and down
5973 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 case 'j':
5975 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005976 // with 'nowrap' it works just like the normal "j" command.
5977 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
5979 oap->motion_type = MLINE;
5980 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5981 }
5982 else
5983 i = nv_screengo(oap, FORWARD, cap->count1);
5984 if (i == FAIL)
5985 clearopbeep(oap);
5986 break;
5987
5988 case 'k':
5989 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005990 // with 'nowrap' it works just like the normal "k" command.
5991 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
5993 oap->motion_type = MLINE;
5994 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5995 }
5996 else
5997 i = nv_screengo(oap, BACKWARD, cap->count1);
5998 if (i == FAIL)
5999 clearopbeep(oap);
6000 break;
6001
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006002 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case 'J':
6004 nv_join(cap);
6005 break;
6006
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006007 // "g0", "g^" : Like "0" and "^" but for screen lines.
6008 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 case '0':
6011 case 'm':
6012 case K_HOME:
6013 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006014 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 break;
6016
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006017 case 'M':
6018 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006019 oap->motion_type = MCHAR;
6020 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006021 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006022 if (cap->count0 > 0 && cap->count0 <= 100)
6023 coladvance((colnr_T)(i * cap->count0 / 100));
6024 else
6025 coladvance((colnr_T)(i / 2));
6026 curwin->w_set_curswant = TRUE;
6027 }
6028 break;
6029
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006030 // "g_": to the last non-blank character in the line or <count> lines
6031 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006033 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 break;
6035
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006036 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 case '$':
6038 case K_END:
6039 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006040 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 break;
6042
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006043 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 case '*':
6045 case '#':
6046#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006047 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006049 case Ctrl_RSB: // :tag or :tselect for current identifier
6050 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 nv_ident(cap);
6052 break;
6053
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006054 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 case 'e':
6056 case 'E':
6057 oap->motion_type = MCHAR;
6058 curwin->w_set_curswant = TRUE;
6059 oap->inclusive = TRUE;
6060 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6061 clearopbeep(oap);
6062 break;
6063
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006064 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006066 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 break;
6068
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006069 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006071 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 break;
6073
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 case 'I':
6076 beginline(0);
6077 if (!checkclearopq(oap))
6078 invoke_edit(cap, FALSE, 'g', FALSE);
6079 break;
6080
6081#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 // "gf": goto file, edit file under cursor
6083 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006085 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 nv_gotofile(cap);
6087 break;
6088#endif
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 case '\'':
6092 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006093 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 case '`':
6095 nv_gomark(cap);
6096 break;
6097
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006098 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006100 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 break;
6102
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006103 // "ga": Display the ascii value of the character under the
6104 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 case 'a':
6106 do_ascii(NULL);
6107 break;
6108
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006109 // "g8": Display the bytes used for the UTF-8 character under the
6110 // cursor. It is displayed in hex.
6111 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006113 if (cap->count0 == 8)
6114 utf_find_illegal();
6115 else
6116 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006119 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006120 case '<':
6121 show_sb_text();
6122 break;
6123
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006124 // "gg": Goto the first line in file. With a count it goes to
6125 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 case 'g':
6127 cap->arg = FALSE;
6128 nv_goto(cap);
6129 break;
6130
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006131 // Two-character operators:
6132 // "gq" Format text
6133 // "gw" Format text and keep cursor position
6134 // "g~" Toggle the case of the text.
6135 // "gu" Change text to lower case.
6136 // "gU" Change text to upper case.
6137 // "g?" rot13 encoding
6138 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 case 'q':
6140 case 'w':
6141 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006142 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 case '~':
6144 case 'u':
6145 case 'U':
6146 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006147 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 nv_operator(cap);
6149 break;
6150
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006151 // "gd": Find first occurrence of pattern under the cursor in the
6152 // current function
6153 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 case 'd':
6155 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006156 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 break;
6158
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006159 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 case K_MIDDLEMOUSE:
6161 case K_MIDDLEDRAG:
6162 case K_MIDDLERELEASE:
6163 case K_LEFTMOUSE:
6164 case K_LEFTDRAG:
6165 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006166 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 case K_RIGHTMOUSE:
6168 case K_RIGHTDRAG:
6169 case K_RIGHTRELEASE:
6170 case K_X1MOUSE:
6171 case K_X1DRAG:
6172 case K_X1RELEASE:
6173 case K_X2MOUSE:
6174 case K_X2DRAG:
6175 case K_X2RELEASE:
6176 mod_mask = MOD_MASK_CTRL;
6177 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 case K_IGNORE:
6181 break;
6182
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006183 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case 'p':
6185 case 'P':
6186 nv_put(cap);
6187 break;
6188
6189#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006190 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 case 'o':
6192 goto_byte(cap->count0);
6193 break;
6194#endif
6195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006196 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006198 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 do_exmode(TRUE);
6200 break;
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 case ',':
6203 nv_pcmark(cap);
6204 break;
6205
6206 case ';':
6207 cap->count1 = -cap->count1;
6208 nv_pcmark(cap);
6209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006212 if (!checkclearop(oap))
6213 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006215 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006216 if (!checkclearop(oap))
6217 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006218 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219
Bram Moolenaar62a23252020-08-09 14:04:42 +02006220 case TAB:
6221 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6222 clearopbeep(oap);
6223 break;
6224
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006225 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006226 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006227 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006228 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006229 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006230 break;
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 default:
6233 clearopbeep(oap);
6234 break;
6235 }
6236}
6237
6238/*
6239 * Handle "o" and "O" commands.
6240 */
6241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006242n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006244#ifdef FEAT_CONCEAL
6245 linenr_T oldline = curwin->w_cursor.lnum;
6246#endif
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 if (!checkclearopq(cap->oap))
6249 {
6250#ifdef FEAT_FOLDING
6251 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006252 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 (void)hasFolding(curwin->w_cursor.lnum,
6254 &curwin->w_cursor.lnum, NULL);
6255 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006256 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 (void)hasFolding(curwin->w_cursor.lnum,
6258 NULL, &curwin->w_cursor.lnum);
6259#endif
6260 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6261 (cap->cmdchar == 'O' ? 1 : 0)),
6262 (linenr_T)(curwin->w_cursor.lnum +
6263 (cap->cmdchar == 'o' ? 1 : 0))
6264 ) == OK
6265 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006266 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006267 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006269#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006270 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006271 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006272#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006273#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006274 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006275 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006276 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006277#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006279 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6280 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6282 }
6283 }
6284}
6285
6286/*
6287 * "." command: redo last change.
6288 */
6289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006290nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 if (!checkclearopq(cap->oap))
6293 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006294 // If "restart_edit" is TRUE, the last but one command is repeated
6295 // instead of the last command (inserting text). This is used for
6296 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6298 clearopbeep(cap->oap);
6299 }
6300}
6301
6302/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006303 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 */
6305 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006306nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006308 if (VIsual_select && VIsual_active)
6309 {
6310 int reg;
6311 // Get register name
6312 ++no_mapping;
6313 ++allow_keys;
6314 reg = plain_vgetc();
6315 LANGMAP_ADJUST(reg, TRUE);
6316 --no_mapping;
6317 --allow_keys;
6318
6319 if (reg == '"')
6320 // the unnamed register is 0
6321 reg = 0;
6322
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006323 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006324 return;
6325 }
6326
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (!checkclearopq(cap->oap))
6328 {
6329 u_redo((int)cap->count1);
6330 curwin->w_set_curswant = TRUE;
6331 }
6332}
6333
6334/*
6335 * Handle "U" command.
6336 */
6337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006338nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006340 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006341 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 cap->cmdchar = 'g';
6345 cap->nchar = 'U';
6346 nv_operator(cap);
6347 }
6348 else if (!checkclearopq(cap->oap))
6349 {
6350 u_undoline();
6351 curwin->w_set_curswant = TRUE;
6352 }
6353}
6354
6355/*
6356 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6357 * single character.
6358 */
6359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006360nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006362 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006363 {
6364#ifdef FEAT_JOB_CHANNEL
6365 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6366 {
6367 clearopbeep(cap->oap);
6368 return;
6369 }
6370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 else
6374 nv_operator(cap);
6375}
6376
6377/*
6378 * Handle an operator command.
6379 * The actual work is done by do_pending_operator().
6380 */
6381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006382nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int op_type;
6385
6386 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006387#ifdef FEAT_JOB_CHANNEL
6388 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6389 {
6390 clearopbeep(cap->oap);
6391 return;
6392 }
6393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006395 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 nv_lineop(cap);
6397 else if (!checkclearop(cap->oap))
6398 {
6399 cap->oap->start = curwin->w_cursor;
6400 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006401#ifdef FEAT_EVAL
6402 set_op_var(op_type);
6403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405}
6406
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006407#ifdef FEAT_EVAL
6408/*
6409 * Set v:operator to the characters for "optype".
6410 */
6411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006412set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006413{
6414 char_u opchars[3];
6415
6416 if (optype == OP_NOP)
6417 set_vim_var_string(VV_OP, NULL, 0);
6418 else
6419 {
6420 opchars[0] = get_op_char(optype);
6421 opchars[1] = get_extra_op_char(optype);
6422 opchars[2] = NUL;
6423 set_vim_var_string(VV_OP, opchars, -1);
6424 }
6425}
6426#endif
6427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428/*
6429 * Handle linewise operator "dd", "yy", etc.
6430 *
6431 * "_" is is a strange motion command that helps make operators more logical.
6432 * It is actually implemented, but not documented in the real Vi. This motion
6433 * command actually refers to "the current line". Commands like "dd" and "yy"
6434 * are really an alternate form of "d_" and "y_". It does accept a count, so
6435 * "d3_" works to delete 3 lines.
6436 */
6437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006438nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 cap->oap->motion_type = MLINE;
6441 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6442 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006444 && cap->oap->motion_force != 'v'
6445 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 || cap->oap->op_type == OP_LSHIFT
6447 || cap->oap->op_type == OP_RSHIFT)
6448 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006449 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 beginline(BL_WHITE | BL_FIX);
6451}
6452
6453/*
6454 * <Home> command.
6455 */
6456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006457nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006460 if (mod_mask & MOD_MASK_CTRL)
6461 nv_goto(cap);
6462 else
6463 {
6464 cap->count0 = 1;
6465 nv_pipe(cap);
6466 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6468 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469}
6470
6471/*
6472 * "|" command.
6473 */
6474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006475nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 cap->oap->motion_type = MCHAR;
6478 cap->oap->inclusive = FALSE;
6479 beginline(0);
6480 if (cap->count0 > 0)
6481 {
6482 coladvance((colnr_T)(cap->count0 - 1));
6483 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6484 }
6485 else
6486 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006487 // keep curswant at the column where we wanted to go, not where
6488 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 curwin->w_set_curswant = FALSE;
6490}
6491
6492/*
6493 * Handle back-word command "b" and "B".
6494 * cap->arg is 1 for "B"
6495 */
6496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006497nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 cap->oap->motion_type = MCHAR;
6500 cap->oap->inclusive = FALSE;
6501 curwin->w_set_curswant = TRUE;
6502 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6503 clearopbeep(cap->oap);
6504#ifdef FEAT_FOLDING
6505 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6506 foldOpenCursor();
6507#endif
6508}
6509
6510/*
6511 * Handle word motion commands "e", "E", "w" and "W".
6512 * cap->arg is TRUE for "E" and "W".
6513 */
6514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006515nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 int n;
6518 int word_end;
6519 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006520 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006522 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6524 word_end = TRUE;
6525 else
6526 word_end = FALSE;
6527 cap->oap->inclusive = word_end;
6528
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006529 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (!word_end && cap->oap->op_type == OP_CHANGE)
6531 {
6532 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006533 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006535 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006537 // Reproduce a funny Vi behaviour: "cw" on a blank only
6538 // changes one character, not all blanks until the start of
6539 // the next word. Only do this when the 'w' flag is included
6540 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6542 {
6543 cap->oap->inclusive = TRUE;
6544 cap->oap->motion_type = MCHAR;
6545 return;
6546 }
6547 }
6548 else
6549 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006550 // This is a little strange. To match what the real Vi does,
6551 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6552 // that we are not on a space or a TAB. This seems impolite
6553 // at first, but it's really more what we mean when we say
6554 // 'cw'.
6555 // Another strangeness: When standing on the end of a word
6556 // "ce" will change until the end of the next word, but "cw"
6557 // will change only one character! This is done by setting
6558 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 cap->oap->inclusive = TRUE;
6560 word_end = TRUE;
6561 flag = TRUE;
6562 }
6563 }
6564 }
6565
6566 cap->oap->motion_type = MCHAR;
6567 curwin->w_set_curswant = TRUE;
6568 if (word_end)
6569 n = end_word(cap->count1, cap->arg, flag, FALSE);
6570 else
6571 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006573 // Don't leave the cursor on the NUL past the end of line. Unless we
6574 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006575 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006576 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 if (n == FAIL && cap->oap->op_type == OP_NOP)
6579 clearopbeep(cap->oap);
6580 else
6581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583#ifdef FEAT_FOLDING
6584 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6585 foldOpenCursor();
6586#endif
6587 }
6588}
6589
6590/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006591 * Used after a movement command: If the cursor ends up on the NUL after the
6592 * end of the line, may move it back to the last character and make the motion
6593 * inclusive.
6594 */
6595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006596adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006597{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006598 // The cursor cannot remain on the NUL when:
6599 // - the column is > 0
6600 // - not in Visual mode or 'selection' is "o"
6601 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006603 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006604 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006605 {
6606 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006607 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006608 if (has_mbyte)
6609 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006610 oap->inclusive = TRUE;
6611 }
6612}
6613
6614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 * "0" and "^" commands.
6616 * cap->arg is the argument for beginline().
6617 */
6618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006619nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 cap->oap->motion_type = MCHAR;
6622 cap->oap->inclusive = FALSE;
6623 beginline(cap->arg);
6624#ifdef FEAT_FOLDING
6625 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6626 foldOpenCursor();
6627#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006628 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6629 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630}
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632/*
6633 * In exclusive Visual mode, may include the last character.
6634 */
6635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006639 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (has_mbyte)
6642 inc_cursor();
6643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 ++curwin->w_cursor.col;
6645 cap->oap->inclusive = FALSE;
6646 }
6647}
6648
6649/*
6650 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6651 * Should check VIsual_mode before calling this.
6652 * Returns TRUE when backed up to the previous line.
6653 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006654 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006655unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 pos_T *pp;
6658
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006659 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006661 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 pp = &curwin->w_cursor;
6663 else
6664 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (pp->coladd > 0)
6666 --pp->coladd;
6667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (pp->col > 0)
6669 {
6670 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006671 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 else if (pp->lnum > 1)
6674 {
6675 --pp->lnum;
6676 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6677 return TRUE;
6678 }
6679 }
6680 return FALSE;
6681}
6682
6683/*
6684 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6685 */
6686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006687nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006692 VIsual_select_reg = 0;
6693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 else if (VIsual_reselect)
6695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006696 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 cap->arg = TRUE;
6698 nv_g_cmd(cap);
6699 }
6700}
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703/*
6704 * "G", "gg", CTRL-END, CTRL-HOME.
6705 * cap->arg is TRUE for "G".
6706 */
6707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006708nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 linenr_T lnum;
6711
6712 if (cap->arg)
6713 lnum = curbuf->b_ml.ml_line_count;
6714 else
6715 lnum = 1L;
6716 cap->oap->motion_type = MLINE;
6717 setpcmark();
6718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006719 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (cap->count0 != 0)
6721 lnum = cap->count0;
6722 if (lnum < 1L)
6723 lnum = 1L;
6724 else if (lnum > curbuf->b_ml.ml_line_count)
6725 lnum = curbuf->b_ml.ml_line_count;
6726 curwin->w_cursor.lnum = lnum;
6727 beginline(BL_SOL | BL_FIX);
6728#ifdef FEAT_FOLDING
6729 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6730 foldOpenCursor();
6731#endif
6732}
6733
6734/*
6735 * CTRL-\ in Normal mode.
6736 */
6737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006738nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6741 {
6742 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006743 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 restart_edit = 0;
6746#ifdef FEAT_CMDWIN
6747 if (cmdwin_type != 0)
6748 cmdwin_result = Ctrl_C;
6749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 if (VIsual_active)
6751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 redraw_curbuf_later(INVERTED);
6754 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (cap->nchar == Ctrl_G && p_im)
6757 restart_edit = 'a';
6758 }
6759 else
6760 clearopbeep(cap->oap);
6761}
6762
6763/*
6764 * ESC in Normal mode: beep, but don't flush buffers.
6765 * Don't even beep if we are canceling a command.
6766 */
6767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006768nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770 int no_reason;
6771
6772 no_reason = (cap->oap->op_type == OP_NOP
6773 && cap->opcount == 0
6774 && cap->count0 == 0
6775 && cap->oap->regname == 0
6776 && !p_im);
6777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006778 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 if (restart_edit == 0
6781#ifdef FEAT_CMDWIN
6782 && cmdwin_type == 0
6783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006786 {
6787 if (anyBufIsChanged())
6788 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6789 else
6790 msg(_("Type :qa and press <Enter> to exit Vim"));
6791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006793 if (restart_edit != 0)
6794 redraw_mode = TRUE; // remove "-- (insert) --"
6795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006796 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6797 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (!p_im)
6799 restart_edit = 0;
6800#ifdef FEAT_CMDWIN
6801 if (cmdwin_type != 0)
6802 {
6803 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006804 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 return;
6806 }
6807#endif
6808 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006809#ifdef FEAT_CMDWIN
6810 else if (cmdwin_type != 0 && ex_normal_busy)
6811 {
6812 // When :normal runs out of characters while in the command line window
6813 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6814 // loop.
6815 cmdwin_result = K_IGNORE;
6816 return;
6817 }
6818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (VIsual_active)
6821 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006822 end_visual_mode(); // stop Visual
6823 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 curwin->w_set_curswant = TRUE;
6825 redraw_curbuf_later(INVERTED);
6826 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006827 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006828 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 clearop(cap->oap);
6830
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006831 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6832 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006833 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 restart_edit = 'a';
6835}
6836
6837/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006838 * Move the cursor for the "A" command.
6839 */
6840 void
6841set_cursor_for_append_to_line(void)
6842{
6843 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006844 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006845 {
6846 int save_State = State;
6847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006848 // Pretend Insert mode here to allow the cursor on the
6849 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006850 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006851 coladvance((colnr_T)MAXCOL);
6852 State = save_State;
6853 }
6854 else
6855 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6856}
6857
6858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006860 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 */
6862 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006863nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006865 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6867 cap->cmdchar = 'i';
6868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006869 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006871 {
6872#ifdef FEAT_TERMINAL
6873 if (term_in_normal_mode())
6874 {
6875 end_visual_mode();
6876 clearop(cap->oap);
6877 term_enter_job_mode();
6878 return;
6879 }
6880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006884 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006885 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6886 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
6888#ifdef FEAT_TEXTOBJ
6889 nv_object(cap);
6890#else
6891 clearopbeep(cap->oap);
6892#endif
6893 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006894#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006895 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006896 {
6897 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006898 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006899 return;
6900 }
6901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 else if (!curbuf->b_p_ma && !p_im)
6903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006905 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006907 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006908 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006909 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006911 else if (cap->cmdchar == K_PS && VIsual_active)
6912 {
6913 pos_T old_pos = curwin->w_cursor;
6914 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006915 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006917 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006918 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6919 {
6920 shift_delete_registers();
6921 cap->oap->regname = '1';
6922 }
6923 else
6924 cap->oap->regname = '-';
6925 cap->cmdchar = 'd';
6926 cap->nchar = NUL;
6927 nv_operator(cap);
6928 do_pending_operator(cap, 0, FALSE);
6929 cap->cmdchar = K_PS;
6930
zeertzjq7a732522022-03-14 20:46:41 +00006931 if (*ml_get_cursor() != NUL)
6932 {
6933 if (old_visual_mode == 'V')
6934 {
6935 // In linewise Visual mode insert before the beginning of the
6936 // next line.
6937 // When the last line in the buffer was deleted then create a
6938 // new line, otherwise there is not need to move cursor.
6939 // Detect this by checking if cursor moved above Visual area.
6940 if (curwin->w_cursor.lnum < old_pos.lnum
6941 && curwin->w_cursor.lnum < old_visual.lnum)
6942 {
6943 if (u_save_cursor() == OK)
6944 {
6945 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6946 FALSE);
6947 appended_lines(curwin->w_cursor.lnum++, 1L);
6948 }
6949 }
6950 }
6951 // When the last char in the line was deleted then append.
6952 // Detect this by checking if cursor moved before Visual area.
6953 else if (curwin->w_cursor.col < old_pos.col
6954 && curwin->w_cursor.col < old_visual.col)
6955 inc_cursor();
6956 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006958 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006959 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 else if (!checkclearopq(cap->oap))
6962 {
6963 switch (cap->cmdchar)
6964 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006965 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006966 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 break;
6968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006969 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006970 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6971 beginline(BL_WHITE);
6972 else
6973 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 break;
6975
Bram Moolenaara1891842017-02-04 21:34:31 +01006976 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006977 // Bracketed paste works like "a"ppend, unless the cursor is in
6978 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006979 if (curwin->w_cursor.col == 0)
6980 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006981 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006983 case 'a': // "a"ppend is like "i"nsert on the next character.
6984 // increment coladd when in virtual space, increment the
6985 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (virtual_active()
6987 && (curwin->w_cursor.coladd > 0
6988 || *ml_get_cursor() == NUL
6989 || *ml_get_cursor() == TAB))
6990 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006991 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 inc_cursor();
6993 break;
6994 }
6995
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
6997 {
6998 int save_State = State;
6999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 // Pretend Insert mode here to allow the cursor on the
7001 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007002 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 coladvance(getviscol());
7004 State = save_State;
7005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7008 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007009 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007010 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007011 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012}
7013
7014/*
7015 * Invoke edit() and take care of "restart_edit" and the return value.
7016 */
7017 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007018invoke_edit(
7019 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007020 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007021 int cmd,
7022 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 int restart_edit_save = 0;
7025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007026 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7027 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7028 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (repl || !stuff_empty())
7030 restart_edit_save = restart_edit;
7031 else
7032 restart_edit_save = 0;
7033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007034 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 restart_edit = 0;
7036
7037 if (edit(cmd, startln, cap->count1))
7038 cap->retval |= CA_COMMAND_BUSY;
7039
7040 if (restart_edit == 0)
7041 restart_edit = restart_edit_save;
7042}
7043
7044#ifdef FEAT_TEXTOBJ
7045/*
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 Moolenaar6e0ce172019-12-05 20:12:41 +01007091 case 't': // "at" = a tag block (xml and html)
7092 // Do not adjust oap->end in do_pending_operator()
7093 // otherwise there are different results for 'dit'
7094 // (note leading whitespace in last line):
7095 // 1) <b> 2) <b>
7096 // foobar foobar
7097 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007098 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007099 flag = current_tagblock(cap->oap, cap->count1, include);
7100 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 flag = current_par(cap->oap, cap->count1, include, 'p');
7103 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 flag = current_sent(cap->oap, cap->count1, include);
7106 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007107 case '"': // "a"" = a double quoted string
7108 case '\'': // "a'" = a single quoted string
7109 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007110 flag = current_quote(cap->oap, cap->count1, include,
7111 cap->nchar);
7112 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007113#if 0 // TODO
7114 case 'S': // "aS" = a section
7115 case 'f': // "af" = a filename
7116 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117#endif
7118 default:
7119 flag = FAIL;
7120 break;
7121 }
7122
7123 curbuf->b_p_mps = mps_save;
7124 if (flag == FAIL)
7125 clearopbeep(cap->oap);
7126 adjust_cursor_col();
7127 curwin->w_set_curswant = TRUE;
7128}
7129#endif
7130
7131/*
7132 * "q" command: Start/stop recording.
7133 * "q:", "q/", "q?": edit command-line in command-line window.
7134 */
7135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007136nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
7138 if (cap->oap->op_type == OP_FORMAT)
7139 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007140 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 cap->cmdchar = 'g';
7142 cap->nchar = 'q';
7143 nv_operator(cap);
7144 }
7145 else if (!checkclearop(cap->oap))
7146 {
7147#ifdef FEAT_CMDWIN
7148 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
7159#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007160 // (stop) recording into a named register, unless executing a
7161 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007162 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 clearopbeep(cap->oap);
7164 }
7165}
7166
7167/*
7168 * Handle the "@r" command.
7169 */
7170 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007171nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 if (checkclearop(cap->oap))
7174 return;
7175#ifdef FEAT_EVAL
7176 if (cap->nchar == '=')
7177 {
7178 if (get_expr_register() == NUL)
7179 return;
7180 }
7181#endif
7182 while (cap->count1-- && !got_int)
7183 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007184 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {
7186 clearopbeep(cap->oap);
7187 break;
7188 }
7189 line_breakcheck();
7190 }
7191}
7192
7193/*
7194 * Handle the CTRL-U and CTRL-D commands.
7195 */
7196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007197nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7200 || (cap->cmdchar == Ctrl_D
7201 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7202 clearopbeep(cap->oap);
7203 else if (!checkclearop(cap->oap))
7204 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7205}
7206
7207/*
7208 * Handle "J" or "gJ" command.
7209 */
7210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007211nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007215 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
7217 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7220 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007222 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007223 if (cap->count0 <= 2)
7224 {
7225 clearopbeep(cap->oap);
7226 return;
7227 }
7228 cap->count0 = curbuf->b_ml.ml_line_count
7229 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007231
7232 prep_redo(cap->oap->regname, cap->count0,
7233 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7234 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
7236}
7237
7238/*
7239 * "P", "gP", "p" and "gp" commands.
7240 */
7241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007242nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007244 nv_put_opt(cap, FALSE);
7245}
7246
7247/*
7248 * "P", "gP", "p" and "gp" commands.
7249 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7250 */
7251 static void
7252nv_put_opt(cmdarg_T *cap, int fix_indent)
7253{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 int regname = 0;
7255 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007256 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007257 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 int dir;
7259 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007260 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262 if (cap->oap->op_type != OP_NOP)
7263 {
7264#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007265 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7267 {
7268 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007269 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 }
7271 else
7272#endif
7273 clearopbeep(cap->oap);
7274 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007275#ifdef FEAT_JOB_CHANNEL
7276 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7277 {
7278 clearopbeep(cap->oap);
7279 }
7280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 else
7282 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007283 if (fix_indent)
7284 {
7285 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7286 ? FORWARD : BACKWARD;
7287 flags |= PUT_FIXINDENT;
7288 }
7289 else
7290 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007291 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7292 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 prep_redo_cmd(cap);
7294 if (cap->cmdchar == 'g')
7295 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007296 else if (cap->cmdchar == 'z')
7297 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (VIsual_active)
7300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007301 // Putting in Visual mode: The put text replaces the selected
7302 // text. First delete the selected text, then put the new text.
7303 // Need to save and restore the registers that the delete
7304 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007305 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 regname = cap->oap->regname;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007307 keep_registers = cap->cmdchar == 'P';
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007308#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007311 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007312 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007313#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
7317 )
7318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007319 // The delete is going to overwrite the register we want to
7320 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 reg1 = get_register(regname, TRUE);
7322 }
7323
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007324 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 cap->cmdchar = 'd';
7326 cap->nchar = NUL;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007327 cap->oap->regname = keep_registers ? '_' : NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007328 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 nv_operator(cap);
7330 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007331 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007332 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007334 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 cap->oap->regname = regname;
7336
7337 if (reg1 != NULL)
7338 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007339 // Delete probably changed the register we want to put, save
7340 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 reg2 = get_register(regname, FALSE);
7342 put_register(regname, reg1);
7343 }
7344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007345 // When deleted a linewise Visual area, put the register as
7346 // lines to avoid it joined with the next line. When deletion was
7347 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if (VIsual_mode == 'V')
7349 flags |= PUT_LINE;
7350 else if (VIsual_mode == 'v')
7351 flags |= PUT_LINE_SPLIT;
7352 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7353 flags |= PUT_LINE_FORWARD;
7354 dir = BACKWARD;
7355 if ((VIsual_mode != 'V'
7356 && curwin->w_cursor.col < curbuf->b_op_start.col)
7357 || (VIsual_mode == 'V'
7358 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007359 // cursor is at the end of the line or end of file, put
7360 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007362 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007363 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007365 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007367 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 if (reg2 != NULL)
7369 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007371 // What to reselect with "gv"? Selecting the just put text seems to
7372 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007373 if (was_visual)
7374 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007375 curbuf->b_visual.vi_start = curbuf->b_op_start;
7376 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007377 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007378 if (*p_sel == 'e')
7379 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007380 }
7381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007382 // When all lines were selected and deleted do_put() leaves an empty
7383 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007384 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007385 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007386 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007387 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007389 // If the cursor was in that line, move it to the end of the last
7390 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007391 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7392 {
7393 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7394 coladvance((colnr_T)MAXCOL);
7395 }
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 auto_format(FALSE, TRUE);
7398 }
7399}
7400
7401/*
7402 * "o" and "O" commands.
7403 */
7404 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007405nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
7407#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007408 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7410 {
7411 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007412 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 }
7414 else
7415#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007416 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007418#ifdef FEAT_JOB_CHANNEL
7419 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007420 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 n_opencmd(cap);
7424}
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426#ifdef FEAT_NETBEANS_INTG
7427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007428nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 netbeans_keycommand(cap->nchar);
7431}
7432#endif
7433
7434#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007436nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007438 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007441
Bram Moolenaar3918c952005-03-15 22:34:55 +00007442/*
7443 * Trigger CursorHold event.
7444 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7445 * input buffer. "did_cursorhold" is set to avoid retriggering.
7446 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007449{
7450 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7451 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007452 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007453}