blob: b6c59d3108cbe562658bc58f88ca860381da5863 [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 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 Moolenaareae1b912019-05-09 15:12:55 +020022static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010025static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
27
28/*
29 * nv_*(): functions called to handle Normal and Visual mode commands.
30 * n_*(): functions called to handle Normal mode commands.
31 * v_*(): functions called to handle Visual mode commands.
32 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010033static void nv_ignore(cmdarg_T *cap);
34static void nv_nop(cmdarg_T *cap);
35static void nv_error(cmdarg_T *cap);
36static void nv_help(cmdarg_T *cap);
37static void nv_addsub(cmdarg_T *cap);
38static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010041static void nv_ver_scrollbar(cmdarg_T *cap);
42static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_tabline(cmdarg_T *cap);
46static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000047#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void nv_exmode(cmdarg_T *cap);
49static void nv_colon(cmdarg_T *cap);
50static void nv_ctrlg(cmdarg_T *cap);
51static void nv_ctrlh(cmdarg_T *cap);
52static void nv_clear(cmdarg_T *cap);
53static void nv_ctrlo(cmdarg_T *cap);
54static void nv_hat(cmdarg_T *cap);
55static void nv_Zet(cmdarg_T *cap);
56static void nv_ident(cmdarg_T *cap);
57static void nv_tagpop(cmdarg_T *cap);
58static void nv_scroll(cmdarg_T *cap);
59static void nv_right(cmdarg_T *cap);
60static void nv_left(cmdarg_T *cap);
61static void nv_up(cmdarg_T *cap);
62static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_end(cmdarg_T *cap);
64static void nv_dollar(cmdarg_T *cap);
65static void nv_search(cmdarg_T *cap);
66static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020067static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010068static void nv_csearch(cmdarg_T *cap);
69static void nv_brackets(cmdarg_T *cap);
70static void nv_percent(cmdarg_T *cap);
71static void nv_brace(cmdarg_T *cap);
72static void nv_mark(cmdarg_T *cap);
73static void nv_findpar(cmdarg_T *cap);
74static void nv_undo(cmdarg_T *cap);
75static void nv_kundo(cmdarg_T *cap);
76static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010077static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
79static void v_visop(cmdarg_T *cap);
80static void nv_subst(cmdarg_T *cap);
81static void nv_abbrev(cmdarg_T *cap);
82static void nv_optrans(cmdarg_T *cap);
83static void nv_gomark(cmdarg_T *cap);
84static void nv_pcmark(cmdarg_T *cap);
85static void nv_regname(cmdarg_T *cap);
86static void nv_visual(cmdarg_T *cap);
87static void n_start_visual_mode(int c);
88static void nv_window(cmdarg_T *cap);
89static void nv_suspend(cmdarg_T *cap);
90static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000092static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010093static void nv_Undo(cmdarg_T *cap);
94static void nv_tilde(cmdarg_T *cap);
95static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000096#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010097static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000098#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010099static void nv_lineop(cmdarg_T *cap);
100static void nv_home(cmdarg_T *cap);
101static void nv_pipe(cmdarg_T *cap);
102static void nv_bck_word(cmdarg_T *cap);
103static void nv_wordcmd(cmdarg_T *cap);
104static void nv_beginline(cmdarg_T *cap);
105static void adjust_cursor(oparg_T *oap);
106static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100107static void nv_select(cmdarg_T *cap);
108static void nv_goto(cmdarg_T *cap);
109static void nv_normal(cmdarg_T *cap);
110static void nv_esc(cmdarg_T *oap);
111static void nv_edit(cmdarg_T *cap);
112static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100114static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100116static void nv_record(cmdarg_T *cap);
117static void nv_at(cmdarg_T *cap);
118static void nv_halfpage(cmdarg_T *cap);
119static void nv_join(cmdarg_T *cap);
120static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200121static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100129static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131/*
132 * Function to be called for a Normal or Visual mode command.
133 * The argument is a cmdarg_T.
134 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100137// Values for cmd_flags.
138#define NV_NCH 0x01 // may need to get a second char
139#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
140#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
141#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100143#define NV_SS 0x10 // may start selection
144#define NV_SSS 0x20 // may start selection with shift modifier
145#define NV_STS 0x40 // may stop selection without shift modif.
146#define NV_RL 0x80 // 'rightleft' modifies command
147#define NV_KEEPREG 0x100 // don't clear regname
148#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150/*
151 * Generally speaking, every Normal mode command should either clear any
152 * pending operator (with *clearop*()), or set the motion type variable
153 * oap->motion_type.
154 *
155 * When a cursor motion command is made, it is marked as being a character or
156 * line oriented motion. Then, if an operator is in effect, the operation
157 * becomes character or line oriented accordingly.
158 */
159
160/*
161 * This table contains one entry for every Normal or Visual mode command.
162 * The order doesn't matter, init_normal_cmds() will create a sorted index.
163 * It is faster when all keys from zero to '~' are present.
164 */
165static const struct nv_cmd
166{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100167 int cmd_char; // (first) command character
168 nv_func_T cmd_func; // function for this command
169 short_u cmd_flags; // NV_ flags
170 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171} nv_cmds[] =
172{
173 {NUL, nv_error, 0, 0},
174 {Ctrl_A, nv_addsub, 0, 0},
175 {Ctrl_B, nv_page, NV_STS, BACKWARD},
176 {Ctrl_C, nv_esc, 0, TRUE},
177 {Ctrl_D, nv_halfpage, 0, 0},
178 {Ctrl_E, nv_scroll_line, 0, TRUE},
179 {Ctrl_F, nv_page, NV_STS, FORWARD},
180 {Ctrl_G, nv_ctrlg, 0, 0},
181 {Ctrl_H, nv_ctrlh, 0, 0},
182 {Ctrl_I, nv_pcmark, 0, 0},
183 {NL, nv_down, 0, FALSE},
184 {Ctrl_K, nv_error, 0, 0},
185 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100186 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {Ctrl_N, nv_down, NV_STS, FALSE},
188 {Ctrl_O, nv_ctrlo, 0, 0},
189 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000190 {Ctrl_Q, nv_visual, 0, FALSE},
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000191 {Ctrl_R, nv_redo_or_register, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {Ctrl_S, nv_ignore, 0, 0},
193 {Ctrl_T, nv_tagpop, NV_NCW, 0},
194 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {Ctrl_V, nv_visual, 0, FALSE},
196 {'V', nv_visual, 0, FALSE},
197 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_W, nv_window, 0, 0},
199 {Ctrl_X, nv_addsub, 0, 0},
200 {Ctrl_Y, nv_scroll_line, 0, FALSE},
201 {Ctrl_Z, nv_suspend, 0, 0},
202 {ESC, nv_esc, 0, FALSE},
203 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
204 {Ctrl_RSB, nv_ident, NV_NCW, 0},
205 {Ctrl_HAT, nv_hat, NV_NCW, 0},
206 {Ctrl__, nv_error, 0, 0},
207 {' ', nv_right, 0, 0},
208 {'!', nv_operator, 0, 0},
209 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
210 {'#', nv_ident, 0, 0},
211 {'$', nv_dollar, 0, 0},
212 {'%', nv_percent, 0, 0},
213 {'&', nv_optrans, 0, 0},
214 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
215 {'(', nv_brace, 0, BACKWARD},
216 {')', nv_brace, 0, FORWARD},
217 {'*', nv_ident, 0, 0},
218 {'+', nv_down, 0, TRUE},
219 {',', nv_csearch, 0, TRUE},
220 {'-', nv_up, 0, TRUE},
221 {'.', nv_dot, NV_KEEPREG, 0},
222 {'/', nv_search, 0, FALSE},
223 {'0', nv_beginline, 0, 0},
224 {'1', nv_ignore, 0, 0},
225 {'2', nv_ignore, 0, 0},
226 {'3', nv_ignore, 0, 0},
227 {'4', nv_ignore, 0, 0},
228 {'5', nv_ignore, 0, 0},
229 {'6', nv_ignore, 0, 0},
230 {'7', nv_ignore, 0, 0},
231 {'8', nv_ignore, 0, 0},
232 {'9', nv_ignore, 0, 0},
233 {':', nv_colon, 0, 0},
234 {';', nv_csearch, 0, FALSE},
235 {'<', nv_operator, NV_RL, 0},
236 {'=', nv_operator, 0, 0},
237 {'>', nv_operator, NV_RL, 0},
238 {'?', nv_search, 0, FALSE},
239 {'@', nv_at, NV_NCH_NOP, FALSE},
240 {'A', nv_edit, 0, 0},
241 {'B', nv_bck_word, 0, 1},
242 {'C', nv_abbrev, NV_KEEPREG, 0},
243 {'D', nv_abbrev, NV_KEEPREG, 0},
244 {'E', nv_wordcmd, 0, TRUE},
245 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
246 {'G', nv_goto, 0, TRUE},
247 {'H', nv_scroll, 0, 0},
248 {'I', nv_edit, 0, 0},
249 {'J', nv_join, 0, 0},
250 {'K', nv_ident, 0, 0},
251 {'L', nv_scroll, 0, 0},
252 {'M', nv_scroll, 0, 0},
253 {'N', nv_next, 0, SEARCH_REV},
254 {'O', nv_open, 0, 0},
255 {'P', nv_put, 0, 0},
256 {'Q', nv_exmode, NV_NCW, 0},
257 {'R', nv_Replace, 0, FALSE},
258 {'S', nv_subst, NV_KEEPREG, 0},
259 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
260 {'U', nv_Undo, 0, 0},
261 {'W', nv_wordcmd, 0, TRUE},
262 {'X', nv_abbrev, NV_KEEPREG, 0},
263 {'Y', nv_abbrev, NV_KEEPREG, 0},
264 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
265 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
266 {'\\', nv_error, 0, 0},
267 {']', nv_brackets, NV_NCH_ALW, FORWARD},
268 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
269 {'_', nv_lineop, 0, 0},
270 {'`', nv_gomark, NV_NCH_ALW, FALSE},
271 {'a', nv_edit, NV_NCH, 0},
272 {'b', nv_bck_word, 0, 0},
273 {'c', nv_operator, 0, 0},
274 {'d', nv_operator, 0, 0},
275 {'e', nv_wordcmd, 0, FALSE},
276 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
277 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
278 {'h', nv_left, NV_RL, 0},
279 {'i', nv_edit, NV_NCH, 0},
280 {'j', nv_down, 0, FALSE},
281 {'k', nv_up, 0, FALSE},
282 {'l', nv_right, NV_RL, 0},
283 {'m', nv_mark, NV_NCH_NOP, 0},
284 {'n', nv_next, 0, 0},
285 {'o', nv_open, 0, 0},
286 {'p', nv_put, 0, 0},
287 {'q', nv_record, NV_NCH, 0},
288 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
289 {'s', nv_subst, NV_KEEPREG, 0},
290 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
291 {'u', nv_undo, 0, 0},
292 {'w', nv_wordcmd, 0, FALSE},
293 {'x', nv_abbrev, NV_KEEPREG, 0},
294 {'y', nv_operator, 0, 0},
295 {'z', nv_zet, NV_NCH_ALW, 0},
296 {'{', nv_findpar, 0, BACKWARD},
297 {'|', nv_pipe, 0, 0},
298 {'}', nv_findpar, 0, FORWARD},
299 {'~', nv_tilde, 0, 0},
300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100301 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200303 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
304 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
305 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
306 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {K_LEFTMOUSE, nv_mouse, 0, 0},
308 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
309 {K_LEFTDRAG, nv_mouse, 0, 0},
310 {K_LEFTRELEASE, nv_mouse, 0, 0},
311 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100312 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
314 {K_MIDDLEDRAG, nv_mouse, 0, 0},
315 {K_MIDDLERELEASE, nv_mouse, 0, 0},
316 {K_RIGHTMOUSE, nv_mouse, 0, 0},
317 {K_RIGHTDRAG, nv_mouse, 0, 0},
318 {K_RIGHTRELEASE, nv_mouse, 0, 0},
319 {K_X1MOUSE, nv_mouse, 0, 0},
320 {K_X1DRAG, nv_mouse, 0, 0},
321 {K_X1RELEASE, nv_mouse, 0, 0},
322 {K_X2MOUSE, nv_mouse, 0, 0},
323 {K_X2DRAG, nv_mouse, 0, 0},
324 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000325 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000326 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {K_INS, nv_edit, 0, 0},
328 {K_KINS, nv_edit, 0, 0},
329 {K_BS, nv_ctrlh, 0, 0},
330 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
331 {K_S_UP, nv_page, NV_SS, BACKWARD},
332 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
333 {K_S_DOWN, nv_page, NV_SS, FORWARD},
334 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
335 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
336 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
337 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
339 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
340 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
341 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
342 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
343 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
344 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
345 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {K_S_END, nv_end, NV_SS, FALSE},
347 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
348 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
349 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {K_S_HOME, nv_home, NV_SS, 0},
351 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
352 {K_DEL, nv_abbrev, 0, 0},
353 {K_KDEL, nv_abbrev, 0, 0},
354 {K_UNDO, nv_kundo, 0, 0},
355 {K_HELP, nv_help, NV_NCW, 0},
356 {K_F1, nv_help, NV_NCW, 0},
357 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef FEAT_GUI
360 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
361 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
362#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000363#ifdef FEAT_GUI_TABLINE
364 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000365 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#ifdef FEAT_NETBEANS_INTG
368 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
369#endif
370#ifdef FEAT_DND
371 {K_DROP, nv_drop, NV_STS, 0},
372#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000373 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100374 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100375 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaare32c3c42022-01-15 18:26:04 +0000376 {K_SCRIPT_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377};
378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379// Number of commands in nv_cmds[].
K.Takataeeec2542021-06-02 13:28:16 +0200380#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000382#ifndef PROTO // cproto doesn't like this
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000482 * Handle the count before a normal command and set cap->count0.
483 */
484 static int
485normal_cmd_get_count(
486 cmdarg_T *cap,
487 int c,
488 int toplevel UNUSED,
489 int set_prevcount UNUSED,
490 int *ctrl_w,
491 int *need_flushbuf UNUSED)
492{
493getcount:
494 if (!(VIsual_active && VIsual_select))
495 {
496 // Handle a count before a command and compute ca.count0.
497 // Note that '0' is a command and not the start of a count, but it's
498 // part of a count after other digits.
499 while ((c >= '1' && c <= '9')
500 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
501 || c == '0')))
502 {
503 if (c == K_DEL || c == K_KDEL)
504 {
505 cap->count0 /= 10;
506#ifdef FEAT_CMDL_INFO
507 del_from_showcmd(4); // delete the digit and ~@%
508#endif
509 }
510 else if (cap->count0 > 99999999L)
511 {
512 cap->count0 = 999999999L;
513 }
514 else
515 {
516 cap->count0 = cap->count0 * 10 + (c - '0');
517 }
518#ifdef FEAT_EVAL
519 // Set v:count here, when called from main() and not a stuffed
520 // command, so that v:count can be used in an expression mapping
521 // right after the count. Do set it for redo.
522 if (toplevel && readbuf1_empty())
523 set_vcount_ca(cap, &set_prevcount);
524#endif
525 if (*ctrl_w)
526 {
527 ++no_mapping;
528 ++allow_keys; // no mapping for nchar, but keys
529 }
530 ++no_zero_mapping; // don't map zero here
531 c = plain_vgetc();
532 LANGMAP_ADJUST(c, TRUE);
533 --no_zero_mapping;
534 if (*ctrl_w)
535 {
536 --no_mapping;
537 --allow_keys;
538 }
539#ifdef FEAT_CMDL_INFO
540 *need_flushbuf |= add_to_showcmd(c);
541#endif
542 }
543
544 // If we got CTRL-W there may be a/another count
545 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
546 {
547 *ctrl_w = TRUE;
548 cap->opcount = cap->count0; // remember first count
549 cap->count0 = 0;
550 ++no_mapping;
551 ++allow_keys; // no mapping for nchar, but keys
552 c = plain_vgetc(); // get next character
553 LANGMAP_ADJUST(c, TRUE);
554 --no_mapping;
555 --allow_keys;
556#ifdef FEAT_CMDL_INFO
557 *need_flushbuf |= add_to_showcmd(c);
558#endif
559 goto getcount; // jump back
560 }
561 }
562
563 if (c == K_CURSORHOLD)
564 {
565 // Save the count values so that ca.opcount and ca.count0 are exactly
566 // the same when coming back here after handling K_CURSORHOLD.
567 cap->oap->prev_opcount = cap->opcount;
568 cap->oap->prev_count0 = cap->count0;
569 }
570 else if (cap->opcount != 0)
571 {
572 // If we're in the middle of an operator (including after entering a
573 // yank buffer with '"') AND we had a count before the operator, then
574 // that count overrides the current value of ca.count0.
575 // What this means effectively, is that commands like "3dw" get turned
576 // into "d3w" which makes things fall into place pretty neatly.
577 // If you give a count before AND after the operator, they are
578 // multiplied.
579 if (cap->count0)
580 {
581 if (cap->opcount >= 999999999L / cap->count0)
582 cap->count0 = 999999999L;
583 else
584 cap->count0 *= cap->opcount;
585 }
586 else
587 cap->count0 = cap->opcount;
588 }
589
590 // Always remember the count. It will be set to zero (on the next call,
591 // above) when there is no pending operator.
592 // When called from main(), save the count for use by the "count" built-in
593 // variable.
594 cap->opcount = cap->count0;
595 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
596
597#ifdef FEAT_EVAL
598 // Only set v:count when called from main() and not a stuffed command.
599 // Do set it for redo.
600 if (toplevel && readbuf1_empty())
601 set_vcount(cap->count0, cap->count1, set_prevcount);
602#endif
603
604 return c;
605}
606
607/*
608 * Returns TRUE if the normal command (cap) needs a second character.
609 */
610 static int
611normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
612{
613 return ((cmd_flags & NV_NCH)
614 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
615 && cap->oap->op_type == OP_NOP)
616 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
617 || (cap->cmdchar == 'q'
618 && cap->oap->op_type == OP_NOP
619 && reg_recording == 0
620 && reg_executing == 0)
621 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
622 && (cap->oap->op_type != OP_NOP || VIsual_active))));
623}
624
625/*
626 * Get one or more additional characters for a normal command.
627 * Return the updated command index (if changed).
628 */
629 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000630normal_cmd_get_more_chars(
631 int idx_arg,
632 cmdarg_T *cap,
633 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000634{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000635 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000636 int c;
637 int *cp;
638 int repl = FALSE; // get character for replace mode
639 int lit = FALSE; // get extra character literally
640 int langmap_active = FALSE; // using :lmap mappings
641 int lang; // getting a text character
642#ifdef HAVE_INPUT_METHOD
643 int save_smd; // saved value of p_smd
644#endif
645
646 ++no_mapping;
647 ++allow_keys; // no mapping for nchar, but allow key codes
648 // Don't generate a CursorHold event here, most commands can't handle
649 // it, e.g., nv_replace(), nv_csearch().
650 did_cursorhold = TRUE;
651 if (cap->cmdchar == 'g')
652 {
653 /*
654 * For 'g' get the next character now, so that we can check for
655 * "gr", "g'" and "g`".
656 */
657 cap->nchar = plain_vgetc();
658 LANGMAP_ADJUST(cap->nchar, TRUE);
659#ifdef FEAT_CMDL_INFO
660 *need_flushbuf |= add_to_showcmd(cap->nchar);
661#endif
662 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
663 || cap->nchar == Ctrl_BSL)
664 {
665 cp = &cap->extra_char; // need to get a third character
666 if (cap->nchar != 'r')
667 lit = TRUE; // get it literally
668 else
669 repl = TRUE; // get it in replace mode
670 }
671 else
672 cp = NULL; // no third character needed
673 }
674 else
675 {
676 if (cap->cmdchar == 'r') // get it in replace mode
677 repl = TRUE;
678 cp = &cap->nchar;
679 }
680 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
681
682 /*
683 * Get a second or third character.
684 */
685 if (cp != NULL)
686 {
687 if (repl)
688 {
689 State = REPLACE; // pretend Replace mode
690#ifdef CURSOR_SHAPE
691 ui_cursor_shape(); // show different cursor shape
692#endif
693 }
694 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
695 {
696 // Allow mappings defined with ":lmap".
697 --no_mapping;
698 --allow_keys;
699 if (repl)
700 State = LREPLACE;
701 else
702 State = LANGMAP;
703 langmap_active = TRUE;
704 }
705#ifdef HAVE_INPUT_METHOD
706 save_smd = p_smd;
707 p_smd = FALSE; // Don't let the IM code show the mode here
708 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
709 im_set_active(TRUE);
710#endif
711 if ((State & INSERT) && !p_ek)
712 {
713#ifdef FEAT_JOB_CHANNEL
714 ch_log_output = TRUE;
715#endif
716 // Disable bracketed paste and modifyOtherKeys here, we won't
717 // recognize the escape sequences with 'esckeys' off.
718 out_str(T_BD);
719 out_str(T_CTE);
720 }
721
722 *cp = plain_vgetc();
723
724 if ((State & INSERT) && !p_ek)
725 {
726#ifdef FEAT_JOB_CHANNEL
727 ch_log_output = TRUE;
728#endif
729 // Re-enable bracketed paste mode and modifyOtherKeys
730 out_str(T_BE);
731 out_str(T_CTI);
732 }
733
734 if (langmap_active)
735 {
736 // Undo the decrement done above
737 ++no_mapping;
738 ++allow_keys;
739 State = NORMAL_BUSY;
740 }
741#ifdef HAVE_INPUT_METHOD
742 if (lang)
743 {
744 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
745 im_save_status(&curbuf->b_p_iminsert);
746 im_set_active(FALSE);
747 }
748 p_smd = save_smd;
749#endif
750 State = NORMAL_BUSY;
751#ifdef FEAT_CMDL_INFO
752 *need_flushbuf |= add_to_showcmd(*cp);
753#endif
754
755 if (!lit)
756 {
757#ifdef FEAT_DIGRAPHS
758 // Typing CTRL-K gets a digraph.
759 if (*cp == Ctrl_K
760 && ((nv_cmds[idx].cmd_flags & NV_LANG)
761 || cp == &cap->extra_char)
762 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
763 {
764 c = get_digraph(FALSE);
765 if (c > 0)
766 {
767 *cp = c;
768# ifdef FEAT_CMDL_INFO
769 // Guessing how to update showcmd here...
770 del_from_showcmd(3);
771 *need_flushbuf |= add_to_showcmd(*cp);
772# endif
773 }
774 }
775#endif
776
777 // adjust chars > 127, except after "tTfFr" commands
778 LANGMAP_ADJUST(*cp, !lang);
779#ifdef FEAT_RIGHTLEFT
780 // adjust Hebrew mapped char
781 if (p_hkmap && lang && KeyTyped)
782 *cp = hkmap(*cp);
783#endif
784 }
785
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000786 // When the next character is CTRL-\ a following CTRL-N means the
787 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000788 if (cp == &cap->extra_char
789 && cap->nchar == Ctrl_BSL
790 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
791 {
792 cap->cmdchar = Ctrl_BSL;
793 cap->nchar = cap->extra_char;
794 idx = find_command(cap->cmdchar);
795 }
796 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
797 cap->oap->op_type = get_op_type(*cp, NUL);
798 else if (*cp == Ctrl_BSL)
799 {
800 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
801
802 // There is a busy wait here when typing "f<C-\>" and then
803 // something different from CTRL-N. Can't be avoided.
804 while ((c = vpeekc()) <= 0 && towait > 0L)
805 {
806 do_sleep(towait > 50L ? 50L : towait, FALSE);
807 towait -= 50L;
808 }
809 if (c > 0)
810 {
811 c = plain_vgetc();
812 if (c != Ctrl_N && c != Ctrl_G)
813 vungetc(c);
814 else
815 {
816 cap->cmdchar = Ctrl_BSL;
817 cap->nchar = c;
818 idx = find_command(cap->cmdchar);
819 }
820 }
821 }
822
823 // When getting a text character and the next character is a
824 // multi-byte character, it could be a composing character.
825 // However, don't wait for it to arrive. Also, do enable mapping,
826 // because if it's put back with vungetc() it's too late to apply
827 // mapping.
828 --no_mapping;
829 while (enc_utf8 && lang && (c = vpeekc()) > 0
830 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
831 {
832 c = plain_vgetc();
833 if (!utf_iscomposing(c))
834 {
835 vungetc(c); // it wasn't, put it back
836 break;
837 }
838 else if (cap->ncharC1 == 0)
839 cap->ncharC1 = c;
840 else
841 cap->ncharC2 = c;
842 }
843 ++no_mapping;
844 }
845 --no_mapping;
846 --allow_keys;
847
848 return idx;
849}
850
851/*
852 * Returns TRUE if after processing a normal mode command, need to wait for a
853 * moment when a message is displayed that will be overwritten by the mode
854 * message.
855 */
856 static int
857normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
858{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000859 // In Visual mode and with "^O" in Insert mode, a short message will be
860 // overwritten by the mode message. Wait a bit, until a key is hit.
861 // In Visual mode, it's more important to keep the Visual area updated
862 // than keeping a message (e.g. from a /pat search).
863 // Only do this if the command was typed, not from a mapping.
864 // Don't wait when emsg_silent is non-zero.
865 // Also wait a bit after an error message, e.g. for "^O:".
866 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000867 return ( ((p_smd
868 && msg_silent == 0
869 && (restart_edit != 0
870 || (VIsual_active
871 && old_pos->lnum == curwin->w_cursor.lnum
872 && old_pos->col == curwin->w_cursor.col)
873 )
874 && (clear_cmdline
875 || redraw_cmdline)
876 && (msg_didout || (msg_didany && msg_scroll))
877 && !msg_nowait
878 && KeyTyped)
879 || (restart_edit != 0
880 && !VIsual_active
881 && (msg_scroll
882 || emsg_on_display)))
883 && cap->oap->regname == 0
884 && !(cap->retval & CA_COMMAND_BUSY)
885 && stuff_empty()
886 && typebuf_typed()
887 && emsg_silent == 0
888 && !in_assert_fails
889 && !did_wait_return
890 && cap->oap->op_type == OP_NOP);
891}
892
893/*
894 * After processing a normal mode command, wait for a moment when a message is
895 * displayed that will be overwritten by the mode message.
896 */
897 static void
898normal_cmd_wait_for_msg(void)
899{
900 int save_State = State;
901
902 // Draw the cursor with the right shape here
903 if (restart_edit != 0)
904 State = INSERT;
905
906 // If need to redraw, and there is a "keep_msg", redraw before the
907 // delay
908 if (must_redraw && keep_msg != NULL && !emsg_on_display)
909 {
910 char_u *kmsg;
911
912 kmsg = keep_msg;
913 keep_msg = NULL;
914 // Showmode() will clear keep_msg, but we want to use it anyway.
915 // First update w_topline.
916 setcursor();
917 update_screen(0);
918 // now reset it, otherwise it's put in the history again
919 keep_msg = kmsg;
920
921 kmsg = vim_strsave(keep_msg);
922 if (kmsg != NULL)
923 {
924 msg_attr((char *)kmsg, keep_msg_attr);
925 vim_free(kmsg);
926 }
927 }
928 setcursor();
929#ifdef CURSOR_SHAPE
930 ui_cursor_shape(); // may show different cursor shape
931#endif
932 cursor_on();
933 out_flush();
934 if (msg_scroll || emsg_on_display)
935 ui_delay(1003L, TRUE); // wait at least one second
936 ui_delay(3003L, FALSE); // wait up to three seconds
937 State = save_State;
938
939 msg_scroll = FALSE;
940 emsg_on_display = FALSE;
941}
942
943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 * Execute a command in Normal mode.
945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100947normal_cmd(
948 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100951 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000955 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100956 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 static int old_mapped_len = 0;
959 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000960 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200961 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaara80faa82020-04-12 19:37:17 +0200963 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // Use a count remembered from before entering an operator. After typing
967 // "3d" we return from normal_cmd() and come back here, the "3" is
968 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ca.opcount = opcount;
970
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000971 // If there is an operator pending, then the command we take this time
972 // will terminate it. Finish_op tells us to finish the operation before
973 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef CURSOR_SHAPE
975 c = finish_op;
976#endif
977 finish_op = (oap->op_type != OP_NOP);
978#ifdef CURSOR_SHAPE
979 if (finish_op != c)
980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100981 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# ifdef FEAT_MOUSESHAPE
983 update_mouseshape(-1);
984# endif
985 }
986#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100987 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 // When not finishing an operator and no register name typed, reset the
990 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000994#ifdef FEAT_EVAL
995 set_prevcount = TRUE;
996#endif
997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100999 // Restore counts from before receiving K_CURSORHOLD. This means after
1000 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
1001 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +00001002 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
1003 {
1004 ca.opcount = oap->prev_opcount;
1005 ca.count0 = oap->prev_count0;
1006 oap->prev_opcount = 0;
1007 oap->prev_count0 = 0;
1008 }
Bram Moolenaara983fe92008-07-31 20:04:27 +00001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 State = NORMAL_BUSY;
1013#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001017#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 // Set v:count here, when called from main() and not a stuffed
1019 // command, so that v:count can be used in an expression mapping
1020 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001021 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001022 set_vcount_ca(&ca, &set_prevcount);
1023#endif
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 /*
1026 * Get the command character from the user.
1027 */
1028 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +01001029 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001031 // If a mapping was started in Visual or Select mode, remember the length
1032 // of the mapping. This is used below to not return to Insert mode for as
1033 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_edit == 0)
1035 old_mapped_len = 0;
1036 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001037 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 old_mapped_len = typebuf_maplen();
1039
1040 if (c == NUL)
1041 c = K_ZERO;
1042
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001043 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (VIsual_active
1045 && VIsual_select
1046 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
1047 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001048 int len;
1049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
1051 // 'insertmode' is set) fake a "d"elete command, Insert mode will
1052 // restart automatically.
1053 // Insert the typed character in the typeahead buffer, so that it can
1054 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001055 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
1056
1057 // When recording the character will be recorded again, remove the
1058 // previously recording.
1059 ungetchars(len);
1060
Bram Moolenaar686f51e2005-05-20 21:19:57 +00001061 if (restart_edit != 0)
1062 c = 'd';
1063 else
1064 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001065 msg_nowait = TRUE; // don't delay going to insert mode
1066 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001069 // If the window was made so small that nothing shows, make it at least one
1070 // line and one column when typing a command.
1071 if (KeyTyped && !KeyStuffed)
1072 win_ensure_size();
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_CMDL_INFO
1075 need_flushbuf = add_to_showcmd(c);
1076#endif
1077
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001078 // Get the command count
1079 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
1080 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001082 // Find the command character in the table of commands.
1083 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (ctrl_w)
1085 {
1086 ca.nchar = c;
1087 ca.cmdchar = Ctrl_W;
1088 }
1089 else
1090 ca.cmdchar = c;
1091 idx = find_command(ca.cmdchar);
1092 if (idx < 0)
1093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001094 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 clearopbeep(oap);
1096 goto normal_end;
1097 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001098
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001099 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001101 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001103 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 goto normal_end;
1105 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001106 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
1107 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001109 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (VIsual_active)
1111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (km_stopsel
1114 && (nv_cmds[idx].cmd_flags & NV_STS)
1115 && !(mod_mask & MOD_MASK_SHIFT))
1116 {
1117 end_visual_mode();
1118 redraw_curbuf_later(INVERTED);
1119 }
1120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (km_startsel)
1123 {
1124 if (nv_cmds[idx].cmd_flags & NV_SS)
1125 {
1126 unshift_special(&ca);
1127 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001128 if (idx < 0)
1129 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001130 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001131 clearopbeep(oap);
1132 goto normal_end;
1133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1136 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141#ifdef FEAT_RIGHTLEFT
1142 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
1143 && (nv_cmds[idx].cmd_flags & NV_RL))
1144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001145 // Invert horizontal movements and operations. Only when typed by the
1146 // user directly, not when the result of a mapping or "x" translated
1147 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 switch (ca.cmdchar)
1149 {
1150 case 'l': ca.cmdchar = 'h'; break;
1151 case K_RIGHT: ca.cmdchar = K_LEFT; break;
1152 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
1153 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
1154 case 'h': ca.cmdchar = 'l'; break;
1155 case K_LEFT: ca.cmdchar = K_RIGHT; break;
1156 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
1157 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
1158 case '>': ca.cmdchar = '<'; break;
1159 case '<': ca.cmdchar = '>'; break;
1160 }
1161 idx = find_command(ca.cmdchar);
1162 }
1163#endif
1164
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001165 // Get additional characters if we need them.
1166 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
1167 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
1169#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001170 // Flush the showcmd characters onto the screen so we can see them while
1171 // the command is being executed. Only do this when the shown command was
1172 // actually displayed, otherwise this will slow down a lot when executing
1173 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (need_flushbuf)
1175 out_flush();
1176#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001177 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001178 {
1179 if (ex_normal_busy)
1180 did_cursorhold = save_did_cursorhold;
1181 else
1182 did_cursorhold = FALSE;
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 State = NORMAL;
1186
1187 if (ca.nchar == ESC)
1188 {
1189 clearop(oap);
1190 if (restart_edit == 0 && goto_im())
1191 restart_edit = 'a';
1192 goto normal_end;
1193 }
1194
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001195 if (ca.cmdchar != K_IGNORE)
1196 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001197 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001198 msg_col = 0;
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001203 // When 'keymodel' contains "startsel" some keys start Select/Visual
1204 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (!VIsual_active && km_startsel)
1206 {
1207 if (nv_cmds[idx].cmd_flags & NV_SS)
1208 {
1209 start_selection();
1210 unshift_special(&ca);
1211 idx = find_command(ca.cmdchar);
1212 }
1213 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1214 && (mod_mask & MOD_MASK_SHIFT))
1215 {
1216 start_selection();
1217 mod_mask &= ~MOD_MASK_SHIFT;
1218 }
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001221 // Execute the command!
1222 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 ca.arg = nv_cmds[idx].cmd_arg;
1224 (nv_cmds[idx].cmd_func)(&ca);
1225
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001226 // If we didn't start or finish an operator, reset oap->regname, unless we
1227 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (!finish_op
1229 && !oap->op_type
1230 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1231 {
1232 clearop(oap);
1233#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001234 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#endif
1236 }
1237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001238 // Get the length of mapped chars again after typing a count, second
1239 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001240 if (old_mapped_len > 0)
1241 old_mapped_len = typebuf_maplen();
1242
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001243 // If an operation is pending, handle it. But not for K_IGNORE or
1244 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001245 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001246 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001248 // Wait for a moment when a message is displayed that will be overwritten
1249 // by the mode message.
1250 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
1251 normal_cmd_wait_for_msg();
1252
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001253 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254normal_end:
1255
1256 msg_nowait = FALSE;
1257
Bram Moolenaarcc613032020-06-07 21:31:18 +02001258#ifdef FEAT_EVAL
1259 if (finish_op)
1260 reset_reg_var();
1261#endif
1262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001263 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#ifdef CURSOR_SHAPE
1265 c = finish_op;
1266#endif
1267 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001268 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001270 // Redraw the cursor with another shape, if we were in Operator-pending
1271 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (c || ca.cmdchar == 'r')
1273 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001274 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275# ifdef FEAT_MOUSESHAPE
1276 update_mouseshape(-1);
1277# endif
1278 }
1279#endif
1280
1281#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001282 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001283 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 clear_showcmd();
1285#endif
1286
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001287 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 vim_free(ca.searchbuf);
1289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (has_mbyte)
1291 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (curwin->w_p_scb && toplevel)
1294 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001295 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 do_check_scrollbind(TRUE);
1297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
Bram Moolenaar860cae12010-06-05 23:22:07 +02001299 if (curwin->w_p_crb && toplevel)
1300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001301 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001302 do_check_cursorbind();
1303 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001304
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001305#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001307 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001308 restart_edit = 0;
1309#endif
1310
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001311 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1312 // if still inside a mapping that started in Visual mode).
1313 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1316 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && !(ca.retval & CA_COMMAND_BUSY)
1318 && stuff_empty()
1319 && oap->regname == 0)
1320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (restart_VIsual_select == 1)
1322 {
1323 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001324 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 showmode();
1326 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001327 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001329 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 (void)edit(restart_edit, FALSE, 1L);
1331 }
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (restart_VIsual_select == 2)
1334 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001336 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 opcount = ca.opcount;
1338}
1339
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001340#ifdef FEAT_EVAL
1341/*
1342 * Set v:count and v:count1 according to "cap".
1343 * Set v:prevcount only when "set_prevcount" is TRUE.
1344 */
1345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001347{
1348 long count = cap->count0;
1349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001351 if (cap->opcount != 0)
1352 count = cap->opcount * (count == 0 ? 1 : count);
1353 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001354 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001355}
1356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001359 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 * if not.
1361 */
1362 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001363check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 static int did_check = FALSE;
1366
1367 if (full_screen)
1368 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001369 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001370 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 did_check = TRUE;
1372 }
1373}
1374
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001375#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1376/*
1377 * Call yank_do_autocmd() for "regname".
1378 */
1379 static void
1380call_yank_do_autocmd(int regname)
1381{
1382 oparg_T oa;
1383 yankreg_T *reg;
1384
1385 clear_oparg(&oa);
1386 oa.regname = regname;
1387 oa.op_type = OP_YANK;
1388 oa.is_VIsual = TRUE;
1389 reg = get_register(regname, TRUE);
1390 yank_do_autocmd(&oa, reg);
1391 free_register(reg);
1392}
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001396 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001397 * This function or the next should ALWAYS be called to end Visual mode, except
1398 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
1400 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001401end_visual_mode()
1402{
1403 end_visual_mode_keep_button();
1404 reset_held_button();
1405}
1406
1407 void
1408end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001411 // If we are using the clipboard, then remember what was selected in case
1412 // we need to paste it somewhere while we still own the selection.
1413 // Only do this when the clipboard is already owned. Don't want to grab
1414 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (clip_star.available && clip_star.owned)
1416 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001417
1418# if defined(FEAT_EVAL)
1419 // Emit a TextYankPost for the automatic copy of the selection into the
1420 // star and/or plus register.
1421 if (has_textyankpost())
1422 {
1423 if (clip_isautosel_star())
1424 call_yank_do_autocmd('*');
1425 if (clip_isautosel_plus())
1426 call_yank_do_autocmd('+');
1427 }
1428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429#endif
1430
1431 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 setmouse();
1433 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001435 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001436 curbuf->b_visual.vi_mode = VIsual_mode;
1437 curbuf->b_visual.vi_start = VIsual;
1438 curbuf->b_visual.vi_end = curwin->w_cursor;
1439 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#ifdef FEAT_EVAL
1441 curbuf->b_visual_mode_eval = VIsual_mode;
1442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (!virtual_active())
1444 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001445 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001447 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001448 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449}
1450
1451/*
1452 * Reset VIsual_active and VIsual_reselect.
1453 */
1454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001455reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (VIsual_active)
1458 {
1459 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001460 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 VIsual_reselect = FALSE;
1463}
1464
1465/*
1466 * Reset VIsual_active and VIsual_reselect if it's set.
1467 */
1468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001469reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 if (VIsual_active)
1472 {
1473 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 VIsual_reselect = FALSE;
1476 }
1477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001479 void
1480restore_visual_mode(void)
1481{
1482 if (VIsual_mode_orig != NUL)
1483 {
1484 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1485 VIsual_mode_orig = NUL;
1486 }
1487}
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
1490 * Check for a balloon-eval special item to include when searching for an
1491 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1492 * Returns TRUE if the character at "*ptr" should be included.
1493 * "dir" is FORWARD or BACKWARD, the direction of searching.
1494 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1495 * "bnp" points to a counter for square brackets.
1496 */
1497 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001498find_is_eval_item(
1499 char_u *ptr,
1500 int *colp,
1501 int *bnp,
1502 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001504 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1506 ++*bnp;
1507 if (*bnp > 0)
1508 {
1509 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1510 --*bnp;
1511 return TRUE;
1512 }
1513
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001514 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (*ptr == '.')
1516 return TRUE;
1517
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001518 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1520 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1521 {
1522 *colp += dir;
1523 return TRUE;
1524 }
1525 return FALSE;
1526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528/*
1529 * Find the identifier under or to the right of the cursor.
1530 * "find_type" can have one of three values:
1531 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 * FIND_STRING: find any non-white text
1533 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001534 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 *
1536 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001537 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001539 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 * This doesn't match the real Vi but I like it a little better and it
1541 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * When FIND_IDENT isn't defined, we backup until a blank.
1544 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001545 * Returns the length of the text, or zero if no text is found.
1546 * If text is found, a pointer to the text is put in "*text". This
1547 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001550find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001553 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554}
1555
1556/*
1557 * Like find_ident_under_cursor(), but for any window and any position.
1558 * However: Uses 'iskeyword' from the current window!.
1559 */
1560 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001561find_ident_at_pos(
1562 win_T *wp,
1563 linenr_T lnum,
1564 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001565 char_u **text,
1566 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001567 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001570 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 int this_class = 0;
1573 int prev_class;
1574 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001575 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001577 // if i == 0: try to find an identifier
1578 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1580 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1581 {
1582 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001583 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 */
1585 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (has_mbyte)
1587 {
1588 while (ptr[col] != NUL)
1589 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001590 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 this_class = mb_get_class(ptr + col);
1594 if (this_class != 0 && (i == 1 || this_class != 1))
1595 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001596 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
1599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001601 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 )
1604 ++col;
1605
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001606 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001610 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (has_mbyte)
1613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1616 this_class = mb_get_class((char_u *)"a");
1617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001619 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1622 prev_class = mb_get_class(ptr + prevcol);
1623 if (this_class != prev_class
1624 && (i == 0
1625 || prev_class == 0
1626 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 && (!(find_type & FIND_EVAL)
1628 || prevcol == 0
1629 || !find_is_eval_item(ptr + prevcol, &prevcol,
1630 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 )
1632 break;
1633 col = prevcol;
1634 }
1635
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001636 // If we don't want just any old text, or we've found an
1637 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (this_class > 2)
1639 this_class = 2;
1640 if (!(find_type & FIND_STRING) || this_class == 2)
1641 break;
1642 }
1643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 while (col > 0
1646 && ((i == 0
1647 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001648 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 && (!(find_type & FIND_IDENT)
1650 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 || ((find_type & FIND_EVAL)
1652 && col > 1
1653 && find_is_eval_item(ptr + col - 1, &col,
1654 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 ))
1656 --col;
1657
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001658 // If we don't want just any old text, or we've found an
1659 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1661 break;
1662 }
1663 }
1664
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001665 if (ptr[col] == NUL || (i == 0
1666 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001668 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001669 if ((find_type & FIND_NOERROR) == 0)
1670 {
1671 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001672 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001673 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001674 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 return 0;
1677 }
1678 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001679 *text = ptr;
1680 if (textcol != NULL)
1681 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001684 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 bn = 0;
1687 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (has_mbyte)
1690 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001691 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 this_class = mb_get_class(ptr);
1693 while (ptr[col] != NUL
1694 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1695 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 || ((find_type & FIND_EVAL)
1697 && col <= (int)startcol
1698 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001700 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001704 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 || ((find_type & FIND_EVAL)
1706 && col <= (int)startcol
1707 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 return col;
1712}
1713
1714/*
1715 * Prepare for redo of a normal command.
1716 */
1717 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001718prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 prep_redo(cap->oap->regname, cap->count0,
1721 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1722}
1723
1724/*
1725 * Prepare for redo of any command.
1726 * Note that only the last argument can be a multi-byte char.
1727 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001729prep_redo(
1730 int regname,
1731 long num,
1732 int cmd1,
1733 int cmd2,
1734 int cmd3,
1735 int cmd4,
1736 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001738 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1739}
1740
1741/*
1742 * Prepare for redo of any command with extra count after "cmd2".
1743 */
1744 void
1745prep_redo_num2(
1746 int regname,
1747 long num1,
1748 int cmd1,
1749 int cmd2,
1750 long num2,
1751 int cmd3,
1752 int cmd4,
1753 int cmd5)
1754{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001756 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
1758 AppendCharToRedobuff('"');
1759 AppendCharToRedobuff(regname);
1760 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001761 if (num1 != 0)
1762 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (cmd1 != NUL)
1764 AppendCharToRedobuff(cmd1);
1765 if (cmd2 != NUL)
1766 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001767 if (num2 != 0)
1768 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (cmd3 != NUL)
1770 AppendCharToRedobuff(cmd3);
1771 if (cmd4 != NUL)
1772 AppendCharToRedobuff(cmd4);
1773 if (cmd5 != NUL)
1774 AppendCharToRedobuff(cmd5);
1775}
1776
1777/*
1778 * check for operator active and clear it
1779 *
1780 * return TRUE if operator was active
1781 */
1782 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001783checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 if (oap->op_type == OP_NOP)
1786 return FALSE;
1787 clearopbeep(oap);
1788 return TRUE;
1789}
1790
1791/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001792 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001794 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 */
1796 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001799 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 return FALSE;
1801 clearopbeep(oap);
1802 return TRUE;
1803}
1804
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 oap->op_type = OP_NOP;
1809 oap->regname = 0;
1810 oap->motion_force = NUL;
1811 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001812 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813}
1814
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001816clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 clearop(oap);
1819 beep_flush();
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * Remove the shift modifier from a special key.
1824 */
1825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 switch (cap->cmdchar)
1829 {
1830 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1831 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1832 case K_S_UP: cap->cmdchar = K_UP; break;
1833 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1834 case K_S_HOME: cap->cmdchar = K_HOME; break;
1835 case K_S_END: cap->cmdchar = K_END; break;
1836 }
1837 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001840/*
1841 * If the mode is currently displayed clear the command line or update the
1842 * command displayed.
1843 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001845may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001846{
1847 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001848 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001849#ifdef FEAT_CMDL_INFO
1850 else
1851 clear_showcmd();
1852#endif
1853}
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1856/*
1857 * Routines for displaying a partly typed command
1858 */
1859
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001860#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001862static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863static int showcmd_is_clear = TRUE;
1864static int showcmd_visual = FALSE;
1865
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001866static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001869clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871 if (!p_sc)
1872 return;
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (VIsual_active && !char_avail())
1875 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001876 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 long lines;
1878 colnr_T leftcol, rightcol;
1879 linenr_T top, bot;
1880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001881 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001882 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 top = VIsual.lnum;
1885 bot = curwin->w_cursor.lnum;
1886 }
1887 else
1888 {
1889 top = curwin->w_cursor.lnum;
1890 bot = VIsual.lnum;
1891 }
1892# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001894 (void)hasFolding(top, &top, NULL);
1895 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896# endif
1897 lines = bot - top + 1;
1898
1899 if (VIsual_mode == Ctrl_V)
1900 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001901# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001902 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001903 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001905 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001906 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001907 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001910# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001911 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001912 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1915 (long)(rightcol - leftcol + 1));
1916 }
1917 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1918 sprintf((char *)showcmd_buf, "%ld", lines);
1919 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001920 {
1921 char_u *s, *e;
1922 int l;
1923 int bytes = 0;
1924 int chars = 0;
1925
1926 if (cursor_bot)
1927 {
1928 s = ml_get_pos(&VIsual);
1929 e = ml_get_cursor();
1930 }
1931 else
1932 {
1933 s = ml_get_cursor();
1934 e = ml_get_pos(&VIsual);
1935 }
1936 while ((*p_sel != 'e') ? s <= e : s < e)
1937 {
1938 l = (*mb_ptr2len)(s);
1939 if (l == 0)
1940 {
1941 ++bytes;
1942 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001943 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001944 }
1945 bytes += l;
1946 ++chars;
1947 s += l;
1948 }
1949 if (bytes == chars)
1950 sprintf((char *)showcmd_buf, "%d", chars);
1951 else
1952 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1953 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001954 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 showcmd_visual = TRUE;
1956 }
1957 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 showcmd_buf[0] = NUL;
1960 showcmd_visual = FALSE;
1961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001962 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (showcmd_is_clear)
1964 return;
1965 }
1966
1967 display_showcmd();
1968}
1969
1970/*
1971 * Add 'c' to string of shown command chars.
1972 * Return TRUE if output has been written (and setcursor() has been called).
1973 */
1974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 char_u *p;
1978 int old_len;
1979 int extra_len;
1980 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 int i;
1982 static int ignore[] =
1983 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001984#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1986 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001987#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001988 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001989 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1991 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001992 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001994 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 0
1996 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar09df3122006-01-23 22:23:09 +00001998 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return FALSE;
2000
2001 if (showcmd_visual)
2002 {
2003 showcmd_buf[0] = NUL;
2004 showcmd_visual = FALSE;
2005 }
2006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002007 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (IS_SPECIAL(c))
2009 for (i = 0; ignore[i] != 0; ++i)
2010 if (ignore[i] == c)
2011 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01002014 if (*p == ' ')
2015 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 old_len = (int)STRLEN(showcmd_buf);
2017 extra_len = (int)STRLEN(p);
2018 overflow = old_len + extra_len - SHOWCMD_COLS;
2019 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002020 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2021 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 STRCAT(showcmd_buf, p);
2023
2024 if (char_avail())
2025 return FALSE;
2026
2027 display_showcmd();
2028
2029 return TRUE;
2030}
2031
2032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002033add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 if (!add_to_showcmd(c))
2036 setcursor();
2037}
2038
2039/*
2040 * Delete 'len' characters from the end of the shown command.
2041 */
2042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002043del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 int old_len;
2046
2047 if (!p_sc)
2048 return;
2049
2050 old_len = (int)STRLEN(showcmd_buf);
2051 if (len > old_len)
2052 len = old_len;
2053 showcmd_buf[old_len - len] = NUL;
2054
2055 if (!char_avail())
2056 display_showcmd();
2057}
2058
2059/*
2060 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2061 * something and there is a partial mapping.
2062 */
2063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002064push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 if (p_sc)
2067 STRCPY(old_showcmd_buf, showcmd_buf);
2068}
2069
2070 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002071pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072{
2073 if (!p_sc)
2074 return;
2075
2076 STRCPY(showcmd_buf, old_showcmd_buf);
2077
2078 display_showcmd();
2079}
2080
2081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002082display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 int len;
2085
2086 cursor_off();
2087
2088 len = (int)STRLEN(showcmd_buf);
2089 if (len == 0)
2090 showcmd_is_clear = TRUE;
2091 else
2092 {
2093 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2094 showcmd_is_clear = FALSE;
2095 }
2096
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002097 // clear the rest of an old message by outputting up to SHOWCMD_COLS
2098 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002101 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102}
2103#endif
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105/*
2106 * When "check" is FALSE, prepare for commands that scroll the window.
2107 * When "check" is TRUE, take care of scroll-binding after the window has
2108 * scrolled. Called from normal_cmd() and edit().
2109 */
2110 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002111do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 static win_T *old_curwin = NULL;
2114 static linenr_T old_topline = 0;
2115#ifdef FEAT_DIFF
2116 static int old_topfill = 0;
2117#endif
2118 static buf_T *old_buf = NULL;
2119 static colnr_T old_leftcol = 0;
2120
2121 if (check && curwin->w_p_scb)
2122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002123 // If a ":syncbind" command was just used, don't scroll, only reset
2124 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (did_syncbind)
2126 did_syncbind = FALSE;
2127 else if (curwin == old_curwin)
2128 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002129 // Synchronize other windows, as necessary according to
2130 // 'scrollbind'. Don't do this after an ":edit" command, except
2131 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if ((curwin->w_buffer == old_buf
2133#ifdef FEAT_DIFF
2134 || curwin->w_p_diff
2135#endif
2136 )
2137 && (curwin->w_topline != old_topline
2138#ifdef FEAT_DIFF
2139 || curwin->w_topfill != old_topfill
2140#endif
2141 || curwin->w_leftcol != old_leftcol))
2142 {
2143 check_scrollbind(curwin->w_topline - old_topline,
2144 (long)(curwin->w_leftcol - old_leftcol));
2145 }
2146 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002147 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002149 // When switching between windows, make sure that the relative
2150 // vertical offset is valid for the new window. The relative
2151 // offset is invalid whenever another 'scrollbind' window has
2152 // scrolled to a point that would force the current window to
2153 // scroll past the beginning or end of its buffer. When the
2154 // resync is performed, some of the other 'scrollbind' windows may
2155 // need to jump so that the current window's relative position is
2156 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2158 }
2159 curwin->w_scbind_pos = curwin->w_topline;
2160 }
2161
2162 old_curwin = curwin;
2163 old_topline = curwin->w_topline;
2164#ifdef FEAT_DIFF
2165 old_topfill = curwin->w_topfill;
2166#endif
2167 old_buf = curwin->w_buffer;
2168 old_leftcol = curwin->w_leftcol;
2169}
2170
2171/*
2172 * Synchronize any windows that have "scrollbind" set, based on the
2173 * number of rows by which the current window has changed
2174 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2175 */
2176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002177check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 int want_ver;
2180 int want_hor;
2181 win_T *old_curwin = curwin;
2182 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 int old_VIsual_select = VIsual_select;
2184 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 colnr_T tgt_leftcol = curwin->w_leftcol;
2186 long topline;
2187 long y;
2188
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002189 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2191#ifdef FEAT_DIFF
2192 want_ver |= old_curwin->w_p_diff;
2193#endif
2194 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2195
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002196 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002198 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
2200 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (curwin != old_curwin && curwin->w_p_scb)
2203 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002204 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (want_ver)
2206 {
2207#ifdef FEAT_DIFF
2208 if (old_curwin->w_p_diff && curwin->w_p_diff)
2209 {
2210 diff_set_topline(old_curwin, curwin);
2211 }
2212 else
2213#endif
2214 {
2215 curwin->w_scbind_pos += topline_diff;
2216 topline = curwin->w_scbind_pos;
2217 if (topline > curbuf->b_ml.ml_line_count)
2218 topline = curbuf->b_ml.ml_line_count;
2219 if (topline < 1)
2220 topline = 1;
2221
2222 y = topline - curwin->w_topline;
2223 if (y > 0)
2224 scrollup(y, FALSE);
2225 else
2226 scrolldown(-y, FALSE);
2227 }
2228
2229 redraw_later(VALID);
2230 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002234 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2236 {
2237 curwin->w_leftcol = tgt_leftcol;
2238 leftcol_changed();
2239 }
2240 }
2241 }
2242
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002243 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 VIsual_select = old_VIsual_select;
2245 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 curwin = old_curwin;
2247 curbuf = old_curbuf;
2248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250/*
2251 * Command character that's ignored.
2252 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002253 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002256nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259}
2260
2261/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002262 * Command character that doesn't do anything, but unlike nv_ignore() does
2263 * start edit(). Used for "startinsert" executed while starting up.
2264 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002267{
2268}
2269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * Command character doesn't exist.
2272 */
2273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002274nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276 clearopbeep(cap->oap);
2277}
2278
2279/*
2280 * <Help> and <F1> commands.
2281 */
2282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 if (!checkclearopq(cap->oap))
2286 ex_help(NULL);
2287}
2288
2289/*
2290 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2291 */
2292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002293nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002295#ifdef FEAT_JOB_CHANNEL
2296 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2297 clearopbeep(cap->oap);
2298 else
2299#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002300 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002301 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002302 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002303 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2304 op_addsub(cap->oap, cap->count1, cap->arg);
2305 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002306 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002307 else if (VIsual_active)
2308 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002309 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002310 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311}
2312
2313/*
2314 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2315 */
2316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002317nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002320 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321 if (mod_mask & MOD_MASK_CTRL)
2322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002323 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002324 if (cap->arg == BACKWARD)
2325 goto_tabpage(-(int)cap->count1);
2326 else
2327 goto_tabpage((int)cap->count0);
2328 }
2329 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002330 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332}
2333
2334/*
2335 * Implementation of "gd" and "gD" command.
2336 */
2337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002338nv_gd(
2339 oparg_T *oap,
2340 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002341 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 char_u *ptr;
2345
Bram Moolenaard9d30582005-05-18 22:10:28 +00002346 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002347 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002348 == FAIL)
2349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002351 }
2352 else
2353 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002354#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002355 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2356 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002357#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002358 // clear any search statistics
2359 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2360 clear_cmdline = TRUE;
2361 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002362}
2363
2364/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002365 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2366 * otherwise.
2367 */
2368 static int
2369is_ident(char_u *line, int offset)
2370{
2371 int i;
2372 int incomment = FALSE;
2373 int instring = 0;
2374 int prev = 0;
2375
2376 for (i = 0; i < offset && line[i] != NUL; i++)
2377 {
2378 if (instring != 0)
2379 {
2380 if (prev != '\\' && line[i] == instring)
2381 instring = 0;
2382 }
2383 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2384 {
2385 instring = line[i];
2386 }
2387 else
2388 {
2389 if (incomment)
2390 {
2391 if (prev == '*' && line[i] == '/')
2392 incomment = FALSE;
2393 }
2394 else if (prev == '/' && line[i] == '*')
2395 {
2396 incomment = TRUE;
2397 }
2398 else if (prev == '/' && line[i] == '/')
2399 {
2400 return FALSE;
2401 }
2402 }
2403
2404 prev = line[i];
2405 }
2406
2407 return incomment == FALSE && instring == 0;
2408}
2409
2410/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002411 * Search for variable declaration of "ptr[len]".
2412 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2413 * current file ("gD").
2414 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002415 * Return FAIL when not found.
2416 */
2417 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002418find_decl(
2419 char_u *ptr,
2420 int len,
2421 int locally,
2422 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002423 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002424{
2425 char_u *pat;
2426 pos_T old_pos;
2427 pos_T par_pos;
2428 pos_T found_pos;
2429 int t;
2430 int save_p_ws;
2431 int save_p_scs;
2432 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002433 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002434 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002435 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002436
2437 if ((pat = alloc(len + 7)) == NULL)
2438 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Put "\V" before the pattern to avoid that the special meaning of "."
2441 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002442 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2443 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 old_pos = curwin->w_cursor;
2445 save_p_ws = p_ws;
2446 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 p_ws = FALSE; // don't wrap around end of file now
2448 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002450 // With "gD" go to line 1.
2451 // With "gd" Search back for the start of the current function, then go
2452 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002457 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459 else
2460 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002461 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2463 --curwin->w_cursor.lnum;
2464 }
2465 curwin->w_cursor.col = 0;
2466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002467 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002468 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002469 for (;;)
2470 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002471 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002472 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002473 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002474 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002475
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002476 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002477 {
2478 pos_T *pos;
2479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // Check that the block the match is in doesn't end before the
2481 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002482 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2483 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2484 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002485 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 // There can't be a useful match before the end of this block.
2487 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002488 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002489 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002490 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002491 }
2492
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002493 if (t == FAIL)
2494 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002495 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 if (found_pos.lnum != 0)
2497 {
2498 curwin->w_cursor = found_pos;
2499 t = OK;
2500 }
2501 break;
2502 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002503 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002505 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002506 ++curwin->w_cursor.lnum;
2507 curwin->w_cursor.col = 0;
2508 continue;
2509 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002510 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // If the current position is not a valid identifier and a previous
2513 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002514 if (!valid && found_pos.lnum != 0)
2515 {
2516 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002517 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002518 }
2519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002520 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002521 if (valid && !locally)
2522 break;
2523 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002525 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002526 if (found_pos.lnum != 0)
2527 curwin->w_cursor = found_pos;
2528 break;
2529 }
2530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // For finding a local variable and the match is before the "{" or
2532 // inside a comment, continue searching. For K&R style function
2533 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002534 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002535 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002536 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002537 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002538 // Remove SEARCH_START from flags to avoid getting stuck at one
2539 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002540 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002542
2543 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002545 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 curwin->w_cursor = old_pos;
2547 }
2548 else
2549 {
2550 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002551 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 reset_search_dir();
2553 }
2554
2555 vim_free(pat);
2556 p_ws = save_p_ws;
2557 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002558
2559 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560}
2561
2562/*
2563 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2564 * lines rather than lines in the file.
2565 * 'dist' must be positive.
2566 *
2567 * Return OK if able to move cursor, FAIL otherwise.
2568 */
2569 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002570nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 int linelen = linetabsize(ml_get_curline());
2573 int retval = OK;
2574 int atend = FALSE;
2575 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002576 int col_off1; // margin offset for first screen line
2577 int col_off2; // margin offset for wrapped screen line
2578 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002579 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002582 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 col_off1 = curwin_col_off();
2585 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002586 width1 = curwin->w_width - col_off1;
2587 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002588 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002589 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002592 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002593 // Instead of sticking at the last character of the buffer line we
2594 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (curwin->w_curswant == MAXCOL)
2596 {
2597 atend = TRUE;
2598 validate_virtcol();
2599 if (width1 <= 0)
2600 curwin->w_curswant = 0;
2601 else
2602 {
2603 curwin->w_curswant = width1 - 1;
2604 if (curwin->w_virtcol > curwin->w_curswant)
2605 curwin->w_curswant += ((curwin->w_virtcol
2606 - curwin->w_curswant - 1) / width2 + 1) * width2;
2607 }
2608 }
2609 else
2610 {
2611 if (linelen > width1)
2612 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2613 else
2614 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002615 if (curwin->w_curswant >= (colnr_T)n)
2616 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
2619 while (dist--)
2620 {
2621 if (dir == BACKWARD)
2622 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002623 if ((long)curwin->w_curswant >= width1
2624#ifdef FEAT_FOLDING
2625 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2626#endif
2627 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002628 // Move back within the line. This can give a negative value
2629 // for w_curswant if width1 < width2 (with cpoptions+=n),
2630 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 curwin->w_curswant -= width2;
2632 else
2633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // Move to the start of a closed fold. Don't do that when
2637 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (!(fdo_flags & FDO_ALL))
2639 (void)hasFolding(curwin->w_cursor.lnum,
2640 &curwin->w_cursor.lnum, NULL);
2641#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002642 if (curwin->w_cursor.lnum == 1)
2643 {
2644 retval = FAIL;
2645 break;
2646 }
2647 --curwin->w_cursor.lnum;
2648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 linelen = linetabsize(ml_get_curline());
2650 if (linelen > width1)
2651 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2652 + 1) * width2;
2653 }
2654 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002655 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
2657 if (linelen > width1)
2658 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2659 else
2660 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002661 if (curwin->w_curswant + width2 < (colnr_T)n
2662#ifdef FEAT_FOLDING
2663 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2664#endif
2665 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 curwin->w_curswant += width2;
2668 else
2669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002672 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2674 &curwin->w_cursor.lnum);
2675#endif
2676 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2677 {
2678 retval = FAIL;
2679 break;
2680 }
2681 curwin->w_cursor.lnum++;
2682 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002683 // Check if the cursor has moved below the number display
2684 // when width1 < width2 (with cpoptions+=n). Subtract width2
2685 // to get a negative value for w_curswant, which will get
2686 // clipped to column 0.
2687 if (curwin->w_curswant >= width1)
2688 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002689 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
2691 }
2692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002695 if (virtual_active() && atend)
2696 coladvance(MAXCOL);
2697 else
2698 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2701 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002702 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002703 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002704
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002705 // Check for landing on a character that got split at the end of the
2706 // last line. We want to advance a screenline, not end up in the same
2707 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002709 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002710#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002711 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2712 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002713#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002714
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002715 c = (*mb_ptr2char)(ml_get_cursor());
2716 if (dir == FORWARD && virtcol < curwin->w_curswant
2717 && (curwin->w_curswant <= (colnr_T)width1)
2718 && !vim_isprintc(c) && c > 255)
2719 oneright();
2720
Bram Moolenaar773b1582014-08-29 14:20:51 +02002721 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 && (curwin->w_curswant < (colnr_T)width1
2723 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2724 : ((curwin->w_curswant - width1) % width2
2725 > (colnr_T)width2 / 2)))
2726 --curwin->w_cursor.col;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 return retval;
2733}
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735/*
2736 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2737 * cap->arg must be TRUE for CTRL-E.
2738 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002740nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 if (!checkclearop(cap->oap))
2743 scroll_redraw(cap->arg, cap->count1);
2744}
2745
2746/*
2747 * Scroll "count" lines up or down, and redraw.
2748 */
2749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002750scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 linenr_T prev_topline = curwin->w_topline;
2753#ifdef FEAT_DIFF
2754 int prev_topfill = curwin->w_topfill;
2755#endif
2756 linenr_T prev_lnum = curwin->w_cursor.lnum;
2757
2758 if (up)
2759 scrollup(count, TRUE);
2760 else
2761 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002762 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2765 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 cursor_correct();
2767 check_cursor_moved(curwin);
2768 curwin->w_valid |= VALID_TOPLINE;
2769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // If moved back to where we were, at least move the cursor, otherwise
2771 // we get stuck at one position. Don't move the cursor up if the
2772 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 while (curwin->w_topline == prev_topline
2774#ifdef FEAT_DIFF
2775 && curwin->w_topfill == prev_topfill
2776#endif
2777 )
2778 {
2779 if (up)
2780 {
2781 if (curwin->w_cursor.lnum > prev_lnum
2782 || cursor_down(1L, FALSE) == FAIL)
2783 break;
2784 }
2785 else
2786 {
2787 if (curwin->w_cursor.lnum < prev_lnum
2788 || prev_topline == 1L
2789 || cursor_up(1L, FALSE) == FAIL)
2790 break;
2791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002792 // Mark w_topline as valid, otherwise the screen jumps back at the
2793 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 check_cursor_moved(curwin);
2795 curwin->w_valid |= VALID_TOPLINE;
2796 }
2797 }
2798 if (curwin->w_cursor.lnum != prev_lnum)
2799 coladvance(curwin->w_curswant);
2800 redraw_later(VALID);
2801}
2802
2803/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002804 * Get the count specified after a 'z' command. Returns TRUE to process
2805 * the 'z' command and FALSE to skip it.
2806 */
2807 static int
2808nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2809{
2810 int nchar = *nchar_arg;
2811 long n;
2812
2813 // "z123{nchar}": edit the count before obtaining {nchar}
2814 if (checkclearop(cap->oap))
2815 return FALSE;
2816 n = nchar - '0';
2817
2818 for (;;)
2819 {
2820#ifdef USE_ON_FLY_SCROLL
2821 dont_scroll = TRUE; // disallow scrolling here
2822#endif
2823 ++no_mapping;
2824 ++allow_keys; // no mapping for nchar, but allow key codes
2825 nchar = plain_vgetc();
2826 LANGMAP_ADJUST(nchar, TRUE);
2827 --no_mapping;
2828 --allow_keys;
2829#ifdef FEAT_CMDL_INFO
2830 (void)add_to_showcmd(nchar);
2831#endif
2832 if (nchar == K_DEL || nchar == K_KDEL)
2833 n /= 10;
2834 else if (VIM_ISDIGIT(nchar))
2835 n = n * 10 + (nchar - '0');
2836 else if (nchar == CAR)
2837 {
2838#ifdef FEAT_GUI
2839 need_mouse_correct = TRUE;
2840#endif
2841 win_setheight((int)n);
2842 break;
2843 }
2844 else if (nchar == 'l'
2845 || nchar == 'h'
2846 || nchar == K_LEFT
2847 || nchar == K_RIGHT)
2848 {
2849 cap->count1 = n ? n * cap->count1 : cap->count1;
2850 *nchar_arg = nchar;
2851 return TRUE;
2852 }
2853 else
2854 {
2855 clearopbeep(cap->oap);
2856 break;
2857 }
2858 }
2859 cap->oap->op_type = OP_NOP;
2860 return FALSE;
2861}
2862
2863#ifdef FEAT_SPELL
2864/*
2865 * "zug" and "zuw": undo "zg" and "zw"
2866 * "zg": add good word to word list
2867 * "zw": add wrong word to word list
2868 * "zG": add good word to temp word list
2869 * "zW": add wrong word to temp word list
2870 */
2871 static int
2872nv_zg_zw(cmdarg_T *cap, int nchar)
2873{
2874 char_u *ptr = NULL;
2875 int len;
2876 int undo = FALSE;
2877
2878 if (nchar == 'u')
2879 {
2880 ++no_mapping;
2881 ++allow_keys; // no mapping for nchar, but allow key codes
2882 nchar = plain_vgetc();
2883 LANGMAP_ADJUST(nchar, TRUE);
2884 --no_mapping;
2885 --allow_keys;
2886#ifdef FEAT_CMDL_INFO
2887 (void)add_to_showcmd(nchar);
2888#endif
2889 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2890 {
2891 clearopbeep(cap->oap);
2892 return OK;
2893 }
2894 undo = TRUE;
2895 }
2896
2897 if (checkclearop(cap->oap))
2898 return OK;
2899 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2900 return FAIL;
2901 if (ptr == NULL)
2902 {
2903 pos_T pos = curwin->w_cursor;
2904
2905 // Find bad word under the cursor. When 'spell' is
2906 // off this fails and find_ident_under_cursor() is
2907 // used below.
2908 emsg_off++;
2909 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2910 emsg_off--;
2911 if (len != 0 && curwin->w_cursor.col <= pos.col)
2912 ptr = ml_get_pos(&curwin->w_cursor);
2913 curwin->w_cursor = pos;
2914 }
2915
2916 if (ptr == NULL
2917 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2918 return FAIL;
2919 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2920 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2921 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2922
2923 return OK;
2924}
2925#endif
2926
2927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 * Commands that start with "z".
2929 */
2930 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002931nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
2933 long n;
2934 colnr_T col;
2935 int nchar = cap->nchar;
2936#ifdef FEAT_FOLDING
2937 long old_fdl = curwin->w_p_fdl;
2938 int old_fen = curwin->w_p_fen;
2939#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002940 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002942 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if (
2946#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2948 // and "zC" only in Visual mode. "zj" and "zk" are motion
2949 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 cap->nchar != 'f' && cap->nchar != 'F'
2951 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2952 && cap->nchar != 'j' && cap->nchar != 'k'
2953 &&
2954#endif
2955 checkclearop(cap->oap))
2956 return;
2957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002958 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2959 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2961 && cap->count0
2962 && cap->count0 != curwin->w_cursor.lnum)
2963 {
2964 setpcmark();
2965 if (cap->count0 > curbuf->b_ml.ml_line_count)
2966 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2967 else
2968 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002969 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971
2972 switch (nchar)
2973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 case '+':
2976 if (cap->count0 == 0)
2977 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // No count given: put cursor at the line below screen
2979 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2981 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2982 else
2983 curwin->w_cursor.lnum = curwin->w_botline;
2984 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 case NL:
2987 case CAR:
2988 case K_KENTER:
2989 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992 case 't': scroll_cursor_top(0, TRUE);
2993 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002994 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 break;
2996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 case 'z': scroll_cursor_halfway(TRUE);
3002 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003003 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 break;
3005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // "z^", "z-" and "zb": put cursor at bottom of screen
3007 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
3008 // when <count> is at bottom of window, and puts that one at
3009 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (cap->count0 != 0)
3011 {
3012 scroll_cursor_bot(0, TRUE);
3013 curwin->w_cursor.lnum = curwin->w_topline;
3014 }
3015 else if (curwin->w_topline == 1)
3016 curwin->w_cursor.lnum = 1;
3017 else
3018 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 case '-':
3021 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003022 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 case 'b': scroll_cursor_bot(0, TRUE);
3025 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003026 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
3028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003029 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02003031 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 case 'h':
3036 case K_LEFT:
3037 if (!curwin->w_p_wrap)
3038 {
3039 if ((colnr_T)cap->count1 > curwin->w_leftcol)
3040 curwin->w_leftcol = 0;
3041 else
3042 curwin->w_leftcol -= (colnr_T)cap->count1;
3043 leftcol_changed();
3044 }
3045 break;
3046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02003048 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003051 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 case 'l':
3053 case K_RIGHT:
3054 if (!curwin->w_p_wrap)
3055 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 curwin->w_leftcol += (colnr_T)cap->count1;
3058 leftcol_changed();
3059 }
3060 break;
3061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003062 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 case 's': if (!curwin->w_p_wrap)
3064 {
3065#ifdef FEAT_FOLDING
3066 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 else
3069#endif
3070 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003071 if ((long)col > siso)
3072 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else
3074 col = 0;
3075 if (curwin->w_leftcol != col)
3076 {
3077 curwin->w_leftcol = col;
3078 redraw_later(NOT_VALID);
3079 }
3080 }
3081 break;
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 case 'e': if (!curwin->w_p_wrap)
3085 {
3086#ifdef FEAT_FOLDING
3087 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else
3090#endif
3091 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003092 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003093 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 col = 0;
3095 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003096 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (curwin->w_leftcol != col)
3098 {
3099 curwin->w_leftcol = col;
3100 redraw_later(NOT_VALID);
3101 }
3102 }
3103 break;
3104
Christian Brabandt2fa93842021-05-30 22:17:25 +02003105 // "zp", "zP" in block mode put without addind trailing spaces
3106 case 'P':
3107 case 'p': nv_put(cap);
3108 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003109 // "zy" Yank without trailing spaces
3110 case 'y': nv_operator(cap);
3111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003113 // "zF": create fold command
3114 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 case 'F':
3116 case 'f': if (foldManualAllowed(TRUE))
3117 {
3118 cap->nchar = 'f';
3119 nv_operator(cap);
3120 curwin->w_p_fen = TRUE;
3121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003122 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3124 {
3125 nv_operator(cap);
3126 finish_op = TRUE;
3127 }
3128 }
3129 else
3130 clearopbeep(cap->oap);
3131 break;
3132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // "zd": delete fold at cursor
3134 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 case 'd':
3136 case 'D': if (foldManualAllowed(FALSE))
3137 {
3138 if (VIsual_active)
3139 nv_operator(cap);
3140 else
3141 deleteFold(curwin->w_cursor.lnum,
3142 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3143 }
3144 break;
3145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 case 'E': if (foldmethodIsManual(curwin))
3148 {
3149 clearFolding(curwin);
3150 changed_window_setting();
3151 }
3152 else if (foldmethodIsMarker(curwin))
3153 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3154 TRUE, FALSE);
3155 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003156 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 break;
3158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 case 'n': curwin->w_p_fen = FALSE;
3161 break;
3162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003163 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 case 'N': curwin->w_p_fen = TRUE;
3165 break;
3166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3169 break;
3170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3173 openFold(curwin->w_cursor.lnum, cap->count1);
3174 else
3175 {
3176 closeFold(curwin->w_cursor.lnum, cap->count1);
3177 curwin->w_p_fen = TRUE;
3178 }
3179 break;
3180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3183 openFoldRecurse(curwin->w_cursor.lnum);
3184 else
3185 {
3186 closeFoldRecurse(curwin->w_cursor.lnum);
3187 curwin->w_p_fen = TRUE;
3188 }
3189 break;
3190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003191 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 case 'o': if (VIsual_active)
3193 nv_operator(cap);
3194 else
3195 openFold(curwin->w_cursor.lnum, cap->count1);
3196 break;
3197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003198 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 case 'O': if (VIsual_active)
3200 nv_operator(cap);
3201 else
3202 openFoldRecurse(curwin->w_cursor.lnum);
3203 break;
3204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 case 'c': if (VIsual_active)
3207 nv_operator(cap);
3208 else
3209 closeFold(curwin->w_cursor.lnum, cap->count1);
3210 curwin->w_p_fen = TRUE;
3211 break;
3212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 case 'C': if (VIsual_active)
3215 nv_operator(cap);
3216 else
3217 closeFoldRecurse(curwin->w_cursor.lnum);
3218 curwin->w_p_fen = TRUE;
3219 break;
3220
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 case 'v': foldOpenCursor();
3223 break;
3224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 curwin->w_foldinvalid = TRUE; // recompute folds
3228 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 foldOpenCursor();
3230 break;
3231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 curwin->w_foldinvalid = TRUE; // recompute folds
3235 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 break;
3237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003240 {
3241 curwin->w_p_fdl -= cap->count1;
3242 if (curwin->w_p_fdl < 0)
3243 curwin->w_p_fdl = 0;
3244 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003245 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 curwin->w_p_fen = TRUE;
3247 break;
3248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003249 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003251 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 curwin->w_p_fen = TRUE;
3253 break;
3254
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003255 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003256 case 'r': curwin->w_p_fdl += cap->count1;
3257 {
3258 int d = getDeepestNesting();
3259
3260 if (curwin->w_p_fdl >= d)
3261 curwin->w_p_fdl = d;
3262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 break;
3264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003265 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 break;
3269
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003270 case 'j': // "zj" move to next fold downwards
3271 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3273 cap->count1) == FAIL)
3274 clearopbeep(cap->oap);
3275 break;
3276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003277#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003279#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003280 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003281 case 'g': // "zg": add good word to word list
3282 case 'w': // "zw": add wrong word to word list
3283 case 'G': // "zG": add good word to temp word list
3284 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003285 if (nv_zg_zw(cap, nchar) == FAIL)
3286 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003289 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003290 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003291 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003293#endif
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 default: clearopbeep(cap->oap);
3296 }
3297
3298#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003299 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (old_fen != curwin->w_p_fen)
3301 {
3302# ifdef FEAT_DIFF
3303 win_T *wp;
3304
3305 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3306 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003307 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 FOR_ALL_WINDOWS(wp)
3309 {
3310 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3311 {
3312 wp->w_p_fen = curwin->w_p_fen;
3313 changed_window_setting_win(wp);
3314 }
3315 }
3316 }
3317# endif
3318 changed_window_setting();
3319 }
3320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (old_fdl != curwin->w_p_fdl)
3323 newFoldLevel();
3324#endif
3325}
3326
3327#ifdef FEAT_GUI
3328/*
3329 * Vertical scrollbar movement.
3330 */
3331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 if (cap->oap->op_type != OP_NOP)
3335 clearopbeep(cap->oap);
3336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003337 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 gui_do_scroll();
3339}
3340
3341/*
3342 * Horizontal scrollbar movement.
3343 */
3344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003345nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 if (cap->oap->op_type != OP_NOP)
3348 clearopbeep(cap->oap);
3349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003350 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003351 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352}
3353#endif
3354
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003355#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003356/*
3357 * Click in GUI tab.
3358 */
3359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003360nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003361{
3362 if (cap->oap->op_type != OP_NOP)
3363 clearopbeep(cap->oap);
3364
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003365 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003366 goto_tabpage(current_tab);
3367}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003368
3369/*
3370 * Selected item in tab line menu.
3371 */
3372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003374{
3375 if (cap->oap->op_type != OP_NOP)
3376 clearopbeep(cap->oap);
3377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003378 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003379 handle_tabmenu();
3380}
3381
3382/*
3383 * Handle selecting an item of the GUI tab line menu.
3384 * Used in Normal and Insert mode.
3385 */
3386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003387handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003388{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003389 switch (current_tabmenu)
3390 {
3391 case TABLINE_MENU_CLOSE:
3392 if (current_tab == 0)
3393 do_cmdline_cmd((char_u *)"tabclose");
3394 else
3395 {
3396 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3397 current_tab);
3398 do_cmdline_cmd(IObuff);
3399 }
3400 break;
3401
3402 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003403 if (current_tab == 0)
3404 do_cmdline_cmd((char_u *)"$tabnew");
3405 else
3406 {
3407 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3408 current_tab - 1);
3409 do_cmdline_cmd(IObuff);
3410 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003411 break;
3412
3413 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003414 if (current_tab == 0)
3415 do_cmdline_cmd((char_u *)"browse $tabnew");
3416 else
3417 {
3418 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3419 current_tab - 1);
3420 do_cmdline_cmd(IObuff);
3421 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003422 break;
3423 }
3424}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003425#endif
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427/*
3428 * "Q" command.
3429 */
3430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003431nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003433 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003435 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003436 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 do_exmode(FALSE);
3438}
3439
3440/*
3441 * Handle a ":" command.
3442 */
3443 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003444nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003446 int old_p_im;
3447 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003448 int is_cmdkey = cap->cmdchar == K_COMMAND
3449 || cap->cmdchar == K_SCRIPT_COMMAND;
3450 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar957cf672020-11-12 14:21:06 +01003452 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 nv_operator(cap);
3454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
3456 if (cap->oap->op_type != OP_NOP)
3457 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 cap->oap->motion_type = MCHAR;
3460 cap->oap->inclusive = FALSE;
3461 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003462 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 stuffcharReadbuff('.');
3466 if (cap->count0 > 1)
3467 {
3468 stuffReadbuff((char_u *)",.+");
3469 stuffnumReadbuff((long)cap->count0 - 1L);
3470 }
3471 }
3472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003473 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (KeyTyped)
3475 compute_cmdrow();
3476
3477 old_p_im = p_im;
3478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003480 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3481 if (is_cmdkey)
3482 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3483 else
3484 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003486 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (p_im != old_p_im)
3488 {
3489 if (p_im)
3490 restart_edit = 'i';
3491 else
3492 restart_edit = 0;
3493 }
3494
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003495 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003496 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003497 clearop(cap->oap);
3498 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3500 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003501 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3502 || did_emsg
3503 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003504 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 clearopbeep(cap->oap);
3506 }
3507}
3508
3509/*
3510 * Handle CTRL-G command.
3511 */
3512 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003513nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003515 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003518 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 showmode();
3520 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003521 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003522 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 fileinfo((int)cap->count0, FALSE, TRUE);
3524}
3525
3526/*
3527 * Handle CTRL-H <Backspace> command.
3528 */
3529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003530nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (VIsual_active && VIsual_select)
3533 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 v_visop(cap);
3536 }
3537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 nv_left(cap);
3539}
3540
3541/*
3542 * CTRL-L: clear screen and redraw.
3543 */
3544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003545nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
3547 if (!checkclearop(cap->oap))
3548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003550 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003551 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003552# ifdef FEAT_RELTIME
3553 {
3554 win_T *wp;
3555
3556 FOR_ALL_WINDOWS(wp)
3557 wp->w_s->b_syn_slow = FALSE;
3558 }
3559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#endif
3561 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003562#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3563# ifdef VIMDLL
3564 if (!gui.in_use)
3565# endif
3566 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569}
3570
3571/*
3572 * CTRL-O: In Select mode: switch to Visual mode for one command.
3573 * Otherwise: Go to older pcmark.
3574 */
3575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003576nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (VIsual_active && VIsual_select)
3579 {
3580 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003581 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 cap->count1 = -cap->count1;
3588 nv_pcmark(cap);
3589 }
3590}
3591
3592/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003593 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3594 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 */
3596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003597nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 if (!checkclearopq(cap->oap))
3600 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3601 GETF_SETMARK|GETF_ALT, FALSE);
3602}
3603
3604/*
3605 * "Z" commands.
3606 */
3607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003608nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 if (!checkclearopq(cap->oap))
3611 {
3612 switch (cap->nchar)
3613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003614 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 case 'Z': do_cmdline_cmd((char_u *)"x");
3616 break;
3617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003618 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 case 'Q': do_cmdline_cmd((char_u *)"q!");
3620 break;
3621
3622 default: clearopbeep(cap->oap);
3623 }
3624 }
3625}
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627/*
3628 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3629 */
3630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003631do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 oparg_T oa;
3634 cmdarg_T ca;
3635
3636 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003637 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 ca.oap = &oa;
3639 ca.cmdchar = c1;
3640 ca.nchar = c2;
3641 nv_ident(&ca);
3642}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003645 * 'K' normal-mode command. Get the command to lookup the keyword under the
3646 * cursor.
3647 */
3648 static int
3649nv_K_getcmd(
3650 cmdarg_T *cap,
3651 char_u *kp,
3652 int kp_help,
3653 int kp_ex,
3654 char_u **ptr_arg,
3655 int n,
3656 char_u *buf,
3657 unsigned buflen)
3658{
3659 char_u *ptr = *ptr_arg;
3660 int isman;
3661 int isman_s;
3662
3663 if (kp_help)
3664 {
3665 // in the help buffer
3666 STRCPY(buf, "he! ");
3667 return n;
3668 }
3669
3670 if (kp_ex)
3671 {
3672 // 'keywordprog' is an ex command
3673 if (cap->count0 != 0)
3674 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3675 else
3676 STRCPY(buf, kp);
3677 STRCAT(buf, " ");
3678 return n;
3679 }
3680
3681 // An external command will probably use an argument starting
3682 // with "-" as an option. To avoid trouble we skip the "-".
3683 while (*ptr == '-' && n > 0)
3684 {
3685 ++ptr;
3686 --n;
3687 }
3688 if (n == 0)
3689 {
3690 // found dashes only
3691 emsg(_(e_no_identifier_under_cursor));
3692 vim_free(buf);
3693 *ptr_arg = ptr;
3694 return 0;
3695 }
3696
3697 // When a count is given, turn it into a range. Is this
3698 // really what we want?
3699 isman = (STRCMP(kp, "man") == 0);
3700 isman_s = (STRCMP(kp, "man -s") == 0);
3701 if (cap->count0 != 0 && !(isman || isman_s))
3702 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3703
3704 STRCAT(buf, "! ");
3705 if (cap->count0 == 0 && isman_s)
3706 STRCAT(buf, "man");
3707 else
3708 STRCAT(buf, kp);
3709 STRCAT(buf, " ");
3710 if (cap->count0 != 0 && (isman || isman_s))
3711 {
3712 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3713 STRCAT(buf, " ");
3714 }
3715
3716 *ptr_arg = ptr;
3717 return n;
3718}
3719
3720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 * Handle the commands that use the word under the cursor.
3722 * [g] CTRL-] :ta to current identifier
3723 * [g] 'K' run program for current identifier
3724 * [g] '*' / to current identifier or string
3725 * [g] '#' ? to current identifier or string
3726 * g ']' :tselect for current identifier
3727 */
3728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003729nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
3731 char_u *ptr = NULL;
3732 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003733 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003734 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003736 char_u *kp; // value of 'keywordprg'
3737 int kp_help; // 'keywordprg' is ":he"
3738 int kp_ex; // 'keywordprg' starts with ":"
3739 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003741 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003742 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003745 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 cmdchar = cap->nchar;
3748 g_cmd = TRUE;
3749 }
3750 else
3751 {
3752 cmdchar = cap->cmdchar;
3753 g_cmd = FALSE;
3754 }
3755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003756 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 cmdchar = '#';
3758
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003759 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3763 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (checkclearopq(cap->oap))
3765 return;
3766 }
3767
3768 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3769 (cmdchar == '*' || cmdchar == '#')
3770 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3771 {
3772 clearop(cap->oap);
3773 return;
3774 }
3775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 // Allocate buffer to put the command in. Inserting backslashes can
3777 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3778 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3780 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3781 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003782 if (kp_help && *skipwhite(ptr) == NUL)
3783 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003784 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003785 return;
3786 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003787 kp_ex = (*kp == ':');
3788 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3789 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (buf == NULL)
3791 return;
3792 buf[0] = NUL;
3793
3794 switch (cmdchar)
3795 {
3796 case '*':
3797 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003798 // Put cursor at start of word, makes search skip the word
3799 // under the cursor.
3800 // Call setpcmark() first, so "*``" puts the cursor back where
3801 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 setpcmark();
3803 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3804
3805 if (!g_cmd && vim_iswordp(ptr))
3806 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003807 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 break;
3809
3810 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003811 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3812 if (n == 0)
3813 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 break;
3815
3816 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003817 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818#ifdef FEAT_CSCOPE
3819 if (p_cst)
3820 STRCPY(buf, "cstag ");
3821 else
3822#endif
3823 STRCPY(buf, "ts ");
3824 break;
3825
3826 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003827 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (curbuf->b_help)
3829 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003831 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003832 if (g_cmd)
3833 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003834 else if (cap->count0 == 0)
3835 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003836 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003837 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003841 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003842 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003844 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003845 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003846 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003847 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003848 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003849 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003850 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003851 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003852 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003854 vim_free(buf);
3855 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003857 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003858 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003859 {
3860 vim_free(buf);
3861 vim_free(p);
3862 return;
3863 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003864 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003865 STRCAT(buf, p);
3866 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003868 else
3869 {
3870 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003871 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003872 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003873 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003874 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003875 {
3876 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003877 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003878 aux_ptr = (char_u *)"";
3879 else
3880 aux_ptr = (char_u *)"\\|\"\n[";
3881 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003882 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003883 aux_ptr = (char_u *)"\\|\"\n*?[";
3884
3885 p = buf + STRLEN(buf);
3886 while (n-- > 0)
3887 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003888 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003889 if (vim_strchr(aux_ptr, *ptr) != NULL)
3890 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003891 // When current byte is a part of multibyte character, copy all
3892 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003893 if (has_mbyte)
3894 {
3895 int i;
3896 int len = (*mb_ptr2len)(ptr) - 1;
3897
3898 for (i = 0; i < len && n >= 1; ++i, --n)
3899 *p++ = *ptr++;
3900 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003901 *p++ = *ptr++;
3902 }
3903 *p = NUL;
3904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003906 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (cmdchar == '*' || cmdchar == '#')
3908 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003909 if (!g_cmd && (has_mbyte
3910 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3911 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003913
3914 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003915 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003917
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003918 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
3920 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003921 {
3922 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003924 g_tag_at_cursor = FALSE;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 vim_free(buf);
3928}
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930/*
3931 * Get visually selected text, within one line only.
3932 * Returns FAIL if more than one line selected.
3933 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003934 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003935get_visual_text(
3936 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 char_u **pp, // return: start of selected text
3938 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
3940 if (VIsual_mode != 'V')
3941 unadjust_for_sel();
3942 if (VIsual.lnum != curwin->w_cursor.lnum)
3943 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003944 if (cap != NULL)
3945 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 return FAIL;
3947 }
3948 if (VIsual_mode == 'V')
3949 {
3950 *pp = ml_get_curline();
3951 *lenp = (int)STRLEN(*pp);
3952 }
3953 else
3954 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003955 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
3957 *pp = ml_get_pos(&curwin->w_cursor);
3958 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3959 }
3960 else
3961 {
3962 *pp = ml_get_pos(&VIsual);
3963 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3964 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003965 if (**pp == NUL)
3966 *lenp = 0;
3967 if (has_mbyte && *lenp > 0)
3968 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003969 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 reset_VIsual_and_resel();
3972 return OK;
3973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975/*
3976 * CTRL-T: backwards in tag stack
3977 */
3978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003979nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 if (!checkclearopq(cap->oap))
3982 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3983}
3984
3985/*
3986 * Handle scrolling command 'H', 'L' and 'M'.
3987 */
3988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003989nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 int used = 0;
3992 long n;
3993#ifdef FEAT_FOLDING
3994 linenr_T lnum;
3995#endif
3996 int half;
3997
3998 cap->oap->motion_type = MLINE;
3999 setpcmark();
4000
4001 if (cap->cmdchar == 'L')
4002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 curwin->w_cursor.lnum = curwin->w_botline - 1;
4005 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
4006 curwin->w_cursor.lnum = 1;
4007 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004008 {
4009#ifdef FEAT_FOLDING
4010 if (hasAnyFolding(curwin))
4011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004012 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004013 for (n = cap->count1 - 1; n > 0
4014 && curwin->w_cursor.lnum > curwin->w_topline; --n)
4015 {
4016 (void)hasFolding(curwin->w_cursor.lnum,
4017 &curwin->w_cursor.lnum, NULL);
4018 --curwin->w_cursor.lnum;
4019 }
4020 }
4021 else
4022#endif
4023 curwin->w_cursor.lnum -= cap->count1 - 1;
4024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 else
4027 {
4028 if (cap->cmdchar == 'M')
4029 {
4030#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004031 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 used -= diff_check_fill(curwin, curwin->w_topline)
4033 - curwin->w_topfill;
4034#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004035 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4037 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4038 {
4039#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004040 // Count half he number of filler lines to be "below this
4041 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4043 + n) / 2 >= half)
4044 {
4045 --n;
4046 break;
4047 }
4048#endif
4049 used += plines(curwin->w_topline + n);
4050 if (used >= half)
4051 break;
4052#ifdef FEAT_FOLDING
4053 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4054 n = lnum - curwin->w_topline;
4055#endif
4056 }
4057 if (n > 0 && used > curwin->w_height)
4058 --n;
4059 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004060 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004063#ifdef FEAT_FOLDING
4064 if (hasAnyFolding(curwin))
4065 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004066 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004067 lnum = curwin->w_topline;
4068 while (n-- > 0 && lnum < curwin->w_botline - 1)
4069 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004070 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004071 ++lnum;
4072 }
4073 n = lnum - curwin->w_topline;
4074 }
4075#endif
4076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 curwin->w_cursor.lnum = curwin->w_topline + n;
4078 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4079 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4080 }
4081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004082 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004083 if (cap->oap->op_type == OP_NOP)
4084 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 beginline(BL_SOL | BL_FIX);
4086}
4087
4088/*
4089 * Cursor right commands.
4090 */
4091 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004092nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
4094 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004095 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004100 if (mod_mask & MOD_MASK_CTRL)
4101 cap->arg = TRUE;
4102 nv_wordcmd(cap);
4103 return;
4104 }
4105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 cap->oap->motion_type = MCHAR;
4107 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004108 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004110 // In virtual edit mode, there's no such thing as "past_line", as lines
4111 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004113 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 for (n = cap->count1; n > 0; --n)
4116 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004117 if ((!past_line && oneright() == FAIL)
4118 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004119 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004121 // <Space> wraps to next line if 'whichwrap' has 's'.
4122 // 'l' wraps to next line if 'whichwrap' has 'l'.
4123 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if ( ((cap->cmdchar == ' '
4125 && vim_strchr(p_ww, 's') != NULL)
4126 || (cap->cmdchar == 'l'
4127 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004128 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 && vim_strchr(p_ww, '>') != NULL))
4130 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4131 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004132 // When deleting we also count the NL as a character.
4133 // Set cap->oap->inclusive when last char in the line is
4134 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004135 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004137 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 cap->oap->inclusive = TRUE;
4139 else
4140 {
4141 ++curwin->w_cursor.lnum;
4142 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 curwin->w_set_curswant = TRUE;
4145 cap->oap->inclusive = FALSE;
4146 }
4147 continue;
4148 }
4149 if (cap->oap->op_type == OP_NOP)
4150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004151 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (n == cap->count1)
4153 beep_flush();
4154 }
4155 else
4156 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004157 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 cap->oap->inclusive = TRUE;
4159 }
4160 break;
4161 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004162 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
4164 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (virtual_active())
4166 oneright();
4167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004170 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 ++curwin->w_cursor.col;
4173 }
4174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176#ifdef FEAT_FOLDING
4177 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4178 && cap->oap->op_type == OP_NOP)
4179 foldOpenCursor();
4180#endif
4181}
4182
4183/*
4184 * Cursor left commands.
4185 *
4186 * Returns TRUE when operator end should not be adjusted.
4187 */
4188 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004189nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
4191 long n;
4192
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004193 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4194 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004195 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004196 if (mod_mask & MOD_MASK_CTRL)
4197 cap->arg = 1;
4198 nv_bck_word(cap);
4199 return;
4200 }
4201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 cap->oap->motion_type = MCHAR;
4203 cap->oap->inclusive = FALSE;
4204 for (n = cap->count1; n > 0; --n)
4205 {
4206 if (oneleft() == FAIL)
4207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004208 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4209 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4210 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if ( (((cap->cmdchar == K_BS
4212 || cap->cmdchar == Ctrl_H)
4213 && vim_strchr(p_ww, 'b') != NULL)
4214 || (cap->cmdchar == 'h'
4215 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004216 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 && vim_strchr(p_ww, '<') != NULL))
4218 && curwin->w_cursor.lnum > 1)
4219 {
4220 --(curwin->w_cursor.lnum);
4221 coladvance((colnr_T)MAXCOL);
4222 curwin->w_set_curswant = TRUE;
4223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004224 // When the NL before the first char has to be deleted we
4225 // put the cursor on the NUL after the previous line.
4226 // This is a very special case, be careful!
4227 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if ( (cap->oap->op_type == OP_DELETE
4229 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004230 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004232 char_u *cp = ml_get_cursor();
4233
4234 if (*cp != NUL)
4235 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004236 if (has_mbyte)
4237 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4238 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004239 ++curwin->w_cursor.col;
4240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 cap->retval |= CA_NO_ADJ_OP_END;
4242 }
4243 continue;
4244 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004245 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4247 beep_flush();
4248 break;
4249 }
4250 }
4251#ifdef FEAT_FOLDING
4252 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4253 && cap->oap->op_type == OP_NOP)
4254 foldOpenCursor();
4255#endif
4256}
4257
4258/*
4259 * Cursor up commands.
4260 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4261 */
4262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004263nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004265 if (mod_mask & MOD_MASK_SHIFT)
4266 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004267 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004268 cap->arg = BACKWARD;
4269 nv_page(cap);
4270 }
4271 else
4272 {
4273 cap->oap->motion_type = MLINE;
4274 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4275 clearopbeep(cap->oap);
4276 else if (cap->arg)
4277 beginline(BL_WHITE | BL_FIX);
4278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279}
4280
4281/*
4282 * Cursor down commands.
4283 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4284 */
4285 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004286nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004288 if (mod_mask & MOD_MASK_SHIFT)
4289 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004290 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004291 cap->arg = FORWARD;
4292 nv_page(cap);
4293 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004294#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004295 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004296 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4297 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004302 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004303 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 cmdwin_result = CAR;
4305 else
4306#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004307#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004308 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004309 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4310 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4311 {
4312 invoke_prompt_callback();
4313 if (restart_edit == 0)
4314 restart_edit = 'a';
4315 }
4316 else
4317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 cap->oap->motion_type = MLINE;
4320 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4321 clearopbeep(cap->oap);
4322 else if (cap->arg)
4323 beginline(BL_WHITE | BL_FIX);
4324 }
4325 }
4326}
4327
4328#ifdef FEAT_SEARCHPATH
4329/*
4330 * Grab the file name under the cursor and edit it.
4331 */
4332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004333nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
4335 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004336 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004338 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
4340 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004341 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 return;
4343 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004344 if (curbuf_locked())
4345 {
4346 clearop(cap->oap);
4347 return;
4348 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004349#ifdef FEAT_PROP_POPUP
4350 if (ERROR_IF_TERM_POPUP_WINDOW)
4351 return;
4352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004354 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 if (ptr != NULL)
4357 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004358 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004359 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004360 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004362 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004363 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004364 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004365 {
4366 curwin->w_cursor.lnum = lnum;
4367 check_cursor_lnum();
4368 beginline(BL_SOL | BL_FIX);
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 vim_free(ptr);
4371 }
4372 else
4373 clearop(cap->oap);
4374}
4375#endif
4376
4377/*
4378 * <End> command: to end of current line or last line.
4379 */
4380 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004381nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004383 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004385 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004387 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 nv_dollar(cap);
4390}
4391
4392/*
4393 * Handle the "$" command.
4394 */
4395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004396nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
4398 cap->oap->motion_type = MCHAR;
4399 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004400 // In virtual mode when off the edge of a line and an operator
4401 // is pending (whew!) keep the cursor where it is.
4402 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (!virtual_active() || gchar_cursor() != NUL
4404 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004405 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 if (cursor_down((long)(cap->count1 - 1),
4407 cap->oap->op_type == OP_NOP) == FAIL)
4408 clearopbeep(cap->oap);
4409#ifdef FEAT_FOLDING
4410 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4411 foldOpenCursor();
4412#endif
4413}
4414
4415/*
4416 * Implementation of '?' and '/' commands.
4417 * If cap->arg is TRUE don't set PC mark.
4418 */
4419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
4422 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004423 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4426 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004427 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 cap->cmdchar = 'g';
4429 cap->nchar = '?';
4430 nv_operator(cap);
4431 return;
4432 }
4433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004434 // When using 'incsearch' the cursor may be moved to set a different search
4435 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004436 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 if (cap->searchbuf == NULL)
4439 {
4440 clearop(oap);
4441 return;
4442 }
4443
Bram Moolenaar46539112015-02-17 15:43:57 +01004444 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004445 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004446 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
4449/*
4450 * Handle "N" and "n" commands.
4451 * cap->arg is SEARCH_REV for "N", 0 for "n".
4452 */
4453 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004454nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004456 pos_T old = curwin->w_cursor;
4457 int wrapped = FALSE;
4458 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004459
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004460 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004461 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004462 // Avoid getting stuck on the current cursor position, which can
4463 // happen when an offset is given and the cursor is on the last char
4464 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004465 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004466 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004467 cap->count1 -= 1;
4468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469}
4470
4471/*
4472 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4473 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004474 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004476 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004477normal_search(
4478 cmdarg_T *cap,
4479 int dir,
4480 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004481 int opt, // extra flags for do_search()
4482 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483{
4484 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004485 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 cap->oap->motion_type = MCHAR;
4488 cap->oap->inclusive = FALSE;
4489 cap->oap->use_reg_one = TRUE;
4490 curwin->w_set_curswant = TRUE;
4491
Bram Moolenaara80faa82020-04-12 19:37:17 +02004492 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004493 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004494 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4495 if (wrapped != NULL)
4496 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (i == 0)
4498 clearop(cap->oap);
4499 else
4500 {
4501 if (i == 2)
4502 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#ifdef FEAT_FOLDING
4505 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4506 foldOpenCursor();
4507#endif
4508 }
4509
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004510 // "/$" will put the cursor after the end of the line, may need to
4511 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004513 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514}
4515
4516/*
4517 * Character search commands.
4518 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4519 * ',' and FALSE for ';'.
4520 * cap->nchar is NUL for ',' and ';' (repeat the search)
4521 */
4522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004523nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 int t_cmd;
4526
4527 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4528 t_cmd = TRUE;
4529 else
4530 t_cmd = FALSE;
4531
4532 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4534 clearopbeep(cap->oap);
4535 else
4536 {
4537 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004538 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4540 && (t_cmd || cap->oap->op_type != OP_NOP))
4541 {
4542 colnr_T scol, ecol;
4543
4544 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4545 curwin->w_cursor.coladd = ecol - scol;
4546 }
4547 else
4548 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550#ifdef FEAT_FOLDING
4551 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4552 foldOpenCursor();
4553#endif
4554 }
4555}
4556
4557/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004558 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4559 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4560 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4561 * "[m" or "]m" search for prev/next start of (Java) method.
4562 * "[M" or "]M" search for prev/next end of (Java) method.
4563 */
4564 static void
4565nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4566{
4567 pos_T new_pos = {0, 0, 0};
4568 pos_T *pos = NULL; // init for GCC
4569 pos_T prev_pos;
4570 long n;
4571 int findc;
4572 int c;
4573
4574 if (cap->nchar == '*')
4575 cap->nchar = '/';
4576 prev_pos.lnum = 0;
4577 if (cap->nchar == 'm' || cap->nchar == 'M')
4578 {
4579 if (cap->cmdchar == '[')
4580 findc = '{';
4581 else
4582 findc = '}';
4583 n = 9999;
4584 }
4585 else
4586 {
4587 findc = cap->nchar;
4588 n = cap->count1;
4589 }
4590 for ( ; n > 0; --n)
4591 {
4592 if ((pos = findmatchlimit(cap->oap, findc,
4593 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4594 {
4595 if (new_pos.lnum == 0) // nothing found
4596 {
4597 if (cap->nchar != 'm' && cap->nchar != 'M')
4598 clearopbeep(cap->oap);
4599 }
4600 else
4601 pos = &new_pos; // use last one found
4602 break;
4603 }
4604 prev_pos = new_pos;
4605 curwin->w_cursor = *pos;
4606 new_pos = *pos;
4607 }
4608 curwin->w_cursor = *old_pos;
4609
4610 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4611 // brought us to the match for "[m" and "]M" when inside a method.
4612 // Try finding the '{' or '}' we want to be at.
4613 // Also repeat for the given count.
4614 if (cap->nchar == 'm' || cap->nchar == 'M')
4615 {
4616 // norm is TRUE for "]M" and "[m"
4617 int norm = ((findc == '{') == (cap->nchar == 'm'));
4618
4619 n = cap->count1;
4620 // found a match: we were inside a method
4621 if (prev_pos.lnum != 0)
4622 {
4623 pos = &prev_pos;
4624 curwin->w_cursor = prev_pos;
4625 if (norm)
4626 --n;
4627 }
4628 else
4629 pos = NULL;
4630 while (n > 0)
4631 {
4632 for (;;)
4633 {
4634 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4635 {
4636 // if not found anything, that's an error
4637 if (pos == NULL)
4638 clearopbeep(cap->oap);
4639 n = 0;
4640 break;
4641 }
4642 c = gchar_cursor();
4643 if (c == '{' || c == '}')
4644 {
4645 // Must have found end/start of class: use it.
4646 // Or found the place to be at.
4647 if ((c == findc && norm) || (n == 1 && !norm))
4648 {
4649 new_pos = curwin->w_cursor;
4650 pos = &new_pos;
4651 n = 0;
4652 }
4653 // if no match found at all, we started outside of the
4654 // class and we're inside now. Just go on.
4655 else if (new_pos.lnum == 0)
4656 {
4657 new_pos = curwin->w_cursor;
4658 pos = &new_pos;
4659 }
4660 // found start/end of other method: go to match
4661 else if ((pos = findmatchlimit(cap->oap, findc,
4662 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4663 0)) == NULL)
4664 n = 0;
4665 else
4666 curwin->w_cursor = *pos;
4667 break;
4668 }
4669 }
4670 --n;
4671 }
4672 curwin->w_cursor = *old_pos;
4673 if (pos == NULL && new_pos.lnum != 0)
4674 clearopbeep(cap->oap);
4675 }
4676 if (pos != NULL)
4677 {
4678 setpcmark();
4679 curwin->w_cursor = *pos;
4680 curwin->w_set_curswant = TRUE;
4681#ifdef FEAT_FOLDING
4682 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4683 && cap->oap->op_type == OP_NOP)
4684 foldOpenCursor();
4685#endif
4686 }
4687}
4688
4689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 * "[" and "]" commands.
4691 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4692 */
4693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004694nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004697 pos_T *pos = NULL; // init for GCC
4698 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 int flag;
4700 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
4702 cap->oap->motion_type = MCHAR;
4703 cap->oap->inclusive = FALSE;
4704 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004705 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
4707#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004708 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 if (cap->nchar == 'f')
4710 nv_gotofile(cap);
4711 else
4712#endif
4713
4714#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004715 // Find the occurrence(s) of the identifier or define under cursor
4716 // in current and included files or jump to the first occurrence.
4717 //
4718 // search list jump
4719 // fwd bwd fwd bwd fwd bwd
4720 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4721 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004723# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004725# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 cap->nchar) != NULL)
4729 {
4730 char_u *ptr;
4731 int len;
4732
4733 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4734 clearop(cap->oap);
4735 else
4736 {
4737 find_pattern_in_path(ptr, 0, len, TRUE,
4738 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4739 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4740 cap->count1,
4741 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4742 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4743 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4744 (linenr_T)MAXLNUM);
4745 curwin->w_set_curswant = TRUE;
4746 }
4747 }
4748 else
4749#endif
4750
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004751 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4752 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4753 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4754 // "[m" or "]m" search for prev/next start of (Java) method.
4755 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if ( (cap->cmdchar == '['
4757 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4758 || (cap->cmdchar == ']'
4759 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004760 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004762 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 else if (cap->nchar == '[' || cap->nchar == ']')
4764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004765 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 flag = '{';
4767 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004768 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004771 // Imitate strange Vi behaviour: When using "]]" with an operator
4772 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004773 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 (cap->oap->op_type != OP_NOP
4775 && cap->arg == FORWARD && flag == '{')))
4776 clearopbeep(cap->oap);
4777 else
4778 {
4779 if (cap->oap->op_type == OP_NOP)
4780 beginline(BL_WHITE | BL_FIX);
4781#ifdef FEAT_FOLDING
4782 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4783 foldOpenCursor();
4784#endif
4785 }
4786 }
4787
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004788 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 else if (cap->nchar == 'p' || cap->nchar == 'P')
4790 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004791 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004794 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 else if (cap->nchar == '\'' || cap->nchar == '`')
4796 {
4797 pos = &curwin->w_cursor;
4798 for (n = cap->count1; n > 0; --n)
4799 {
4800 prev_pos = *pos;
4801 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4802 cap->nchar == '\'');
4803 if (pos == NULL)
4804 break;
4805 }
4806 if (pos == NULL)
4807 pos = &prev_pos;
4808 nv_cursormark(cap, cap->nchar == '\'', pos);
4809 }
4810
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004811 // [ or ] followed by a middle mouse click: put selected text with
4812 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004813 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 (void)do_mouse(cap->oap, cap->nchar,
4816 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4817 cap->count1, PUT_FIXINDENT);
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004821 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 else if (cap->nchar == 'z')
4823 {
4824 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4825 cap->count1) == FAIL)
4826 clearopbeep(cap->oap);
4827 }
4828#endif
4829
4830#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004831 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else if (cap->nchar == 'c')
4833 {
4834 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4835 cap->count1) == FAIL)
4836 clearopbeep(cap->oap);
4837 }
4838#endif
4839
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004840#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004841 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842 else if (cap->nchar == 's' || cap->nchar == 'S')
4843 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004844 setpcmark();
4845 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004846 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4847 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004848 {
4849 clearopbeep(cap->oap);
4850 break;
4851 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004852 else
4853 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004854# ifdef FEAT_FOLDING
4855 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4856 foldOpenCursor();
4857# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004858 }
4859#endif
4860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else
4863 clearopbeep(cap->oap);
4864}
4865
4866/*
4867 * Handle Normal mode "%" command.
4868 */
4869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004870nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004873#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 linenr_T lnum = curwin->w_cursor.lnum;
4875#endif
4876
4877 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004878 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 if (cap->count0 > 100)
4881 clearopbeep(cap->oap);
4882 else
4883 {
4884 cap->oap->motion_type = MLINE;
4885 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004886 // Round up, so 'normal 100%' always jumps at the line line.
4887 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4888 // overflow on 32-bits, so use a formula with less accuracy
4889 // to avoid overflows.
4890 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4892 / 100L * cap->count0;
4893 else
4894 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4895 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004896 if (curwin->w_cursor.lnum < 1)
4897 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4899 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4900 beginline(BL_SOL | BL_FIX);
4901 }
4902 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004903 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 cap->oap->motion_type = MCHAR;
4906 cap->oap->use_reg_one = TRUE;
4907 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4908 clearopbeep(cap->oap);
4909 else
4910 {
4911 setpcmark();
4912 curwin->w_cursor = *pos;
4913 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 }
4918#ifdef FEAT_FOLDING
4919 if (cap->oap->op_type == OP_NOP
4920 && lnum != curwin->w_cursor.lnum
4921 && (fdo_flags & FDO_PERCENT)
4922 && KeyTyped)
4923 foldOpenCursor();
4924#endif
4925}
4926
4927/*
4928 * Handle "(" and ")" commands.
4929 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4930 */
4931 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004932nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 cap->oap->motion_type = MCHAR;
4935 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004937 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 curwin->w_set_curswant = TRUE;
4939
4940 if (findsent(cap->arg, cap->count1) == FAIL)
4941 clearopbeep(cap->oap);
4942 else
4943 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004944 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004945 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#ifdef FEAT_FOLDING
4948 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4949 foldOpenCursor();
4950#endif
4951 }
4952}
4953
4954/*
4955 * "m" command: Mark a position.
4956 */
4957 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004958nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
4960 if (!checkclearop(cap->oap))
4961 {
4962 if (setmark(cap->nchar) == FAIL)
4963 clearopbeep(cap->oap);
4964 }
4965}
4966
4967/*
4968 * "{" and "}" commands.
4969 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4970 */
4971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004972nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 cap->oap->motion_type = MCHAR;
4975 cap->oap->inclusive = FALSE;
4976 cap->oap->use_reg_one = TRUE;
4977 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004978 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 clearopbeep(cap->oap);
4980 else
4981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#ifdef FEAT_FOLDING
4984 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4985 foldOpenCursor();
4986#endif
4987 }
4988}
4989
4990/*
4991 * "u" command: Undo or make lower case.
4992 */
4993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004994nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004996 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004998 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 cap->cmdchar = 'g';
5000 cap->nchar = 'u';
5001 nv_operator(cap);
5002 }
5003 else
5004 nv_kundo(cap);
5005}
5006
5007/*
5008 * <Undo> command.
5009 */
5010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005011nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 if (!checkclearopq(cap->oap))
5014 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02005015#ifdef FEAT_JOB_CHANNEL
5016 if (bt_prompt(curbuf))
5017 {
5018 clearopbeep(cap->oap);
5019 return;
5020 }
5021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 u_undo((int)cap->count1);
5023 curwin->w_set_curswant = TRUE;
5024 }
5025}
5026
5027/*
5028 * Handle the "r" command.
5029 */
5030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005031nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 char_u *ptr;
5034 int had_ctrl_v;
5035 long n;
5036
5037 if (checkclearop(cap->oap))
5038 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005039#ifdef FEAT_JOB_CHANNEL
5040 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5041 {
5042 clearopbeep(cap->oap);
5043 return;
5044 }
5045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (cap->nchar == Ctrl_V)
5049 {
5050 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005051 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005052 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (cap->nchar > DEL)
5054 had_ctrl_v = NUL;
5055 }
5056 else
5057 had_ctrl_v = NUL;
5058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005059 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005060 if (IS_SPECIAL(cap->nchar))
5061 {
5062 clearopbeep(cap->oap);
5063 return;
5064 }
5065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005066 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (VIsual_active)
5068 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005069 if (got_int)
5070 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005071 if (had_ctrl_v)
5072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005073 // Use a special (negative) number to make a difference between a
5074 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005075 if (cap->nchar == CAR)
5076 cap->nchar = REPLACE_CR_NCHAR;
5077 else if (cap->nchar == NL)
5078 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 nv_operator(cap);
5081 return;
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005084 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (virtual_active())
5086 {
5087 if (u_save_cursor() == FAIL)
5088 return;
5089 if (gchar_cursor() == NUL)
5090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005091 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 coladvance_force((colnr_T)(getviscol() + cap->count1));
5093 curwin->w_cursor.col -= cap->count1;
5094 }
5095 else if (gchar_cursor() == TAB)
5096 coladvance_force(getviscol());
5097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005099 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005101 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005102 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 clearopbeep(cap->oap);
5105 return;
5106 }
5107
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005108 // Replacing with a TAB is done by edit() when it is complicated because
5109 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5110 // Other characters are done below to avoid problems with things like
5111 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5113 {
5114 stuffnumReadbuff(cap->count1);
5115 stuffcharReadbuff('R');
5116 stuffcharReadbuff('\t');
5117 stuffcharReadbuff(ESC);
5118 return;
5119 }
5120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005121 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (u_save_cursor() == FAIL)
5123 return;
5124
5125 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5126 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005127 // Replace character(s) by a single newline.
5128 // Strange vi behaviour: Only one newline is inserted.
5129 // Delete the characters here.
5130 // Insert the newline with an insert command, takes care of
5131 // autoindent. The insert command depends on being on the last
5132 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005133 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 stuffcharReadbuff('\r');
5135 stuffcharReadbuff(ESC);
5136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005137 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 invoke_edit(cap, TRUE, 'r', FALSE);
5139 }
5140 else
5141 {
5142 prep_redo(cap->oap->regname, cap->count1,
5143 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5144
5145 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (has_mbyte)
5147 {
5148 int old_State = State;
5149
5150 if (cap->ncharC1 != 0)
5151 AppendCharToRedobuff(cap->ncharC1);
5152 if (cap->ncharC2 != 0)
5153 AppendCharToRedobuff(cap->ncharC2);
5154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005155 // This is slow, but it handles replacing a single-byte with a
5156 // multi-byte and the other way around. Also handles adding
5157 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 for (n = cap->count1; n > 0; --n)
5159 {
5160 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005161 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5162 {
5163 int c = ins_copychar(curwin->w_cursor.lnum
5164 + (cap->nchar == Ctrl_Y ? -1 : 1));
5165 if (c != NUL)
5166 ins_char(c);
5167 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005168 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005169 ++curwin->w_cursor.col;
5170 }
5171 else
5172 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 State = old_State;
5174 if (cap->ncharC1 != 0)
5175 ins_char(cap->ncharC1);
5176 if (cap->ncharC2 != 0)
5177 ins_char(cap->ncharC2);
5178 }
5179 }
5180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005182 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 for (n = cap->count1; n > 0; --n)
5184 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005185 // Get ptr again, because u_save and/or showmatch() will have
5186 // released the line. This may also happen in ins_copychar().
5187 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02005188 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5189 {
5190 int c = ins_copychar(curwin->w_cursor.lnum
5191 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005192
5193 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005194 if (c != NUL)
5195 ptr[curwin->w_cursor.col] = c;
5196 }
5197 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005198 {
5199 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005200 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (p_sm && msg_silent == 0)
5203 showmatch(cap->nchar);
5204 ++curwin->w_cursor.col;
5205 }
5206#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005207 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005209 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
Bram Moolenaar009b2592004-10-24 19:18:58 +00005211 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005212 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005214 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216#endif
5217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005218 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 changed_bytes(curwin->w_cursor.lnum,
5220 (colnr_T)(curwin->w_cursor.col - cap->count1));
5221 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005222 --curwin->w_cursor.col; // cursor on the last replaced char
5223 // if the character on the left of the current cursor is a multi-byte
5224 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (has_mbyte)
5226 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 curbuf->b_op_end = curwin->w_cursor;
5228 curwin->w_set_curswant = TRUE;
5229 set_last_insert(cap->nchar);
5230 }
5231}
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233/*
5234 * 'o': Exchange start and end of Visual area.
5235 * 'O': same, but in block mode exchange left and right corners.
5236 */
5237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 pos_T old_cursor;
5241 colnr_T left, right;
5242
5243 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5244 {
5245 old_cursor = curwin->w_cursor;
5246 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5247 curwin->w_cursor.lnum = VIsual.lnum;
5248 coladvance(left);
5249 VIsual = curwin->w_cursor;
5250
5251 curwin->w_cursor.lnum = old_cursor.lnum;
5252 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005253 // 'selection "exclusive" and cursor at right-bottom corner: move it
5254 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5256 ++curwin->w_curswant;
5257 coladvance(curwin->w_curswant);
5258 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005260 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 curwin->w_cursor.lnum = VIsual.lnum;
5263 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5264 ++right;
5265 coladvance(right);
5266 VIsual = curwin->w_cursor;
5267
5268 curwin->w_cursor.lnum = old_cursor.lnum;
5269 coladvance(left);
5270 curwin->w_curswant = left;
5271 }
5272 }
5273 else
5274 {
5275 old_cursor = curwin->w_cursor;
5276 curwin->w_cursor = VIsual;
5277 VIsual = old_cursor;
5278 curwin->w_set_curswant = TRUE;
5279 }
5280}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282/*
5283 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5284 */
5285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005288 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {
5290 cap->cmdchar = 'c';
5291 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005292 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 VIsual_mode = 'V';
5294 nv_operator(cap);
5295 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005296 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {
5298 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005299 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 else
5301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (virtual_active())
5303 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5305 }
5306 }
5307}
5308
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309/*
5310 * "gr".
5311 */
5312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (VIsual_active)
5316 {
5317 cap->cmdchar = 'r';
5318 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005319 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005321 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005324 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 else
5326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005327 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005328 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 stuffcharReadbuff(cap->extra_char);
5330 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (virtual_active())
5332 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 invoke_edit(cap, TRUE, 'v', FALSE);
5334 }
5335 }
5336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
5338/*
5339 * Swap case for "~" command, when it does not work like an operator.
5340 */
5341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005342n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
5344 long n;
5345 pos_T startpos;
5346 int did_change = 0;
5347#ifdef FEAT_NETBEANS_INTG
5348 pos_T pos;
5349 char_u *ptr;
5350 int count;
5351#endif
5352
5353 if (checkclearopq(cap->oap))
5354 return;
5355
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005356 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
5358 clearopbeep(cap->oap);
5359 return;
5360 }
5361
5362 prep_redo_cmd(cap);
5363
5364 if (u_save_cursor() == FAIL)
5365 return;
5366
5367 startpos = curwin->w_cursor;
5368#ifdef FEAT_NETBEANS_INTG
5369 pos = startpos;
5370#endif
5371 for (n = cap->count1; n > 0; --n)
5372 {
5373 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5374 inc_cursor();
5375 if (gchar_cursor() == NUL)
5376 {
5377 if (vim_strchr(p_ww, '~') != NULL
5378 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5379 {
5380#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005381 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 if (did_change)
5384 {
5385 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005386 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005387 netbeans_removed(curbuf, pos.lnum, pos.col,
5388 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005390 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
5392 pos.col = 0;
5393 pos.lnum++;
5394 }
5395#endif
5396 ++curwin->w_cursor.lnum;
5397 curwin->w_cursor.col = 0;
5398 if (n > 1)
5399 {
5400 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5401 break;
5402 u_clearline();
5403 }
5404 }
5405 else
5406 break;
5407 }
5408 }
5409#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005410 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
5412 ptr = ml_get(pos.lnum);
5413 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005414 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5415 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
5417#endif
5418
5419
5420 check_cursor();
5421 curwin->w_set_curswant = TRUE;
5422 if (did_change)
5423 {
5424 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5425 0L);
5426 curbuf->b_op_start = startpos;
5427 curbuf->b_op_end = curwin->w_cursor;
5428 if (curbuf->b_op_end.col > 0)
5429 --curbuf->b_op_end.col;
5430 }
5431}
5432
5433/*
5434 * Move cursor to mark.
5435 */
5436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005437nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 if (check_mark(pos) == FAIL)
5440 clearop(cap->oap);
5441 else
5442 {
5443 if (cap->cmdchar == '\''
5444 || cap->cmdchar == '`'
5445 || cap->cmdchar == '['
5446 || cap->cmdchar == ']')
5447 setpcmark();
5448 curwin->w_cursor = *pos;
5449 if (flag)
5450 beginline(BL_WHITE | BL_FIX);
5451 else
5452 check_cursor();
5453 }
5454 cap->oap->motion_type = flag ? MLINE : MCHAR;
5455 if (cap->cmdchar == '`')
5456 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 curwin->w_set_curswant = TRUE;
5459}
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461/*
5462 * Handle commands that are operators in Visual mode.
5463 */
5464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005465v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 // Uppercase means linewise, except in block mode, then "D" deletes till
5470 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if (isupper(cap->cmdchar))
5472 {
5473 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005474 {
5475 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5479 curwin->w_curswant = MAXCOL;
5480 }
5481 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5482 nv_operator(cap);
5483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
5485/*
5486 * "s" and "S" commands.
5487 */
5488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005489nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005491#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005492 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005493 if (term_swap_diff() == OK)
5494 return;
5495#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005496#ifdef FEAT_JOB_CHANNEL
5497 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5498 {
5499 clearopbeep(cap->oap);
5500 return;
5501 }
5502#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005503 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005506 {
5507 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 cap->cmdchar = 'c';
5511 nv_operator(cap);
5512 }
5513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 nv_optrans(cap);
5515}
5516
5517/*
5518 * Abbreviated commands.
5519 */
5520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005521nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 if (VIsual_active)
5528 v_visop(cap);
5529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 nv_optrans(cap);
5531}
5532
5533/*
5534 * Translate a command into another command.
5535 */
5536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5540 (char_u *)"d$", (char_u *)"c$",
5541 (char_u *)"cl", (char_u *)"cc",
5542 (char_u *)"yy", (char_u *)":s\r"};
5543 static char_u *str = (char_u *)"xXDCsSY&";
5544
5545 if (!checkclearopq(cap->oap))
5546 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005547 // In Vi "2D" doesn't delete the next line. Can't translate it
5548 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005549 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5550 {
5551 cap->oap->start = curwin->w_cursor;
5552 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005553#ifdef FEAT_EVAL
5554 set_op_var(OP_DELETE);
5555#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005556 cap->count1 = 1;
5557 nv_dollar(cap);
5558 finish_op = TRUE;
5559 ResetRedobuff();
5560 AppendCharToRedobuff('D');
5561 }
5562 else
5563 {
5564 if (cap->count0)
5565 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005566 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 cap->opcount = 0;
5570}
5571
5572/*
5573 * "'" and "`" commands. Also for "g'" and "g`".
5574 * cap->arg is TRUE for "'" and "g'".
5575 */
5576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005577nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 pos_T *pos;
5580 int c;
5581#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005582 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005583 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584#endif
5585
5586 if (cap->cmdchar == 'g')
5587 c = cap->extra_char;
5588 else
5589 c = cap->nchar;
5590 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 if (cap->arg)
5594 {
5595 check_cursor_lnum();
5596 beginline(BL_WHITE | BL_FIX);
5597 }
5598 else
5599 check_cursor();
5600 }
5601 else
5602 nv_cursormark(cap, cap->arg, pos);
5603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (!virtual_active())
5606 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005607 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#ifdef FEAT_FOLDING
5609 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005610 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005611 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 && (fdo_flags & FDO_MARK)
5613 && old_KeyTyped)
5614 foldOpenCursor();
5615#endif
5616}
5617
5618/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005619 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 */
5621 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005622nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005625#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
5630 if (!checkclearopq(cap->oap))
5631 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005632 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5633 {
5634 if (goto_tabpage_lastused() == FAIL)
5635 clearopbeep(cap->oap);
5636 return;
5637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (cap->cmdchar == 'g')
5639 pos = movechangelist((int)cap->count1);
5640 else
5641 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005642 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
5644 curwin->w_set_curswant = TRUE;
5645 check_cursor();
5646 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005647 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 nv_cursormark(cap, FALSE, pos);
5649 else if (cap->cmdchar == 'g')
5650 {
5651 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005652 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005654 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005656 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else
5659 clearopbeep(cap->oap);
5660# ifdef FEAT_FOLDING
5661 if (cap->oap->op_type == OP_NOP
5662 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5663 && (fdo_flags & FDO_MARK)
5664 && old_KeyTyped)
5665 foldOpenCursor();
5666# endif
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668}
5669
5670/*
5671 * Handle '"' command.
5672 */
5673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005674nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 if (checkclearop(cap->oap))
5677 return;
5678#ifdef FEAT_EVAL
5679 if (cap->nchar == '=')
5680 cap->nchar = get_expr_register();
5681#endif
5682 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5683 {
5684 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005685 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#ifdef FEAT_EVAL
5687 set_reg_var(cap->oap->regname);
5688#endif
5689 }
5690 else
5691 clearopbeep(cap->oap);
5692}
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694/*
5695 * Handle "v", "V" and "CTRL-V" commands.
5696 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5697 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005698 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
5700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005701nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005703 if (cap->cmdchar == Ctrl_Q)
5704 cap->cmdchar = Ctrl_V;
5705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005706 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5707 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (cap->oap->op_type != OP_NOP)
5709 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005710 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005711 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 return;
5713 }
5714
5715 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005716 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005720 else // toggle char/block mode
5721 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 VIsual_mode = cap->cmdchar;
5723 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005724 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005726 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005728 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005731 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005733 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 VIsual = curwin->w_cursor;
5735
5736 VIsual_active = TRUE;
5737 VIsual_reselect = TRUE;
5738 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005739 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005742 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005743 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005744 // For V and ^V, we multiply the number of lines even if there
5745 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5747 {
5748 curwin->w_cursor.lnum +=
5749 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005750 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 VIsual_mode = resel_VIsual_mode;
5753 if (VIsual_mode == 'v')
5754 {
5755 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005756 {
5757 validate_virtcol();
5758 curwin->w_curswant = curwin->w_virtcol
5759 + resel_VIsual_vcol * cap->count0 - 1;
5760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005762 curwin->w_curswant = resel_VIsual_vcol;
5763 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005765 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 curwin->w_curswant = MAXCOL;
5768 coladvance((colnr_T)MAXCOL);
5769 }
5770 else if (VIsual_mode == Ctrl_V)
5771 {
5772 validate_virtcol();
5773 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005774 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 coladvance(curwin->w_curswant);
5776 }
5777 else
5778 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005779 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 else
5782 {
5783 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005784 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 may_start_select('c');
5786 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005787 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005788 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005789 if (cap->count0 > 0 && --cap->count1 > 0)
5790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005791 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005792 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5793 nv_right(cap);
5794 else if (VIsual_mode == 'V')
5795 nv_down(cap);
5796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
5798 }
5799}
5800
5801/*
5802 * Start selection for Shift-movement keys.
5803 */
5804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005805start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005807 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 may_start_select('k');
5809 n_start_visual_mode('v');
5810}
5811
5812/*
5813 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5814 */
5815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005816may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 VIsual_select = (stuff_empty() && typebuf_typed()
5819 && (vim_strchr(p_slm, c) != NULL));
5820}
5821
5822/*
5823 * Start Visual mode "c".
5824 * Should set VIsual_select before calling this.
5825 */
5826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005827n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005829#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005830 int cursor_line_was_concealed = curwin->w_p_cole > 0
5831 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005832#endif
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 VIsual_mode = c;
5835 VIsual_active = TRUE;
5836 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005837
5838 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005839 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005840 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005841 {
5842 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 VIsual = curwin->w_cursor;
5846
5847#ifdef FEAT_FOLDING
5848 foldAdjustVisual();
5849#endif
5850
Bram Moolenaara0620062021-11-17 16:52:40 +00005851 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005853#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005854 // Check if redraw is needed after changing the state.
5855 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005856#endif
5857
Bram Moolenaar09df3122006-01-23 22:23:09 +00005858 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005859 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 // Make sure the clipboard gets updated. Needed because start and
5862 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 clip_star.vmode = NUL;
5864#endif
5865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005866 // Only need to redraw this line, unless still need to redraw an old
5867 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 if (curwin->w_redr_type < INVERTED)
5869 {
5870 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5871 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5872 }
5873}
5874
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876/*
5877 * CTRL-W: Window commands
5878 */
5879 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005880nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005882 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005884 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005885 cap->cmdchar = ':';
5886 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005887 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005888 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005889 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005890 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891}
5892
5893/*
5894 * CTRL-Z: Suspend
5895 */
5896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005897nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005901 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005902 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903}
5904
5905/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005906 * "gv": Reselect the previous Visual area. If Visual already active,
5907 * exchange previous and current Visual area.
5908 */
5909 static void
5910nv_gv_cmd(cmdarg_T *cap)
5911{
5912 pos_T tpos;
5913 int i;
5914
5915 if (checkclearop(cap->oap))
5916 return;
5917
5918 if (curbuf->b_visual.vi_start.lnum == 0
5919 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5920 || curbuf->b_visual.vi_end.lnum == 0)
5921 {
5922 beep_flush();
5923 return;
5924 }
5925
5926 // set w_cursor to the start of the Visual area, tpos to the end
5927 if (VIsual_active)
5928 {
5929 i = VIsual_mode;
5930 VIsual_mode = curbuf->b_visual.vi_mode;
5931 curbuf->b_visual.vi_mode = i;
5932# ifdef FEAT_EVAL
5933 curbuf->b_visual_mode_eval = i;
5934# endif
5935 i = curwin->w_curswant;
5936 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5937 curbuf->b_visual.vi_curswant = i;
5938
5939 tpos = curbuf->b_visual.vi_end;
5940 curbuf->b_visual.vi_end = curwin->w_cursor;
5941 curwin->w_cursor = curbuf->b_visual.vi_start;
5942 curbuf->b_visual.vi_start = VIsual;
5943 }
5944 else
5945 {
5946 VIsual_mode = curbuf->b_visual.vi_mode;
5947 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5948 tpos = curbuf->b_visual.vi_end;
5949 curwin->w_cursor = curbuf->b_visual.vi_start;
5950 }
5951
5952 VIsual_active = TRUE;
5953 VIsual_reselect = TRUE;
5954
5955 // Set Visual to the start and w_cursor to the end of the Visual
5956 // area. Make sure they are on an existing character.
5957 check_cursor();
5958 VIsual = curwin->w_cursor;
5959 curwin->w_cursor = tpos;
5960 check_cursor();
5961 update_topline();
5962
5963 // When called from normal "g" command: start Select mode when
5964 // 'selectmode' contains "cmd". When called for K_SELECT, always
5965 // start Select mode.
5966 if (cap->arg)
5967 {
5968 VIsual_select = TRUE;
5969 VIsual_select_reg = 0;
5970 }
5971 else
5972 may_start_select('c');
5973 setmouse();
5974#ifdef FEAT_CLIPBOARD
5975 // Make sure the clipboard gets updated. Needed because start and
5976 // end are still the same, and the selection needs to be owned
5977 clip_star.vmode = NUL;
5978#endif
5979 redraw_curbuf_later(INVERTED);
5980 showmode();
5981}
5982
5983/*
5984 * "g0", "g^" : Like "0" and "^" but for screen lines.
5985 * "gm": middle of "g0" and "g$".
5986 */
5987 static void
5988nv_g_home_m_cmd(cmdarg_T *cap)
5989{
5990 int i;
5991 int flag = FALSE;
5992
5993 if (cap->nchar == '^')
5994 flag = TRUE;
5995
5996 cap->oap->motion_type = MCHAR;
5997 cap->oap->inclusive = FALSE;
5998 if (curwin->w_p_wrap && curwin->w_width != 0)
5999 {
6000 int width1 = curwin->w_width - curwin_col_off();
6001 int width2 = width1 + curwin_col_off2();
6002
6003 validate_virtcol();
6004 i = 0;
6005 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6006 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6007 }
6008 else
6009 i = curwin->w_leftcol;
6010 // Go to the middle of the screen line. When 'number' or
6011 // 'relativenumber' is on and lines are wrapping the middle can be more
6012 // to the left.
6013 if (cap->nchar == 'm')
6014 i += (curwin->w_width - curwin_col_off()
6015 + ((curwin->w_p_wrap && i > 0)
6016 ? curwin_col_off2() : 0)) / 2;
6017 coladvance((colnr_T)i);
6018 if (flag)
6019 {
6020 do
6021 i = gchar_cursor();
6022 while (VIM_ISWHITE(i) && oneright() == OK);
6023 curwin->w_valid &= ~VALID_WCOL;
6024 }
6025 curwin->w_set_curswant = TRUE;
6026}
6027
6028/*
6029 * "g_": to the last non-blank character in the line or <count> lines
6030 * downward.
6031 */
6032 static void
6033nv_g_underscore_cmd(cmdarg_T *cap)
6034{
6035 char_u *ptr;
6036
6037 cap->oap->motion_type = MCHAR;
6038 cap->oap->inclusive = TRUE;
6039 curwin->w_curswant = MAXCOL;
6040 if (cursor_down((long)(cap->count1 - 1),
6041 cap->oap->op_type == OP_NOP) == FAIL)
6042 {
6043 clearopbeep(cap->oap);
6044 return;
6045 }
6046
6047 ptr = ml_get_curline();
6048
6049 // In Visual mode we may end up after the line.
6050 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6051 --curwin->w_cursor.col;
6052
6053 // Decrease the cursor column until it's on a non-blank.
6054 while (curwin->w_cursor.col > 0
6055 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
6056 --curwin->w_cursor.col;
6057 curwin->w_set_curswant = TRUE;
6058 adjust_for_sel(cap);
6059}
6060
6061/*
6062 * "g$" : Like "$" but for screen lines.
6063 */
6064 static void
6065nv_g_dollar_cmd(cmdarg_T *cap)
6066{
6067 oparg_T *oap = cap->oap;
6068 int i;
6069 int col_off = curwin_col_off();
6070
6071 oap->motion_type = MCHAR;
6072 oap->inclusive = TRUE;
6073 if (curwin->w_p_wrap && curwin->w_width != 0)
6074 {
6075 curwin->w_curswant = MAXCOL; // so we stay at the end
6076 if (cap->count1 == 1)
6077 {
6078 int width1 = curwin->w_width - col_off;
6079 int width2 = width1 + curwin_col_off2();
6080
6081 validate_virtcol();
6082 i = width1 - 1;
6083 if (curwin->w_virtcol >= (colnr_T)width1)
6084 i += ((curwin->w_virtcol - width1) / width2 + 1)
6085 * width2;
6086 coladvance((colnr_T)i);
6087
6088 // Make sure we stick in this column.
6089 validate_virtcol();
6090 curwin->w_curswant = curwin->w_virtcol;
6091 curwin->w_set_curswant = FALSE;
6092 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6093 {
6094 // Check for landing on a character that got split at
6095 // the end of the line. We do not want to advance to
6096 // the next screen line.
6097 if (curwin->w_virtcol > (colnr_T)i)
6098 --curwin->w_cursor.col;
6099 }
6100 }
6101 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6102 clearopbeep(oap);
6103 }
6104 else
6105 {
6106 if (cap->count1 > 1)
6107 // if it fails, let the cursor still move to the last char
6108 (void)cursor_down(cap->count1 - 1, FALSE);
6109
6110 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
6111 coladvance((colnr_T)i);
6112
6113 // if the character doesn't fit move one back
6114 if (curwin->w_cursor.col > 0
6115 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6116 {
6117 colnr_T vcol;
6118
6119 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6120 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6121 --curwin->w_cursor.col;
6122 }
6123
6124 // Make sure we stick in this column.
6125 validate_virtcol();
6126 curwin->w_curswant = curwin->w_virtcol;
6127 curwin->w_set_curswant = FALSE;
6128 }
6129}
6130
6131/*
6132 * "gi": start Insert at the last position.
6133 */
6134 static void
6135nv_gi_cmd(cmdarg_T *cap)
6136{
6137 int i;
6138
6139 if (curbuf->b_last_insert.lnum != 0)
6140 {
6141 curwin->w_cursor = curbuf->b_last_insert;
6142 check_cursor_lnum();
6143 i = (int)STRLEN(ml_get_curline());
6144 if (curwin->w_cursor.col > (colnr_T)i)
6145 {
6146 if (virtual_active())
6147 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
6148 curwin->w_cursor.col = i;
6149 }
6150 }
6151 cap->cmdchar = 'i';
6152 nv_edit(cap);
6153}
6154
6155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 * Commands starting with "g".
6157 */
6158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006159nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 switch (cap->nchar)
6165 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006166 case Ctrl_A:
6167 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006169 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006170 if (!VIsual_active && cap->nchar == Ctrl_A)
6171 vim_mem_profile_dump();
6172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006174 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006175 if (VIsual_active)
6176 {
6177 cap->arg = TRUE;
6178 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006179 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006180 nv_addsub(cap);
6181 }
6182 else
6183 clearopbeep(oap);
6184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006186 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 case 'R':
6188 cap->arg = TRUE;
6189 nv_Replace(cap);
6190 break;
6191
6192 case 'r':
6193 nv_vreplace(cap);
6194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
6196 case '&':
6197 do_cmdline_cmd((char_u *)"%s//~/&");
6198 break;
6199
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006200 // "gv": Reselect the previous Visual area. If Visual already active,
6201 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006203 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006205
6206 // "gV": Don't reselect the previous Visual area after a Select mode
6207 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 case 'V':
6209 VIsual_reselect = FALSE;
6210 break;
6211
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006212 // "gh": start Select mode.
6213 // "gH": start Select line mode.
6214 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 case K_BS:
6216 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006217 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 case 'h':
6219 case 'H':
6220 case Ctrl_H:
6221# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006222 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (cap->nchar == Ctrl_H)
6224 cap->cmdchar = Ctrl_V;
6225 else
6226# endif
6227 cap->cmdchar = cap->nchar + ('v' - 'h');
6228 cap->arg = TRUE;
6229 nv_visual(cap);
6230 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006232 // "gn", "gN" visually select next/previous search match
6233 // "gn" selects next match
6234 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006235 case 'N':
6236 case 'n':
6237 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006238 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006241 // "gj" and "gk" two new funny movement keys -- up and down
6242 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 case 'j':
6244 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006245 // with 'nowrap' it works just like the normal "j" command.
6246 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
6248 oap->motion_type = MLINE;
6249 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6250 }
6251 else
6252 i = nv_screengo(oap, FORWARD, cap->count1);
6253 if (i == FAIL)
6254 clearopbeep(oap);
6255 break;
6256
6257 case 'k':
6258 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006259 // with 'nowrap' it works just like the normal "k" command.
6260 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
6262 oap->motion_type = MLINE;
6263 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6264 }
6265 else
6266 i = nv_screengo(oap, BACKWARD, cap->count1);
6267 if (i == FAIL)
6268 clearopbeep(oap);
6269 break;
6270
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006271 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 case 'J':
6273 nv_join(cap);
6274 break;
6275
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006276 // "g0", "g^" : Like "0" and "^" but for screen lines.
6277 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 case '0':
6280 case 'm':
6281 case K_HOME:
6282 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006283 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 break;
6285
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006286 case 'M':
6287 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006288 oap->motion_type = MCHAR;
6289 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006290 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006291 if (cap->count0 > 0 && cap->count0 <= 100)
6292 coladvance((colnr_T)(i * cap->count0 / 100));
6293 else
6294 coladvance((colnr_T)(i / 2));
6295 curwin->w_set_curswant = TRUE;
6296 }
6297 break;
6298
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006299 // "g_": to the last non-blank character in the line or <count> lines
6300 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006302 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 break;
6304
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006305 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 case '$':
6307 case K_END:
6308 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006309 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 break;
6311
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006312 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 case '*':
6314 case '#':
6315#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006316 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006318 case Ctrl_RSB: // :tag or :tselect for current identifier
6319 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 nv_ident(cap);
6321 break;
6322
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006323 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 case 'e':
6325 case 'E':
6326 oap->motion_type = MCHAR;
6327 curwin->w_set_curswant = TRUE;
6328 oap->inclusive = TRUE;
6329 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6330 clearopbeep(oap);
6331 break;
6332
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006333 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006335 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 break;
6337
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006338 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006340 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 break;
6342
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006343 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 case 'I':
6345 beginline(0);
6346 if (!checkclearopq(oap))
6347 invoke_edit(cap, FALSE, 'g', FALSE);
6348 break;
6349
6350#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006351 // "gf": goto file, edit file under cursor
6352 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006354 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 nv_gotofile(cap);
6356 break;
6357#endif
6358
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006359 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 case '\'':
6361 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006362 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 case '`':
6364 nv_gomark(cap);
6365 break;
6366
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006367 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006369 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 break;
6371
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006372 // "ga": Display the ascii value of the character under the
6373 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 case 'a':
6375 do_ascii(NULL);
6376 break;
6377
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006378 // "g8": Display the bytes used for the UTF-8 character under the
6379 // cursor. It is displayed in hex.
6380 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006382 if (cap->count0 == 8)
6383 utf_find_illegal();
6384 else
6385 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006388 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006389 case '<':
6390 show_sb_text();
6391 break;
6392
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006393 // "gg": Goto the first line in file. With a count it goes to
6394 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 case 'g':
6396 cap->arg = FALSE;
6397 nv_goto(cap);
6398 break;
6399
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006400 // Two-character operators:
6401 // "gq" Format text
6402 // "gw" Format text and keep cursor position
6403 // "g~" Toggle the case of the text.
6404 // "gu" Change text to lower case.
6405 // "gU" Change text to upper case.
6406 // "g?" rot13 encoding
6407 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 case 'q':
6409 case 'w':
6410 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006411 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 case '~':
6413 case 'u':
6414 case 'U':
6415 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006416 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 nv_operator(cap);
6418 break;
6419
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006420 // "gd": Find first occurrence of pattern under the cursor in the
6421 // current function
6422 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case 'd':
6424 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006425 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 break;
6427
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006428 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 case K_MIDDLEMOUSE:
6430 case K_MIDDLEDRAG:
6431 case K_MIDDLERELEASE:
6432 case K_LEFTMOUSE:
6433 case K_LEFTDRAG:
6434 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006435 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 case K_RIGHTMOUSE:
6437 case K_RIGHTDRAG:
6438 case K_RIGHTRELEASE:
6439 case K_X1MOUSE:
6440 case K_X1DRAG:
6441 case K_X1RELEASE:
6442 case K_X2MOUSE:
6443 case K_X2DRAG:
6444 case K_X2RELEASE:
6445 mod_mask = MOD_MASK_CTRL;
6446 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
6449 case K_IGNORE:
6450 break;
6451
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006452 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 case 'p':
6454 case 'P':
6455 nv_put(cap);
6456 break;
6457
6458#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 case 'o':
6461 goto_byte(cap->count0);
6462 break;
6463#endif
6464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006465 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006467 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
6469 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006470 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 break;
6472 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (!checkclearopq(oap))
6475 do_exmode(TRUE);
6476 break;
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 case ',':
6479 nv_pcmark(cap);
6480 break;
6481
6482 case ';':
6483 cap->count1 = -cap->count1;
6484 nv_pcmark(cap);
6485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006488 if (!checkclearop(oap))
6489 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006491 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006492 if (!checkclearop(oap))
6493 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006494 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495
Bram Moolenaar62a23252020-08-09 14:04:42 +02006496 case TAB:
6497 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6498 clearopbeep(oap);
6499 break;
6500
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006501 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006502 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006503 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006504 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006505 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006506 break;
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 default:
6509 clearopbeep(oap);
6510 break;
6511 }
6512}
6513
6514/*
6515 * Handle "o" and "O" commands.
6516 */
6517 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006518n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006520#ifdef FEAT_CONCEAL
6521 linenr_T oldline = curwin->w_cursor.lnum;
6522#endif
6523
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (!checkclearopq(cap->oap))
6525 {
6526#ifdef FEAT_FOLDING
6527 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 (void)hasFolding(curwin->w_cursor.lnum,
6530 &curwin->w_cursor.lnum, NULL);
6531 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006532 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 (void)hasFolding(curwin->w_cursor.lnum,
6534 NULL, &curwin->w_cursor.lnum);
6535#endif
6536 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6537 (cap->cmdchar == 'O' ? 1 : 0)),
6538 (linenr_T)(curwin->w_cursor.lnum +
6539 (cap->cmdchar == 'o' ? 1 : 0))
6540 ) == OK
6541 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006542 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006543 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006545#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006546 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006547 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006548#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006549#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006550 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006551 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006552 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006553#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006554 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006555 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6556 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6558 }
6559 }
6560}
6561
6562/*
6563 * "." command: redo last change.
6564 */
6565 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006566nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
6568 if (!checkclearopq(cap->oap))
6569 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006570 // If "restart_edit" is TRUE, the last but one command is repeated
6571 // instead of the last command (inserting text). This is used for
6572 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6574 clearopbeep(cap->oap);
6575 }
6576}
6577
6578/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006579 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 */
6581 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006582nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006584 if (VIsual_select && VIsual_active)
6585 {
6586 int reg;
6587 // Get register name
6588 ++no_mapping;
6589 ++allow_keys;
6590 reg = plain_vgetc();
6591 LANGMAP_ADJUST(reg, TRUE);
6592 --no_mapping;
6593 --allow_keys;
6594
6595 if (reg == '"')
6596 // the unnamed register is 0
6597 reg = 0;
6598
6599 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6600 return;
6601 }
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (!checkclearopq(cap->oap))
6604 {
6605 u_redo((int)cap->count1);
6606 curwin->w_set_curswant = TRUE;
6607 }
6608}
6609
6610/*
6611 * Handle "U" command.
6612 */
6613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006616 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006617 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006619 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 cap->cmdchar = 'g';
6621 cap->nchar = 'U';
6622 nv_operator(cap);
6623 }
6624 else if (!checkclearopq(cap->oap))
6625 {
6626 u_undoline();
6627 curwin->w_set_curswant = TRUE;
6628 }
6629}
6630
6631/*
6632 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6633 * single character.
6634 */
6635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006638 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006639 {
6640#ifdef FEAT_JOB_CHANNEL
6641 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6642 {
6643 clearopbeep(cap->oap);
6644 return;
6645 }
6646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 else
6650 nv_operator(cap);
6651}
6652
6653/*
6654 * Handle an operator command.
6655 * The actual work is done by do_pending_operator().
6656 */
6657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006658nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 int op_type;
6661
6662 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006663#ifdef FEAT_JOB_CHANNEL
6664 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6665 {
6666 clearopbeep(cap->oap);
6667 return;
6668 }
6669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006671 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 nv_lineop(cap);
6673 else if (!checkclearop(cap->oap))
6674 {
6675 cap->oap->start = curwin->w_cursor;
6676 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006677#ifdef FEAT_EVAL
6678 set_op_var(op_type);
6679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 }
6681}
6682
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006683#ifdef FEAT_EVAL
6684/*
6685 * Set v:operator to the characters for "optype".
6686 */
6687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006688set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006689{
6690 char_u opchars[3];
6691
6692 if (optype == OP_NOP)
6693 set_vim_var_string(VV_OP, NULL, 0);
6694 else
6695 {
6696 opchars[0] = get_op_char(optype);
6697 opchars[1] = get_extra_op_char(optype);
6698 opchars[2] = NUL;
6699 set_vim_var_string(VV_OP, opchars, -1);
6700 }
6701}
6702#endif
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704/*
6705 * Handle linewise operator "dd", "yy", etc.
6706 *
6707 * "_" is is a strange motion command that helps make operators more logical.
6708 * It is actually implemented, but not documented in the real Vi. This motion
6709 * command actually refers to "the current line". Commands like "dd" and "yy"
6710 * are really an alternate form of "d_" and "y_". It does accept a count, so
6711 * "d3_" works to delete 3 lines.
6712 */
6713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006714nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 cap->oap->motion_type = MLINE;
6717 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6718 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006719 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006720 && cap->oap->motion_force != 'v'
6721 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 || cap->oap->op_type == OP_LSHIFT
6723 || cap->oap->op_type == OP_RSHIFT)
6724 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006725 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 beginline(BL_WHITE | BL_FIX);
6727}
6728
6729/*
6730 * <Home> command.
6731 */
6732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006733nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006735 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006736 if (mod_mask & MOD_MASK_CTRL)
6737 nv_goto(cap);
6738 else
6739 {
6740 cap->count0 = 1;
6741 nv_pipe(cap);
6742 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006743 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6744 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745}
6746
6747/*
6748 * "|" command.
6749 */
6750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 cap->oap->motion_type = MCHAR;
6754 cap->oap->inclusive = FALSE;
6755 beginline(0);
6756 if (cap->count0 > 0)
6757 {
6758 coladvance((colnr_T)(cap->count0 - 1));
6759 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6760 }
6761 else
6762 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006763 // keep curswant at the column where we wanted to go, not where
6764 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 curwin->w_set_curswant = FALSE;
6766}
6767
6768/*
6769 * Handle back-word command "b" and "B".
6770 * cap->arg is 1 for "B"
6771 */
6772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006773nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 cap->oap->motion_type = MCHAR;
6776 cap->oap->inclusive = FALSE;
6777 curwin->w_set_curswant = TRUE;
6778 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6779 clearopbeep(cap->oap);
6780#ifdef FEAT_FOLDING
6781 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6782 foldOpenCursor();
6783#endif
6784}
6785
6786/*
6787 * Handle word motion commands "e", "E", "w" and "W".
6788 * cap->arg is TRUE for "E" and "W".
6789 */
6790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006791nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 int n;
6794 int word_end;
6795 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006796 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006798 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6800 word_end = TRUE;
6801 else
6802 word_end = FALSE;
6803 cap->oap->inclusive = word_end;
6804
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006805 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (!word_end && cap->oap->op_type == OP_CHANGE)
6807 {
6808 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006809 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006811 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006813 // Reproduce a funny Vi behaviour: "cw" on a blank only
6814 // changes one character, not all blanks until the start of
6815 // the next word. Only do this when the 'w' flag is included
6816 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6818 {
6819 cap->oap->inclusive = TRUE;
6820 cap->oap->motion_type = MCHAR;
6821 return;
6822 }
6823 }
6824 else
6825 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006826 // This is a little strange. To match what the real Vi does,
6827 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6828 // that we are not on a space or a TAB. This seems impolite
6829 // at first, but it's really more what we mean when we say
6830 // 'cw'.
6831 // Another strangeness: When standing on the end of a word
6832 // "ce" will change until the end of the next word, but "cw"
6833 // will change only one character! This is done by setting
6834 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 cap->oap->inclusive = TRUE;
6836 word_end = TRUE;
6837 flag = TRUE;
6838 }
6839 }
6840 }
6841
6842 cap->oap->motion_type = MCHAR;
6843 curwin->w_set_curswant = TRUE;
6844 if (word_end)
6845 n = end_word(cap->count1, cap->arg, flag, FALSE);
6846 else
6847 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006849 // Don't leave the cursor on the NUL past the end of line. Unless we
6850 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006851 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006852 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854 if (n == FAIL && cap->oap->op_type == OP_NOP)
6855 clearopbeep(cap->oap);
6856 else
6857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859#ifdef FEAT_FOLDING
6860 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6861 foldOpenCursor();
6862#endif
6863 }
6864}
6865
6866/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006867 * Used after a movement command: If the cursor ends up on the NUL after the
6868 * end of the line, may move it back to the last character and make the motion
6869 * inclusive.
6870 */
6871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006872adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006873{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 // The cursor cannot remain on the NUL when:
6875 // - the column is > 0
6876 // - not in Visual mode or 'selection' is "o"
6877 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006878 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006879 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006880 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006881 {
6882 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006883 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006884 if (has_mbyte)
6885 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006886 oap->inclusive = TRUE;
6887 }
6888}
6889
6890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 * "0" and "^" commands.
6892 * cap->arg is the argument for beginline().
6893 */
6894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006895nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
6897 cap->oap->motion_type = MCHAR;
6898 cap->oap->inclusive = FALSE;
6899 beginline(cap->arg);
6900#ifdef FEAT_FOLDING
6901 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6902 foldOpenCursor();
6903#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6905 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906}
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908/*
6909 * In exclusive Visual mode, may include the last character.
6910 */
6911 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006912adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006915 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (has_mbyte)
6918 inc_cursor();
6919 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 ++curwin->w_cursor.col;
6921 cap->oap->inclusive = FALSE;
6922 }
6923}
6924
6925/*
6926 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6927 * Should check VIsual_mode before calling this.
6928 * Returns TRUE when backed up to the previous line.
6929 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006930 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006931unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932{
6933 pos_T *pp;
6934
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006935 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006937 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 pp = &curwin->w_cursor;
6939 else
6940 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (pp->coladd > 0)
6942 --pp->coladd;
6943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (pp->col > 0)
6945 {
6946 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006947 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 else if (pp->lnum > 1)
6950 {
6951 --pp->lnum;
6952 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6953 return TRUE;
6954 }
6955 }
6956 return FALSE;
6957}
6958
6959/*
6960 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6961 */
6962 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006963nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006968 VIsual_select_reg = 0;
6969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 else if (VIsual_reselect)
6971 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 cap->arg = TRUE;
6974 nv_g_cmd(cap);
6975 }
6976}
6977
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979/*
6980 * "G", "gg", CTRL-END, CTRL-HOME.
6981 * cap->arg is TRUE for "G".
6982 */
6983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006984nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 linenr_T lnum;
6987
6988 if (cap->arg)
6989 lnum = curbuf->b_ml.ml_line_count;
6990 else
6991 lnum = 1L;
6992 cap->oap->motion_type = MLINE;
6993 setpcmark();
6994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006995 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (cap->count0 != 0)
6997 lnum = cap->count0;
6998 if (lnum < 1L)
6999 lnum = 1L;
7000 else if (lnum > curbuf->b_ml.ml_line_count)
7001 lnum = curbuf->b_ml.ml_line_count;
7002 curwin->w_cursor.lnum = lnum;
7003 beginline(BL_SOL | BL_FIX);
7004#ifdef FEAT_FOLDING
7005 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
7006 foldOpenCursor();
7007#endif
7008}
7009
7010/*
7011 * CTRL-\ in Normal mode.
7012 */
7013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
7017 {
7018 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00007019 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007020 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 restart_edit = 0;
7022#ifdef FEAT_CMDWIN
7023 if (cmdwin_type != 0)
7024 cmdwin_result = Ctrl_C;
7025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (VIsual_active)
7027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 redraw_curbuf_later(INVERTED);
7030 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007031 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 if (cap->nchar == Ctrl_G && p_im)
7033 restart_edit = 'a';
7034 }
7035 else
7036 clearopbeep(cap->oap);
7037}
7038
7039/*
7040 * ESC in Normal mode: beep, but don't flush buffers.
7041 * Don't even beep if we are canceling a command.
7042 */
7043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007044nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 int no_reason;
7047
7048 no_reason = (cap->oap->op_type == OP_NOP
7049 && cap->opcount == 0
7050 && cap->count0 == 0
7051 && cap->oap->regname == 0
7052 && !p_im);
7053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 if (restart_edit == 0
7057#ifdef FEAT_CMDWIN
7058 && cmdwin_type == 0
7059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007062 {
7063 if (anyBufIsChanged())
7064 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7065 else
7066 msg(_("Type :qa and press <Enter> to exit Vim"));
7067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7070 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (!p_im)
7072 restart_edit = 0;
7073#ifdef FEAT_CMDWIN
7074 if (cmdwin_type != 0)
7075 {
7076 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 return;
7079 }
7080#endif
7081 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007082#ifdef FEAT_CMDWIN
7083 else if (cmdwin_type != 0 && ex_normal_busy)
7084 {
7085 // When :normal runs out of characters while in the command line window
7086 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7087 // loop.
7088 cmdwin_result = K_IGNORE;
7089 return;
7090 }
7091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (VIsual_active)
7094 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007095 end_visual_mode(); // stop Visual
7096 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 curwin->w_set_curswant = TRUE;
7098 redraw_curbuf_later(INVERTED);
7099 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007100 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007101 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 clearop(cap->oap);
7103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7105 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007106 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 restart_edit = 'a';
7108}
7109
7110/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007111 * Move the cursor for the "A" command.
7112 */
7113 void
7114set_cursor_for_append_to_line(void)
7115{
7116 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007117 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007118 {
7119 int save_State = State;
7120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121 // Pretend Insert mode here to allow the cursor on the
7122 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007123 State = INSERT;
7124 coladvance((colnr_T)MAXCOL);
7125 State = save_State;
7126 }
7127 else
7128 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7129}
7130
7131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007133 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 */
7135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007136nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7140 cap->cmdchar = 'i';
7141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007144 {
7145#ifdef FEAT_TERMINAL
7146 if (term_in_normal_mode())
7147 {
7148 end_visual_mode();
7149 clearop(cap->oap);
7150 term_enter_job_mode();
7151 return;
7152 }
7153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007157 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007158 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7159 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
7161#ifdef FEAT_TEXTOBJ
7162 nv_object(cap);
7163#else
7164 clearopbeep(cap->oap);
7165#endif
7166 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007167#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007168 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007169 {
7170 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007171 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007172 return;
7173 }
7174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 else if (!curbuf->b_p_ma && !p_im)
7176 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007177 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007178 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007180 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007182 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007184 else if (cap->cmdchar == K_PS && VIsual_active)
7185 {
7186 pos_T old_pos = curwin->w_cursor;
7187 pos_T old_visual = VIsual;
7188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007189 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007190 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7191 {
7192 shift_delete_registers();
7193 cap->oap->regname = '1';
7194 }
7195 else
7196 cap->oap->regname = '-';
7197 cap->cmdchar = 'd';
7198 cap->nchar = NUL;
7199 nv_operator(cap);
7200 do_pending_operator(cap, 0, FALSE);
7201 cap->cmdchar = K_PS;
7202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007203 // When the last char in the line was deleted then append. Detect this
7204 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007205 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7206 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007207 inc_cursor();
7208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007209 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007210 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 else if (!checkclearopq(cap->oap))
7213 {
7214 switch (cap->cmdchar)
7215 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007216 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007217 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 break;
7219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007221 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7222 beginline(BL_WHITE);
7223 else
7224 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 break;
7226
Bram Moolenaara1891842017-02-04 21:34:31 +01007227 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007228 // Bracketed paste works like "a"ppend, unless the cursor is in
7229 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007230 if (curwin->w_cursor.col == 0)
7231 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007234 case 'a': // "a"ppend is like "i"nsert on the next character.
7235 // increment coladd when in virtual space, increment the
7236 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (virtual_active()
7238 && (curwin->w_cursor.coladd > 0
7239 || *ml_get_cursor() == NUL
7240 || *ml_get_cursor() == TAB))
7241 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007242 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 inc_cursor();
7244 break;
7245 }
7246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7248 {
7249 int save_State = State;
7250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007251 // Pretend Insert mode here to allow the cursor on the
7252 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 State = INSERT;
7254 coladvance(getviscol());
7255 State = save_State;
7256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7259 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007260 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007261 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007262 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
7264
7265/*
7266 * Invoke edit() and take care of "restart_edit" and the return value.
7267 */
7268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007269invoke_edit(
7270 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007271 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007272 int cmd,
7273 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 int restart_edit_save = 0;
7276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007277 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7278 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7279 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (repl || !stuff_empty())
7281 restart_edit_save = restart_edit;
7282 else
7283 restart_edit_save = 0;
7284
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007285 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 restart_edit = 0;
7287
7288 if (edit(cmd, startln, cap->count1))
7289 cap->retval |= CA_COMMAND_BUSY;
7290
7291 if (restart_edit == 0)
7292 restart_edit = restart_edit_save;
7293}
7294
7295#ifdef FEAT_TEXTOBJ
7296/*
7297 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7298 */
7299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007300nv_object(
7301 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 int flag;
7304 int include;
7305 char_u *mps_save;
7306
7307 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007308 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007310 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007312 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 mps_save = curbuf->b_p_mps;
7314 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7315
7316 switch (cap->nchar)
7317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007318 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 flag = current_word(cap->oap, cap->count1, include, FALSE);
7320 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007321 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 flag = current_word(cap->oap, cap->count1, include, TRUE);
7323 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007324 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 case '(':
7326 case ')':
7327 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7328 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007329 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 case '{':
7331 case '}':
7332 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7333 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007334 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 case ']':
7336 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7337 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 case '>':
7340 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7341 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 case 't': // "at" = a tag block (xml and html)
7343 // Do not adjust oap->end in do_pending_operator()
7344 // otherwise there are different results for 'dit'
7345 // (note leading whitespace in last line):
7346 // 1) <b> 2) <b>
7347 // foobar foobar
7348 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007349 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007350 flag = current_tagblock(cap->oap, cap->count1, include);
7351 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007352 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 flag = current_par(cap->oap, cap->count1, include, 'p');
7354 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 flag = current_sent(cap->oap, cap->count1, include);
7357 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 case '"': // "a"" = a double quoted string
7359 case '\'': // "a'" = a single quoted string
7360 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007361 flag = current_quote(cap->oap, cap->count1, include,
7362 cap->nchar);
7363 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364#if 0 // TODO
7365 case 'S': // "aS" = a section
7366 case 'f': // "af" = a filename
7367 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368#endif
7369 default:
7370 flag = FAIL;
7371 break;
7372 }
7373
7374 curbuf->b_p_mps = mps_save;
7375 if (flag == FAIL)
7376 clearopbeep(cap->oap);
7377 adjust_cursor_col();
7378 curwin->w_set_curswant = TRUE;
7379}
7380#endif
7381
7382/*
7383 * "q" command: Start/stop recording.
7384 * "q:", "q/", "q?": edit command-line in command-line window.
7385 */
7386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007387nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
7389 if (cap->oap->op_type == OP_FORMAT)
7390 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007391 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 cap->cmdchar = 'g';
7393 cap->nchar = 'q';
7394 nv_operator(cap);
7395 }
7396 else if (!checkclearop(cap->oap))
7397 {
7398#ifdef FEAT_CMDWIN
7399 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7400 {
7401 stuffcharReadbuff(cap->nchar);
7402 stuffcharReadbuff(K_CMDWIN);
7403 }
7404 else
7405#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007406 // (stop) recording into a named register, unless executing a
7407 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007408 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 clearopbeep(cap->oap);
7410 }
7411}
7412
7413/*
7414 * Handle the "@r" command.
7415 */
7416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007417nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
7419 if (checkclearop(cap->oap))
7420 return;
7421#ifdef FEAT_EVAL
7422 if (cap->nchar == '=')
7423 {
7424 if (get_expr_register() == NUL)
7425 return;
7426 }
7427#endif
7428 while (cap->count1-- && !got_int)
7429 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007430 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
7432 clearopbeep(cap->oap);
7433 break;
7434 }
7435 line_breakcheck();
7436 }
7437}
7438
7439/*
7440 * Handle the CTRL-U and CTRL-D commands.
7441 */
7442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007443nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
7445 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7446 || (cap->cmdchar == Ctrl_D
7447 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7448 clearopbeep(cap->oap);
7449 else if (!checkclearop(cap->oap))
7450 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7451}
7452
7453/*
7454 * Handle "J" or "gJ" command.
7455 */
7456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007457nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007459 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007461 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 {
7463 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007464 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7466 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007468 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007469 if (cap->count0 <= 2)
7470 {
7471 clearopbeep(cap->oap);
7472 return;
7473 }
7474 cap->count0 = curbuf->b_ml.ml_line_count
7475 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007477
7478 prep_redo(cap->oap->regname, cap->count0,
7479 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7480 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482}
7483
7484/*
7485 * "P", "gP", "p" and "gp" commands.
7486 */
7487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007488nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007490 nv_put_opt(cap, FALSE);
7491}
7492
7493/*
7494 * "P", "gP", "p" and "gp" commands.
7495 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7496 */
7497 static void
7498nv_put_opt(cmdarg_T *cap, int fix_indent)
7499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 int regname = 0;
7501 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007502 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007503 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 int dir;
7505 int flags = 0;
7506
7507 if (cap->oap->op_type != OP_NOP)
7508 {
7509#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007510 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7512 {
7513 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007514 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516 else
7517#endif
7518 clearopbeep(cap->oap);
7519 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007520#ifdef FEAT_JOB_CHANNEL
7521 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7522 {
7523 clearopbeep(cap->oap);
7524 }
7525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 else
7527 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007528 if (fix_indent)
7529 {
7530 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7531 ? FORWARD : BACKWARD;
7532 flags |= PUT_FIXINDENT;
7533 }
7534 else
7535 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007536 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7537 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 prep_redo_cmd(cap);
7539 if (cap->cmdchar == 'g')
7540 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007541 else if (cap->cmdchar == 'z')
7542 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (VIsual_active)
7545 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007546 // Putting in Visual mode: The put text replaces the selected
7547 // text. First delete the selected text, then put the new text.
7548 // Need to save and restore the registers that the delete
7549 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007550 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007552#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007554#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007555 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007556 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007557#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560
7561 )
7562 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007563 // The delete is going to overwrite the register we want to
7564 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 reg1 = get_register(regname, TRUE);
7566 }
7567
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007568 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 cap->cmdchar = 'd';
7570 cap->nchar = NUL;
7571 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007572 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 nv_operator(cap);
7574 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007575 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007576 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007578 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 cap->oap->regname = regname;
7580
7581 if (reg1 != NULL)
7582 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007583 // Delete probably changed the register we want to put, save
7584 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 reg2 = get_register(regname, FALSE);
7586 put_register(regname, reg1);
7587 }
7588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007589 // When deleted a linewise Visual area, put the register as
7590 // lines to avoid it joined with the next line. When deletion was
7591 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 if (VIsual_mode == 'V')
7593 flags |= PUT_LINE;
7594 else if (VIsual_mode == 'v')
7595 flags |= PUT_LINE_SPLIT;
7596 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7597 flags |= PUT_LINE_FORWARD;
7598 dir = BACKWARD;
7599 if ((VIsual_mode != 'V'
7600 && curwin->w_cursor.col < curbuf->b_op_start.col)
7601 || (VIsual_mode == 'V'
7602 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007603 // cursor is at the end of the line or end of file, put
7604 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007606 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007607 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007609 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007611 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (reg2 != NULL)
7613 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007615 // What to reselect with "gv"? Selecting the just put text seems to
7616 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007617 if (was_visual)
7618 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007619 curbuf->b_visual.vi_start = curbuf->b_op_start;
7620 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007621 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007622 if (*p_sel == 'e')
7623 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007624 }
7625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007626 // When all lines were selected and deleted do_put() leaves an empty
7627 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007628 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007629 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007630 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007631 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007632
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007633 // If the cursor was in that line, move it to the end of the last
7634 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007635 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7636 {
7637 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7638 coladvance((colnr_T)MAXCOL);
7639 }
7640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 auto_format(FALSE, TRUE);
7642 }
7643}
7644
7645/*
7646 * "o" and "O" commands.
7647 */
7648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007649nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007652 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7654 {
7655 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007656 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
7658 else
7659#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007660 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007662#ifdef FEAT_JOB_CHANNEL
7663 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007664 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 n_opencmd(cap);
7668}
7669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670#ifdef FEAT_NETBEANS_INTG
7671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007672nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674 netbeans_keycommand(cap->nchar);
7675}
7676#endif
7677
7678#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007680nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007682 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683}
7684#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007685
Bram Moolenaar3918c952005-03-15 22:34:55 +00007686/*
7687 * Trigger CursorHold event.
7688 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7689 * input buffer. "did_cursorhold" is set to avoid retriggering.
7690 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007692nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007693{
7694 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7695 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007696 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007697}