blob: 92135c18c3f1706f30f325b4c40cefe1924c91b5 [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 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100378 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379};
380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static short nv_cmd_idx[NV_CMDS_SIZE];
386
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/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // When not finishing an operator and no register name typed, reset the
533 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 // Restore counts from before receiving K_CURSORHOLD. This means after
543 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Set v:count here, when called from main() and not a stuffed
562 // command, so that v:count can be used in an expression mapping
563 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 // 'insertmode' is set) fake a "d"elete command, Insert mode will
597 // restart automatically.
598 // Insert the typed character in the typeahead buffer, so that it can
599 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200600 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 msg_nowait = TRUE; // don't delay going to insert mode
606 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_CMDL_INFO
610 need_flushbuf = add_to_showcmd(c);
611#endif
612
613getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /*
617 * Handle a count before a command and compute ca.count0.
618 * Note that '0' is a command and not the start of a count, but it's
619 * part of a count after other digits.
620 */
621 while ( (c >= '1' && c <= '9')
622 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
623 {
624 if (c == K_DEL || c == K_KDEL)
625 {
626 ca.count0 /= 10;
627#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
630 }
631 else
632 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100633 if (ca.count0 < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000635#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100636 // Set v:count here, when called from main() and not a stuffed
637 // command, so that v:count can be used in an expression mapping
638 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100639 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100640 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (ctrl_w)
643 {
644 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100647 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000648 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 --no_zero_mapping;
651 if (ctrl_w)
652 {
653 --no_mapping;
654 --allow_keys;
655 }
656#ifdef FEAT_CMDL_INFO
657 need_flushbuf |= add_to_showcmd(c);
658#endif
659 }
660
661 /*
662 * If we got CTRL-W there may be a/another count
663 */
664 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
665 {
666 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 ca.count0 = 0;
669 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 ++allow_keys; // no mapping for nchar, but keys
671 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --no_mapping;
674 --allow_keys;
675#ifdef FEAT_CMDL_INFO
676 need_flushbuf |= add_to_showcmd(c);
677#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 // Save the count values so that ca.opcount and ca.count0 are exactly
685 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000686 oap->prev_opcount = ca.opcount;
687 oap->prev_count0 = ca.count0;
688 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100689 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000690 {
691 /*
692 * If we're in the middle of an operator (including after entering a
693 * yank buffer with '"') AND we had a count before the operator, then
694 * that count overrides the current value of ca.count0.
695 * What this means effectively, is that commands like "3dw" get turned
696 * into "d3w" which makes things fall into place pretty neatly.
697 * If you give a count before AND after the operator, they are
698 * multiplied.
699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ca.count0)
701 ca.count0 *= ca.opcount;
702 else
703 ca.count0 = ca.opcount;
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100704 if (ca.count0 < 0) // overflow
705 ca.count0 = 999999999L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707
708 /*
709 * Always remember the count. It will be set to zero (on the next call,
710 * above) when there is no pending operator.
711 * When called from main(), save the count for use by the "count" built-in
712 * variable.
713 */
714 ca.opcount = ca.count0;
715 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
716
717#ifdef FEAT_EVAL
718 /*
719 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100722 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000723 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#endif
725
726 /*
727 * Find the command character in the table of commands.
728 * For CTRL-W we already got nchar when looking for a count.
729 */
730 if (ctrl_w)
731 {
732 ca.nchar = c;
733 ca.cmdchar = Ctrl_W;
734 }
735 else
736 ca.cmdchar = c;
737 idx = find_command(ca.cmdchar);
738 if (idx < 0)
739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100740 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 clearopbeep(oap);
742 goto normal_end;
743 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000744
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000745 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000749 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 goto normal_end;
751 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000752 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
753 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /*
756 * In Visual/Select mode, a few keys are handled in a special way.
757 */
758 if (VIsual_active)
759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (km_stopsel
762 && (nv_cmds[idx].cmd_flags & NV_STS)
763 && !(mod_mask & MOD_MASK_SHIFT))
764 {
765 end_visual_mode();
766 redraw_curbuf_later(INVERTED);
767 }
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (km_startsel)
771 {
772 if (nv_cmds[idx].cmd_flags & NV_SS)
773 {
774 unshift_special(&ca);
775 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 if (idx < 0)
777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100778 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000779 clearopbeep(oap);
780 goto normal_end;
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
784 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789#ifdef FEAT_RIGHTLEFT
790 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
791 && (nv_cmds[idx].cmd_flags & NV_RL))
792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100793 // Invert horizontal movements and operations. Only when typed by the
794 // user directly, not when the result of a mapping or "x" translated
795 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 switch (ca.cmdchar)
797 {
798 case 'l': ca.cmdchar = 'h'; break;
799 case K_RIGHT: ca.cmdchar = K_LEFT; break;
800 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
801 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
802 case 'h': ca.cmdchar = 'l'; break;
803 case K_LEFT: ca.cmdchar = K_RIGHT; break;
804 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
805 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
806 case '>': ca.cmdchar = '<'; break;
807 case '<': ca.cmdchar = '>'; break;
808 }
809 idx = find_command(ca.cmdchar);
810 }
811#endif
812
813 /*
814 * Get an additional character if we need one.
815 */
816 if ((nv_cmds[idx].cmd_flags & NV_NCH)
817 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
818 && oap->op_type == OP_NOP)
819 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
820 || (ca.cmdchar == 'q'
821 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200822 && reg_recording == 0
823 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100825 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 int repl = FALSE; // get character for replace mode
829 int lit = FALSE; // get extra character literally
830 int langmap_active = FALSE; // using :lmap mappings
831 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100832#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100833 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif
835
836 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 ++allow_keys; // no mapping for nchar, but allow key codes
838 // Don't generate a CursorHold event here, most commands can't handle
839 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000840 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (ca.cmdchar == 'g')
842 {
843 /*
844 * For 'g' get the next character now, so that we can check for
845 * "gr", "g'" and "g`".
846 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000847 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#ifdef FEAT_CMDL_INFO
850 need_flushbuf |= add_to_showcmd(ca.nchar);
851#endif
852 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100853 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 else
865 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100866 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 repl = TRUE;
868 cp = &ca.nchar;
869 }
870 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
871
872 /*
873 * Get a second or third character.
874 */
875 if (cp != NULL)
876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (repl)
878 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100880#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100881 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 --no_mapping;
888 --allow_keys;
889 if (repl)
890 State = LREPLACE;
891 else
892 State = LANGMAP;
893 langmap_active = TRUE;
894 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100895#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100897 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
899 im_set_active(TRUE);
900#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200901 if ((State & INSERT) && !p_ek)
902 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200903#ifdef FEAT_JOB_CHANNEL
904 ch_log_output = TRUE;
905#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200906 // Disable bracketed paste and modifyOtherKeys here, we won't
907 // recognize the escape sequences with 'esckeys' off.
908 out_str(T_BD);
909 out_str(T_CTE);
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000912 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaarca774f62020-08-30 20:46:38 +0200914 if ((State & INSERT) && !p_ek)
915 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200916#ifdef FEAT_JOB_CHANNEL
917 ch_log_output = TRUE;
918#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200919 // Re-enable bracketed paste mode and modifyOtherKeys
920 out_str(T_BE);
921 out_str(T_CTI);
922 }
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (langmap_active)
925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100926 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 ++no_mapping;
928 ++allow_keys;
929 State = NORMAL_BUSY;
930 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100931#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (lang)
933 {
934 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
935 im_save_status(&curbuf->b_p_iminsert);
936 im_set_active(FALSE);
937 }
938 p_smd = save_smd;
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#ifdef FEAT_CMDL_INFO
942 need_flushbuf |= add_to_showcmd(*cp);
943#endif
944
945 if (!lit)
946 {
947#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (*cp == Ctrl_K
950 && ((nv_cmds[idx].cmd_flags & NV_LANG)
951 || cp == &ca.extra_char)
952 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
953 {
954 c = get_digraph(FALSE);
955 if (c > 0)
956 {
957 *cp = c;
958# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 del_from_showcmd(3);
961 need_flushbuf |= add_to_showcmd(*cp);
962# endif
963 }
964 }
965#endif
966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100967 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100970 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (p_hkmap && lang && KeyTyped)
972 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#endif
974 }
975
976 /*
977 * When the next character is CTRL-\ a following CTRL-N means the
978 * command is aborted and we go to Normal mode.
979 */
980 if (cp == &ca.extra_char
981 && ca.nchar == Ctrl_BSL
982 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
983 {
984 ca.cmdchar = Ctrl_BSL;
985 ca.nchar = ca.extra_char;
986 idx = find_command(ca.cmdchar);
987 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200988 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200989 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 else if (*cp == Ctrl_BSL)
991 {
992 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100994 // There is a busy wait here when typing "f<C-\>" and then
995 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 while ((c = vpeekc()) <= 0 && towait > 0L)
997 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +0100998 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 towait -= 50L;
1000 }
1001 if (c > 0)
1002 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001003 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (c != Ctrl_N && c != Ctrl_G)
1005 vungetc(c);
1006 else
1007 {
1008 ca.cmdchar = Ctrl_BSL;
1009 ca.nchar = c;
1010 idx = find_command(ca.cmdchar);
1011 }
1012 }
1013 }
1014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001015 // When getting a text character and the next character is a
1016 // multi-byte character, it could be a composing character.
1017 // However, don't wait for it to arrive. Also, do enable mapping,
1018 // because if it's put back with vungetc() it's too late to apply
1019 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001020 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 while (enc_utf8 && lang && (c = vpeekc()) > 0
1022 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1023 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001024 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (!utf_iscomposing(c))
1026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001027 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 break;
1029 }
1030 else if (ca.ncharC1 == 0)
1031 ca.ncharC1 = c;
1032 else
1033 ca.ncharC2 = c;
1034 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001035 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037 --no_mapping;
1038 --allow_keys;
1039 }
1040
1041#ifdef FEAT_CMDL_INFO
1042 /*
1043 * Flush the showcmd characters onto the screen so we can see them while
1044 * the command is being executed. Only do this when the shown command was
1045 * actually displayed, otherwise this will slow down a lot when executing
1046 * mappings.
1047 */
1048 if (need_flushbuf)
1049 out_flush();
1050#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001051 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001052 {
1053 if (ex_normal_busy)
1054 did_cursorhold = save_did_cursorhold;
1055 else
1056 did_cursorhold = FALSE;
1057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 State = NORMAL;
1060
1061 if (ca.nchar == ESC)
1062 {
1063 clearop(oap);
1064 if (restart_edit == 0 && goto_im())
1065 restart_edit = 'a';
1066 goto normal_end;
1067 }
1068
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 if (ca.cmdchar != K_IGNORE)
1070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001072 msg_col = 0;
1073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // When 'keymodel' contains "startsel" some keys start Select/Visual
1078 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (!VIsual_active && km_startsel)
1080 {
1081 if (nv_cmds[idx].cmd_flags & NV_SS)
1082 {
1083 start_selection();
1084 unshift_special(&ca);
1085 idx = find_command(ca.cmdchar);
1086 }
1087 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1088 && (mod_mask & MOD_MASK_SHIFT))
1089 {
1090 start_selection();
1091 mod_mask &= ~MOD_MASK_SHIFT;
1092 }
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 /*
1096 * Execute the command!
1097 * Call the command function found in the commands table.
1098 */
1099 ca.arg = nv_cmds[idx].cmd_arg;
1100 (nv_cmds[idx].cmd_func)(&ca);
1101
1102 /*
1103 * If we didn't start or finish an operator, reset oap->regname, unless we
1104 * need it later.
1105 */
1106 if (!finish_op
1107 && !oap->op_type
1108 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1109 {
1110 clearop(oap);
1111#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001112 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#endif
1114 }
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 // Get the length of mapped chars again after typing a count, second
1117 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001118 if (old_mapped_len > 0)
1119 old_mapped_len = typebuf_maplen();
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001122 * If an operation is pending, handle it. But not for K_IGNORE or
1123 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001125 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001126 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 /*
1129 * Wait for a moment when a message is displayed that will be overwritten
1130 * by the mode message.
1131 * In Visual mode and with "^O" in Insert mode, a short message will be
1132 * overwritten by the mode message. Wait a bit, until a key is hit.
1133 * In Visual mode, it's more important to keep the Visual area updated
1134 * than keeping a message (e.g. from a /pat search).
1135 * Only do this if the command was typed, not from a mapping.
1136 * Don't wait when emsg_silent is non-zero.
1137 * Also wait a bit after an error message, e.g. for "^O:".
1138 * Don't redraw the screen, it would remove the message.
1139 */
1140 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001141 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 || (VIsual_active
1144 && old_pos.lnum == curwin->w_cursor.lnum
1145 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 )
1147 && (clear_cmdline
1148 || redraw_cmdline)
1149 && (msg_didout || (msg_didany && msg_scroll))
1150 && !msg_nowait
1151 && KeyTyped)
1152 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 && (msg_scroll
1155 || emsg_on_display)))
1156 && oap->regname == 0
1157 && !(ca.retval & CA_COMMAND_BUSY)
1158 && stuff_empty()
1159 && typebuf_typed()
1160 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001161 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 && !did_wait_return
1163 && oap->op_type == OP_NOP)
1164 {
1165 int save_State = State;
1166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001167 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (restart_edit != 0)
1169 State = INSERT;
1170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001171 // If need to redraw, and there is a "keep_msg", redraw before the
1172 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1174 {
1175 char_u *kmsg;
1176
1177 kmsg = keep_msg;
1178 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001179 // Showmode() will clear keep_msg, but we want to use it anyway.
1180 // First update w_topline.
1181 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001183 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001185
1186 kmsg = vim_strsave(keep_msg);
1187 if (kmsg != NULL)
1188 {
1189 msg_attr((char *)kmsg, keep_msg_attr);
1190 vim_free(kmsg);
1191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001194#ifdef CURSOR_SHAPE
1195 ui_cursor_shape(); // may show different cursor shape
1196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 cursor_on();
1198 out_flush();
1199 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001200 ui_delay(1003L, TRUE); // wait at least one second
1201 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 State = save_State;
1203
1204 msg_scroll = FALSE;
1205 emsg_on_display = FALSE;
1206 }
1207
1208 /*
1209 * Finish up after executing a Normal mode command.
1210 */
1211normal_end:
1212
1213 msg_nowait = FALSE;
1214
Bram Moolenaarcc613032020-06-07 21:31:18 +02001215#ifdef FEAT_EVAL
1216 if (finish_op)
1217 reset_reg_var();
1218#endif
1219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#ifdef CURSOR_SHAPE
1222 c = finish_op;
1223#endif
1224 finish_op = FALSE;
1225#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 // Redraw the cursor with another shape, if we were in Operator-pending
1227 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (c || ca.cmdchar == 'r')
1229 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001230 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231# ifdef FEAT_MOUSESHAPE
1232 update_mouseshape(-1);
1233# endif
1234 }
1235#endif
1236
1237#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001238 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001239 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 clear_showcmd();
1241#endif
1242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001243 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 vim_free(ca.searchbuf);
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (has_mbyte)
1247 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (curwin->w_p_scb && toplevel)
1250 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001251 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 do_check_scrollbind(TRUE);
1253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 if (curwin->w_p_crb && toplevel)
1256 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001258 do_check_cursorbind();
1259 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001261#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001262 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001263 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001264 restart_edit = 0;
1265#endif
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 /*
1268 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1269 * if still inside a mapping that started in Visual mode).
1270 * May switch from Visual to Select mode after CTRL-O command.
1271 */
1272 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1274 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 && !(ca.retval & CA_COMMAND_BUSY)
1276 && stuff_empty()
1277 && oap->regname == 0)
1278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (restart_VIsual_select == 1)
1280 {
1281 VIsual_select = TRUE;
1282 showmode();
1283 restart_VIsual_select = 0;
1284 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001285 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 (void)edit(restart_edit, FALSE, 1L);
1287 }
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (restart_VIsual_select == 2)
1290 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001292 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 opcount = ca.opcount;
1294}
1295
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001296#ifdef FEAT_EVAL
1297/*
1298 * Set v:count and v:count1 according to "cap".
1299 * Set v:prevcount only when "set_prevcount" is TRUE.
1300 */
1301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001302set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001303{
1304 long count = cap->count0;
1305
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001307 if (cap->opcount != 0)
1308 count = cap->opcount * (count == 0 ? 1 : count);
1309 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001311}
1312#endif
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001315 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 * if not.
1317 */
1318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001319check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 static int did_check = FALSE;
1322
1323 if (full_screen)
1324 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001325 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001326 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 did_check = TRUE;
1328 }
1329}
1330
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001331#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1332/*
1333 * Call yank_do_autocmd() for "regname".
1334 */
1335 static void
1336call_yank_do_autocmd(int regname)
1337{
1338 oparg_T oa;
1339 yankreg_T *reg;
1340
1341 clear_oparg(&oa);
1342 oa.regname = regname;
1343 oa.op_type = OP_YANK;
1344 oa.is_VIsual = TRUE;
1345 reg = get_register(regname, TRUE);
1346 yank_do_autocmd(&oa, reg);
1347 free_register(reg);
1348}
1349#endif
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001352 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * This function should ALWAYS be called to end Visual mode, except from
1354 * do_pending_operator().
1355 */
1356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001357end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359#ifdef FEAT_CLIPBOARD
1360 /*
1361 * If we are using the clipboard, then remember what was selected in case
1362 * we need to paste it somewhere while we still own the selection.
1363 * Only do this when the clipboard is already owned. Don't want to grab
1364 * the selection when hitting ESC.
1365 */
1366 if (clip_star.available && clip_star.owned)
1367 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001368
1369# if defined(FEAT_EVAL)
1370 // Emit a TextYankPost for the automatic copy of the selection into the
1371 // star and/or plus register.
1372 if (has_textyankpost())
1373 {
1374 if (clip_isautosel_star())
1375 call_yank_do_autocmd('*');
1376 if (clip_isautosel_plus())
1377 call_yank_do_autocmd('+');
1378 }
1379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#endif
1381
1382 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 setmouse();
1384 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001386 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001387 curbuf->b_visual.vi_mode = VIsual_mode;
1388 curbuf->b_visual.vi_start = VIsual;
1389 curbuf->b_visual.vi_end = curwin->w_cursor;
1390 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifdef FEAT_EVAL
1392 curbuf->b_visual_mode_eval = VIsual_mode;
1393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (!virtual_active())
1395 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001396 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001398 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399}
1400
1401/*
1402 * Reset VIsual_active and VIsual_reselect.
1403 */
1404 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001405reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 if (VIsual_active)
1408 {
1409 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001410 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 VIsual_reselect = FALSE;
1413}
1414
1415/*
1416 * Reset VIsual_active and VIsual_reselect if it's set.
1417 */
1418 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001419reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 if (VIsual_active)
1422 {
1423 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001424 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 VIsual_reselect = FALSE;
1426 }
1427}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001429 void
1430restore_visual_mode(void)
1431{
1432 if (VIsual_mode_orig != NUL)
1433 {
1434 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1435 VIsual_mode_orig = NUL;
1436 }
1437}
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439/*
1440 * Check for a balloon-eval special item to include when searching for an
1441 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1442 * Returns TRUE if the character at "*ptr" should be included.
1443 * "dir" is FORWARD or BACKWARD, the direction of searching.
1444 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1445 * "bnp" points to a counter for square brackets.
1446 */
1447 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448find_is_eval_item(
1449 char_u *ptr,
1450 int *colp,
1451 int *bnp,
1452 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001454 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1456 ++*bnp;
1457 if (*bnp > 0)
1458 {
1459 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1460 --*bnp;
1461 return TRUE;
1462 }
1463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001464 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (*ptr == '.')
1466 return TRUE;
1467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001468 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1470 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1471 {
1472 *colp += dir;
1473 return TRUE;
1474 }
1475 return FALSE;
1476}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478/*
1479 * Find the identifier under or to the right of the cursor.
1480 * "find_type" can have one of three values:
1481 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001482 * FIND_STRING: find any non-white text
1483 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001484 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 *
1486 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001487 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001489 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 * This doesn't match the real Vi but I like it a little better and it
1491 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001492 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 * When FIND_IDENT isn't defined, we backup until a blank.
1494 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001495 * Returns the length of the text, or zero if no text is found.
1496 * If text is found, a pointer to the text is put in "*text". This
1497 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 */
1499 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001500find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001503 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504}
1505
1506/*
1507 * Like find_ident_under_cursor(), but for any window and any position.
1508 * However: Uses 'iskeyword' from the current window!.
1509 */
1510 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001511find_ident_at_pos(
1512 win_T *wp,
1513 linenr_T lnum,
1514 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001515 char_u **text,
1516 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001520 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int this_class = 0;
1523 int prev_class;
1524 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001525 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 /*
1528 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001529 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 */
1531 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1532 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1533 {
1534 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001535 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
1537 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (has_mbyte)
1539 {
1540 while (ptr[col] != NUL)
1541 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 this_class = mb_get_class(ptr + col);
1546 if (this_class != 0 && (i == 1 || this_class != 1))
1547 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001548 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 }
1551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001553 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 )
1556 ++col;
1557
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001558 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
1561 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001562 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if (has_mbyte)
1565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001566 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1568 this_class = mb_get_class((char_u *)"a");
1569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001571 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
1573 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1574 prev_class = mb_get_class(ptr + prevcol);
1575 if (this_class != prev_class
1576 && (i == 0
1577 || prev_class == 0
1578 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 && (!(find_type & FIND_EVAL)
1580 || prevcol == 0
1581 || !find_is_eval_item(ptr + prevcol, &prevcol,
1582 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 )
1584 break;
1585 col = prevcol;
1586 }
1587
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001588 // If we don't want just any old text, or we've found an
1589 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (this_class > 2)
1591 this_class = 2;
1592 if (!(find_type & FIND_STRING) || this_class == 2)
1593 break;
1594 }
1595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 while (col > 0
1598 && ((i == 0
1599 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001600 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 && (!(find_type & FIND_IDENT)
1602 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 || ((find_type & FIND_EVAL)
1604 && col > 1
1605 && find_is_eval_item(ptr + col - 1, &col,
1606 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 ))
1608 --col;
1609
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001610 // If we don't want just any old text, or we've found an
1611 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1613 break;
1614 }
1615 }
1616
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001617 if (ptr[col] == NUL || (i == 0
1618 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001620 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001621 if ((find_type & FIND_NOERROR) == 0)
1622 {
1623 if (find_type & FIND_STRING)
1624 emsg(_("E348: No string under cursor"));
1625 else
1626 emsg(_(e_noident));
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 return 0;
1629 }
1630 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001631 *text = ptr;
1632 if (textcol != NULL)
1633 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
1635 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001636 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 bn = 0;
1639 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (has_mbyte)
1642 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001643 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 this_class = mb_get_class(ptr);
1645 while (ptr[col] != NUL
1646 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1647 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 || ((find_type & FIND_EVAL)
1649 && col <= (int)startcol
1650 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001652 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001656 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 || ((find_type & FIND_EVAL)
1658 && col <= (int)startcol
1659 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 return col;
1664}
1665
1666/*
1667 * Prepare for redo of a normal command.
1668 */
1669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001670prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 prep_redo(cap->oap->regname, cap->count0,
1673 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1674}
1675
1676/*
1677 * Prepare for redo of any command.
1678 * Note that only the last argument can be a multi-byte char.
1679 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681prep_redo(
1682 int regname,
1683 long num,
1684 int cmd1,
1685 int cmd2,
1686 int cmd3,
1687 int cmd4,
1688 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001691 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
1693 AppendCharToRedobuff('"');
1694 AppendCharToRedobuff(regname);
1695 }
1696 if (num)
1697 AppendNumberToRedobuff(num);
1698
1699 if (cmd1 != NUL)
1700 AppendCharToRedobuff(cmd1);
1701 if (cmd2 != NUL)
1702 AppendCharToRedobuff(cmd2);
1703 if (cmd3 != NUL)
1704 AppendCharToRedobuff(cmd3);
1705 if (cmd4 != NUL)
1706 AppendCharToRedobuff(cmd4);
1707 if (cmd5 != NUL)
1708 AppendCharToRedobuff(cmd5);
1709}
1710
1711/*
1712 * check for operator active and clear it
1713 *
1714 * return TRUE if operator was active
1715 */
1716 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001717checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 if (oap->op_type == OP_NOP)
1720 return FALSE;
1721 clearopbeep(oap);
1722 return TRUE;
1723}
1724
1725/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001726 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001728 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
1730 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001731checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001733 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 return FALSE;
1735 clearopbeep(oap);
1736 return TRUE;
1737}
1738
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001740clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 oap->op_type = OP_NOP;
1743 oap->regname = 0;
1744 oap->motion_force = NUL;
1745 oap->use_reg_one = FALSE;
1746}
1747
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 clearop(oap);
1752 beep_flush();
1753}
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Remove the shift modifier from a special key.
1757 */
1758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 switch (cap->cmdchar)
1762 {
1763 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1764 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1765 case K_S_UP: cap->cmdchar = K_UP; break;
1766 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1767 case K_S_HOME: cap->cmdchar = K_HOME; break;
1768 case K_S_END: cap->cmdchar = K_END; break;
1769 }
1770 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001773/*
1774 * If the mode is currently displayed clear the command line or update the
1775 * command displayed.
1776 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001778may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001779{
1780 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001781 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001782#ifdef FEAT_CMDL_INFO
1783 else
1784 clear_showcmd();
1785#endif
1786}
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1789/*
1790 * Routines for displaying a partly typed command
1791 */
1792
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001793#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001795static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796static int showcmd_is_clear = TRUE;
1797static int showcmd_visual = FALSE;
1798
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001799static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001802clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 if (!p_sc)
1805 return;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (VIsual_active && !char_avail())
1808 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001809 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 long lines;
1811 colnr_T leftcol, rightcol;
1812 linenr_T top, bot;
1813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001814 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001815 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817 top = VIsual.lnum;
1818 bot = curwin->w_cursor.lnum;
1819 }
1820 else
1821 {
1822 top = curwin->w_cursor.lnum;
1823 bot = VIsual.lnum;
1824 }
1825# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001826 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001827 (void)hasFolding(top, &top, NULL);
1828 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829# endif
1830 lines = bot - top + 1;
1831
1832 if (VIsual_mode == Ctrl_V)
1833 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001834# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001835 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001836 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001838 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001839 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001840 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001843# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001844 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001845 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1848 (long)(rightcol - leftcol + 1));
1849 }
1850 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1851 sprintf((char *)showcmd_buf, "%ld", lines);
1852 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001853 {
1854 char_u *s, *e;
1855 int l;
1856 int bytes = 0;
1857 int chars = 0;
1858
1859 if (cursor_bot)
1860 {
1861 s = ml_get_pos(&VIsual);
1862 e = ml_get_cursor();
1863 }
1864 else
1865 {
1866 s = ml_get_cursor();
1867 e = ml_get_pos(&VIsual);
1868 }
1869 while ((*p_sel != 'e') ? s <= e : s < e)
1870 {
1871 l = (*mb_ptr2len)(s);
1872 if (l == 0)
1873 {
1874 ++bytes;
1875 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001876 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001877 }
1878 bytes += l;
1879 ++chars;
1880 s += l;
1881 }
1882 if (bytes == chars)
1883 sprintf((char *)showcmd_buf, "%d", chars);
1884 else
1885 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1886 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001887 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 showcmd_visual = TRUE;
1889 }
1890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 showcmd_buf[0] = NUL;
1893 showcmd_visual = FALSE;
1894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001895 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (showcmd_is_clear)
1897 return;
1898 }
1899
1900 display_showcmd();
1901}
1902
1903/*
1904 * Add 'c' to string of shown command chars.
1905 * Return TRUE if output has been written (and setcursor() has been called).
1906 */
1907 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001908add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 char_u *p;
1911 int old_len;
1912 int extra_len;
1913 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int i;
1915 static int ignore[] =
1916 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001917#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1919 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001920#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001921 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001922 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1924 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001925 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001927 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 0
1929 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
Bram Moolenaar09df3122006-01-23 22:23:09 +00001931 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 return FALSE;
1933
1934 if (showcmd_visual)
1935 {
1936 showcmd_buf[0] = NUL;
1937 showcmd_visual = FALSE;
1938 }
1939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001940 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (IS_SPECIAL(c))
1942 for (i = 0; ignore[i] != 0; ++i)
1943 if (ignore[i] == c)
1944 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001947 if (*p == ' ')
1948 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 old_len = (int)STRLEN(showcmd_buf);
1950 extra_len = (int)STRLEN(p);
1951 overflow = old_len + extra_len - SHOWCMD_COLS;
1952 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001953 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1954 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 STRCAT(showcmd_buf, p);
1956
1957 if (char_avail())
1958 return FALSE;
1959
1960 display_showcmd();
1961
1962 return TRUE;
1963}
1964
1965 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001966add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
1968 if (!add_to_showcmd(c))
1969 setcursor();
1970}
1971
1972/*
1973 * Delete 'len' characters from the end of the shown command.
1974 */
1975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
1978 int old_len;
1979
1980 if (!p_sc)
1981 return;
1982
1983 old_len = (int)STRLEN(showcmd_buf);
1984 if (len > old_len)
1985 len = old_len;
1986 showcmd_buf[old_len - len] = NUL;
1987
1988 if (!char_avail())
1989 display_showcmd();
1990}
1991
1992/*
1993 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1994 * something and there is a partial mapping.
1995 */
1996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 if (p_sc)
2000 STRCPY(old_showcmd_buf, showcmd_buf);
2001}
2002
2003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 if (!p_sc)
2007 return;
2008
2009 STRCPY(showcmd_buf, old_showcmd_buf);
2010
2011 display_showcmd();
2012}
2013
2014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002015display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 int len;
2018
2019 cursor_off();
2020
2021 len = (int)STRLEN(showcmd_buf);
2022 if (len == 0)
2023 showcmd_is_clear = TRUE;
2024 else
2025 {
2026 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2027 showcmd_is_clear = FALSE;
2028 }
2029
2030 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002031 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2032 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
2034 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037}
2038#endif
2039
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040/*
2041 * When "check" is FALSE, prepare for commands that scroll the window.
2042 * When "check" is TRUE, take care of scroll-binding after the window has
2043 * scrolled. Called from normal_cmd() and edit().
2044 */
2045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002046do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
2048 static win_T *old_curwin = NULL;
2049 static linenr_T old_topline = 0;
2050#ifdef FEAT_DIFF
2051 static int old_topfill = 0;
2052#endif
2053 static buf_T *old_buf = NULL;
2054 static colnr_T old_leftcol = 0;
2055
2056 if (check && curwin->w_p_scb)
2057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 // If a ":syncbind" command was just used, don't scroll, only reset
2059 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (did_syncbind)
2061 did_syncbind = FALSE;
2062 else if (curwin == old_curwin)
2063 {
2064 /*
2065 * Synchronize other windows, as necessary according to
2066 * 'scrollbind'. Don't do this after an ":edit" command, except
2067 * when 'diff' is set.
2068 */
2069 if ((curwin->w_buffer == old_buf
2070#ifdef FEAT_DIFF
2071 || curwin->w_p_diff
2072#endif
2073 )
2074 && (curwin->w_topline != old_topline
2075#ifdef FEAT_DIFF
2076 || curwin->w_topfill != old_topfill
2077#endif
2078 || curwin->w_leftcol != old_leftcol))
2079 {
2080 check_scrollbind(curwin->w_topline - old_topline,
2081 (long)(curwin->w_leftcol - old_leftcol));
2082 }
2083 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002084 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 /*
2087 * When switching between windows, make sure that the relative
2088 * vertical offset is valid for the new window. The relative
2089 * offset is invalid whenever another 'scrollbind' window has
2090 * scrolled to a point that would force the current window to
2091 * scroll past the beginning or end of its buffer. When the
2092 * resync is performed, some of the other 'scrollbind' windows may
2093 * need to jump so that the current window's relative position is
2094 * visible on-screen.
2095 */
2096 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2097 }
2098 curwin->w_scbind_pos = curwin->w_topline;
2099 }
2100
2101 old_curwin = curwin;
2102 old_topline = curwin->w_topline;
2103#ifdef FEAT_DIFF
2104 old_topfill = curwin->w_topfill;
2105#endif
2106 old_buf = curwin->w_buffer;
2107 old_leftcol = curwin->w_leftcol;
2108}
2109
2110/*
2111 * Synchronize any windows that have "scrollbind" set, based on the
2112 * number of rows by which the current window has changed
2113 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2114 */
2115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002116check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
2118 int want_ver;
2119 int want_hor;
2120 win_T *old_curwin = curwin;
2121 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 int old_VIsual_select = VIsual_select;
2123 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 colnr_T tgt_leftcol = curwin->w_leftcol;
2125 long topline;
2126 long y;
2127
2128 /*
2129 * check 'scrollopt' string for vertical and horizontal scroll options
2130 */
2131 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2132#ifdef FEAT_DIFF
2133 want_ver |= old_curwin->w_p_diff;
2134#endif
2135 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2136
2137 /*
2138 * loop through the scrollbound windows and scroll accordingly
2139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002141 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
2143 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002144 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (curwin != old_curwin && curwin->w_p_scb)
2146 {
2147 /*
2148 * do the vertical scroll
2149 */
2150 if (want_ver)
2151 {
2152#ifdef FEAT_DIFF
2153 if (old_curwin->w_p_diff && curwin->w_p_diff)
2154 {
2155 diff_set_topline(old_curwin, curwin);
2156 }
2157 else
2158#endif
2159 {
2160 curwin->w_scbind_pos += topline_diff;
2161 topline = curwin->w_scbind_pos;
2162 if (topline > curbuf->b_ml.ml_line_count)
2163 topline = curbuf->b_ml.ml_line_count;
2164 if (topline < 1)
2165 topline = 1;
2166
2167 y = topline - curwin->w_topline;
2168 if (y > 0)
2169 scrollup(y, FALSE);
2170 else
2171 scrolldown(-y, FALSE);
2172 }
2173
2174 redraw_later(VALID);
2175 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178
2179 /*
2180 * do the horizontal scroll
2181 */
2182 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2183 {
2184 curwin->w_leftcol = tgt_leftcol;
2185 leftcol_changed();
2186 }
2187 }
2188 }
2189
2190 /*
2191 * reset current-window
2192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 VIsual_select = old_VIsual_select;
2194 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 curwin = old_curwin;
2196 curbuf = old_curbuf;
2197}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199/*
2200 * Command character that's ignored.
2201 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002202 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002205nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208}
2209
2210/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002211 * Command character that doesn't do anything, but unlike nv_ignore() does
2212 * start edit(). Used for "startinsert" executed while starting up.
2213 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002215nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002216{
2217}
2218
2219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * Command character doesn't exist.
2221 */
2222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002223nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 clearopbeep(cap->oap);
2226}
2227
2228/*
2229 * <Help> and <F1> commands.
2230 */
2231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 if (!checkclearopq(cap->oap))
2235 ex_help(NULL);
2236}
2237
2238/*
2239 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2240 */
2241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002242nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002244#ifdef FEAT_JOB_CHANNEL
2245 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2246 clearopbeep(cap->oap);
2247 else
2248#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002249 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002250 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002251 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002252 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2253 op_addsub(cap->oap, cap->count1, cap->arg);
2254 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002255 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002256 else if (VIsual_active)
2257 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002258 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002259 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260}
2261
2262/*
2263 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2264 */
2265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 if (mod_mask & MOD_MASK_CTRL)
2271 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002272 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273 if (cap->arg == BACKWARD)
2274 goto_tabpage(-(int)cap->count1);
2275 else
2276 goto_tabpage((int)cap->count0);
2277 }
2278 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002279 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281}
2282
2283/*
2284 * Implementation of "gd" and "gD" command.
2285 */
2286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287nv_gd(
2288 oparg_T *oap,
2289 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002290 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 char_u *ptr;
2294
Bram Moolenaard9d30582005-05-18 22:10:28 +00002295 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002296 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2297 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002299#ifdef FEAT_FOLDING
2300 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2301 foldOpenCursor();
2302#endif
2303}
2304
2305/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002306 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2307 * otherwise.
2308 */
2309 static int
2310is_ident(char_u *line, int offset)
2311{
2312 int i;
2313 int incomment = FALSE;
2314 int instring = 0;
2315 int prev = 0;
2316
2317 for (i = 0; i < offset && line[i] != NUL; i++)
2318 {
2319 if (instring != 0)
2320 {
2321 if (prev != '\\' && line[i] == instring)
2322 instring = 0;
2323 }
2324 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2325 {
2326 instring = line[i];
2327 }
2328 else
2329 {
2330 if (incomment)
2331 {
2332 if (prev == '*' && line[i] == '/')
2333 incomment = FALSE;
2334 }
2335 else if (prev == '/' && line[i] == '*')
2336 {
2337 incomment = TRUE;
2338 }
2339 else if (prev == '/' && line[i] == '/')
2340 {
2341 return FALSE;
2342 }
2343 }
2344
2345 prev = line[i];
2346 }
2347
2348 return incomment == FALSE && instring == 0;
2349}
2350
2351/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002352 * Search for variable declaration of "ptr[len]".
2353 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2354 * current file ("gD").
2355 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002356 * Return FAIL when not found.
2357 */
2358 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002359find_decl(
2360 char_u *ptr,
2361 int len,
2362 int locally,
2363 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002364 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002365{
2366 char_u *pat;
2367 pos_T old_pos;
2368 pos_T par_pos;
2369 pos_T found_pos;
2370 int t;
2371 int save_p_ws;
2372 int save_p_scs;
2373 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002374 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002375 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002376 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002377
2378 if ((pat = alloc(len + 7)) == NULL)
2379 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002381 // Put "\V" before the pattern to avoid that the special meaning of "."
2382 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002383 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2384 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 old_pos = curwin->w_cursor;
2386 save_p_ws = p_ws;
2387 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 p_ws = FALSE; // don't wrap around end of file now
2389 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /*
2392 * With "gD" go to line 1.
2393 * With "gd" Search back for the start of the current function, then go
2394 * back until a blank line. If this fails go to line 1.
2395 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002396 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002398 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002400 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 else
2403 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002404 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2406 --curwin->w_cursor.lnum;
2407 }
2408 curwin->w_cursor.col = 0;
2409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002410 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002411 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002412 for (;;)
2413 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002414 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002415 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002416 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002418
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002419 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002420 {
2421 pos_T *pos;
2422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002423 // Check that the block the match is in doesn't end before the
2424 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002425 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2426 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2427 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002428 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002429 // There can't be a useful match before the end of this block.
2430 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002431 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002432 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002433 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002434 }
2435
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002436 if (t == FAIL)
2437 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002439 if (found_pos.lnum != 0)
2440 {
2441 curwin->w_cursor = found_pos;
2442 t = OK;
2443 }
2444 break;
2445 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002446 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002448 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002449 ++curwin->w_cursor.lnum;
2450 curwin->w_cursor.col = 0;
2451 continue;
2452 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002453 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 // If the current position is not a valid identifier and a previous
2456 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002457 if (!valid && found_pos.lnum != 0)
2458 {
2459 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002460 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002461 }
2462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002463 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002464 if (valid && !locally)
2465 break;
2466 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002469 if (found_pos.lnum != 0)
2470 curwin->w_cursor = found_pos;
2471 break;
2472 }
2473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002474 // For finding a local variable and the match is before the "{" or
2475 // inside a comment, continue searching. For K&R style function
2476 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002477 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002478 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002479 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002480 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // Remove SEARCH_START from flags to avoid getting stuck at one
2482 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002483 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002485
2486 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002488 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 curwin->w_cursor = old_pos;
2490 }
2491 else
2492 {
2493 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002494 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 reset_search_dir();
2496 }
2497
2498 vim_free(pat);
2499 p_ws = save_p_ws;
2500 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002501
2502 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503}
2504
2505/*
2506 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2507 * lines rather than lines in the file.
2508 * 'dist' must be positive.
2509 *
2510 * Return OK if able to move cursor, FAIL otherwise.
2511 */
2512 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002513nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 int linelen = linetabsize(ml_get_curline());
2516 int retval = OK;
2517 int atend = FALSE;
2518 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002519 int col_off1; // margin offset for first screen line
2520 int col_off2; // margin offset for wrapped screen line
2521 int width1; // text width for first screen line
2522 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002525 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 col_off1 = curwin_col_off();
2528 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002529 width1 = curwin->w_width - col_off1;
2530 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002531 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002532 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /*
2537 * Instead of sticking at the last character of the buffer line we
2538 * try to stick in the last column of the screen.
2539 */
2540 if (curwin->w_curswant == MAXCOL)
2541 {
2542 atend = TRUE;
2543 validate_virtcol();
2544 if (width1 <= 0)
2545 curwin->w_curswant = 0;
2546 else
2547 {
2548 curwin->w_curswant = width1 - 1;
2549 if (curwin->w_virtcol > curwin->w_curswant)
2550 curwin->w_curswant += ((curwin->w_virtcol
2551 - curwin->w_curswant - 1) / width2 + 1) * width2;
2552 }
2553 }
2554 else
2555 {
2556 if (linelen > width1)
2557 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2558 else
2559 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002560 if (curwin->w_curswant >= (colnr_T)n)
2561 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563
2564 while (dist--)
2565 {
2566 if (dir == BACKWARD)
2567 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002568 if ((long)curwin->w_curswant >= width1
2569#ifdef FEAT_FOLDING
2570 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2571#endif
2572 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002573 // Move back within the line. This can give a negative value
2574 // for w_curswant if width1 < width2 (with cpoptions+=n),
2575 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 curwin->w_curswant -= width2;
2577 else
2578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002579 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002581 // Move to the start of a closed fold. Don't do that when
2582 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (!(fdo_flags & FDO_ALL))
2584 (void)hasFolding(curwin->w_cursor.lnum,
2585 &curwin->w_cursor.lnum, NULL);
2586#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002587 if (curwin->w_cursor.lnum == 1)
2588 {
2589 retval = FAIL;
2590 break;
2591 }
2592 --curwin->w_cursor.lnum;
2593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 linelen = linetabsize(ml_get_curline());
2595 if (linelen > width1)
2596 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2597 + 1) * width2;
2598 }
2599 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002600 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
2602 if (linelen > width1)
2603 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2604 else
2605 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002606 if (curwin->w_curswant + width2 < (colnr_T)n
2607#ifdef FEAT_FOLDING
2608 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2609#endif
2610 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002611 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 curwin->w_curswant += width2;
2613 else
2614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002615 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002617 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2619 &curwin->w_cursor.lnum);
2620#endif
2621 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2622 {
2623 retval = FAIL;
2624 break;
2625 }
2626 curwin->w_cursor.lnum++;
2627 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002628 // Check if the cursor has moved below the number display
2629 // when width1 < width2 (with cpoptions+=n). Subtract width2
2630 // to get a negative value for w_curswant, which will get
2631 // clipped to column 0.
2632 if (curwin->w_curswant >= width1)
2633 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002634 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636 }
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002640 if (virtual_active() && atend)
2641 coladvance(MAXCOL);
2642 else
2643 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2646 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002647 colnr_T virtcol;
2648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 /*
2650 * Check for landing on a character that got split at the end of the
2651 * last line. We want to advance a screenline, not end up in the same
2652 * screenline or move two screenlines.
2653 */
2654 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002655 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002656#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002657 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2658 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002659#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002660
2661 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 && (curwin->w_curswant < (colnr_T)width1
2663 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2664 : ((curwin->w_curswant - width1) % width2
2665 > (colnr_T)width2 / 2)))
2666 --curwin->w_cursor.col;
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
2669 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 return retval;
2673}
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675/*
2676 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2677 * cap->arg must be TRUE for CTRL-E.
2678 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002680nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 if (!checkclearop(cap->oap))
2683 scroll_redraw(cap->arg, cap->count1);
2684}
2685
2686/*
2687 * Scroll "count" lines up or down, and redraw.
2688 */
2689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 linenr_T prev_topline = curwin->w_topline;
2693#ifdef FEAT_DIFF
2694 int prev_topfill = curwin->w_topfill;
2695#endif
2696 linenr_T prev_lnum = curwin->w_cursor.lnum;
2697
2698 if (up)
2699 scrollup(count, TRUE);
2700 else
2701 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002702 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2705 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 cursor_correct();
2707 check_cursor_moved(curwin);
2708 curwin->w_valid |= VALID_TOPLINE;
2709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002710 // If moved back to where we were, at least move the cursor, otherwise
2711 // we get stuck at one position. Don't move the cursor up if the
2712 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 while (curwin->w_topline == prev_topline
2714#ifdef FEAT_DIFF
2715 && curwin->w_topfill == prev_topfill
2716#endif
2717 )
2718 {
2719 if (up)
2720 {
2721 if (curwin->w_cursor.lnum > prev_lnum
2722 || cursor_down(1L, FALSE) == FAIL)
2723 break;
2724 }
2725 else
2726 {
2727 if (curwin->w_cursor.lnum < prev_lnum
2728 || prev_topline == 1L
2729 || cursor_up(1L, FALSE) == FAIL)
2730 break;
2731 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // Mark w_topline as valid, otherwise the screen jumps back at the
2733 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 check_cursor_moved(curwin);
2735 curwin->w_valid |= VALID_TOPLINE;
2736 }
2737 }
2738 if (curwin->w_cursor.lnum != prev_lnum)
2739 coladvance(curwin->w_curswant);
2740 redraw_later(VALID);
2741}
2742
2743/*
2744 * Commands that start with "z".
2745 */
2746 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002747nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
2749 long n;
2750 colnr_T col;
2751 int nchar = cap->nchar;
2752#ifdef FEAT_FOLDING
2753 long old_fdl = curwin->w_p_fdl;
2754 int old_fen = curwin->w_p_fen;
2755#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002756#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002757 int undo = FALSE;
2758#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002759 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 if (VIM_ISDIGIT(nchar))
2762 {
2763 /*
2764 * "z123{nchar}": edit the count before obtaining {nchar}
2765 */
2766 if (checkclearop(cap->oap))
2767 return;
2768 n = nchar - '0';
2769 for (;;)
2770 {
2771#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
2774 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002776 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 --no_mapping;
2779 --allow_keys;
2780#ifdef FEAT_CMDL_INFO
2781 (void)add_to_showcmd(nchar);
2782#endif
2783 if (nchar == K_DEL || nchar == K_KDEL)
2784 n /= 10;
2785 else if (VIM_ISDIGIT(nchar))
2786 n = n * 10 + (nchar - '0');
2787 else if (nchar == CAR)
2788 {
2789#ifdef FEAT_GUI
2790 need_mouse_correct = TRUE;
2791#endif
2792 win_setheight((int)n);
2793 break;
2794 }
2795 else if (nchar == 'l'
2796 || nchar == 'h'
2797 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002798 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
2800 cap->count1 = n ? n * cap->count1 : cap->count1;
2801 goto dozet;
2802 }
2803 else
2804 {
2805 clearopbeep(cap->oap);
2806 break;
2807 }
2808 }
2809 cap->oap->op_type = OP_NOP;
2810 return;
2811 }
2812
2813dozet:
2814 if (
2815#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2817 // and "zC" only in Visual mode. "zj" and "zk" are motion
2818 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 cap->nchar != 'f' && cap->nchar != 'F'
2820 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2821 && cap->nchar != 'j' && cap->nchar != 'k'
2822 &&
2823#endif
2824 checkclearop(cap->oap))
2825 return;
2826
2827 /*
2828 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2829 * If line number given, set cursor.
2830 */
2831 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2832 && cap->count0
2833 && cap->count0 != curwin->w_cursor.lnum)
2834 {
2835 setpcmark();
2836 if (cap->count0 > curbuf->b_ml.ml_line_count)
2837 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2838 else
2839 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002840 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
2842
2843 switch (nchar)
2844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case '+':
2847 if (cap->count0 == 0)
2848 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // No count given: put cursor at the line below screen
2850 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2852 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2853 else
2854 curwin->w_cursor.lnum = curwin->w_botline;
2855 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 case NL:
2858 case CAR:
2859 case K_KENTER:
2860 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 case 't': scroll_cursor_top(0, TRUE);
2864 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002865 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 break;
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 case 'z': scroll_cursor_halfway(TRUE);
2873 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002874 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "z^", "z-" and "zb": put cursor at bottom of screen
2878 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2879 // when <count> is at bottom of window, and puts that one at
2880 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (cap->count0 != 0)
2882 {
2883 scroll_cursor_bot(0, TRUE);
2884 curwin->w_cursor.lnum = curwin->w_topline;
2885 }
2886 else if (curwin->w_topline == 1)
2887 curwin->w_cursor.lnum = 1;
2888 else
2889 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 case '-':
2892 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
2895 case 'b': scroll_cursor_bot(0, TRUE);
2896 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002897 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 break;
2899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002902 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'h':
2907 case K_LEFT:
2908 if (!curwin->w_p_wrap)
2909 {
2910 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2911 curwin->w_leftcol = 0;
2912 else
2913 curwin->w_leftcol -= (colnr_T)cap->count1;
2914 leftcol_changed();
2915 }
2916 break;
2917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002919 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 case 'l':
2924 case K_RIGHT:
2925 if (!curwin->w_p_wrap)
2926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 curwin->w_leftcol += (colnr_T)cap->count1;
2929 leftcol_changed();
2930 }
2931 break;
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 case 's': if (!curwin->w_p_wrap)
2935 {
2936#ifdef FEAT_FOLDING
2937 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 else
2940#endif
2941 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002942 if ((long)col > siso)
2943 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 else
2945 col = 0;
2946 if (curwin->w_leftcol != col)
2947 {
2948 curwin->w_leftcol = col;
2949 redraw_later(NOT_VALID);
2950 }
2951 }
2952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case 'e': if (!curwin->w_p_wrap)
2956 {
2957#ifdef FEAT_FOLDING
2958 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 else
2961#endif
2962 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002963 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002964 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 col = 0;
2966 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002967 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (curwin->w_leftcol != col)
2969 {
2970 curwin->w_leftcol = col;
2971 redraw_later(NOT_VALID);
2972 }
2973 }
2974 break;
2975
2976#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 // "zF": create fold command
2978 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 case 'F':
2980 case 'f': if (foldManualAllowed(TRUE))
2981 {
2982 cap->nchar = 'f';
2983 nv_operator(cap);
2984 curwin->w_p_fen = TRUE;
2985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2988 {
2989 nv_operator(cap);
2990 finish_op = TRUE;
2991 }
2992 }
2993 else
2994 clearopbeep(cap->oap);
2995 break;
2996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // "zd": delete fold at cursor
2998 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 case 'd':
3000 case 'D': if (foldManualAllowed(FALSE))
3001 {
3002 if (VIsual_active)
3003 nv_operator(cap);
3004 else
3005 deleteFold(curwin->w_cursor.lnum,
3006 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3007 }
3008 break;
3009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 case 'E': if (foldmethodIsManual(curwin))
3012 {
3013 clearFolding(curwin);
3014 changed_window_setting();
3015 }
3016 else if (foldmethodIsMarker(curwin))
3017 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3018 TRUE, FALSE);
3019 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003020 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 break;
3022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 case 'n': curwin->w_p_fen = FALSE;
3025 break;
3026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 case 'N': curwin->w_p_fen = TRUE;
3029 break;
3030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003031 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3033 break;
3034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003035 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3037 openFold(curwin->w_cursor.lnum, cap->count1);
3038 else
3039 {
3040 closeFold(curwin->w_cursor.lnum, cap->count1);
3041 curwin->w_p_fen = TRUE;
3042 }
3043 break;
3044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3047 openFoldRecurse(curwin->w_cursor.lnum);
3048 else
3049 {
3050 closeFoldRecurse(curwin->w_cursor.lnum);
3051 curwin->w_p_fen = TRUE;
3052 }
3053 break;
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 case 'o': if (VIsual_active)
3057 nv_operator(cap);
3058 else
3059 openFold(curwin->w_cursor.lnum, cap->count1);
3060 break;
3061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003062 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 case 'O': if (VIsual_active)
3064 nv_operator(cap);
3065 else
3066 openFoldRecurse(curwin->w_cursor.lnum);
3067 break;
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 case 'c': if (VIsual_active)
3071 nv_operator(cap);
3072 else
3073 closeFold(curwin->w_cursor.lnum, cap->count1);
3074 curwin->w_p_fen = TRUE;
3075 break;
3076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003077 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 case 'C': if (VIsual_active)
3079 nv_operator(cap);
3080 else
3081 closeFoldRecurse(curwin->w_cursor.lnum);
3082 curwin->w_p_fen = TRUE;
3083 break;
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 case 'v': foldOpenCursor();
3087 break;
3088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 curwin->w_foldinvalid = TRUE; // recompute folds
3092 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 foldOpenCursor();
3094 break;
3095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 curwin->w_foldinvalid = TRUE; // recompute folds
3099 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 break;
3101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003104 {
3105 curwin->w_p_fdl -= cap->count1;
3106 if (curwin->w_p_fdl < 0)
3107 curwin->w_p_fdl = 0;
3108 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 curwin->w_p_fen = TRUE;
3111 break;
3112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003113 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 curwin->w_p_fen = TRUE;
3117 break;
3118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003119 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003120 case 'r': curwin->w_p_fdl += cap->count1;
3121 {
3122 int d = getDeepestNesting();
3123
3124 if (curwin->w_p_fdl >= d)
3125 curwin->w_p_fdl = d;
3126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
3128
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003131 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 break;
3133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 case 'j': // "zj" move to next fold downwards
3135 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3137 cap->count1) == FAIL)
3138 clearopbeep(cap->oap);
3139 break;
3140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003141#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003143#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003144 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003145 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003147 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003148 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003149 --no_mapping;
3150 --allow_keys;
3151#ifdef FEAT_CMDL_INFO
3152 (void)add_to_showcmd(nchar);
3153#endif
3154 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3155 {
3156 clearopbeep(cap->oap);
3157 break;
3158 }
3159 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003161
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 case 'g': // "zg": add good word to word list
3163 case 'w': // "zw": add wrong word to word list
3164 case 'G': // "zG": add good word to temp word list
3165 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003166 {
3167 char_u *ptr = NULL;
3168 int len;
3169
3170 if (checkclearop(cap->oap))
3171 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003172 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3173 == FAIL)
3174 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003175 if (ptr == NULL)
3176 {
3177 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003178
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003179 // Find bad word under the cursor. When 'spell' is
3180 // off this fails and find_ident_under_cursor() is
3181 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003182 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003183 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003184 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003185 if (len != 0 && curwin->w_cursor.col <= pos.col)
3186 ptr = ml_get_pos(&curwin->w_cursor);
3187 curwin->w_cursor = pos;
3188 }
3189
Bram Moolenaarb765d632005-06-07 21:00:02 +00003190 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3191 FIND_IDENT)) == 0)
3192 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003193 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3194 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003195 (nchar == 'G' || nchar == 'W')
3196 ? 0 : (int)cap->count1,
3197 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003198 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003199 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003202 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003203 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003204 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003205#endif
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 default: clearopbeep(cap->oap);
3208 }
3209
3210#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (old_fen != curwin->w_p_fen)
3213 {
3214# ifdef FEAT_DIFF
3215 win_T *wp;
3216
3217 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003219 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 FOR_ALL_WINDOWS(wp)
3221 {
3222 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3223 {
3224 wp->w_p_fen = curwin->w_p_fen;
3225 changed_window_setting_win(wp);
3226 }
3227 }
3228 }
3229# endif
3230 changed_window_setting();
3231 }
3232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 if (old_fdl != curwin->w_p_fdl)
3235 newFoldLevel();
3236#endif
3237}
3238
3239#ifdef FEAT_GUI
3240/*
3241 * Vertical scrollbar movement.
3242 */
3243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003244nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 if (cap->oap->op_type != OP_NOP)
3247 clearopbeep(cap->oap);
3248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003249 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 gui_do_scroll();
3251}
3252
3253/*
3254 * Horizontal scrollbar movement.
3255 */
3256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003257nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 if (cap->oap->op_type != OP_NOP)
3260 clearopbeep(cap->oap);
3261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003262 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003263 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265#endif
3266
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003267#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003268/*
3269 * Click in GUI tab.
3270 */
3271 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003272nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003273{
3274 if (cap->oap->op_type != OP_NOP)
3275 clearopbeep(cap->oap);
3276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003277 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003278 goto_tabpage(current_tab);
3279}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003280
3281/*
3282 * Selected item in tab line menu.
3283 */
3284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003285nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003286{
3287 if (cap->oap->op_type != OP_NOP)
3288 clearopbeep(cap->oap);
3289
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003291 handle_tabmenu();
3292}
3293
3294/*
3295 * Handle selecting an item of the GUI tab line menu.
3296 * Used in Normal and Insert mode.
3297 */
3298 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003300{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003301 switch (current_tabmenu)
3302 {
3303 case TABLINE_MENU_CLOSE:
3304 if (current_tab == 0)
3305 do_cmdline_cmd((char_u *)"tabclose");
3306 else
3307 {
3308 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3309 current_tab);
3310 do_cmdline_cmd(IObuff);
3311 }
3312 break;
3313
3314 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003315 if (current_tab == 0)
3316 do_cmdline_cmd((char_u *)"$tabnew");
3317 else
3318 {
3319 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3320 current_tab - 1);
3321 do_cmdline_cmd(IObuff);
3322 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003323 break;
3324
3325 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003326 if (current_tab == 0)
3327 do_cmdline_cmd((char_u *)"browse $tabnew");
3328 else
3329 {
3330 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3331 current_tab - 1);
3332 do_cmdline_cmd(IObuff);
3333 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003334 break;
3335 }
3336}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003337#endif
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339/*
3340 * "Q" command.
3341 */
3342 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003343nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 /*
3346 * Ignore 'Q' in Visual mode, just give a beep.
3347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003349 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003350 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 do_exmode(FALSE);
3352}
3353
3354/*
3355 * Handle a ":" command.
3356 */
3357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003358nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003360 int old_p_im;
3361 int cmd_result;
3362 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar957cf672020-11-12 14:21:06 +01003364 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 nv_operator(cap);
3366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 if (cap->oap->op_type != OP_NOP)
3369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003370 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 cap->oap->motion_type = MCHAR;
3372 cap->oap->inclusive = FALSE;
3373 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003374 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003376 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 stuffcharReadbuff('.');
3378 if (cap->count0 > 1)
3379 {
3380 stuffReadbuff((char_u *)",.+");
3381 stuffnumReadbuff((long)cap->count0 - 1L);
3382 }
3383 }
3384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003385 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (KeyTyped)
3387 compute_cmdrow();
3388
3389 old_p_im = p_im;
3390
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003391 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003392 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003395 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (p_im != old_p_im)
3397 {
3398 if (p_im)
3399 restart_edit = 'i';
3400 else
3401 restart_edit = 0;
3402 }
3403
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003404 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003405 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003406 clearop(cap->oap);
3407 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3409 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003410 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3411 || did_emsg
3412 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003413 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 clearopbeep(cap->oap);
3415 }
3416}
3417
3418/*
3419 * Handle CTRL-G command.
3420 */
3421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003422nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003424 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
3426 VIsual_select = !VIsual_select;
3427 showmode();
3428 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003429 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003430 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 fileinfo((int)cap->count0, FALSE, TRUE);
3432}
3433
3434/*
3435 * Handle CTRL-H <Backspace> command.
3436 */
3437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003438nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (VIsual_active && VIsual_select)
3441 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 v_visop(cap);
3444 }
3445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 nv_left(cap);
3447}
3448
3449/*
3450 * CTRL-L: clear screen and redraw.
3451 */
3452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003453nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 if (!checkclearop(cap->oap))
3456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003459 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003460# ifdef FEAT_RELTIME
3461 {
3462 win_T *wp;
3463
3464 FOR_ALL_WINDOWS(wp)
3465 wp->w_s->b_syn_slow = FALSE;
3466 }
3467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#endif
3469 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003470#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3471# ifdef VIMDLL
3472 if (!gui.in_use)
3473# endif
3474 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477}
3478
3479/*
3480 * CTRL-O: In Select mode: switch to Visual mode for one command.
3481 * Otherwise: Go to older pcmark.
3482 */
3483 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003484nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (VIsual_active && VIsual_select)
3487 {
3488 VIsual_select = FALSE;
3489 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
3494 cap->count1 = -cap->count1;
3495 nv_pcmark(cap);
3496 }
3497}
3498
3499/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003500 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3501 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
3503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003504nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506 if (!checkclearopq(cap->oap))
3507 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3508 GETF_SETMARK|GETF_ALT, FALSE);
3509}
3510
3511/*
3512 * "Z" commands.
3513 */
3514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003515nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 if (!checkclearopq(cap->oap))
3518 {
3519 switch (cap->nchar)
3520 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003521 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 case 'Z': do_cmdline_cmd((char_u *)"x");
3523 break;
3524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003525 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 case 'Q': do_cmdline_cmd((char_u *)"q!");
3527 break;
3528
3529 default: clearopbeep(cap->oap);
3530 }
3531 }
3532}
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534/*
3535 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3536 */
3537 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003538do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
3540 oparg_T oa;
3541 cmdarg_T ca;
3542
3543 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003544 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 ca.oap = &oa;
3546 ca.cmdchar = c1;
3547 ca.nchar = c2;
3548 nv_ident(&ca);
3549}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551/*
3552 * Handle the commands that use the word under the cursor.
3553 * [g] CTRL-] :ta to current identifier
3554 * [g] 'K' run program for current identifier
3555 * [g] '*' / to current identifier or string
3556 * [g] '#' ? to current identifier or string
3557 * g ']' :tselect for current identifier
3558 */
3559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003560nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561{
3562 char_u *ptr = NULL;
3563 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003564 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003565 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 char_u *kp; // value of 'keywordprg'
3568 int kp_help; // 'keywordprg' is ":he"
3569 int kp_ex; // 'keywordprg' starts with ":"
3570 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003573 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 char_u *aux_ptr;
3575 int isman;
3576 int isman_s;
3577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 cmdchar = cap->nchar;
3581 g_cmd = TRUE;
3582 }
3583 else
3584 {
3585 cmdchar = cap->cmdchar;
3586 g_cmd = FALSE;
3587 }
3588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003589 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 cmdchar = '#';
3591
3592 /*
3593 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3594 */
3595 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3598 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (checkclearopq(cap->oap))
3600 return;
3601 }
3602
3603 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3604 (cmdchar == '*' || cmdchar == '#')
3605 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3606 {
3607 clearop(cap->oap);
3608 return;
3609 }
3610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003611 // Allocate buffer to put the command in. Inserting backslashes can
3612 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3613 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3615 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3616 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003617 if (kp_help && *skipwhite(ptr) == NUL)
3618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003619 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003620 return;
3621 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003622 kp_ex = (*kp == ':');
3623 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3624 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (buf == NULL)
3626 return;
3627 buf[0] = NUL;
3628
3629 switch (cmdchar)
3630 {
3631 case '*':
3632 case '#':
3633 /*
3634 * Put cursor at start of word, makes search skip the word
3635 * under the cursor.
3636 * Call setpcmark() first, so "*``" puts the cursor back where
3637 * it was.
3638 */
3639 setpcmark();
3640 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3641
3642 if (!g_cmd && vim_iswordp(ptr))
3643 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 break;
3646
3647 case 'K':
3648 if (kp_help)
3649 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003650 else if (kp_ex)
3651 {
3652 if (cap->count0 != 0)
3653 vim_snprintf((char *)buf, buflen, "%s %ld",
3654 kp, cap->count0);
3655 else
3656 STRCPY(buf, kp);
3657 STRCAT(buf, " ");
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 else
3660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 // An external command will probably use an argument starting
3662 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003663 while (*ptr == '-' && n > 0)
3664 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003665 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003666 --n;
3667 }
3668 if (n == 0)
3669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003670 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003671 vim_free(buf);
3672 return;
3673 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003674
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003675 // When a count is given, turn it into a range. Is this
3676 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 isman = (STRCMP(kp, "man") == 0);
3678 isman_s = (STRCMP(kp, "man -s") == 0);
3679 if (cap->count0 != 0 && !(isman || isman_s))
3680 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3681
3682 STRCAT(buf, "! ");
3683 if (cap->count0 == 0 && isman_s)
3684 STRCAT(buf, "man");
3685 else
3686 STRCAT(buf, kp);
3687 STRCAT(buf, " ");
3688 if (cap->count0 != 0 && (isman || isman_s))
3689 {
3690 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3691 STRCAT(buf, " ");
3692 }
3693 }
3694 break;
3695
3696 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003697 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698#ifdef FEAT_CSCOPE
3699 if (p_cst)
3700 STRCPY(buf, "cstag ");
3701 else
3702#endif
3703 STRCPY(buf, "ts ");
3704 break;
3705
3706 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003707 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (curbuf->b_help)
3709 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003711 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003712 if (g_cmd)
3713 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003714 else if (cap->count0 == 0)
3715 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003716 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003717 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720
3721 /*
3722 * Now grab the chars in the identifier
3723 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003724 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003726 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003727 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003728 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003729 p = vim_strsave_fnameescape(ptr, FALSE);
3730 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003731 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003732 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003733 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003734 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003736 vim_free(buf);
3737 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003739 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003740 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003741 {
3742 vim_free(buf);
3743 vim_free(p);
3744 return;
3745 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003746 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003747 STRCAT(buf, p);
3748 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003750 else
3751 {
3752 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003753 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003754 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003755 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003756 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003757 {
3758 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003760 aux_ptr = (char_u *)"";
3761 else
3762 aux_ptr = (char_u *)"\\|\"\n[";
3763 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003764 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003765 aux_ptr = (char_u *)"\\|\"\n*?[";
3766
3767 p = buf + STRLEN(buf);
3768 while (n-- > 0)
3769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003770 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003771 if (vim_strchr(aux_ptr, *ptr) != NULL)
3772 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 // When current byte is a part of multibyte character, copy all
3774 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003775 if (has_mbyte)
3776 {
3777 int i;
3778 int len = (*mb_ptr2len)(ptr) - 1;
3779
3780 for (i = 0; i < len && n >= 1; ++i, --n)
3781 *p++ = *ptr++;
3782 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003783 *p++ = *ptr++;
3784 }
3785 *p = NUL;
3786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 /*
3789 * Execute the command.
3790 */
3791 if (cmdchar == '*' || cmdchar == '#')
3792 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003793 if (!g_cmd && (has_mbyte
3794 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3795 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003797
3798 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003799 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003801
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003802 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003805 {
3806 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003808 g_tag_at_cursor = FALSE;
3809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 vim_free(buf);
3812}
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814/*
3815 * Get visually selected text, within one line only.
3816 * Returns FAIL if more than one line selected.
3817 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003819get_visual_text(
3820 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003821 char_u **pp, // return: start of selected text
3822 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 if (VIsual_mode != 'V')
3825 unadjust_for_sel();
3826 if (VIsual.lnum != curwin->w_cursor.lnum)
3827 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003828 if (cap != NULL)
3829 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return FAIL;
3831 }
3832 if (VIsual_mode == 'V')
3833 {
3834 *pp = ml_get_curline();
3835 *lenp = (int)STRLEN(*pp);
3836 }
3837 else
3838 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003839 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 {
3841 *pp = ml_get_pos(&curwin->w_cursor);
3842 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3843 }
3844 else
3845 {
3846 *pp = ml_get_pos(&VIsual);
3847 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003850 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003851 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853 reset_VIsual_and_resel();
3854 return OK;
3855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857/*
3858 * CTRL-T: backwards in tag stack
3859 */
3860 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003861nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 if (!checkclearopq(cap->oap))
3864 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3865}
3866
3867/*
3868 * Handle scrolling command 'H', 'L' and 'M'.
3869 */
3870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003871nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 int used = 0;
3874 long n;
3875#ifdef FEAT_FOLDING
3876 linenr_T lnum;
3877#endif
3878 int half;
3879
3880 cap->oap->motion_type = MLINE;
3881 setpcmark();
3882
3883 if (cap->cmdchar == 'L')
3884 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003885 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 curwin->w_cursor.lnum = curwin->w_botline - 1;
3887 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3888 curwin->w_cursor.lnum = 1;
3889 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003890 {
3891#ifdef FEAT_FOLDING
3892 if (hasAnyFolding(curwin))
3893 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003894 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003895 for (n = cap->count1 - 1; n > 0
3896 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3897 {
3898 (void)hasFolding(curwin->w_cursor.lnum,
3899 &curwin->w_cursor.lnum, NULL);
3900 --curwin->w_cursor.lnum;
3901 }
3902 }
3903 else
3904#endif
3905 curwin->w_cursor.lnum -= cap->count1 - 1;
3906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 else
3909 {
3910 if (cap->cmdchar == 'M')
3911 {
3912#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003913 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 used -= diff_check_fill(curwin, curwin->w_topline)
3915 - curwin->w_topfill;
3916#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003917 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3919 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3920 {
3921#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 // Count half he number of filler lines to be "below this
3923 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3925 + n) / 2 >= half)
3926 {
3927 --n;
3928 break;
3929 }
3930#endif
3931 used += plines(curwin->w_topline + n);
3932 if (used >= half)
3933 break;
3934#ifdef FEAT_FOLDING
3935 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3936 n = lnum - curwin->w_topline;
3937#endif
3938 }
3939 if (n > 0 && used > curwin->w_height)
3940 --n;
3941 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003942 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003945#ifdef FEAT_FOLDING
3946 if (hasAnyFolding(curwin))
3947 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003948 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003949 lnum = curwin->w_topline;
3950 while (n-- > 0 && lnum < curwin->w_botline - 1)
3951 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003952 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003953 ++lnum;
3954 }
3955 n = lnum - curwin->w_topline;
3956 }
3957#endif
3958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 curwin->w_cursor.lnum = curwin->w_topline + n;
3960 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3961 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3962 }
3963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003964 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003965 if (cap->oap->op_type == OP_NOP)
3966 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 beginline(BL_SOL | BL_FIX);
3968}
3969
3970/*
3971 * Cursor right commands.
3972 */
3973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003974nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003977 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003979 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003981 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 if (mod_mask & MOD_MASK_CTRL)
3983 cap->arg = TRUE;
3984 nv_wordcmd(cap);
3985 return;
3986 }
3987
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 cap->oap->motion_type = MCHAR;
3989 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003990 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003993 * In virtual edit mode, there's no such thing as "past_line", as lines
3994 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 */
3996 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003997 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 for (n = cap->count1; n > 0; --n)
4000 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004001 if ((!past_line && oneright() == FAIL)
4002 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004003 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004006 * <Space> wraps to next line if 'whichwrap' has 's'.
4007 * 'l' wraps to next line if 'whichwrap' has 'l'.
4008 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
4010 if ( ((cap->cmdchar == ' '
4011 && vim_strchr(p_ww, 's') != NULL)
4012 || (cap->cmdchar == 'l'
4013 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004014 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 && vim_strchr(p_ww, '>') != NULL))
4016 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // When deleting we also count the NL as a character.
4019 // Set cap->oap->inclusive when last char in the line is
4020 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004021 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004023 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 cap->oap->inclusive = TRUE;
4025 else
4026 {
4027 ++curwin->w_cursor.lnum;
4028 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 curwin->w_set_curswant = TRUE;
4031 cap->oap->inclusive = FALSE;
4032 }
4033 continue;
4034 }
4035 if (cap->oap->op_type == OP_NOP)
4036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004037 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (n == cap->count1)
4039 beep_flush();
4040 }
4041 else
4042 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004043 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 cap->oap->inclusive = TRUE;
4045 }
4046 break;
4047 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004048 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
4050 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if (virtual_active())
4052 oneright();
4053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004056 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 ++curwin->w_cursor.col;
4059 }
4060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062#ifdef FEAT_FOLDING
4063 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4064 && cap->oap->op_type == OP_NOP)
4065 foldOpenCursor();
4066#endif
4067}
4068
4069/*
4070 * Cursor left commands.
4071 *
4072 * Returns TRUE when operator end should not be adjusted.
4073 */
4074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004075nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 long n;
4078
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004079 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4080 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004082 if (mod_mask & MOD_MASK_CTRL)
4083 cap->arg = 1;
4084 nv_bck_word(cap);
4085 return;
4086 }
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 cap->oap->motion_type = MCHAR;
4089 cap->oap->inclusive = FALSE;
4090 for (n = cap->count1; n > 0; --n)
4091 {
4092 if (oneleft() == FAIL)
4093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004094 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4095 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4096 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if ( (((cap->cmdchar == K_BS
4098 || cap->cmdchar == Ctrl_H)
4099 && vim_strchr(p_ww, 'b') != NULL)
4100 || (cap->cmdchar == 'h'
4101 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004102 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 && vim_strchr(p_ww, '<') != NULL))
4104 && curwin->w_cursor.lnum > 1)
4105 {
4106 --(curwin->w_cursor.lnum);
4107 coladvance((colnr_T)MAXCOL);
4108 curwin->w_set_curswant = TRUE;
4109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004110 // When the NL before the first char has to be deleted we
4111 // put the cursor on the NUL after the previous line.
4112 // This is a very special case, be careful!
4113 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if ( (cap->oap->op_type == OP_DELETE
4115 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004116 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004118 char_u *cp = ml_get_cursor();
4119
4120 if (*cp != NUL)
4121 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004122 if (has_mbyte)
4123 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4124 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004125 ++curwin->w_cursor.col;
4126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 cap->retval |= CA_NO_ADJ_OP_END;
4128 }
4129 continue;
4130 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004131 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4133 beep_flush();
4134 break;
4135 }
4136 }
4137#ifdef FEAT_FOLDING
4138 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4139 && cap->oap->op_type == OP_NOP)
4140 foldOpenCursor();
4141#endif
4142}
4143
4144/*
4145 * Cursor up commands.
4146 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4147 */
4148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004149nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004151 if (mod_mask & MOD_MASK_SHIFT)
4152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004153 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004154 cap->arg = BACKWARD;
4155 nv_page(cap);
4156 }
4157 else
4158 {
4159 cap->oap->motion_type = MLINE;
4160 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4161 clearopbeep(cap->oap);
4162 else if (cap->arg)
4163 beginline(BL_WHITE | BL_FIX);
4164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165}
4166
4167/*
4168 * Cursor down commands.
4169 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4170 */
4171 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004172nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004174 if (mod_mask & MOD_MASK_SHIFT)
4175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004176 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004177 cap->arg = FORWARD;
4178 nv_page(cap);
4179 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004180#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004181 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004182 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4183 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004185 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004188 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004189 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 cmdwin_result = CAR;
4191 else
4192#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004193#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004194 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004195 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4196 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4197 {
4198 invoke_prompt_callback();
4199 if (restart_edit == 0)
4200 restart_edit = 'a';
4201 }
4202 else
4203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 cap->oap->motion_type = MLINE;
4206 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4207 clearopbeep(cap->oap);
4208 else if (cap->arg)
4209 beginline(BL_WHITE | BL_FIX);
4210 }
4211 }
4212}
4213
4214#ifdef FEAT_SEARCHPATH
4215/*
4216 * Grab the file name under the cursor and edit it.
4217 */
4218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004219nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004222 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004224 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004227 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return;
4229 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004230 if (curbuf_locked())
4231 {
4232 clearop(cap->oap);
4233 return;
4234 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004235#ifdef FEAT_PROP_POPUP
4236 if (ERROR_IF_TERM_POPUP_WINDOW)
4237 return;
4238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004240 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 if (ptr != NULL)
4243 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004244 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004245 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004246 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004248 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004249 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004250 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004251 {
4252 curwin->w_cursor.lnum = lnum;
4253 check_cursor_lnum();
4254 beginline(BL_SOL | BL_FIX);
4255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 vim_free(ptr);
4257 }
4258 else
4259 clearop(cap->oap);
4260}
4261#endif
4262
4263/*
4264 * <End> command: to end of current line or last line.
4265 */
4266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004267nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004269 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004271 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004273 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 nv_dollar(cap);
4276}
4277
4278/*
4279 * Handle the "$" command.
4280 */
4281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004282nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283{
4284 cap->oap->motion_type = MCHAR;
4285 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004286 // In virtual mode when off the edge of a line and an operator
4287 // is pending (whew!) keep the cursor where it is.
4288 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (!virtual_active() || gchar_cursor() != NUL
4290 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004291 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (cursor_down((long)(cap->count1 - 1),
4293 cap->oap->op_type == OP_NOP) == FAIL)
4294 clearopbeep(cap->oap);
4295#ifdef FEAT_FOLDING
4296 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4297 foldOpenCursor();
4298#endif
4299}
4300
4301/*
4302 * Implementation of '?' and '/' commands.
4303 * If cap->arg is TRUE don't set PC mark.
4304 */
4305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004306nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
4308 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004309 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4312 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004313 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 cap->cmdchar = 'g';
4315 cap->nchar = '?';
4316 nv_operator(cap);
4317 return;
4318 }
4319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004320 // When using 'incsearch' the cursor may be moved to set a different search
4321 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004322 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 if (cap->searchbuf == NULL)
4325 {
4326 clearop(oap);
4327 return;
4328 }
4329
Bram Moolenaar46539112015-02-17 15:43:57 +01004330 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004331 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004332 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333}
4334
4335/*
4336 * Handle "N" and "n" commands.
4337 * cap->arg is SEARCH_REV for "N", 0 for "n".
4338 */
4339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004340nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004342 pos_T old = curwin->w_cursor;
4343 int wrapped = FALSE;
4344 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004345
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004346 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004347 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004348 // Avoid getting stuck on the current cursor position, which can
4349 // happen when an offset is given and the cursor is on the last char
4350 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004351 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004352 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004353 cap->count1 -= 1;
4354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355}
4356
4357/*
4358 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4359 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004360 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004362 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004363normal_search(
4364 cmdarg_T *cap,
4365 int dir,
4366 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004367 int opt, // extra flags for do_search()
4368 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004371 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 cap->oap->motion_type = MCHAR;
4374 cap->oap->inclusive = FALSE;
4375 cap->oap->use_reg_one = TRUE;
4376 curwin->w_set_curswant = TRUE;
4377
Bram Moolenaara80faa82020-04-12 19:37:17 +02004378 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004379 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004380 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4381 if (wrapped != NULL)
4382 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (i == 0)
4384 clearop(cap->oap);
4385 else
4386 {
4387 if (i == 2)
4388 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_FOLDING
4391 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4392 foldOpenCursor();
4393#endif
4394 }
4395
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004396 // "/$" will put the cursor after the end of the line, may need to
4397 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004399 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400}
4401
4402/*
4403 * Character search commands.
4404 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4405 * ',' and FALSE for ';'.
4406 * cap->nchar is NUL for ',' and ';' (repeat the search)
4407 */
4408 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004409nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 int t_cmd;
4412
4413 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4414 t_cmd = TRUE;
4415 else
4416 t_cmd = FALSE;
4417
4418 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4420 clearopbeep(cap->oap);
4421 else
4422 {
4423 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004424 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4426 && (t_cmd || cap->oap->op_type != OP_NOP))
4427 {
4428 colnr_T scol, ecol;
4429
4430 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4431 curwin->w_cursor.coladd = ecol - scol;
4432 }
4433 else
4434 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#ifdef FEAT_FOLDING
4437 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4438 foldOpenCursor();
4439#endif
4440 }
4441}
4442
4443/*
4444 * "[" and "]" commands.
4445 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4446 */
4447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004448nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004450 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004452 pos_T *pos = NULL; // init for GCC
4453 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 int flag;
4455 long n;
4456 int findc;
4457 int c;
4458
4459 cap->oap->motion_type = MCHAR;
4460 cap->oap->inclusive = FALSE;
4461 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004462 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464#ifdef FEAT_SEARCHPATH
4465 /*
4466 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4467 */
4468 if (cap->nchar == 'f')
4469 nv_gotofile(cap);
4470 else
4471#endif
4472
4473#ifdef FEAT_FIND_ID
4474 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004475 * Find the occurrence(s) of the identifier or define under cursor
4476 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 *
4478 * search list jump
4479 * fwd bwd fwd bwd fwd bwd
4480 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4481 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4482 */
4483 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004484# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004486# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 cap->nchar) != NULL)
4490 {
4491 char_u *ptr;
4492 int len;
4493
4494 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4495 clearop(cap->oap);
4496 else
4497 {
4498 find_pattern_in_path(ptr, 0, len, TRUE,
4499 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4500 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4501 cap->count1,
4502 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4503 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4504 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4505 (linenr_T)MAXLNUM);
4506 curwin->w_set_curswant = TRUE;
4507 }
4508 }
4509 else
4510#endif
4511
4512 /*
4513 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4514 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4515 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4516 * "[m" or "]m" search for prev/next start of (Java) method.
4517 * "[M" or "]M" search for prev/next end of (Java) method.
4518 */
4519 if ( (cap->cmdchar == '['
4520 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4521 || (cap->cmdchar == ']'
4522 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4523 {
4524 if (cap->nchar == '*')
4525 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 prev_pos.lnum = 0;
4527 if (cap->nchar == 'm' || cap->nchar == 'M')
4528 {
4529 if (cap->cmdchar == '[')
4530 findc = '{';
4531 else
4532 findc = '}';
4533 n = 9999;
4534 }
4535 else
4536 {
4537 findc = cap->nchar;
4538 n = cap->count1;
4539 }
4540 for ( ; n > 0; --n)
4541 {
4542 if ((pos = findmatchlimit(cap->oap, findc,
4543 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4544 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004545 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 if (cap->nchar != 'm' && cap->nchar != 'M')
4548 clearopbeep(cap->oap);
4549 }
4550 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004551 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 break;
4553 }
4554 prev_pos = new_pos;
4555 curwin->w_cursor = *pos;
4556 new_pos = *pos;
4557 }
4558 curwin->w_cursor = old_pos;
4559
4560 /*
4561 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4562 * brought us to the match for "[m" and "]M" when inside a method.
4563 * Try finding the '{' or '}' we want to be at.
4564 * Also repeat for the given count.
4565 */
4566 if (cap->nchar == 'm' || cap->nchar == 'M')
4567 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004568 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 int norm = ((findc == '{') == (cap->nchar == 'm'));
4570
4571 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004572 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (prev_pos.lnum != 0)
4574 {
4575 pos = &prev_pos;
4576 curwin->w_cursor = prev_pos;
4577 if (norm)
4578 --n;
4579 }
4580 else
4581 pos = NULL;
4582 while (n > 0)
4583 {
4584 for (;;)
4585 {
4586 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4587 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004588 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (pos == NULL)
4590 clearopbeep(cap->oap);
4591 n = 0;
4592 break;
4593 }
4594 c = gchar_cursor();
4595 if (c == '{' || c == '}')
4596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 // Must have found end/start of class: use it.
4598 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if ((c == findc && norm) || (n == 1 && !norm))
4600 {
4601 new_pos = curwin->w_cursor;
4602 pos = &new_pos;
4603 n = 0;
4604 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 // if no match found at all, we started outside of the
4606 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 else if (new_pos.lnum == 0)
4608 {
4609 new_pos = curwin->w_cursor;
4610 pos = &new_pos;
4611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 else if ((pos = findmatchlimit(cap->oap, findc,
4614 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4615 0)) == NULL)
4616 n = 0;
4617 else
4618 curwin->w_cursor = *pos;
4619 break;
4620 }
4621 }
4622 --n;
4623 }
4624 curwin->w_cursor = old_pos;
4625 if (pos == NULL && new_pos.lnum != 0)
4626 clearopbeep(cap->oap);
4627 }
4628 if (pos != NULL)
4629 {
4630 setpcmark();
4631 curwin->w_cursor = *pos;
4632 curwin->w_set_curswant = TRUE;
4633#ifdef FEAT_FOLDING
4634 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4635 && cap->oap->op_type == OP_NOP)
4636 foldOpenCursor();
4637#endif
4638 }
4639 }
4640
4641 /*
4642 * "[[", "[]", "]]" and "][": move to start or end of function
4643 */
4644 else if (cap->nchar == '[' || cap->nchar == ']')
4645 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 flag = '{';
4648 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004649 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
4651 curwin->w_set_curswant = TRUE;
4652 /*
4653 * Imitate strange Vi behaviour: When using "]]" with an operator
4654 * we also stop at '}'.
4655 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004656 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 (cap->oap->op_type != OP_NOP
4658 && cap->arg == FORWARD && flag == '{')))
4659 clearopbeep(cap->oap);
4660 else
4661 {
4662 if (cap->oap->op_type == OP_NOP)
4663 beginline(BL_WHITE | BL_FIX);
4664#ifdef FEAT_FOLDING
4665 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4666 foldOpenCursor();
4667#endif
4668 }
4669 }
4670
4671 /*
4672 * "[p", "[P", "]P" and "]p": put with indent adjustment
4673 */
4674 else if (cap->nchar == 'p' || cap->nchar == 'P')
4675 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004676 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678
4679 /*
4680 * "['", "[`", "]'" and "]`": jump to next mark
4681 */
4682 else if (cap->nchar == '\'' || cap->nchar == '`')
4683 {
4684 pos = &curwin->w_cursor;
4685 for (n = cap->count1; n > 0; --n)
4686 {
4687 prev_pos = *pos;
4688 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4689 cap->nchar == '\'');
4690 if (pos == NULL)
4691 break;
4692 }
4693 if (pos == NULL)
4694 pos = &prev_pos;
4695 nv_cursormark(cap, cap->nchar == '\'', pos);
4696 }
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /*
4699 * [ or ] followed by a middle mouse click: put selected text with
4700 * indent adjustment. Any other button just does as usual.
4701 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004702 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 (void)do_mouse(cap->oap, cap->nchar,
4705 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4706 cap->count1, PUT_FIXINDENT);
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709#ifdef FEAT_FOLDING
4710 /*
4711 * "[z" and "]z": move to start or end of open fold.
4712 */
4713 else if (cap->nchar == 'z')
4714 {
4715 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4716 cap->count1) == FAIL)
4717 clearopbeep(cap->oap);
4718 }
4719#endif
4720
4721#ifdef FEAT_DIFF
4722 /*
4723 * "[c" and "]c": move to next or previous diff-change.
4724 */
4725 else if (cap->nchar == 'c')
4726 {
4727 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4728 cap->count1) == FAIL)
4729 clearopbeep(cap->oap);
4730 }
4731#endif
4732
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004733#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004734 /*
4735 * "[s", "[S", "]s" and "]S": move to next spell error.
4736 */
4737 else if (cap->nchar == 's' || cap->nchar == 'S')
4738 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004739 setpcmark();
4740 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004741 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4742 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004743 {
4744 clearopbeep(cap->oap);
4745 break;
4746 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004747 else
4748 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004749# ifdef FEAT_FOLDING
4750 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4751 foldOpenCursor();
4752# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004753 }
4754#endif
4755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004756 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 else
4758 clearopbeep(cap->oap);
4759}
4760
4761/*
4762 * Handle Normal mode "%" command.
4763 */
4764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004765nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
4767 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004768#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 linenr_T lnum = curwin->w_cursor.lnum;
4770#endif
4771
4772 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 if (cap->count0 > 100)
4776 clearopbeep(cap->oap);
4777 else
4778 {
4779 cap->oap->motion_type = MLINE;
4780 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004781 // Round up, so 'normal 100%' always jumps at the line line.
4782 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4783 // overflow on 32-bits, so use a formula with less accuracy
4784 // to avoid overflows.
4785 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4787 / 100L * cap->count0;
4788 else
4789 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4790 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004791 if (curwin->w_cursor.lnum < 1)
4792 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4794 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4795 beginline(BL_SOL | BL_FIX);
4796 }
4797 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 cap->oap->motion_type = MCHAR;
4801 cap->oap->use_reg_one = TRUE;
4802 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4803 clearopbeep(cap->oap);
4804 else
4805 {
4806 setpcmark();
4807 curwin->w_cursor = *pos;
4808 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 }
4813#ifdef FEAT_FOLDING
4814 if (cap->oap->op_type == OP_NOP
4815 && lnum != curwin->w_cursor.lnum
4816 && (fdo_flags & FDO_PERCENT)
4817 && KeyTyped)
4818 foldOpenCursor();
4819#endif
4820}
4821
4822/*
4823 * Handle "(" and ")" commands.
4824 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4825 */
4826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004827nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 cap->oap->motion_type = MCHAR;
4830 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004831 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004832 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 curwin->w_set_curswant = TRUE;
4834
4835 if (findsent(cap->arg, cap->count1) == FAIL)
4836 clearopbeep(cap->oap);
4837 else
4838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004839 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004840 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#ifdef FEAT_FOLDING
4843 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4844 foldOpenCursor();
4845#endif
4846 }
4847}
4848
4849/*
4850 * "m" command: Mark a position.
4851 */
4852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004853nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 if (!checkclearop(cap->oap))
4856 {
4857 if (setmark(cap->nchar) == FAIL)
4858 clearopbeep(cap->oap);
4859 }
4860}
4861
4862/*
4863 * "{" and "}" commands.
4864 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4865 */
4866 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
4869 cap->oap->motion_type = MCHAR;
4870 cap->oap->inclusive = FALSE;
4871 cap->oap->use_reg_one = TRUE;
4872 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004873 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 clearopbeep(cap->oap);
4875 else
4876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#ifdef FEAT_FOLDING
4879 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4880 foldOpenCursor();
4881#endif
4882 }
4883}
4884
4885/*
4886 * "u" command: Undo or make lower case.
4887 */
4888 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004891 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004893 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 cap->cmdchar = 'g';
4895 cap->nchar = 'u';
4896 nv_operator(cap);
4897 }
4898 else
4899 nv_kundo(cap);
4900}
4901
4902/*
4903 * <Undo> command.
4904 */
4905 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004906nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907{
4908 if (!checkclearopq(cap->oap))
4909 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004910#ifdef FEAT_JOB_CHANNEL
4911 if (bt_prompt(curbuf))
4912 {
4913 clearopbeep(cap->oap);
4914 return;
4915 }
4916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 u_undo((int)cap->count1);
4918 curwin->w_set_curswant = TRUE;
4919 }
4920}
4921
4922/*
4923 * Handle the "r" command.
4924 */
4925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004926nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
4928 char_u *ptr;
4929 int had_ctrl_v;
4930 long n;
4931
4932 if (checkclearop(cap->oap))
4933 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004934#ifdef FEAT_JOB_CHANNEL
4935 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4936 {
4937 clearopbeep(cap->oap);
4938 return;
4939 }
4940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004942 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (cap->nchar == Ctrl_V)
4944 {
4945 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004946 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004947 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (cap->nchar > DEL)
4949 had_ctrl_v = NUL;
4950 }
4951 else
4952 had_ctrl_v = NUL;
4953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004955 if (IS_SPECIAL(cap->nchar))
4956 {
4957 clearopbeep(cap->oap);
4958 return;
4959 }
4960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (VIsual_active)
4963 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004964 if (got_int)
4965 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004966 if (had_ctrl_v)
4967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004968 // Use a special (negative) number to make a difference between a
4969 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004970 if (cap->nchar == CAR)
4971 cap->nchar = REPLACE_CR_NCHAR;
4972 else if (cap->nchar == NL)
4973 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 nv_operator(cap);
4976 return;
4977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (virtual_active())
4981 {
4982 if (u_save_cursor() == FAIL)
4983 return;
4984 if (gchar_cursor() == NUL)
4985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004986 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 coladvance_force((colnr_T)(getviscol() + cap->count1));
4988 curwin->w_cursor.col -= cap->count1;
4989 }
4990 else if (gchar_cursor() == TAB)
4991 coladvance_force(getviscol());
4992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004994 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004996 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004997 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 clearopbeep(cap->oap);
5000 return;
5001 }
5002
5003 /*
5004 * Replacing with a TAB is done by edit() when it is complicated because
5005 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5006 * Other characters are done below to avoid problems with things like
5007 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5008 */
5009 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5010 {
5011 stuffnumReadbuff(cap->count1);
5012 stuffcharReadbuff('R');
5013 stuffcharReadbuff('\t');
5014 stuffcharReadbuff(ESC);
5015 return;
5016 }
5017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (u_save_cursor() == FAIL)
5020 return;
5021
5022 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5023 {
5024 /*
5025 * Replace character(s) by a single newline.
5026 * Strange vi behaviour: Only one newline is inserted.
5027 * Delete the characters here.
5028 * Insert the newline with an insert command, takes care of
5029 * autoindent. The insert command depends on being on the last
5030 * character of a line or not.
5031 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005032 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 stuffcharReadbuff('\r');
5034 stuffcharReadbuff(ESC);
5035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 invoke_edit(cap, TRUE, 'r', FALSE);
5038 }
5039 else
5040 {
5041 prep_redo(cap->oap->regname, cap->count1,
5042 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5043
5044 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (has_mbyte)
5046 {
5047 int old_State = State;
5048
5049 if (cap->ncharC1 != 0)
5050 AppendCharToRedobuff(cap->ncharC1);
5051 if (cap->ncharC2 != 0)
5052 AppendCharToRedobuff(cap->ncharC2);
5053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005054 // This is slow, but it handles replacing a single-byte with a
5055 // multi-byte and the other way around. Also handles adding
5056 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 for (n = cap->count1; n > 0; --n)
5058 {
5059 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005060 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5061 {
5062 int c = ins_copychar(curwin->w_cursor.lnum
5063 + (cap->nchar == Ctrl_Y ? -1 : 1));
5064 if (c != NUL)
5065 ins_char(c);
5066 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005067 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005068 ++curwin->w_cursor.col;
5069 }
5070 else
5071 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 State = old_State;
5073 if (cap->ncharC1 != 0)
5074 ins_char(cap->ncharC1);
5075 if (cap->ncharC2 != 0)
5076 ins_char(cap->ncharC2);
5077 }
5078 }
5079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 /*
5082 * Replace the characters within one line.
5083 */
5084 for (n = cap->count1; n > 0; --n)
5085 {
5086 /*
5087 * Get ptr again, because u_save and/or showmatch() will have
5088 * released the line. At the same time we let know that the
5089 * line will be changed.
5090 */
5091 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005092 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5093 {
5094 int c = ins_copychar(curwin->w_cursor.lnum
5095 + (cap->nchar == Ctrl_Y ? -1 : 1));
5096 if (c != NUL)
5097 ptr[curwin->w_cursor.col] = c;
5098 }
5099 else
5100 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (p_sm && msg_silent == 0)
5102 showmatch(cap->nchar);
5103 ++curwin->w_cursor.col;
5104 }
5105#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005106 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005108 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
Bram Moolenaar009b2592004-10-24 19:18:58 +00005110 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005111 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005113 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115#endif
5116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005117 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 changed_bytes(curwin->w_cursor.lnum,
5119 (colnr_T)(curwin->w_cursor.col - cap->count1));
5120 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005121 --curwin->w_cursor.col; // cursor on the last replaced char
5122 // if the character on the left of the current cursor is a multi-byte
5123 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (has_mbyte)
5125 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 curbuf->b_op_end = curwin->w_cursor;
5127 curwin->w_set_curswant = TRUE;
5128 set_last_insert(cap->nchar);
5129 }
5130}
5131
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132/*
5133 * 'o': Exchange start and end of Visual area.
5134 * 'O': same, but in block mode exchange left and right corners.
5135 */
5136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005137v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
5139 pos_T old_cursor;
5140 colnr_T left, right;
5141
5142 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5143 {
5144 old_cursor = curwin->w_cursor;
5145 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5146 curwin->w_cursor.lnum = VIsual.lnum;
5147 coladvance(left);
5148 VIsual = curwin->w_cursor;
5149
5150 curwin->w_cursor.lnum = old_cursor.lnum;
5151 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005152 // 'selection "exclusive" and cursor at right-bottom corner: move it
5153 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5155 ++curwin->w_curswant;
5156 coladvance(curwin->w_curswant);
5157 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005159 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 curwin->w_cursor.lnum = VIsual.lnum;
5162 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5163 ++right;
5164 coladvance(right);
5165 VIsual = curwin->w_cursor;
5166
5167 curwin->w_cursor.lnum = old_cursor.lnum;
5168 coladvance(left);
5169 curwin->w_curswant = left;
5170 }
5171 }
5172 else
5173 {
5174 old_cursor = curwin->w_cursor;
5175 curwin->w_cursor = VIsual;
5176 VIsual = old_cursor;
5177 curwin->w_set_curswant = TRUE;
5178 }
5179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181/*
5182 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5183 */
5184 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005185nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005187 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 cap->cmdchar = 'c';
5190 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005191 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 VIsual_mode = 'V';
5193 nv_operator(cap);
5194 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005195 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005198 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 else
5200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (virtual_active())
5202 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5204 }
5205 }
5206}
5207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208/*
5209 * "gr".
5210 */
5211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (VIsual_active)
5215 {
5216 cap->cmdchar = 'r';
5217 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005218 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005220 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005223 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 else
5225 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005226 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005227 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 stuffcharReadbuff(cap->extra_char);
5229 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (virtual_active())
5231 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 invoke_edit(cap, TRUE, 'v', FALSE);
5233 }
5234 }
5235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237/*
5238 * Swap case for "~" command, when it does not work like an operator.
5239 */
5240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005241n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242{
5243 long n;
5244 pos_T startpos;
5245 int did_change = 0;
5246#ifdef FEAT_NETBEANS_INTG
5247 pos_T pos;
5248 char_u *ptr;
5249 int count;
5250#endif
5251
5252 if (checkclearopq(cap->oap))
5253 return;
5254
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005255 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
5257 clearopbeep(cap->oap);
5258 return;
5259 }
5260
5261 prep_redo_cmd(cap);
5262
5263 if (u_save_cursor() == FAIL)
5264 return;
5265
5266 startpos = curwin->w_cursor;
5267#ifdef FEAT_NETBEANS_INTG
5268 pos = startpos;
5269#endif
5270 for (n = cap->count1; n > 0; --n)
5271 {
5272 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5273 inc_cursor();
5274 if (gchar_cursor() == NUL)
5275 {
5276 if (vim_strchr(p_ww, '~') != NULL
5277 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5278 {
5279#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005280 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 if (did_change)
5283 {
5284 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005285 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005286 netbeans_removed(curbuf, pos.lnum, pos.col,
5287 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005289 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 pos.col = 0;
5292 pos.lnum++;
5293 }
5294#endif
5295 ++curwin->w_cursor.lnum;
5296 curwin->w_cursor.col = 0;
5297 if (n > 1)
5298 {
5299 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5300 break;
5301 u_clearline();
5302 }
5303 }
5304 else
5305 break;
5306 }
5307 }
5308#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005309 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
5311 ptr = ml_get(pos.lnum);
5312 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005313 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5314 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316#endif
5317
5318
5319 check_cursor();
5320 curwin->w_set_curswant = TRUE;
5321 if (did_change)
5322 {
5323 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5324 0L);
5325 curbuf->b_op_start = startpos;
5326 curbuf->b_op_end = curwin->w_cursor;
5327 if (curbuf->b_op_end.col > 0)
5328 --curbuf->b_op_end.col;
5329 }
5330}
5331
5332/*
5333 * Move cursor to mark.
5334 */
5335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005336nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 if (check_mark(pos) == FAIL)
5339 clearop(cap->oap);
5340 else
5341 {
5342 if (cap->cmdchar == '\''
5343 || cap->cmdchar == '`'
5344 || cap->cmdchar == '['
5345 || cap->cmdchar == ']')
5346 setpcmark();
5347 curwin->w_cursor = *pos;
5348 if (flag)
5349 beginline(BL_WHITE | BL_FIX);
5350 else
5351 check_cursor();
5352 }
5353 cap->oap->motion_type = flag ? MLINE : MCHAR;
5354 if (cap->cmdchar == '`')
5355 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005356 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 curwin->w_set_curswant = TRUE;
5358}
5359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360/*
5361 * Handle commands that are operators in Visual mode.
5362 */
5363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005364v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005368 // Uppercase means linewise, except in block mode, then "D" deletes till
5369 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (isupper(cap->cmdchar))
5371 {
5372 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005373 {
5374 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5378 curwin->w_curswant = MAXCOL;
5379 }
5380 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5381 nv_operator(cap);
5382}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384/*
5385 * "s" and "S" commands.
5386 */
5387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005388nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005390#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005391 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005392 if (term_swap_diff() == OK)
5393 return;
5394#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005395#ifdef FEAT_JOB_CHANNEL
5396 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5397 {
5398 clearopbeep(cap->oap);
5399 return;
5400 }
5401#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005402 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
5404 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005405 {
5406 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 cap->cmdchar = 'c';
5410 nv_operator(cap);
5411 }
5412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 nv_optrans(cap);
5414}
5415
5416/*
5417 * Abbreviated commands.
5418 */
5419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005420nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005423 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005425 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (VIsual_active)
5427 v_visop(cap);
5428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 nv_optrans(cap);
5430}
5431
5432/*
5433 * Translate a command into another command.
5434 */
5435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005436nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437{
5438 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5439 (char_u *)"d$", (char_u *)"c$",
5440 (char_u *)"cl", (char_u *)"cc",
5441 (char_u *)"yy", (char_u *)":s\r"};
5442 static char_u *str = (char_u *)"xXDCsSY&";
5443
5444 if (!checkclearopq(cap->oap))
5445 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005446 // In Vi "2D" doesn't delete the next line. Can't translate it
5447 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005448 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5449 {
5450 cap->oap->start = curwin->w_cursor;
5451 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005452#ifdef FEAT_EVAL
5453 set_op_var(OP_DELETE);
5454#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005455 cap->count1 = 1;
5456 nv_dollar(cap);
5457 finish_op = TRUE;
5458 ResetRedobuff();
5459 AppendCharToRedobuff('D');
5460 }
5461 else
5462 {
5463 if (cap->count0)
5464 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005465 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 cap->opcount = 0;
5469}
5470
5471/*
5472 * "'" and "`" commands. Also for "g'" and "g`".
5473 * cap->arg is TRUE for "'" and "g'".
5474 */
5475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005476nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 pos_T *pos;
5479 int c;
5480#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005481 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005482 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483#endif
5484
5485 if (cap->cmdchar == 'g')
5486 c = cap->extra_char;
5487 else
5488 c = cap->nchar;
5489 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005490 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 if (cap->arg)
5493 {
5494 check_cursor_lnum();
5495 beginline(BL_WHITE | BL_FIX);
5496 }
5497 else
5498 check_cursor();
5499 }
5500 else
5501 nv_cursormark(cap, cap->arg, pos);
5502
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005503 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (!virtual_active())
5505 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005506 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef FEAT_FOLDING
5508 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005509 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005510 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 && (fdo_flags & FDO_MARK)
5512 && old_KeyTyped)
5513 foldOpenCursor();
5514#endif
5515}
5516
5517/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005518 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 */
5520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005521nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523#ifdef FEAT_JUMPLIST
5524 pos_T *pos;
5525# ifdef FEAT_FOLDING
5526 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528# endif
5529
5530 if (!checkclearopq(cap->oap))
5531 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005532 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5533 {
5534 if (goto_tabpage_lastused() == FAIL)
5535 clearopbeep(cap->oap);
5536 return;
5537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (cap->cmdchar == 'g')
5539 pos = movechangelist((int)cap->count1);
5540 else
5541 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005542 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 curwin->w_set_curswant = TRUE;
5545 check_cursor();
5546 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005547 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 nv_cursormark(cap, FALSE, pos);
5549 else if (cap->cmdchar == 'g')
5550 {
5551 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005554 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005556 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 else
5559 clearopbeep(cap->oap);
5560# ifdef FEAT_FOLDING
5561 if (cap->oap->op_type == OP_NOP
5562 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5563 && (fdo_flags & FDO_MARK)
5564 && old_KeyTyped)
5565 foldOpenCursor();
5566# endif
5567 }
5568#else
5569 clearopbeep(cap->oap);
5570#endif
5571}
5572
5573/*
5574 * Handle '"' command.
5575 */
5576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005577nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 if (checkclearop(cap->oap))
5580 return;
5581#ifdef FEAT_EVAL
5582 if (cap->nchar == '=')
5583 cap->nchar = get_expr_register();
5584#endif
5585 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5586 {
5587 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005588 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589#ifdef FEAT_EVAL
5590 set_reg_var(cap->oap->regname);
5591#endif
5592 }
5593 else
5594 clearopbeep(cap->oap);
5595}
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597/*
5598 * Handle "v", "V" and "CTRL-V" commands.
5599 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5600 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005601 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 */
5603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005604nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005606 if (cap->cmdchar == Ctrl_Q)
5607 cap->cmdchar = Ctrl_V;
5608
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005609 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5610 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (cap->oap->op_type != OP_NOP)
5612 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005613 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 return;
5616 }
5617
5618 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 else // toggle char/block mode
5624 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 VIsual_mode = cap->cmdchar;
5626 showmode();
5627 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005633 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005635 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 VIsual = curwin->w_cursor;
5637
5638 VIsual_active = TRUE;
5639 VIsual_reselect = TRUE;
5640 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005644 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005645 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /*
5647 * For V and ^V, we multiply the number of lines even if there
5648 * was only one -- webb
5649 */
5650 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5651 {
5652 curwin->w_cursor.lnum +=
5653 resel_VIsual_line_count * cap->count0 - 1;
5654 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5655 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5656 }
5657 VIsual_mode = resel_VIsual_mode;
5658 if (VIsual_mode == 'v')
5659 {
5660 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005661 {
5662 validate_virtcol();
5663 curwin->w_curswant = curwin->w_virtcol
5664 + resel_VIsual_vcol * cap->count0 - 1;
5665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005667 curwin->w_curswant = resel_VIsual_vcol;
5668 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005670 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 curwin->w_curswant = MAXCOL;
5673 coladvance((colnr_T)MAXCOL);
5674 }
5675 else if (VIsual_mode == Ctrl_V)
5676 {
5677 validate_virtcol();
5678 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005679 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 coladvance(curwin->w_curswant);
5681 }
5682 else
5683 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005684 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else
5687 {
5688 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005689 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 may_start_select('c');
5691 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005692 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005693 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005694 if (cap->count0 > 0 && --cap->count1 > 0)
5695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005696 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005697 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5698 nv_right(cap);
5699 else if (VIsual_mode == 'V')
5700 nv_down(cap);
5701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
5703 }
5704}
5705
5706/*
5707 * Start selection for Shift-movement keys.
5708 */
5709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005710start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 may_start_select('k');
5714 n_start_visual_mode('v');
5715}
5716
5717/*
5718 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5719 */
5720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005721may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 VIsual_select = (stuff_empty() && typebuf_typed()
5724 && (vim_strchr(p_slm, c) != NULL));
5725}
5726
5727/*
5728 * Start Visual mode "c".
5729 * Should set VIsual_select before calling this.
5730 */
5731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005732n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005734#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005735 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005736 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005737#endif
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 VIsual_mode = c;
5740 VIsual_active = TRUE;
5741 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005742
5743 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005744 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005746 {
5747 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 VIsual = curwin->w_cursor;
5751
5752#ifdef FEAT_FOLDING
5753 foldAdjustVisual();
5754#endif
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005757#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005758 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005759 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005760#endif
5761
Bram Moolenaar09df3122006-01-23 22:23:09 +00005762 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005763 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005765 // Make sure the clipboard gets updated. Needed because start and
5766 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 clip_star.vmode = NUL;
5768#endif
5769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005770 // Only need to redraw this line, unless still need to redraw an old
5771 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (curwin->w_redr_type < INVERTED)
5773 {
5774 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5775 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5776 }
5777}
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780/*
5781 * CTRL-W: Window commands
5782 */
5783 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005784nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005786 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005787 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005788 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005789 cap->cmdchar = ':';
5790 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005791 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005792 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005793 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005794 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795}
5796
5797/*
5798 * CTRL-Z: Suspend
5799 */
5800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005801nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005805 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005806 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807}
5808
5809/*
5810 * Commands starting with "g".
5811 */
5812 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005813nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 int i;
5818 int flag = FALSE;
5819
5820 switch (cap->nchar)
5821 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005822 case Ctrl_A:
5823 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#ifdef MEM_PROFILE
5825 /*
5826 * "g^A": dump log of used memory.
5827 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005828 if (!VIsual_active && cap->nchar == Ctrl_A)
5829 vim_mem_profile_dump();
5830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005832 /*
5833 * "g^A/g^X": sequentially increment visually selected region
5834 */
5835 if (VIsual_active)
5836 {
5837 cap->arg = TRUE;
5838 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005839 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005840 nv_addsub(cap);
5841 }
5842 else
5843 clearopbeep(oap);
5844 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 /*
5847 * "gR": Enter virtual replace mode.
5848 */
5849 case 'R':
5850 cap->arg = TRUE;
5851 nv_Replace(cap);
5852 break;
5853
5854 case 'r':
5855 nv_vreplace(cap);
5856 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 case '&':
5859 do_cmdline_cmd((char_u *)"%s//~/&");
5860 break;
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 /*
5863 * "gv": Reselect the previous Visual area. If Visual already active,
5864 * exchange previous and current Visual area.
5865 */
5866 case 'v':
5867 if (checkclearop(oap))
5868 break;
5869
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005870 if ( curbuf->b_visual.vi_start.lnum == 0
5871 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5872 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 beep_flush();
5874 else
5875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005876 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (VIsual_active)
5878 {
5879 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005880 VIsual_mode = curbuf->b_visual.vi_mode;
5881 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882# ifdef FEAT_EVAL
5883 curbuf->b_visual_mode_eval = i;
5884# endif
5885 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005886 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5887 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005889 tpos = curbuf->b_visual.vi_end;
5890 curbuf->b_visual.vi_end = curwin->w_cursor;
5891 curwin->w_cursor = curbuf->b_visual.vi_start;
5892 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
5894 else
5895 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005896 VIsual_mode = curbuf->b_visual.vi_mode;
5897 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5898 tpos = curbuf->b_visual.vi_end;
5899 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901
5902 VIsual_active = TRUE;
5903 VIsual_reselect = TRUE;
5904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005905 // Set Visual to the start and w_cursor to the end of the Visual
5906 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 check_cursor();
5908 VIsual = curwin->w_cursor;
5909 curwin->w_cursor = tpos;
5910 check_cursor();
5911 update_topline();
5912 /*
5913 * When called from normal "g" command: start Select mode when
5914 * 'selectmode' contains "cmd". When called for K_SELECT, always
5915 * start Select mode.
5916 */
5917 if (cap->arg)
5918 VIsual_select = TRUE;
5919 else
5920 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005923 // Make sure the clipboard gets updated. Needed because start and
5924 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 clip_star.vmode = NUL;
5926#endif
5927 redraw_curbuf_later(INVERTED);
5928 showmode();
5929 }
5930 break;
5931 /*
5932 * "gV": Don't reselect the previous Visual area after a Select mode
5933 * mapping of menu.
5934 */
5935 case 'V':
5936 VIsual_reselect = FALSE;
5937 break;
5938
5939 /*
5940 * "gh": start Select mode.
5941 * "gH": start Select line mode.
5942 * "g^H": start Select block mode.
5943 */
5944 case K_BS:
5945 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005946 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 case 'h':
5948 case 'H':
5949 case Ctrl_H:
5950# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005951 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (cap->nchar == Ctrl_H)
5953 cap->cmdchar = Ctrl_V;
5954 else
5955# endif
5956 cap->cmdchar = cap->nchar + ('v' - 'h');
5957 cap->arg = TRUE;
5958 nv_visual(cap);
5959 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005961 // "gn", "gN" visually select next/previous search match
5962 // "gn" selects next match
5963 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005964 case 'N':
5965 case 'n':
5966 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005967 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 /*
5971 * "gj" and "gk" two new funny movement keys -- up and down
5972 * movement based on *screen* line rather than *file* line.
5973 */
5974 case 'j':
5975 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005976 // with 'nowrap' it works just like the normal "j" command.
5977 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
5979 oap->motion_type = MLINE;
5980 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5981 }
5982 else
5983 i = nv_screengo(oap, FORWARD, cap->count1);
5984 if (i == FAIL)
5985 clearopbeep(oap);
5986 break;
5987
5988 case 'k':
5989 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005990 // with 'nowrap' it works just like the normal "k" command.
5991 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
5993 oap->motion_type = MLINE;
5994 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5995 }
5996 else
5997 i = nv_screengo(oap, BACKWARD, cap->count1);
5998 if (i == FAIL)
5999 clearopbeep(oap);
6000 break;
6001
6002 /*
6003 * "gJ": join two lines without inserting a space.
6004 */
6005 case 'J':
6006 nv_join(cap);
6007 break;
6008
6009 /*
6010 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6011 * "gm": middle of "g0" and "g$".
6012 */
6013 case '^':
6014 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006015 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
6017 case '0':
6018 case 'm':
6019 case K_HOME:
6020 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 oap->motion_type = MCHAR;
6022 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006023 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006025 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 int width2 = width1 + curwin_col_off2();
6027
6028 validate_virtcol();
6029 i = 0;
6030 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6031 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6032 }
6033 else
6034 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006035 // Go to the middle of the screen line. When 'number' or
6036 // 'relativenumber' is on and lines are wrapping the middle can be more
6037 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006039 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 + ((curwin->w_p_wrap && i > 0)
6041 ? curwin_col_off2() : 0)) / 2;
6042 coladvance((colnr_T)i);
6043 if (flag)
6044 {
6045 do
6046 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006047 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006048 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050 curwin->w_set_curswant = TRUE;
6051 break;
6052
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006053 case 'M':
6054 {
6055 char_u *ptr = ml_get_curline();
6056
6057 oap->motion_type = MCHAR;
6058 oap->inclusive = FALSE;
6059 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006060 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006061 else
6062 i = (int)STRLEN(ptr);
6063 if (cap->count0 > 0 && cap->count0 <= 100)
6064 coladvance((colnr_T)(i * cap->count0 / 100));
6065 else
6066 coladvance((colnr_T)(i / 2));
6067 curwin->w_set_curswant = TRUE;
6068 }
6069 break;
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006072 // "g_": to the last non-blank character in the line or <count> lines
6073 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 cap->oap->motion_type = MCHAR;
6075 cap->oap->inclusive = TRUE;
6076 curwin->w_curswant = MAXCOL;
6077 if (cursor_down((long)(cap->count1 - 1),
6078 cap->oap->op_type == OP_NOP) == FAIL)
6079 clearopbeep(cap->oap);
6080 else
6081 {
6082 char_u *ptr = ml_get_curline();
6083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006084 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6086 --curwin->w_cursor.col;
6087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006088 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006090 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 --curwin->w_cursor.col;
6092 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006093 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 }
6095 break;
6096
6097 case '$':
6098 case K_END:
6099 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 int col_off = curwin_col_off();
6102
6103 oap->motion_type = MCHAR;
6104 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006105 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006107 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (cap->count1 == 1)
6109 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006110 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 int width2 = width1 + curwin_col_off2();
6112
6113 validate_virtcol();
6114 i = width1 - 1;
6115 if (curwin->w_virtcol >= (colnr_T)width1)
6116 i += ((curwin->w_virtcol - width1) / width2 + 1)
6117 * width2;
6118 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006119
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006120 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006121 validate_virtcol();
6122 curwin->w_curswant = curwin->w_virtcol;
6123 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6125 {
6126 /*
6127 * Check for landing on a character that got split at
6128 * the end of the line. We do not want to advance to
6129 * the next screen line.
6130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 if (curwin->w_virtcol > (colnr_T)i)
6132 --curwin->w_cursor.col;
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6136 clearopbeep(oap);
6137 }
6138 else
6139 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006140 if (cap->count1 > 1)
6141 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006142 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006143
Bram Moolenaar02631462017-09-22 15:20:32 +02006144 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006146
Bram Moolenaard5c82342019-07-27 18:44:57 +02006147 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006148 validate_virtcol();
6149 curwin->w_curswant = curwin->w_virtcol;
6150 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 }
6152 }
6153 break;
6154
6155 /*
6156 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6157 */
6158 case '*':
6159 case '#':
6160#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006161 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006163 case Ctrl_RSB: // :tag or :tselect for current identifier
6164 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 nv_ident(cap);
6166 break;
6167
6168 /*
6169 * ge and gE: go back to end of word
6170 */
6171 case 'e':
6172 case 'E':
6173 oap->motion_type = MCHAR;
6174 curwin->w_set_curswant = TRUE;
6175 oap->inclusive = TRUE;
6176 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6177 clearopbeep(oap);
6178 break;
6179
6180 /*
6181 * "g CTRL-G": display info about cursor position
6182 */
6183 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006184 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 break;
6186
6187 /*
6188 * "gi": start Insert at the last position.
6189 */
6190 case 'i':
6191 if (curbuf->b_last_insert.lnum != 0)
6192 {
6193 curwin->w_cursor = curbuf->b_last_insert;
6194 check_cursor_lnum();
6195 i = (int)STRLEN(ml_get_curline());
6196 if (curwin->w_cursor.col > (colnr_T)i)
6197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (virtual_active())
6199 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 curwin->w_cursor.col = i;
6201 }
6202 }
6203 cap->cmdchar = 'i';
6204 nv_edit(cap);
6205 break;
6206
6207 /*
6208 * "gI": Start insert in column 1.
6209 */
6210 case 'I':
6211 beginline(0);
6212 if (!checkclearopq(oap))
6213 invoke_edit(cap, FALSE, 'g', FALSE);
6214 break;
6215
6216#ifdef FEAT_SEARCHPATH
6217 /*
6218 * "gf": goto file, edit file under cursor
6219 * "]f" and "[f": can also be used.
6220 */
6221 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006222 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 nv_gotofile(cap);
6224 break;
6225#endif
6226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006227 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 case '\'':
6229 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006230 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 case '`':
6232 nv_gomark(cap);
6233 break;
6234
6235 /*
6236 * "gs": Goto sleep.
6237 */
6238 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006239 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 break;
6241
6242 /*
6243 * "ga": Display the ascii value of the character under the
6244 * cursor. It is displayed in decimal, hex, and octal. -- webb
6245 */
6246 case 'a':
6247 do_ascii(NULL);
6248 break;
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 /*
6251 * "g8": Display the bytes used for the UTF-8 character under the
6252 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006253 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 */
6255 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006256 if (cap->count0 == 8)
6257 utf_find_illegal();
6258 else
6259 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006262 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006263 case '<':
6264 show_sb_text();
6265 break;
6266
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 /*
6268 * "gg": Goto the first line in file. With a count it goes to
6269 * that line number like for "G". -- webb
6270 */
6271 case 'g':
6272 cap->arg = FALSE;
6273 nv_goto(cap);
6274 break;
6275
6276 /*
6277 * Two-character operators:
6278 * "gq" Format text
6279 * "gw" Format text and keep cursor position
6280 * "g~" Toggle the case of the text.
6281 * "gu" Change text to lower case.
6282 * "gU" Change text to upper case.
6283 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006284 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 */
6286 case 'q':
6287 case 'w':
6288 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006289 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 case '~':
6291 case 'u':
6292 case 'U':
6293 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006294 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 nv_operator(cap);
6296 break;
6297
6298 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006299 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 * current function
6301 * "gD": idem, but in the current file.
6302 */
6303 case 'd':
6304 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006305 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 break;
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /*
6309 * g<*Mouse> : <C-*mouse>
6310 */
6311 case K_MIDDLEMOUSE:
6312 case K_MIDDLEDRAG:
6313 case K_MIDDLERELEASE:
6314 case K_LEFTMOUSE:
6315 case K_LEFTDRAG:
6316 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006317 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 case K_RIGHTMOUSE:
6319 case K_RIGHTDRAG:
6320 case K_RIGHTRELEASE:
6321 case K_X1MOUSE:
6322 case K_X1DRAG:
6323 case K_X1RELEASE:
6324 case K_X2MOUSE:
6325 case K_X2DRAG:
6326 case K_X2RELEASE:
6327 mod_mask = MOD_MASK_CTRL;
6328 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6329 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 case K_IGNORE:
6332 break;
6333
6334 /*
6335 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6336 */
6337 case 'p':
6338 case 'P':
6339 nv_put(cap);
6340 break;
6341
6342#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 case 'o':
6345 goto_byte(cap->count0);
6346 break;
6347#endif
6348
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006349 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006351 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 {
6353 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006354 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 break;
6356 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 if (!checkclearopq(oap))
6359 do_exmode(TRUE);
6360 break;
6361
6362#ifdef FEAT_JUMPLIST
6363 case ',':
6364 nv_pcmark(cap);
6365 break;
6366
6367 case ';':
6368 cap->count1 = -cap->count1;
6369 nv_pcmark(cap);
6370 break;
6371#endif
6372
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006374 if (!checkclearop(oap))
6375 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006377 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006378 if (!checkclearop(oap))
6379 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006380 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006381
Bram Moolenaar62a23252020-08-09 14:04:42 +02006382 case TAB:
6383 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6384 clearopbeep(oap);
6385 break;
6386
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006387 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006388 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006389 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006390 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006391 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006392 break;
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 default:
6395 clearopbeep(oap);
6396 break;
6397 }
6398}
6399
6400/*
6401 * Handle "o" and "O" commands.
6402 */
6403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006404n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006406#ifdef FEAT_CONCEAL
6407 linenr_T oldline = curwin->w_cursor.lnum;
6408#endif
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (!checkclearopq(cap->oap))
6411 {
6412#ifdef FEAT_FOLDING
6413 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006414 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 (void)hasFolding(curwin->w_cursor.lnum,
6416 &curwin->w_cursor.lnum, NULL);
6417 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006418 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 (void)hasFolding(curwin->w_cursor.lnum,
6420 NULL, &curwin->w_cursor.lnum);
6421#endif
6422 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6423 (cap->cmdchar == 'O' ? 1 : 0)),
6424 (linenr_T)(curwin->w_cursor.lnum +
6425 (cap->cmdchar == 'o' ? 1 : 0))
6426 ) == OK
6427 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006428 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6429 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006431#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006432 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006433 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006434#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006435#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006436 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006437 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006438 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006439#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006440 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006441 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6442 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6444 }
6445 }
6446}
6447
6448/*
6449 * "." command: redo last change.
6450 */
6451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006452nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 if (!checkclearopq(cap->oap))
6455 {
6456 /*
6457 * If "restart_edit" is TRUE, the last but one command is repeated
6458 * instead of the last command (inserting text). This is used for
6459 * CTRL-O <.> in insert mode.
6460 */
6461 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6462 clearopbeep(cap->oap);
6463 }
6464}
6465
6466/*
6467 * CTRL-R: undo undo
6468 */
6469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006470nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 if (!checkclearopq(cap->oap))
6473 {
6474 u_redo((int)cap->count1);
6475 curwin->w_set_curswant = TRUE;
6476 }
6477}
6478
6479/*
6480 * Handle "U" command.
6481 */
6482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006483nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006485 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006486 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006488 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 cap->cmdchar = 'g';
6490 cap->nchar = 'U';
6491 nv_operator(cap);
6492 }
6493 else if (!checkclearopq(cap->oap))
6494 {
6495 u_undoline();
6496 curwin->w_set_curswant = TRUE;
6497 }
6498}
6499
6500/*
6501 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6502 * single character.
6503 */
6504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006507 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006508 {
6509#ifdef FEAT_JOB_CHANNEL
6510 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6511 {
6512 clearopbeep(cap->oap);
6513 return;
6514 }
6515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 else
6519 nv_operator(cap);
6520}
6521
6522/*
6523 * Handle an operator command.
6524 * The actual work is done by do_pending_operator().
6525 */
6526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006527nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 int op_type;
6530
6531 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006532#ifdef FEAT_JOB_CHANNEL
6533 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6534 {
6535 clearopbeep(cap->oap);
6536 return;
6537 }
6538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006540 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 nv_lineop(cap);
6542 else if (!checkclearop(cap->oap))
6543 {
6544 cap->oap->start = curwin->w_cursor;
6545 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006546#ifdef FEAT_EVAL
6547 set_op_var(op_type);
6548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550}
6551
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006552#ifdef FEAT_EVAL
6553/*
6554 * Set v:operator to the characters for "optype".
6555 */
6556 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006557set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006558{
6559 char_u opchars[3];
6560
6561 if (optype == OP_NOP)
6562 set_vim_var_string(VV_OP, NULL, 0);
6563 else
6564 {
6565 opchars[0] = get_op_char(optype);
6566 opchars[1] = get_extra_op_char(optype);
6567 opchars[2] = NUL;
6568 set_vim_var_string(VV_OP, opchars, -1);
6569 }
6570}
6571#endif
6572
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573/*
6574 * Handle linewise operator "dd", "yy", etc.
6575 *
6576 * "_" is is a strange motion command that helps make operators more logical.
6577 * It is actually implemented, but not documented in the real Vi. This motion
6578 * command actually refers to "the current line". Commands like "dd" and "yy"
6579 * are really an alternate form of "d_" and "y_". It does accept a count, so
6580 * "d3_" works to delete 3 lines.
6581 */
6582 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 cap->oap->motion_type = MLINE;
6586 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6587 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006588 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006589 && cap->oap->motion_force != 'v'
6590 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 || cap->oap->op_type == OP_LSHIFT
6592 || cap->oap->op_type == OP_RSHIFT)
6593 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006594 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 beginline(BL_WHITE | BL_FIX);
6596}
6597
6598/*
6599 * <Home> command.
6600 */
6601 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006602nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006604 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006605 if (mod_mask & MOD_MASK_CTRL)
6606 nv_goto(cap);
6607 else
6608 {
6609 cap->count0 = 1;
6610 nv_pipe(cap);
6611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006612 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6613 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
6615
6616/*
6617 * "|" command.
6618 */
6619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006620nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 cap->oap->motion_type = MCHAR;
6623 cap->oap->inclusive = FALSE;
6624 beginline(0);
6625 if (cap->count0 > 0)
6626 {
6627 coladvance((colnr_T)(cap->count0 - 1));
6628 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6629 }
6630 else
6631 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006632 // keep curswant at the column where we wanted to go, not where
6633 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 curwin->w_set_curswant = FALSE;
6635}
6636
6637/*
6638 * Handle back-word command "b" and "B".
6639 * cap->arg is 1 for "B"
6640 */
6641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006642nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 cap->oap->motion_type = MCHAR;
6645 cap->oap->inclusive = FALSE;
6646 curwin->w_set_curswant = TRUE;
6647 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6648 clearopbeep(cap->oap);
6649#ifdef FEAT_FOLDING
6650 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6651 foldOpenCursor();
6652#endif
6653}
6654
6655/*
6656 * Handle word motion commands "e", "E", "w" and "W".
6657 * cap->arg is TRUE for "E" and "W".
6658 */
6659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006660nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 int n;
6663 int word_end;
6664 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006665 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /*
6668 * Set inclusive for the "E" and "e" command.
6669 */
6670 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6671 word_end = TRUE;
6672 else
6673 word_end = FALSE;
6674 cap->oap->inclusive = word_end;
6675
6676 /*
6677 * "cw" and "cW" are a special case.
6678 */
6679 if (!word_end && cap->oap->op_type == OP_CHANGE)
6680 {
6681 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006682 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006684 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 {
6686 /*
6687 * Reproduce a funny Vi behaviour: "cw" on a blank only
6688 * changes one character, not all blanks until the start of
6689 * the next word. Only do this when the 'w' flag is included
6690 * in 'cpoptions'.
6691 */
6692 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6693 {
6694 cap->oap->inclusive = TRUE;
6695 cap->oap->motion_type = MCHAR;
6696 return;
6697 }
6698 }
6699 else
6700 {
6701 /*
6702 * This is a little strange. To match what the real Vi does,
6703 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6704 * that we are not on a space or a TAB. This seems impolite
6705 * at first, but it's really more what we mean when we say
6706 * 'cw'.
6707 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006708 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 * will change only one character! This is done by setting
6710 * flag.
6711 */
6712 cap->oap->inclusive = TRUE;
6713 word_end = TRUE;
6714 flag = TRUE;
6715 }
6716 }
6717 }
6718
6719 cap->oap->motion_type = MCHAR;
6720 curwin->w_set_curswant = TRUE;
6721 if (word_end)
6722 n = end_word(cap->count1, cap->arg, flag, FALSE);
6723 else
6724 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006726 // Don't leave the cursor on the NUL past the end of line. Unless we
6727 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006728 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006729 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731 if (n == FAIL && cap->oap->op_type == OP_NOP)
6732 clearopbeep(cap->oap);
6733 else
6734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736#ifdef FEAT_FOLDING
6737 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6738 foldOpenCursor();
6739#endif
6740 }
6741}
6742
6743/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006744 * Used after a movement command: If the cursor ends up on the NUL after the
6745 * end of the line, may move it back to the last character and make the motion
6746 * inclusive.
6747 */
6748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006750{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 // The cursor cannot remain on the NUL when:
6752 // - the column is > 0
6753 // - not in Visual mode or 'selection' is "o"
6754 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006755 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006756 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006757 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006758 {
6759 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006760 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006761 if (has_mbyte)
6762 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006763 oap->inclusive = TRUE;
6764 }
6765}
6766
6767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 * "0" and "^" commands.
6769 * cap->arg is the argument for beginline().
6770 */
6771 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006772nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773{
6774 cap->oap->motion_type = MCHAR;
6775 cap->oap->inclusive = FALSE;
6776 beginline(cap->arg);
6777#ifdef FEAT_FOLDING
6778 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6779 foldOpenCursor();
6780#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006781 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6782 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783}
6784
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785/*
6786 * In exclusive Visual mode, may include the last character.
6787 */
6788 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006789adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006792 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (has_mbyte)
6795 inc_cursor();
6796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 ++curwin->w_cursor.col;
6798 cap->oap->inclusive = FALSE;
6799 }
6800}
6801
6802/*
6803 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6804 * Should check VIsual_mode before calling this.
6805 * Returns TRUE when backed up to the previous line.
6806 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006807 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006808unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809{
6810 pos_T *pp;
6811
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006812 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006814 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 pp = &curwin->w_cursor;
6816 else
6817 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (pp->coladd > 0)
6819 --pp->coladd;
6820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (pp->col > 0)
6822 {
6823 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006824 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826 else if (pp->lnum > 1)
6827 {
6828 --pp->lnum;
6829 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6830 return TRUE;
6831 }
6832 }
6833 return FALSE;
6834}
6835
6836/*
6837 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6838 */
6839 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006840nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 if (VIsual_active)
6843 VIsual_select = TRUE;
6844 else if (VIsual_reselect)
6845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006846 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 cap->arg = TRUE;
6848 nv_g_cmd(cap);
6849 }
6850}
6851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853/*
6854 * "G", "gg", CTRL-END, CTRL-HOME.
6855 * cap->arg is TRUE for "G".
6856 */
6857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 linenr_T lnum;
6861
6862 if (cap->arg)
6863 lnum = curbuf->b_ml.ml_line_count;
6864 else
6865 lnum = 1L;
6866 cap->oap->motion_type = MLINE;
6867 setpcmark();
6868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006869 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (cap->count0 != 0)
6871 lnum = cap->count0;
6872 if (lnum < 1L)
6873 lnum = 1L;
6874 else if (lnum > curbuf->b_ml.ml_line_count)
6875 lnum = curbuf->b_ml.ml_line_count;
6876 curwin->w_cursor.lnum = lnum;
6877 beginline(BL_SOL | BL_FIX);
6878#ifdef FEAT_FOLDING
6879 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6880 foldOpenCursor();
6881#endif
6882}
6883
6884/*
6885 * CTRL-\ in Normal mode.
6886 */
6887 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006888nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6891 {
6892 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006893 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006894 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 restart_edit = 0;
6896#ifdef FEAT_CMDWIN
6897 if (cmdwin_type != 0)
6898 cmdwin_result = Ctrl_C;
6899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (VIsual_active)
6901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006902 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 redraw_curbuf_later(INVERTED);
6904 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (cap->nchar == Ctrl_G && p_im)
6907 restart_edit = 'a';
6908 }
6909 else
6910 clearopbeep(cap->oap);
6911}
6912
6913/*
6914 * ESC in Normal mode: beep, but don't flush buffers.
6915 * Don't even beep if we are canceling a command.
6916 */
6917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006918nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 int no_reason;
6921
6922 no_reason = (cap->oap->op_type == OP_NOP
6923 && cap->opcount == 0
6924 && cap->count0 == 0
6925 && cap->oap->regname == 0
6926 && !p_im);
6927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006928 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
6930 if (restart_edit == 0
6931#ifdef FEAT_CMDWIN
6932 && cmdwin_type == 0
6933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006936 {
6937 if (anyBufIsChanged())
6938 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6939 else
6940 msg(_("Type :qa and press <Enter> to exit Vim"));
6941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006943 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6944 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 if (!p_im)
6946 restart_edit = 0;
6947#ifdef FEAT_CMDWIN
6948 if (cmdwin_type != 0)
6949 {
6950 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006951 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 return;
6953 }
6954#endif
6955 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006956#ifdef FEAT_CMDWIN
6957 else if (cmdwin_type != 0 && ex_normal_busy)
6958 {
6959 // When :normal runs out of characters while in the command line window
6960 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6961 // loop.
6962 cmdwin_result = K_IGNORE;
6963 return;
6964 }
6965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (VIsual_active)
6968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006969 end_visual_mode(); // stop Visual
6970 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 curwin->w_set_curswant = TRUE;
6972 redraw_curbuf_later(INVERTED);
6973 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006974 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006975 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 clearop(cap->oap);
6977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6979 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006980 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 restart_edit = 'a';
6982}
6983
6984/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006985 * Move the cursor for the "A" command.
6986 */
6987 void
6988set_cursor_for_append_to_line(void)
6989{
6990 curwin->w_set_curswant = TRUE;
6991 if (ve_flags == VE_ALL)
6992 {
6993 int save_State = State;
6994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006995 // Pretend Insert mode here to allow the cursor on the
6996 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006997 State = INSERT;
6998 coladvance((colnr_T)MAXCOL);
6999 State = save_State;
7000 }
7001 else
7002 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7003}
7004
7005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007007 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 */
7009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007010nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007012 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7014 cap->cmdchar = 'i';
7015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007016 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007018 {
7019#ifdef FEAT_TERMINAL
7020 if (term_in_normal_mode())
7021 {
7022 end_visual_mode();
7023 clearop(cap->oap);
7024 term_enter_job_mode();
7025 return;
7026 }
7027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007031 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007032 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7033 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
7035#ifdef FEAT_TEXTOBJ
7036 nv_object(cap);
7037#else
7038 clearopbeep(cap->oap);
7039#endif
7040 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007041#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007042 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007043 {
7044 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007045 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007046 return;
7047 }
7048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 else if (!curbuf->b_p_ma && !p_im)
7050 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007052 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007054 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007056 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007058 else if (cap->cmdchar == K_PS && VIsual_active)
7059 {
7060 pos_T old_pos = curwin->w_cursor;
7061 pos_T old_visual = VIsual;
7062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007063 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007064 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7065 {
7066 shift_delete_registers();
7067 cap->oap->regname = '1';
7068 }
7069 else
7070 cap->oap->regname = '-';
7071 cap->cmdchar = 'd';
7072 cap->nchar = NUL;
7073 nv_operator(cap);
7074 do_pending_operator(cap, 0, FALSE);
7075 cap->cmdchar = K_PS;
7076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 // When the last char in the line was deleted then append. Detect this
7078 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007079 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7080 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007081 inc_cursor();
7082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007084 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 else if (!checkclearopq(cap->oap))
7087 {
7088 switch (cap->cmdchar)
7089 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007091 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 break;
7093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007095 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7096 beginline(BL_WHITE);
7097 else
7098 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 break;
7100
Bram Moolenaara1891842017-02-04 21:34:31 +01007101 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007102 // Bracketed paste works like "a"ppend, unless the cursor is in
7103 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007104 if (curwin->w_cursor.col == 0)
7105 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 case 'a': // "a"ppend is like "i"nsert on the next character.
7109 // increment coladd when in virtual space, increment the
7110 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 if (virtual_active()
7112 && (curwin->w_cursor.coladd > 0
7113 || *ml_get_cursor() == NUL
7114 || *ml_get_cursor() == TAB))
7115 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007116 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 inc_cursor();
7118 break;
7119 }
7120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7122 {
7123 int save_State = State;
7124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 // Pretend Insert mode here to allow the cursor on the
7126 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 State = INSERT;
7128 coladvance(getviscol());
7129 State = save_State;
7130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
7132 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7133 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007134 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007136 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137}
7138
7139/*
7140 * Invoke edit() and take care of "restart_edit" and the return value.
7141 */
7142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007143invoke_edit(
7144 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007145 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007146 int cmd,
7147 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
7149 int restart_edit_save = 0;
7150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007151 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7152 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7153 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (repl || !stuff_empty())
7155 restart_edit_save = restart_edit;
7156 else
7157 restart_edit_save = 0;
7158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007159 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 restart_edit = 0;
7161
7162 if (edit(cmd, startln, cap->count1))
7163 cap->retval |= CA_COMMAND_BUSY;
7164
7165 if (restart_edit == 0)
7166 restart_edit = restart_edit_save;
7167}
7168
7169#ifdef FEAT_TEXTOBJ
7170/*
7171 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7172 */
7173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007174nv_object(
7175 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 int flag;
7178 int include;
7179 char_u *mps_save;
7180
7181 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007184 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007186 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 mps_save = curbuf->b_p_mps;
7188 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7189
7190 switch (cap->nchar)
7191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007192 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 flag = current_word(cap->oap, cap->count1, include, FALSE);
7194 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007195 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 flag = current_word(cap->oap, cap->count1, include, TRUE);
7197 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007198 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 case '(':
7200 case ')':
7201 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7202 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007203 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 case '{':
7205 case '}':
7206 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7207 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 case ']':
7210 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7211 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007212 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 case '>':
7214 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7215 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007216 case 't': // "at" = a tag block (xml and html)
7217 // Do not adjust oap->end in do_pending_operator()
7218 // otherwise there are different results for 'dit'
7219 // (note leading whitespace in last line):
7220 // 1) <b> 2) <b>
7221 // foobar foobar
7222 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007223 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007224 flag = current_tagblock(cap->oap, cap->count1, include);
7225 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007226 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 flag = current_par(cap->oap, cap->count1, include, 'p');
7228 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007229 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 flag = current_sent(cap->oap, cap->count1, include);
7231 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 case '"': // "a"" = a double quoted string
7233 case '\'': // "a'" = a single quoted string
7234 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007235 flag = current_quote(cap->oap, cap->count1, include,
7236 cap->nchar);
7237 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007238#if 0 // TODO
7239 case 'S': // "aS" = a section
7240 case 'f': // "af" = a filename
7241 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#endif
7243 default:
7244 flag = FAIL;
7245 break;
7246 }
7247
7248 curbuf->b_p_mps = mps_save;
7249 if (flag == FAIL)
7250 clearopbeep(cap->oap);
7251 adjust_cursor_col();
7252 curwin->w_set_curswant = TRUE;
7253}
7254#endif
7255
7256/*
7257 * "q" command: Start/stop recording.
7258 * "q:", "q/", "q?": edit command-line in command-line window.
7259 */
7260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007261nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 if (cap->oap->op_type == OP_FORMAT)
7264 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007265 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 cap->cmdchar = 'g';
7267 cap->nchar = 'q';
7268 nv_operator(cap);
7269 }
7270 else if (!checkclearop(cap->oap))
7271 {
7272#ifdef FEAT_CMDWIN
7273 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7274 {
7275 stuffcharReadbuff(cap->nchar);
7276 stuffcharReadbuff(K_CMDWIN);
7277 }
7278 else
7279#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007280 // (stop) recording into a named register, unless executing a
7281 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007282 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 clearopbeep(cap->oap);
7284 }
7285}
7286
7287/*
7288 * Handle the "@r" command.
7289 */
7290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007291nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
7293 if (checkclearop(cap->oap))
7294 return;
7295#ifdef FEAT_EVAL
7296 if (cap->nchar == '=')
7297 {
7298 if (get_expr_register() == NUL)
7299 return;
7300 }
7301#endif
7302 while (cap->count1-- && !got_int)
7303 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007304 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 {
7306 clearopbeep(cap->oap);
7307 break;
7308 }
7309 line_breakcheck();
7310 }
7311}
7312
7313/*
7314 * Handle the CTRL-U and CTRL-D commands.
7315 */
7316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007317nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
7319 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7320 || (cap->cmdchar == Ctrl_D
7321 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7322 clearopbeep(cap->oap);
7323 else if (!checkclearop(cap->oap))
7324 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7325}
7326
7327/*
7328 * Handle "J" or "gJ" command.
7329 */
7330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007331nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007333 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007335 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
7337 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7340 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007343 if (cap->count0 <= 2)
7344 {
7345 clearopbeep(cap->oap);
7346 return;
7347 }
7348 cap->count0 = curbuf->b_ml.ml_line_count
7349 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007351
7352 prep_redo(cap->oap->regname, cap->count0,
7353 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7354 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356}
7357
7358/*
7359 * "P", "gP", "p" and "gp" commands.
7360 */
7361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007362nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007364 nv_put_opt(cap, FALSE);
7365}
7366
7367/*
7368 * "P", "gP", "p" and "gp" commands.
7369 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7370 */
7371 static void
7372nv_put_opt(cmdarg_T *cap, int fix_indent)
7373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 int regname = 0;
7375 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007376 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007377 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 int dir;
7379 int flags = 0;
7380
7381 if (cap->oap->op_type != OP_NOP)
7382 {
7383#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007384 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7386 {
7387 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007388 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
7390 else
7391#endif
7392 clearopbeep(cap->oap);
7393 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007394#ifdef FEAT_JOB_CHANNEL
7395 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7396 {
7397 clearopbeep(cap->oap);
7398 }
7399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 else
7401 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007402 if (fix_indent)
7403 {
7404 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7405 ? FORWARD : BACKWARD;
7406 flags |= PUT_FIXINDENT;
7407 }
7408 else
7409 dir = (cap->cmdchar == 'P'
7410 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 ? BACKWARD : FORWARD;
7412 prep_redo_cmd(cap);
7413 if (cap->cmdchar == 'g')
7414 flags |= PUT_CURSEND;
7415
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 if (VIsual_active)
7417 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007418 // Putting in Visual mode: The put text replaces the selected
7419 // text. First delete the selected text, then put the new text.
7420 // Need to save and restore the registers that the delete
7421 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007422 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007424#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007426#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007427 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007428 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007429#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433 )
7434 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007435 // The delete is going to overwrite the register we want to
7436 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 reg1 = get_register(regname, TRUE);
7438 }
7439
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007440 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 cap->cmdchar = 'd';
7442 cap->nchar = NUL;
7443 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007444 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 nv_operator(cap);
7446 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007447 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007448 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007450 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 cap->oap->regname = regname;
7452
7453 if (reg1 != NULL)
7454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007455 // Delete probably changed the register we want to put, save
7456 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 reg2 = get_register(regname, FALSE);
7458 put_register(regname, reg1);
7459 }
7460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007461 // When deleted a linewise Visual area, put the register as
7462 // lines to avoid it joined with the next line. When deletion was
7463 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (VIsual_mode == 'V')
7465 flags |= PUT_LINE;
7466 else if (VIsual_mode == 'v')
7467 flags |= PUT_LINE_SPLIT;
7468 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7469 flags |= PUT_LINE_FORWARD;
7470 dir = BACKWARD;
7471 if ((VIsual_mode != 'V'
7472 && curwin->w_cursor.col < curbuf->b_op_start.col)
7473 || (VIsual_mode == 'V'
7474 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007475 // cursor is at the end of the line or end of file, put
7476 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007478 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007479 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007481 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007483 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 if (reg2 != NULL)
7485 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007487 // What to reselect with "gv"? Selecting the just put text seems to
7488 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007489 if (was_visual)
7490 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007491 curbuf->b_visual.vi_start = curbuf->b_op_start;
7492 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007493 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007494 if (*p_sel == 'e')
7495 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007496 }
7497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007498 // When all lines were selected and deleted do_put() leaves an empty
7499 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007500 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007501 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007502 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007503 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007505 // If the cursor was in that line, move it to the end of the last
7506 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007507 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7508 {
7509 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7510 coladvance((colnr_T)MAXCOL);
7511 }
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 auto_format(FALSE, TRUE);
7514 }
7515}
7516
7517/*
7518 * "o" and "O" commands.
7519 */
7520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007521nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007524 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7526 {
7527 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007528 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530 else
7531#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007532 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007534#ifdef FEAT_JOB_CHANNEL
7535 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007536 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 n_opencmd(cap);
7540}
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542#ifdef FEAT_NETBEANS_INTG
7543 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007544nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 netbeans_keycommand(cap->nchar);
7547}
7548#endif
7549
7550#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007552nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007554 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555}
7556#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007557
Bram Moolenaar3918c952005-03-15 22:34:55 +00007558/*
7559 * Trigger CursorHold event.
7560 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7561 * input buffer. "did_cursorhold" is set to avoid retriggering.
7562 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007564nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007565{
7566 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7567 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007568 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007569}