blob: c4c167935ed64cf615df74402dfaffabc613dac1 [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[].
K.Takataeeec2542021-06-02 13:28:16 +0200382#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383
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 Moolenaar4f3c57f2021-06-03 22:11:08 +02001353 * This function or the next should ALWAYS be called to end Visual mode, except
1354 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 */
1356 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001357end_visual_mode()
1358{
1359 end_visual_mode_keep_button();
1360 reset_held_button();
1361}
1362
1363 void
1364end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366#ifdef FEAT_CLIPBOARD
1367 /*
1368 * If we are using the clipboard, then remember what was selected in case
1369 * we need to paste it somewhere while we still own the selection.
1370 * Only do this when the clipboard is already owned. Don't want to grab
1371 * the selection when hitting ESC.
1372 */
1373 if (clip_star.available && clip_star.owned)
1374 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001375
1376# if defined(FEAT_EVAL)
1377 // Emit a TextYankPost for the automatic copy of the selection into the
1378 // star and/or plus register.
1379 if (has_textyankpost())
1380 {
1381 if (clip_isautosel_star())
1382 call_yank_do_autocmd('*');
1383 if (clip_isautosel_plus())
1384 call_yank_do_autocmd('+');
1385 }
1386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
1389 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 setmouse();
1391 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001393 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001394 curbuf->b_visual.vi_mode = VIsual_mode;
1395 curbuf->b_visual.vi_start = VIsual;
1396 curbuf->b_visual.vi_end = curwin->w_cursor;
1397 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_EVAL
1399 curbuf->b_visual_mode_eval = VIsual_mode;
1400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (!virtual_active())
1402 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001403 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001405 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408/*
1409 * Reset VIsual_active and VIsual_reselect.
1410 */
1411 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 if (VIsual_active)
1415 {
1416 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001417 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 VIsual_reselect = FALSE;
1420}
1421
1422/*
1423 * Reset VIsual_active and VIsual_reselect if it's set.
1424 */
1425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001426reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 if (VIsual_active)
1429 {
1430 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001431 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 VIsual_reselect = FALSE;
1433 }
1434}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001436 void
1437restore_visual_mode(void)
1438{
1439 if (VIsual_mode_orig != NUL)
1440 {
1441 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1442 VIsual_mode_orig = NUL;
1443 }
1444}
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446/*
1447 * Check for a balloon-eval special item to include when searching for an
1448 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1449 * Returns TRUE if the character at "*ptr" should be included.
1450 * "dir" is FORWARD or BACKWARD, the direction of searching.
1451 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1452 * "bnp" points to a counter for square brackets.
1453 */
1454 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001455find_is_eval_item(
1456 char_u *ptr,
1457 int *colp,
1458 int *bnp,
1459 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001461 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1463 ++*bnp;
1464 if (*bnp > 0)
1465 {
1466 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1467 --*bnp;
1468 return TRUE;
1469 }
1470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (*ptr == '.')
1473 return TRUE;
1474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1477 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1478 {
1479 *colp += dir;
1480 return TRUE;
1481 }
1482 return FALSE;
1483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485/*
1486 * Find the identifier under or to the right of the cursor.
1487 * "find_type" can have one of three values:
1488 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001489 * FIND_STRING: find any non-white text
1490 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001491 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 *
1493 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001494 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001496 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 * This doesn't match the real Vi but I like it a little better and it
1498 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * When FIND_IDENT isn't defined, we backup until a blank.
1501 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001502 * Returns the length of the text, or zero if no text is found.
1503 * If text is found, a pointer to the text is put in "*text". This
1504 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
1506 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001507find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001510 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511}
1512
1513/*
1514 * Like find_ident_under_cursor(), but for any window and any position.
1515 * However: Uses 'iskeyword' from the current window!.
1516 */
1517 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001518find_ident_at_pos(
1519 win_T *wp,
1520 linenr_T lnum,
1521 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522 char_u **text,
1523 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001524 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001527 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 int this_class = 0;
1530 int prev_class;
1531 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
1534 /*
1535 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001536 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 */
1538 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1539 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1540 {
1541 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (has_mbyte)
1546 {
1547 while (ptr[col] != NUL)
1548 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001549 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1551 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 this_class = mb_get_class(ptr + col);
1553 if (this_class != 0 && (i == 1 || this_class != 1))
1554 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001555 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 }
1558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001560 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 )
1563 ++col;
1564
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001565 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001569 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (has_mbyte)
1572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001573 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1575 this_class = mb_get_class((char_u *)"a");
1576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001578 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1581 prev_class = mb_get_class(ptr + prevcol);
1582 if (this_class != prev_class
1583 && (i == 0
1584 || prev_class == 0
1585 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 && (!(find_type & FIND_EVAL)
1587 || prevcol == 0
1588 || !find_is_eval_item(ptr + prevcol, &prevcol,
1589 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 )
1591 break;
1592 col = prevcol;
1593 }
1594
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001595 // If we don't want just any old text, or we've found an
1596 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (this_class > 2)
1598 this_class = 2;
1599 if (!(find_type & FIND_STRING) || this_class == 2)
1600 break;
1601 }
1602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 while (col > 0
1605 && ((i == 0
1606 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001607 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 && (!(find_type & FIND_IDENT)
1609 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 || ((find_type & FIND_EVAL)
1611 && col > 1
1612 && find_is_eval_item(ptr + col - 1, &col,
1613 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 ))
1615 --col;
1616
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001617 // If we don't want just any old text, or we've found an
1618 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1620 break;
1621 }
1622 }
1623
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001624 if (ptr[col] == NUL || (i == 0
1625 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001627 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001628 if ((find_type & FIND_NOERROR) == 0)
1629 {
1630 if (find_type & FIND_STRING)
1631 emsg(_("E348: No string under cursor"));
1632 else
1633 emsg(_(e_noident));
1634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 return 0;
1636 }
1637 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001638 *text = ptr;
1639 if (textcol != NULL)
1640 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001643 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 bn = 0;
1646 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (has_mbyte)
1649 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001650 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 this_class = mb_get_class(ptr);
1652 while (ptr[col] != NUL
1653 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1654 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 || ((find_type & FIND_EVAL)
1656 && col <= (int)startcol
1657 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001659 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001663 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 || ((find_type & FIND_EVAL)
1665 && col <= (int)startcol
1666 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 return col;
1671}
1672
1673/*
1674 * Prepare for redo of a normal command.
1675 */
1676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001677prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 prep_redo(cap->oap->regname, cap->count0,
1680 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1681}
1682
1683/*
1684 * Prepare for redo of any command.
1685 * Note that only the last argument can be a multi-byte char.
1686 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688prep_redo(
1689 int regname,
1690 long num,
1691 int cmd1,
1692 int cmd2,
1693 int cmd3,
1694 int cmd4,
1695 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001698 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 AppendCharToRedobuff('"');
1701 AppendCharToRedobuff(regname);
1702 }
1703 if (num)
1704 AppendNumberToRedobuff(num);
1705
1706 if (cmd1 != NUL)
1707 AppendCharToRedobuff(cmd1);
1708 if (cmd2 != NUL)
1709 AppendCharToRedobuff(cmd2);
1710 if (cmd3 != NUL)
1711 AppendCharToRedobuff(cmd3);
1712 if (cmd4 != NUL)
1713 AppendCharToRedobuff(cmd4);
1714 if (cmd5 != NUL)
1715 AppendCharToRedobuff(cmd5);
1716}
1717
1718/*
1719 * check for operator active and clear it
1720 *
1721 * return TRUE if operator was active
1722 */
1723 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 if (oap->op_type == OP_NOP)
1727 return FALSE;
1728 clearopbeep(oap);
1729 return TRUE;
1730}
1731
1732/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001733 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001735 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
1737 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001738checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001740 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return FALSE;
1742 clearopbeep(oap);
1743 return TRUE;
1744}
1745
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001747clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 oap->op_type = OP_NOP;
1750 oap->regname = 0;
1751 oap->motion_force = NUL;
1752 oap->use_reg_one = FALSE;
1753}
1754
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 clearop(oap);
1759 beep_flush();
1760}
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762/*
1763 * Remove the shift modifier from a special key.
1764 */
1765 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 switch (cap->cmdchar)
1769 {
1770 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1771 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1772 case K_S_UP: cap->cmdchar = K_UP; break;
1773 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1774 case K_S_HOME: cap->cmdchar = K_HOME; break;
1775 case K_S_END: cap->cmdchar = K_END; break;
1776 }
1777 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1778}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001780/*
1781 * If the mode is currently displayed clear the command line or update the
1782 * command displayed.
1783 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001785may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001786{
1787 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001788 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001789#ifdef FEAT_CMDL_INFO
1790 else
1791 clear_showcmd();
1792#endif
1793}
1794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1796/*
1797 * Routines for displaying a partly typed command
1798 */
1799
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001800#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803static int showcmd_is_clear = TRUE;
1804static int showcmd_visual = FALSE;
1805
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001806static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 if (!p_sc)
1812 return;
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (VIsual_active && !char_avail())
1815 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001816 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 long lines;
1818 colnr_T leftcol, rightcol;
1819 linenr_T top, bot;
1820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001821 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001822 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 top = VIsual.lnum;
1825 bot = curwin->w_cursor.lnum;
1826 }
1827 else
1828 {
1829 top = curwin->w_cursor.lnum;
1830 bot = VIsual.lnum;
1831 }
1832# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001834 (void)hasFolding(top, &top, NULL);
1835 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836# endif
1837 lines = bot - top + 1;
1838
1839 if (VIsual_mode == Ctrl_V)
1840 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001841# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001842 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001843 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001845 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001846 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001847 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001850# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001851 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001852 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1855 (long)(rightcol - leftcol + 1));
1856 }
1857 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1858 sprintf((char *)showcmd_buf, "%ld", lines);
1859 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001860 {
1861 char_u *s, *e;
1862 int l;
1863 int bytes = 0;
1864 int chars = 0;
1865
1866 if (cursor_bot)
1867 {
1868 s = ml_get_pos(&VIsual);
1869 e = ml_get_cursor();
1870 }
1871 else
1872 {
1873 s = ml_get_cursor();
1874 e = ml_get_pos(&VIsual);
1875 }
1876 while ((*p_sel != 'e') ? s <= e : s < e)
1877 {
1878 l = (*mb_ptr2len)(s);
1879 if (l == 0)
1880 {
1881 ++bytes;
1882 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001883 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001884 }
1885 bytes += l;
1886 ++chars;
1887 s += l;
1888 }
1889 if (bytes == chars)
1890 sprintf((char *)showcmd_buf, "%d", chars);
1891 else
1892 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1893 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 showcmd_visual = TRUE;
1896 }
1897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 showcmd_buf[0] = NUL;
1900 showcmd_visual = FALSE;
1901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001902 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (showcmd_is_clear)
1904 return;
1905 }
1906
1907 display_showcmd();
1908}
1909
1910/*
1911 * Add 'c' to string of shown command chars.
1912 * Return TRUE if output has been written (and setcursor() has been called).
1913 */
1914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001915add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 char_u *p;
1918 int old_len;
1919 int extra_len;
1920 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 int i;
1922 static int ignore[] =
1923 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001924#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1926 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001927#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001928 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001929 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1931 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001932 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001934 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 0
1936 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
Bram Moolenaar09df3122006-01-23 22:23:09 +00001938 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 return FALSE;
1940
1941 if (showcmd_visual)
1942 {
1943 showcmd_buf[0] = NUL;
1944 showcmd_visual = FALSE;
1945 }
1946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (IS_SPECIAL(c))
1949 for (i = 0; ignore[i] != 0; ++i)
1950 if (ignore[i] == c)
1951 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001954 if (*p == ' ')
1955 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 old_len = (int)STRLEN(showcmd_buf);
1957 extra_len = (int)STRLEN(p);
1958 overflow = old_len + extra_len - SHOWCMD_COLS;
1959 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001960 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1961 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 STRCAT(showcmd_buf, p);
1963
1964 if (char_avail())
1965 return FALSE;
1966
1967 display_showcmd();
1968
1969 return TRUE;
1970}
1971
1972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 if (!add_to_showcmd(c))
1976 setcursor();
1977}
1978
1979/*
1980 * Delete 'len' characters from the end of the shown command.
1981 */
1982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001983del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
1985 int old_len;
1986
1987 if (!p_sc)
1988 return;
1989
1990 old_len = (int)STRLEN(showcmd_buf);
1991 if (len > old_len)
1992 len = old_len;
1993 showcmd_buf[old_len - len] = NUL;
1994
1995 if (!char_avail())
1996 display_showcmd();
1997}
1998
1999/*
2000 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2001 * something and there is a partial mapping.
2002 */
2003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 if (p_sc)
2007 STRCPY(old_showcmd_buf, showcmd_buf);
2008}
2009
2010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 if (!p_sc)
2014 return;
2015
2016 STRCPY(showcmd_buf, old_showcmd_buf);
2017
2018 display_showcmd();
2019}
2020
2021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002022display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int len;
2025
2026 cursor_off();
2027
2028 len = (int)STRLEN(showcmd_buf);
2029 if (len == 0)
2030 showcmd_is_clear = TRUE;
2031 else
2032 {
2033 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2034 showcmd_is_clear = FALSE;
2035 }
2036
2037 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002038 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2039 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 */
2041 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044}
2045#endif
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047/*
2048 * When "check" is FALSE, prepare for commands that scroll the window.
2049 * When "check" is TRUE, take care of scroll-binding after the window has
2050 * scrolled. Called from normal_cmd() and edit().
2051 */
2052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 static win_T *old_curwin = NULL;
2056 static linenr_T old_topline = 0;
2057#ifdef FEAT_DIFF
2058 static int old_topfill = 0;
2059#endif
2060 static buf_T *old_buf = NULL;
2061 static colnr_T old_leftcol = 0;
2062
2063 if (check && curwin->w_p_scb)
2064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 // If a ":syncbind" command was just used, don't scroll, only reset
2066 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (did_syncbind)
2068 did_syncbind = FALSE;
2069 else if (curwin == old_curwin)
2070 {
2071 /*
2072 * Synchronize other windows, as necessary according to
2073 * 'scrollbind'. Don't do this after an ":edit" command, except
2074 * when 'diff' is set.
2075 */
2076 if ((curwin->w_buffer == old_buf
2077#ifdef FEAT_DIFF
2078 || curwin->w_p_diff
2079#endif
2080 )
2081 && (curwin->w_topline != old_topline
2082#ifdef FEAT_DIFF
2083 || curwin->w_topfill != old_topfill
2084#endif
2085 || curwin->w_leftcol != old_leftcol))
2086 {
2087 check_scrollbind(curwin->w_topline - old_topline,
2088 (long)(curwin->w_leftcol - old_leftcol));
2089 }
2090 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002091 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 /*
2094 * When switching between windows, make sure that the relative
2095 * vertical offset is valid for the new window. The relative
2096 * offset is invalid whenever another 'scrollbind' window has
2097 * scrolled to a point that would force the current window to
2098 * scroll past the beginning or end of its buffer. When the
2099 * resync is performed, some of the other 'scrollbind' windows may
2100 * need to jump so that the current window's relative position is
2101 * visible on-screen.
2102 */
2103 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2104 }
2105 curwin->w_scbind_pos = curwin->w_topline;
2106 }
2107
2108 old_curwin = curwin;
2109 old_topline = curwin->w_topline;
2110#ifdef FEAT_DIFF
2111 old_topfill = curwin->w_topfill;
2112#endif
2113 old_buf = curwin->w_buffer;
2114 old_leftcol = curwin->w_leftcol;
2115}
2116
2117/*
2118 * Synchronize any windows that have "scrollbind" set, based on the
2119 * number of rows by which the current window has changed
2120 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2121 */
2122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 int want_ver;
2126 int want_hor;
2127 win_T *old_curwin = curwin;
2128 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 int old_VIsual_select = VIsual_select;
2130 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 colnr_T tgt_leftcol = curwin->w_leftcol;
2132 long topline;
2133 long y;
2134
2135 /*
2136 * check 'scrollopt' string for vertical and horizontal scroll options
2137 */
2138 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2139#ifdef FEAT_DIFF
2140 want_ver |= old_curwin->w_p_diff;
2141#endif
2142 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2143
2144 /*
2145 * loop through the scrollbound windows and scroll accordingly
2146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002148 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002151 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (curwin != old_curwin && curwin->w_p_scb)
2153 {
2154 /*
2155 * do the vertical scroll
2156 */
2157 if (want_ver)
2158 {
2159#ifdef FEAT_DIFF
2160 if (old_curwin->w_p_diff && curwin->w_p_diff)
2161 {
2162 diff_set_topline(old_curwin, curwin);
2163 }
2164 else
2165#endif
2166 {
2167 curwin->w_scbind_pos += topline_diff;
2168 topline = curwin->w_scbind_pos;
2169 if (topline > curbuf->b_ml.ml_line_count)
2170 topline = curbuf->b_ml.ml_line_count;
2171 if (topline < 1)
2172 topline = 1;
2173
2174 y = topline - curwin->w_topline;
2175 if (y > 0)
2176 scrollup(y, FALSE);
2177 else
2178 scrolldown(-y, FALSE);
2179 }
2180
2181 redraw_later(VALID);
2182 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185
2186 /*
2187 * do the horizontal scroll
2188 */
2189 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2190 {
2191 curwin->w_leftcol = tgt_leftcol;
2192 leftcol_changed();
2193 }
2194 }
2195 }
2196
2197 /*
2198 * reset current-window
2199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 VIsual_select = old_VIsual_select;
2201 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 curwin = old_curwin;
2203 curbuf = old_curbuf;
2204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
2206/*
2207 * Command character that's ignored.
2208 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002209 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002212nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
2217/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002218 * Command character that doesn't do anything, but unlike nv_ignore() does
2219 * start edit(). Used for "startinsert" executed while starting up.
2220 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002222nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002223{
2224}
2225
2226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * Command character doesn't exist.
2228 */
2229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002230nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 clearopbeep(cap->oap);
2233}
2234
2235/*
2236 * <Help> and <F1> commands.
2237 */
2238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241 if (!checkclearopq(cap->oap))
2242 ex_help(NULL);
2243}
2244
2245/*
2246 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2247 */
2248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002249nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002251#ifdef FEAT_JOB_CHANNEL
2252 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2253 clearopbeep(cap->oap);
2254 else
2255#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002256 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002257 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002258 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002259 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2260 op_addsub(cap->oap, cap->count1, cap->arg);
2261 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002262 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002263 else if (VIsual_active)
2264 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002265 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002266 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267}
2268
2269/*
2270 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2271 */
2272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277 if (mod_mask & MOD_MASK_CTRL)
2278 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002279 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002280 if (cap->arg == BACKWARD)
2281 goto_tabpage(-(int)cap->count1);
2282 else
2283 goto_tabpage((int)cap->count0);
2284 }
2285 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002286 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288}
2289
2290/*
2291 * Implementation of "gd" and "gD" command.
2292 */
2293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294nv_gd(
2295 oparg_T *oap,
2296 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002297 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 char_u *ptr;
2301
Bram Moolenaard9d30582005-05-18 22:10:28 +00002302 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002303 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2304 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002306#ifdef FEAT_FOLDING
2307 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2308 foldOpenCursor();
2309#endif
2310}
2311
2312/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002313 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2314 * otherwise.
2315 */
2316 static int
2317is_ident(char_u *line, int offset)
2318{
2319 int i;
2320 int incomment = FALSE;
2321 int instring = 0;
2322 int prev = 0;
2323
2324 for (i = 0; i < offset && line[i] != NUL; i++)
2325 {
2326 if (instring != 0)
2327 {
2328 if (prev != '\\' && line[i] == instring)
2329 instring = 0;
2330 }
2331 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2332 {
2333 instring = line[i];
2334 }
2335 else
2336 {
2337 if (incomment)
2338 {
2339 if (prev == '*' && line[i] == '/')
2340 incomment = FALSE;
2341 }
2342 else if (prev == '/' && line[i] == '*')
2343 {
2344 incomment = TRUE;
2345 }
2346 else if (prev == '/' && line[i] == '/')
2347 {
2348 return FALSE;
2349 }
2350 }
2351
2352 prev = line[i];
2353 }
2354
2355 return incomment == FALSE && instring == 0;
2356}
2357
2358/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002359 * Search for variable declaration of "ptr[len]".
2360 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2361 * current file ("gD").
2362 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002363 * Return FAIL when not found.
2364 */
2365 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002366find_decl(
2367 char_u *ptr,
2368 int len,
2369 int locally,
2370 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002372{
2373 char_u *pat;
2374 pos_T old_pos;
2375 pos_T par_pos;
2376 pos_T found_pos;
2377 int t;
2378 int save_p_ws;
2379 int save_p_scs;
2380 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002381 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002382 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002383 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002384
2385 if ((pat = alloc(len + 7)) == NULL)
2386 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // Put "\V" before the pattern to avoid that the special meaning of "."
2389 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002390 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2391 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 old_pos = curwin->w_cursor;
2393 save_p_ws = p_ws;
2394 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 p_ws = FALSE; // don't wrap around end of file now
2396 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
2399 * With "gD" go to line 1.
2400 * With "gd" Search back for the start of the current function, then go
2401 * back until a blank line. If this fails go to line 1.
2402 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002403 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002405 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002407 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 else
2410 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002411 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2413 --curwin->w_cursor.lnum;
2414 }
2415 curwin->w_cursor.col = 0;
2416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002418 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002419 for (;;)
2420 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002421 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002422 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002423 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002425
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002426 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002427 {
2428 pos_T *pos;
2429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 // Check that the block the match is in doesn't end before the
2431 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002432 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2433 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2434 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 // There can't be a useful match before the end of this block.
2437 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002438 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002439 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002440 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002441 }
2442
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002443 if (t == FAIL)
2444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 if (found_pos.lnum != 0)
2447 {
2448 curwin->w_cursor = found_pos;
2449 t = OK;
2450 }
2451 break;
2452 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002453 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002456 ++curwin->w_cursor.lnum;
2457 curwin->w_cursor.col = 0;
2458 continue;
2459 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002460 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002462 // If the current position is not a valid identifier and a previous
2463 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002464 if (!valid && found_pos.lnum != 0)
2465 {
2466 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002467 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002468 }
2469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002470 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002471 if (valid && !locally)
2472 break;
2473 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002474 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002476 if (found_pos.lnum != 0)
2477 curwin->w_cursor = found_pos;
2478 break;
2479 }
2480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // For finding a local variable and the match is before the "{" or
2482 // inside a comment, continue searching. For K&R style function
2483 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002484 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002485 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002486 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002487 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002488 // Remove SEARCH_START from flags to avoid getting stuck at one
2489 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002490 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002492
2493 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002495 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 curwin->w_cursor = old_pos;
2497 }
2498 else
2499 {
2500 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002501 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 reset_search_dir();
2503 }
2504
2505 vim_free(pat);
2506 p_ws = save_p_ws;
2507 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002508
2509 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510}
2511
2512/*
2513 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2514 * lines rather than lines in the file.
2515 * 'dist' must be positive.
2516 *
2517 * Return OK if able to move cursor, FAIL otherwise.
2518 */
2519 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002520nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
2522 int linelen = linetabsize(ml_get_curline());
2523 int retval = OK;
2524 int atend = FALSE;
2525 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002526 int col_off1; // margin offset for first screen line
2527 int col_off2; // margin offset for wrapped screen line
2528 int width1; // text width for first screen line
2529 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002532 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 col_off1 = curwin_col_off();
2535 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002536 width1 = curwin->w_width - col_off1;
2537 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002538 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /*
2544 * Instead of sticking at the last character of the buffer line we
2545 * try to stick in the last column of the screen.
2546 */
2547 if (curwin->w_curswant == MAXCOL)
2548 {
2549 atend = TRUE;
2550 validate_virtcol();
2551 if (width1 <= 0)
2552 curwin->w_curswant = 0;
2553 else
2554 {
2555 curwin->w_curswant = width1 - 1;
2556 if (curwin->w_virtcol > curwin->w_curswant)
2557 curwin->w_curswant += ((curwin->w_virtcol
2558 - curwin->w_curswant - 1) / width2 + 1) * width2;
2559 }
2560 }
2561 else
2562 {
2563 if (linelen > width1)
2564 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2565 else
2566 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002567 if (curwin->w_curswant >= (colnr_T)n)
2568 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570
2571 while (dist--)
2572 {
2573 if (dir == BACKWARD)
2574 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002575 if ((long)curwin->w_curswant >= width1
2576#ifdef FEAT_FOLDING
2577 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2578#endif
2579 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002580 // Move back within the line. This can give a negative value
2581 // for w_curswant if width1 < width2 (with cpoptions+=n),
2582 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 curwin->w_curswant -= width2;
2584 else
2585 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002586 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002588 // Move to the start of a closed fold. Don't do that when
2589 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (!(fdo_flags & FDO_ALL))
2591 (void)hasFolding(curwin->w_cursor.lnum,
2592 &curwin->w_cursor.lnum, NULL);
2593#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002594 if (curwin->w_cursor.lnum == 1)
2595 {
2596 retval = FAIL;
2597 break;
2598 }
2599 --curwin->w_cursor.lnum;
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 linelen = linetabsize(ml_get_curline());
2602 if (linelen > width1)
2603 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2604 + 1) * width2;
2605 }
2606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002607 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 if (linelen > width1)
2610 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2611 else
2612 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002613 if (curwin->w_curswant + width2 < (colnr_T)n
2614#ifdef FEAT_FOLDING
2615 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2616#endif
2617 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002618 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 curwin->w_curswant += width2;
2620 else
2621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002622 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002624 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2626 &curwin->w_cursor.lnum);
2627#endif
2628 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2629 {
2630 retval = FAIL;
2631 break;
2632 }
2633 curwin->w_cursor.lnum++;
2634 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002635 // Check if the cursor has moved below the number display
2636 // when width1 < width2 (with cpoptions+=n). Subtract width2
2637 // to get a negative value for w_curswant, which will get
2638 // clipped to column 0.
2639 if (curwin->w_curswant >= width1)
2640 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002641 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 }
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002647 if (virtual_active() && atend)
2648 coladvance(MAXCOL);
2649 else
2650 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2653 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002654 colnr_T virtcol;
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 /*
2657 * Check for landing on a character that got split at the end of the
2658 * last line. We want to advance a screenline, not end up in the same
2659 * screenline or move two screenlines.
2660 */
2661 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002662 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002663#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002664 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2665 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002666#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002667
2668 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && (curwin->w_curswant < (colnr_T)width1
2670 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2671 : ((curwin->w_curswant - width1) % width2
2672 > (colnr_T)width2 / 2)))
2673 --curwin->w_cursor.col;
2674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 return retval;
2680}
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682/*
2683 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2684 * cap->arg must be TRUE for CTRL-E.
2685 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002687nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 if (!checkclearop(cap->oap))
2690 scroll_redraw(cap->arg, cap->count1);
2691}
2692
2693/*
2694 * Scroll "count" lines up or down, and redraw.
2695 */
2696 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002697scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 linenr_T prev_topline = curwin->w_topline;
2700#ifdef FEAT_DIFF
2701 int prev_topfill = curwin->w_topfill;
2702#endif
2703 linenr_T prev_lnum = curwin->w_cursor.lnum;
2704
2705 if (up)
2706 scrollup(count, TRUE);
2707 else
2708 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002709 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2712 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 cursor_correct();
2714 check_cursor_moved(curwin);
2715 curwin->w_valid |= VALID_TOPLINE;
2716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // If moved back to where we were, at least move the cursor, otherwise
2718 // we get stuck at one position. Don't move the cursor up if the
2719 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 while (curwin->w_topline == prev_topline
2721#ifdef FEAT_DIFF
2722 && curwin->w_topfill == prev_topfill
2723#endif
2724 )
2725 {
2726 if (up)
2727 {
2728 if (curwin->w_cursor.lnum > prev_lnum
2729 || cursor_down(1L, FALSE) == FAIL)
2730 break;
2731 }
2732 else
2733 {
2734 if (curwin->w_cursor.lnum < prev_lnum
2735 || prev_topline == 1L
2736 || cursor_up(1L, FALSE) == FAIL)
2737 break;
2738 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // Mark w_topline as valid, otherwise the screen jumps back at the
2740 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 check_cursor_moved(curwin);
2742 curwin->w_valid |= VALID_TOPLINE;
2743 }
2744 }
2745 if (curwin->w_cursor.lnum != prev_lnum)
2746 coladvance(curwin->w_curswant);
2747 redraw_later(VALID);
2748}
2749
2750/*
2751 * Commands that start with "z".
2752 */
2753 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002754nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 long n;
2757 colnr_T col;
2758 int nchar = cap->nchar;
2759#ifdef FEAT_FOLDING
2760 long old_fdl = curwin->w_p_fdl;
2761 int old_fen = curwin->w_p_fen;
2762#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002763#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002764 int undo = FALSE;
2765#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002766 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 if (VIM_ISDIGIT(nchar))
2769 {
2770 /*
2771 * "z123{nchar}": edit the count before obtaining {nchar}
2772 */
2773 if (checkclearop(cap->oap))
2774 return;
2775 n = nchar - '0';
2776 for (;;)
2777 {
2778#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#endif
2781 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002782 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002783 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 --no_mapping;
2786 --allow_keys;
2787#ifdef FEAT_CMDL_INFO
2788 (void)add_to_showcmd(nchar);
2789#endif
2790 if (nchar == K_DEL || nchar == K_KDEL)
2791 n /= 10;
2792 else if (VIM_ISDIGIT(nchar))
2793 n = n * 10 + (nchar - '0');
2794 else if (nchar == CAR)
2795 {
2796#ifdef FEAT_GUI
2797 need_mouse_correct = TRUE;
2798#endif
2799 win_setheight((int)n);
2800 break;
2801 }
2802 else if (nchar == 'l'
2803 || nchar == 'h'
2804 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002805 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
2807 cap->count1 = n ? n * cap->count1 : cap->count1;
2808 goto dozet;
2809 }
2810 else
2811 {
2812 clearopbeep(cap->oap);
2813 break;
2814 }
2815 }
2816 cap->oap->op_type = OP_NOP;
2817 return;
2818 }
2819
2820dozet:
2821 if (
2822#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2824 // and "zC" only in Visual mode. "zj" and "zk" are motion
2825 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 cap->nchar != 'f' && cap->nchar != 'F'
2827 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2828 && cap->nchar != 'j' && cap->nchar != 'k'
2829 &&
2830#endif
2831 checkclearop(cap->oap))
2832 return;
2833
2834 /*
2835 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2836 * If line number given, set cursor.
2837 */
2838 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2839 && cap->count0
2840 && cap->count0 != curwin->w_cursor.lnum)
2841 {
2842 setpcmark();
2843 if (cap->count0 > curbuf->b_ml.ml_line_count)
2844 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2845 else
2846 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002847 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
2850 switch (nchar)
2851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 case '+':
2854 if (cap->count0 == 0)
2855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // No count given: put cursor at the line below screen
2857 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2860 else
2861 curwin->w_cursor.lnum = curwin->w_botline;
2862 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case NL:
2865 case CAR:
2866 case K_KENTER:
2867 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 case 't': scroll_cursor_top(0, TRUE);
2871 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002872 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
2874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 case 'z': scroll_cursor_halfway(TRUE);
2880 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002881 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 break;
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // "z^", "z-" and "zb": put cursor at bottom of screen
2885 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2886 // when <count> is at bottom of window, and puts that one at
2887 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (cap->count0 != 0)
2889 {
2890 scroll_cursor_bot(0, TRUE);
2891 curwin->w_cursor.lnum = curwin->w_topline;
2892 }
2893 else if (curwin->w_topline == 1)
2894 curwin->w_cursor.lnum = 1;
2895 else
2896 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case '-':
2899 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
2902 case 'b': scroll_cursor_bot(0, TRUE);
2903 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002904 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 break;
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'h':
2914 case K_LEFT:
2915 if (!curwin->w_p_wrap)
2916 {
2917 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2918 curwin->w_leftcol = 0;
2919 else
2920 curwin->w_leftcol -= (colnr_T)cap->count1;
2921 leftcol_changed();
2922 }
2923 break;
2924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002926 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 case 'l':
2931 case K_RIGHT:
2932 if (!curwin->w_p_wrap)
2933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 curwin->w_leftcol += (colnr_T)cap->count1;
2936 leftcol_changed();
2937 }
2938 break;
2939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 case 's': if (!curwin->w_p_wrap)
2942 {
2943#ifdef FEAT_FOLDING
2944 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else
2947#endif
2948 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002949 if ((long)col > siso)
2950 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else
2952 col = 0;
2953 if (curwin->w_leftcol != col)
2954 {
2955 curwin->w_leftcol = col;
2956 redraw_later(NOT_VALID);
2957 }
2958 }
2959 break;
2960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 case 'e': if (!curwin->w_p_wrap)
2963 {
2964#ifdef FEAT_FOLDING
2965 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
2968#endif
2969 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002970 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002971 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 col = 0;
2973 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002974 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (curwin->w_leftcol != col)
2976 {
2977 curwin->w_leftcol = col;
2978 redraw_later(NOT_VALID);
2979 }
2980 }
2981 break;
2982
Christian Brabandt2fa93842021-05-30 22:17:25 +02002983 // "zp", "zP" in block mode put without addind trailing spaces
2984 case 'P':
2985 case 'p': nv_put(cap);
2986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // "zF": create fold command
2989 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 case 'F':
2991 case 'f': if (foldManualAllowed(TRUE))
2992 {
2993 cap->nchar = 'f';
2994 nv_operator(cap);
2995 curwin->w_p_fen = TRUE;
2996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2999 {
3000 nv_operator(cap);
3001 finish_op = TRUE;
3002 }
3003 }
3004 else
3005 clearopbeep(cap->oap);
3006 break;
3007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // "zd": delete fold at cursor
3009 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 case 'd':
3011 case 'D': if (foldManualAllowed(FALSE))
3012 {
3013 if (VIsual_active)
3014 nv_operator(cap);
3015 else
3016 deleteFold(curwin->w_cursor.lnum,
3017 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3018 }
3019 break;
3020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 case 'E': if (foldmethodIsManual(curwin))
3023 {
3024 clearFolding(curwin);
3025 changed_window_setting();
3026 }
3027 else if (foldmethodIsMarker(curwin))
3028 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3029 TRUE, FALSE);
3030 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003031 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 break;
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 case 'n': curwin->w_p_fen = FALSE;
3036 break;
3037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003038 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 case 'N': curwin->w_p_fen = TRUE;
3040 break;
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3044 break;
3045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3048 openFold(curwin->w_cursor.lnum, cap->count1);
3049 else
3050 {
3051 closeFold(curwin->w_cursor.lnum, cap->count1);
3052 curwin->w_p_fen = TRUE;
3053 }
3054 break;
3055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3058 openFoldRecurse(curwin->w_cursor.lnum);
3059 else
3060 {
3061 closeFoldRecurse(curwin->w_cursor.lnum);
3062 curwin->w_p_fen = TRUE;
3063 }
3064 break;
3065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003066 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 case 'o': if (VIsual_active)
3068 nv_operator(cap);
3069 else
3070 openFold(curwin->w_cursor.lnum, cap->count1);
3071 break;
3072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 case 'O': if (VIsual_active)
3075 nv_operator(cap);
3076 else
3077 openFoldRecurse(curwin->w_cursor.lnum);
3078 break;
3079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 case 'c': if (VIsual_active)
3082 nv_operator(cap);
3083 else
3084 closeFold(curwin->w_cursor.lnum, cap->count1);
3085 curwin->w_p_fen = TRUE;
3086 break;
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 case 'C': if (VIsual_active)
3090 nv_operator(cap);
3091 else
3092 closeFoldRecurse(curwin->w_cursor.lnum);
3093 curwin->w_p_fen = TRUE;
3094 break;
3095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 case 'v': foldOpenCursor();
3098 break;
3099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 curwin->w_foldinvalid = TRUE; // recompute folds
3103 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 foldOpenCursor();
3105 break;
3106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 curwin->w_foldinvalid = TRUE; // recompute folds
3110 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 break;
3112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003113 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003115 {
3116 curwin->w_p_fdl -= cap->count1;
3117 if (curwin->w_p_fdl < 0)
3118 curwin->w_p_fdl = 0;
3119 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003120 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 curwin->w_p_fen = TRUE;
3122 break;
3123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003126 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 curwin->w_p_fen = TRUE;
3128 break;
3129
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003131 case 'r': curwin->w_p_fdl += cap->count1;
3132 {
3133 int d = getDeepestNesting();
3134
3135 if (curwin->w_p_fdl >= d)
3136 curwin->w_p_fdl = d;
3137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 break;
3139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003142 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 break;
3144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 case 'j': // "zj" move to next fold downwards
3146 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3148 cap->count1) == FAIL)
3149 clearopbeep(cap->oap);
3150 break;
3151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003154#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003156 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003158 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003159 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003160 --no_mapping;
3161 --allow_keys;
3162#ifdef FEAT_CMDL_INFO
3163 (void)add_to_showcmd(nchar);
3164#endif
3165 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3166 {
3167 clearopbeep(cap->oap);
3168 break;
3169 }
3170 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 case 'g': // "zg": add good word to word list
3174 case 'w': // "zw": add wrong word to word list
3175 case 'G': // "zG": add good word to temp word list
3176 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003177 {
3178 char_u *ptr = NULL;
3179 int len;
3180
3181 if (checkclearop(cap->oap))
3182 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003183 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3184 == FAIL)
3185 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003186 if (ptr == NULL)
3187 {
3188 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 // Find bad word under the cursor. When 'spell' is
3191 // off this fails and find_ident_under_cursor() is
3192 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003193 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003194 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003195 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003196 if (len != 0 && curwin->w_cursor.col <= pos.col)
3197 ptr = ml_get_pos(&curwin->w_cursor);
3198 curwin->w_cursor = pos;
3199 }
3200
Bram Moolenaarb765d632005-06-07 21:00:02 +00003201 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3202 FIND_IDENT)) == 0)
3203 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003204 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3205 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003206 (nchar == 'G' || nchar == 'W')
3207 ? 0 : (int)cap->count1,
3208 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003209 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003210 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003212 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003213 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003214 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003215 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003216#endif
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 default: clearopbeep(cap->oap);
3219 }
3220
3221#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (old_fen != curwin->w_p_fen)
3224 {
3225# ifdef FEAT_DIFF
3226 win_T *wp;
3227
3228 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3229 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003230 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 FOR_ALL_WINDOWS(wp)
3232 {
3233 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3234 {
3235 wp->w_p_fen = curwin->w_p_fen;
3236 changed_window_setting_win(wp);
3237 }
3238 }
3239 }
3240# endif
3241 changed_window_setting();
3242 }
3243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003244 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (old_fdl != curwin->w_p_fdl)
3246 newFoldLevel();
3247#endif
3248}
3249
3250#ifdef FEAT_GUI
3251/*
3252 * Vertical scrollbar movement.
3253 */
3254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003255nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 if (cap->oap->op_type != OP_NOP)
3258 clearopbeep(cap->oap);
3259
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003260 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 gui_do_scroll();
3262}
3263
3264/*
3265 * Horizontal scrollbar movement.
3266 */
3267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003268nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if (cap->oap->op_type != OP_NOP)
3271 clearopbeep(cap->oap);
3272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003274 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275}
3276#endif
3277
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003278#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003279/*
3280 * Click in GUI tab.
3281 */
3282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003284{
3285 if (cap->oap->op_type != OP_NOP)
3286 clearopbeep(cap->oap);
3287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003288 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003289 goto_tabpage(current_tab);
3290}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003291
3292/*
3293 * Selected item in tab line menu.
3294 */
3295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003296nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003297{
3298 if (cap->oap->op_type != OP_NOP)
3299 clearopbeep(cap->oap);
3300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003301 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003302 handle_tabmenu();
3303}
3304
3305/*
3306 * Handle selecting an item of the GUI tab line menu.
3307 * Used in Normal and Insert mode.
3308 */
3309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003310handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003311{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003312 switch (current_tabmenu)
3313 {
3314 case TABLINE_MENU_CLOSE:
3315 if (current_tab == 0)
3316 do_cmdline_cmd((char_u *)"tabclose");
3317 else
3318 {
3319 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3320 current_tab);
3321 do_cmdline_cmd(IObuff);
3322 }
3323 break;
3324
3325 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003326 if (current_tab == 0)
3327 do_cmdline_cmd((char_u *)"$tabnew");
3328 else
3329 {
3330 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3331 current_tab - 1);
3332 do_cmdline_cmd(IObuff);
3333 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003334 break;
3335
3336 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003337 if (current_tab == 0)
3338 do_cmdline_cmd((char_u *)"browse $tabnew");
3339 else
3340 {
3341 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3342 current_tab - 1);
3343 do_cmdline_cmd(IObuff);
3344 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003345 break;
3346 }
3347}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003348#endif
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350/*
3351 * "Q" command.
3352 */
3353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 /*
3357 * Ignore 'Q' in Visual mode, just give a beep.
3358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003360 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003361 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 do_exmode(FALSE);
3363}
3364
3365/*
3366 * Handle a ":" command.
3367 */
3368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003369nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003371 int old_p_im;
3372 int cmd_result;
3373 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar957cf672020-11-12 14:21:06 +01003375 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 nv_operator(cap);
3377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 if (cap->oap->op_type != OP_NOP)
3380 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003381 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 cap->oap->motion_type = MCHAR;
3383 cap->oap->inclusive = FALSE;
3384 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003385 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003387 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 stuffcharReadbuff('.');
3389 if (cap->count0 > 1)
3390 {
3391 stuffReadbuff((char_u *)",.+");
3392 stuffnumReadbuff((long)cap->count0 - 1L);
3393 }
3394 }
3395
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003396 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (KeyTyped)
3398 compute_cmdrow();
3399
3400 old_p_im = p_im;
3401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003402 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003403 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003406 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (p_im != old_p_im)
3408 {
3409 if (p_im)
3410 restart_edit = 'i';
3411 else
3412 restart_edit = 0;
3413 }
3414
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003415 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003416 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003417 clearop(cap->oap);
3418 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3420 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003421 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3422 || did_emsg
3423 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003424 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 clearopbeep(cap->oap);
3426 }
3427}
3428
3429/*
3430 * Handle CTRL-G command.
3431 */
3432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003433nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003435 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
3437 VIsual_select = !VIsual_select;
3438 showmode();
3439 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003440 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003441 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 fileinfo((int)cap->count0, FALSE, TRUE);
3443}
3444
3445/*
3446 * Handle CTRL-H <Backspace> command.
3447 */
3448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003449nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (VIsual_active && VIsual_select)
3452 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003453 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 v_visop(cap);
3455 }
3456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 nv_left(cap);
3458}
3459
3460/*
3461 * CTRL-L: clear screen and redraw.
3462 */
3463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003464nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 if (!checkclearop(cap->oap))
3467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003469 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003470 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003471# ifdef FEAT_RELTIME
3472 {
3473 win_T *wp;
3474
3475 FOR_ALL_WINDOWS(wp)
3476 wp->w_s->b_syn_slow = FALSE;
3477 }
3478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#endif
3480 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003481#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3482# ifdef VIMDLL
3483 if (!gui.in_use)
3484# endif
3485 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488}
3489
3490/*
3491 * CTRL-O: In Select mode: switch to Visual mode for one command.
3492 * Otherwise: Go to older pcmark.
3493 */
3494 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003495nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (VIsual_active && VIsual_select)
3498 {
3499 VIsual_select = FALSE;
3500 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003501 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 cap->count1 = -cap->count1;
3506 nv_pcmark(cap);
3507 }
3508}
3509
3510/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003511 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3512 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 */
3514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003515nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 if (!checkclearopq(cap->oap))
3518 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3519 GETF_SETMARK|GETF_ALT, FALSE);
3520}
3521
3522/*
3523 * "Z" commands.
3524 */
3525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003526nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528 if (!checkclearopq(cap->oap))
3529 {
3530 switch (cap->nchar)
3531 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003532 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 case 'Z': do_cmdline_cmd((char_u *)"x");
3534 break;
3535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 case 'Q': do_cmdline_cmd((char_u *)"q!");
3538 break;
3539
3540 default: clearopbeep(cap->oap);
3541 }
3542 }
3543}
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
3546 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3547 */
3548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003549do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
3551 oparg_T oa;
3552 cmdarg_T ca;
3553
3554 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003555 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 ca.oap = &oa;
3557 ca.cmdchar = c1;
3558 ca.nchar = c2;
3559 nv_ident(&ca);
3560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562/*
3563 * Handle the commands that use the word under the cursor.
3564 * [g] CTRL-] :ta to current identifier
3565 * [g] 'K' run program for current identifier
3566 * [g] '*' / to current identifier or string
3567 * [g] '#' ? to current identifier or string
3568 * g ']' :tselect for current identifier
3569 */
3570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003571nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 char_u *ptr = NULL;
3574 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003575 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003576 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 char_u *kp; // value of 'keywordprg'
3579 int kp_help; // 'keywordprg' is ":he"
3580 int kp_ex; // 'keywordprg' starts with ":"
3581 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003584 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *aux_ptr;
3586 int isman;
3587 int isman_s;
3588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003589 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 cmdchar = cap->nchar;
3592 g_cmd = TRUE;
3593 }
3594 else
3595 {
3596 cmdchar = cap->cmdchar;
3597 g_cmd = FALSE;
3598 }
3599
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003600 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 cmdchar = '#';
3602
3603 /*
3604 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3605 */
3606 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3609 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (checkclearopq(cap->oap))
3611 return;
3612 }
3613
3614 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3615 (cmdchar == '*' || cmdchar == '#')
3616 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3617 {
3618 clearop(cap->oap);
3619 return;
3620 }
3621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003622 // Allocate buffer to put the command in. Inserting backslashes can
3623 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3624 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3626 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3627 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003628 if (kp_help && *skipwhite(ptr) == NUL)
3629 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003630 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003631 return;
3632 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003633 kp_ex = (*kp == ':');
3634 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3635 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (buf == NULL)
3637 return;
3638 buf[0] = NUL;
3639
3640 switch (cmdchar)
3641 {
3642 case '*':
3643 case '#':
3644 /*
3645 * Put cursor at start of word, makes search skip the word
3646 * under the cursor.
3647 * Call setpcmark() first, so "*``" puts the cursor back where
3648 * it was.
3649 */
3650 setpcmark();
3651 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3652
3653 if (!g_cmd && vim_iswordp(ptr))
3654 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003655 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 break;
3657
3658 case 'K':
3659 if (kp_help)
3660 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003661 else if (kp_ex)
3662 {
3663 if (cap->count0 != 0)
3664 vim_snprintf((char *)buf, buflen, "%s %ld",
3665 kp, cap->count0);
3666 else
3667 STRCPY(buf, kp);
3668 STRCAT(buf, " ");
3669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 else
3671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003672 // An external command will probably use an argument starting
3673 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003674 while (*ptr == '-' && n > 0)
3675 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003676 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003677 --n;
3678 }
3679 if (n == 0)
3680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003681 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003682 vim_free(buf);
3683 return;
3684 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003686 // When a count is given, turn it into a range. Is this
3687 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 isman = (STRCMP(kp, "man") == 0);
3689 isman_s = (STRCMP(kp, "man -s") == 0);
3690 if (cap->count0 != 0 && !(isman || isman_s))
3691 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3692
3693 STRCAT(buf, "! ");
3694 if (cap->count0 == 0 && isman_s)
3695 STRCAT(buf, "man");
3696 else
3697 STRCAT(buf, kp);
3698 STRCAT(buf, " ");
3699 if (cap->count0 != 0 && (isman || isman_s))
3700 {
3701 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3702 STRCAT(buf, " ");
3703 }
3704 }
3705 break;
3706
3707 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003708 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709#ifdef FEAT_CSCOPE
3710 if (p_cst)
3711 STRCPY(buf, "cstag ");
3712 else
3713#endif
3714 STRCPY(buf, "ts ");
3715 break;
3716
3717 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003718 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (curbuf->b_help)
3720 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003722 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003723 if (g_cmd)
3724 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003725 else if (cap->count0 == 0)
3726 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003727 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003728 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731
3732 /*
3733 * Now grab the chars in the identifier
3734 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003735 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003737 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003738 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003739 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003740 p = vim_strsave_fnameescape(ptr, FALSE);
3741 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003743 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003744 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003745 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003747 vim_free(buf);
3748 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003750 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003751 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003752 {
3753 vim_free(buf);
3754 vim_free(p);
3755 return;
3756 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003757 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003758 STRCAT(buf, p);
3759 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003761 else
3762 {
3763 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003764 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003765 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003766 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003767 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003768 {
3769 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003770 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003771 aux_ptr = (char_u *)"";
3772 else
3773 aux_ptr = (char_u *)"\\|\"\n[";
3774 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003775 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003776 aux_ptr = (char_u *)"\\|\"\n*?[";
3777
3778 p = buf + STRLEN(buf);
3779 while (n-- > 0)
3780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003781 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003782 if (vim_strchr(aux_ptr, *ptr) != NULL)
3783 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003784 // When current byte is a part of multibyte character, copy all
3785 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003786 if (has_mbyte)
3787 {
3788 int i;
3789 int len = (*mb_ptr2len)(ptr) - 1;
3790
3791 for (i = 0; i < len && n >= 1; ++i, --n)
3792 *p++ = *ptr++;
3793 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003794 *p++ = *ptr++;
3795 }
3796 *p = NUL;
3797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 /*
3800 * Execute the command.
3801 */
3802 if (cmdchar == '*' || cmdchar == '#')
3803 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003804 if (!g_cmd && (has_mbyte
3805 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3806 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003808
3809 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003810 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003812
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003813 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003816 {
3817 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003819 g_tag_at_cursor = FALSE;
3820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
3822 vim_free(buf);
3823}
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825/*
3826 * Get visually selected text, within one line only.
3827 * Returns FAIL if more than one line selected.
3828 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003830get_visual_text(
3831 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 char_u **pp, // return: start of selected text
3833 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
3835 if (VIsual_mode != 'V')
3836 unadjust_for_sel();
3837 if (VIsual.lnum != curwin->w_cursor.lnum)
3838 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003839 if (cap != NULL)
3840 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return FAIL;
3842 }
3843 if (VIsual_mode == 'V')
3844 {
3845 *pp = ml_get_curline();
3846 *lenp = (int)STRLEN(*pp);
3847 }
3848 else
3849 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003850 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 *pp = ml_get_pos(&curwin->w_cursor);
3853 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3854 }
3855 else
3856 {
3857 *pp = ml_get_pos(&VIsual);
3858 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003861 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003862 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 reset_VIsual_and_resel();
3865 return OK;
3866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868/*
3869 * CTRL-T: backwards in tag stack
3870 */
3871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003872nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
3874 if (!checkclearopq(cap->oap))
3875 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3876}
3877
3878/*
3879 * Handle scrolling command 'H', 'L' and 'M'.
3880 */
3881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003882nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
3884 int used = 0;
3885 long n;
3886#ifdef FEAT_FOLDING
3887 linenr_T lnum;
3888#endif
3889 int half;
3890
3891 cap->oap->motion_type = MLINE;
3892 setpcmark();
3893
3894 if (cap->cmdchar == 'L')
3895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003896 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 curwin->w_cursor.lnum = curwin->w_botline - 1;
3898 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3899 curwin->w_cursor.lnum = 1;
3900 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003901 {
3902#ifdef FEAT_FOLDING
3903 if (hasAnyFolding(curwin))
3904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003905 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003906 for (n = cap->count1 - 1; n > 0
3907 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3908 {
3909 (void)hasFolding(curwin->w_cursor.lnum,
3910 &curwin->w_cursor.lnum, NULL);
3911 --curwin->w_cursor.lnum;
3912 }
3913 }
3914 else
3915#endif
3916 curwin->w_cursor.lnum -= cap->count1 - 1;
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 else
3920 {
3921 if (cap->cmdchar == 'M')
3922 {
3923#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003924 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 used -= diff_check_fill(curwin, curwin->w_topline)
3926 - curwin->w_topfill;
3927#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003928 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3930 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3931 {
3932#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003933 // Count half he number of filler lines to be "below this
3934 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3936 + n) / 2 >= half)
3937 {
3938 --n;
3939 break;
3940 }
3941#endif
3942 used += plines(curwin->w_topline + n);
3943 if (used >= half)
3944 break;
3945#ifdef FEAT_FOLDING
3946 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3947 n = lnum - curwin->w_topline;
3948#endif
3949 }
3950 if (n > 0 && used > curwin->w_height)
3951 --n;
3952 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003953 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003956#ifdef FEAT_FOLDING
3957 if (hasAnyFolding(curwin))
3958 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003959 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003960 lnum = curwin->w_topline;
3961 while (n-- > 0 && lnum < curwin->w_botline - 1)
3962 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003963 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003964 ++lnum;
3965 }
3966 n = lnum - curwin->w_topline;
3967 }
3968#endif
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 curwin->w_cursor.lnum = curwin->w_topline + n;
3971 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3972 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3973 }
3974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003975 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003976 if (cap->oap->op_type == OP_NOP)
3977 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 beginline(BL_SOL | BL_FIX);
3979}
3980
3981/*
3982 * Cursor right commands.
3983 */
3984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003985nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003988 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003990 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003992 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993 if (mod_mask & MOD_MASK_CTRL)
3994 cap->arg = TRUE;
3995 nv_wordcmd(cap);
3996 return;
3997 }
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 cap->oap->motion_type = MCHAR;
4000 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004001 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004004 * In virtual edit mode, there's no such thing as "past_line", as lines
4005 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 */
4007 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004008 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 for (n = cap->count1; n > 0; --n)
4011 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004012 if ((!past_line && oneright() == FAIL)
4013 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004014 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004017 * <Space> wraps to next line if 'whichwrap' has 's'.
4018 * 'l' wraps to next line if 'whichwrap' has 'l'.
4019 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 */
4021 if ( ((cap->cmdchar == ' '
4022 && vim_strchr(p_ww, 's') != NULL)
4023 || (cap->cmdchar == 'l'
4024 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004025 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 && vim_strchr(p_ww, '>') != NULL))
4027 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004029 // When deleting we also count the NL as a character.
4030 // Set cap->oap->inclusive when last char in the line is
4031 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004032 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004034 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 cap->oap->inclusive = TRUE;
4036 else
4037 {
4038 ++curwin->w_cursor.lnum;
4039 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 curwin->w_set_curswant = TRUE;
4042 cap->oap->inclusive = FALSE;
4043 }
4044 continue;
4045 }
4046 if (cap->oap->op_type == OP_NOP)
4047 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004048 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (n == cap->count1)
4050 beep_flush();
4051 }
4052 else
4053 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004054 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 cap->oap->inclusive = TRUE;
4056 }
4057 break;
4058 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004059 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
4061 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (virtual_active())
4063 oneright();
4064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004067 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 ++curwin->w_cursor.col;
4070 }
4071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073#ifdef FEAT_FOLDING
4074 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4075 && cap->oap->op_type == OP_NOP)
4076 foldOpenCursor();
4077#endif
4078}
4079
4080/*
4081 * Cursor left commands.
4082 *
4083 * Returns TRUE when operator end should not be adjusted.
4084 */
4085 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004086nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 long n;
4089
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004090 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004092 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004093 if (mod_mask & MOD_MASK_CTRL)
4094 cap->arg = 1;
4095 nv_bck_word(cap);
4096 return;
4097 }
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 cap->oap->motion_type = MCHAR;
4100 cap->oap->inclusive = FALSE;
4101 for (n = cap->count1; n > 0; --n)
4102 {
4103 if (oneleft() == FAIL)
4104 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4106 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4107 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if ( (((cap->cmdchar == K_BS
4109 || cap->cmdchar == Ctrl_H)
4110 && vim_strchr(p_ww, 'b') != NULL)
4111 || (cap->cmdchar == 'h'
4112 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004113 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 && vim_strchr(p_ww, '<') != NULL))
4115 && curwin->w_cursor.lnum > 1)
4116 {
4117 --(curwin->w_cursor.lnum);
4118 coladvance((colnr_T)MAXCOL);
4119 curwin->w_set_curswant = TRUE;
4120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004121 // When the NL before the first char has to be deleted we
4122 // put the cursor on the NUL after the previous line.
4123 // This is a very special case, be careful!
4124 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if ( (cap->oap->op_type == OP_DELETE
4126 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004127 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004129 char_u *cp = ml_get_cursor();
4130
4131 if (*cp != NUL)
4132 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004133 if (has_mbyte)
4134 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4135 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004136 ++curwin->w_cursor.col;
4137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 cap->retval |= CA_NO_ADJ_OP_END;
4139 }
4140 continue;
4141 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004142 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4144 beep_flush();
4145 break;
4146 }
4147 }
4148#ifdef FEAT_FOLDING
4149 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4150 && cap->oap->op_type == OP_NOP)
4151 foldOpenCursor();
4152#endif
4153}
4154
4155/*
4156 * Cursor up commands.
4157 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4158 */
4159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004160nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004162 if (mod_mask & MOD_MASK_SHIFT)
4163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004164 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004165 cap->arg = BACKWARD;
4166 nv_page(cap);
4167 }
4168 else
4169 {
4170 cap->oap->motion_type = MLINE;
4171 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4172 clearopbeep(cap->oap);
4173 else if (cap->arg)
4174 beginline(BL_WHITE | BL_FIX);
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176}
4177
4178/*
4179 * Cursor down commands.
4180 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4181 */
4182 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004183nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004185 if (mod_mask & MOD_MASK_SHIFT)
4186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004187 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004188 cap->arg = FORWARD;
4189 nv_page(cap);
4190 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004191#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004192 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004193 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4194 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004199 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004200 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 cmdwin_result = CAR;
4202 else
4203#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004204#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004205 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004206 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4207 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4208 {
4209 invoke_prompt_callback();
4210 if (restart_edit == 0)
4211 restart_edit = 'a';
4212 }
4213 else
4214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 cap->oap->motion_type = MLINE;
4217 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4218 clearopbeep(cap->oap);
4219 else if (cap->arg)
4220 beginline(BL_WHITE | BL_FIX);
4221 }
4222 }
4223}
4224
4225#ifdef FEAT_SEARCHPATH
4226/*
4227 * Grab the file name under the cursor and edit it.
4228 */
4229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004230nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004233 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004235 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004238 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return;
4240 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004241 if (curbuf_locked())
4242 {
4243 clearop(cap->oap);
4244 return;
4245 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004246#ifdef FEAT_PROP_POPUP
4247 if (ERROR_IF_TERM_POPUP_WINDOW)
4248 return;
4249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004251 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 if (ptr != NULL)
4254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004255 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004256 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004257 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004259 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004260 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004261 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004262 {
4263 curwin->w_cursor.lnum = lnum;
4264 check_cursor_lnum();
4265 beginline(BL_SOL | BL_FIX);
4266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 vim_free(ptr);
4268 }
4269 else
4270 clearop(cap->oap);
4271}
4272#endif
4273
4274/*
4275 * <End> command: to end of current line or last line.
4276 */
4277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004278nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004280 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004284 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 nv_dollar(cap);
4287}
4288
4289/*
4290 * Handle the "$" command.
4291 */
4292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004293nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294{
4295 cap->oap->motion_type = MCHAR;
4296 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004297 // In virtual mode when off the edge of a line and an operator
4298 // is pending (whew!) keep the cursor where it is.
4299 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (!virtual_active() || gchar_cursor() != NUL
4301 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004302 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (cursor_down((long)(cap->count1 - 1),
4304 cap->oap->op_type == OP_NOP) == FAIL)
4305 clearopbeep(cap->oap);
4306#ifdef FEAT_FOLDING
4307 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4308 foldOpenCursor();
4309#endif
4310}
4311
4312/*
4313 * Implementation of '?' and '/' commands.
4314 * If cap->arg is TRUE don't set PC mark.
4315 */
4316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004317nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
4319 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004320 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4323 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004324 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 cap->cmdchar = 'g';
4326 cap->nchar = '?';
4327 nv_operator(cap);
4328 return;
4329 }
4330
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004331 // When using 'incsearch' the cursor may be moved to set a different search
4332 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004333 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 if (cap->searchbuf == NULL)
4336 {
4337 clearop(oap);
4338 return;
4339 }
4340
Bram Moolenaar46539112015-02-17 15:43:57 +01004341 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004342 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004343 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344}
4345
4346/*
4347 * Handle "N" and "n" commands.
4348 * cap->arg is SEARCH_REV for "N", 0 for "n".
4349 */
4350 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004351nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004353 pos_T old = curwin->w_cursor;
4354 int wrapped = FALSE;
4355 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004356
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004357 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004358 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004359 // Avoid getting stuck on the current cursor position, which can
4360 // happen when an offset is given and the cursor is on the last char
4361 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004362 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004363 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004364 cap->count1 -= 1;
4365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366}
4367
4368/*
4369 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4370 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004371 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004373 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004374normal_search(
4375 cmdarg_T *cap,
4376 int dir,
4377 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004378 int opt, // extra flags for do_search()
4379 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
4381 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004382 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 cap->oap->motion_type = MCHAR;
4385 cap->oap->inclusive = FALSE;
4386 cap->oap->use_reg_one = TRUE;
4387 curwin->w_set_curswant = TRUE;
4388
Bram Moolenaara80faa82020-04-12 19:37:17 +02004389 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004390 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004391 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4392 if (wrapped != NULL)
4393 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (i == 0)
4395 clearop(cap->oap);
4396 else
4397 {
4398 if (i == 2)
4399 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401#ifdef FEAT_FOLDING
4402 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4403 foldOpenCursor();
4404#endif
4405 }
4406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004407 // "/$" will put the cursor after the end of the line, may need to
4408 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004410 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411}
4412
4413/*
4414 * Character search commands.
4415 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4416 * ',' and FALSE for ';'.
4417 * cap->nchar is NUL for ',' and ';' (repeat the search)
4418 */
4419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
4422 int t_cmd;
4423
4424 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4425 t_cmd = TRUE;
4426 else
4427 t_cmd = FALSE;
4428
4429 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4431 clearopbeep(cap->oap);
4432 else
4433 {
4434 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004435 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4437 && (t_cmd || cap->oap->op_type != OP_NOP))
4438 {
4439 colnr_T scol, ecol;
4440
4441 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4442 curwin->w_cursor.coladd = ecol - scol;
4443 }
4444 else
4445 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_FOLDING
4448 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4449 foldOpenCursor();
4450#endif
4451 }
4452}
4453
4454/*
4455 * "[" and "]" commands.
4456 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4457 */
4458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004459nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004461 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004463 pos_T *pos = NULL; // init for GCC
4464 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int flag;
4466 long n;
4467 int findc;
4468 int c;
4469
4470 cap->oap->motion_type = MCHAR;
4471 cap->oap->inclusive = FALSE;
4472 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004473 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475#ifdef FEAT_SEARCHPATH
4476 /*
4477 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4478 */
4479 if (cap->nchar == 'f')
4480 nv_gotofile(cap);
4481 else
4482#endif
4483
4484#ifdef FEAT_FIND_ID
4485 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004486 * Find the occurrence(s) of the identifier or define under cursor
4487 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 *
4489 * search list jump
4490 * fwd bwd fwd bwd fwd bwd
4491 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4492 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4493 */
4494 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004495# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004497# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 cap->nchar) != NULL)
4501 {
4502 char_u *ptr;
4503 int len;
4504
4505 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4506 clearop(cap->oap);
4507 else
4508 {
4509 find_pattern_in_path(ptr, 0, len, TRUE,
4510 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4511 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4512 cap->count1,
4513 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4514 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4515 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4516 (linenr_T)MAXLNUM);
4517 curwin->w_set_curswant = TRUE;
4518 }
4519 }
4520 else
4521#endif
4522
4523 /*
4524 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4525 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4526 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4527 * "[m" or "]m" search for prev/next start of (Java) method.
4528 * "[M" or "]M" search for prev/next end of (Java) method.
4529 */
4530 if ( (cap->cmdchar == '['
4531 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4532 || (cap->cmdchar == ']'
4533 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4534 {
4535 if (cap->nchar == '*')
4536 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 prev_pos.lnum = 0;
4538 if (cap->nchar == 'm' || cap->nchar == 'M')
4539 {
4540 if (cap->cmdchar == '[')
4541 findc = '{';
4542 else
4543 findc = '}';
4544 n = 9999;
4545 }
4546 else
4547 {
4548 findc = cap->nchar;
4549 n = cap->count1;
4550 }
4551 for ( ; n > 0; --n)
4552 {
4553 if ((pos = findmatchlimit(cap->oap, findc,
4554 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4555 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004556 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558 if (cap->nchar != 'm' && cap->nchar != 'M')
4559 clearopbeep(cap->oap);
4560 }
4561 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004562 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 break;
4564 }
4565 prev_pos = new_pos;
4566 curwin->w_cursor = *pos;
4567 new_pos = *pos;
4568 }
4569 curwin->w_cursor = old_pos;
4570
4571 /*
4572 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4573 * brought us to the match for "[m" and "]M" when inside a method.
4574 * Try finding the '{' or '}' we want to be at.
4575 * Also repeat for the given count.
4576 */
4577 if (cap->nchar == 'm' || cap->nchar == 'M')
4578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004579 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 int norm = ((findc == '{') == (cap->nchar == 'm'));
4581
4582 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004583 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (prev_pos.lnum != 0)
4585 {
4586 pos = &prev_pos;
4587 curwin->w_cursor = prev_pos;
4588 if (norm)
4589 --n;
4590 }
4591 else
4592 pos = NULL;
4593 while (n > 0)
4594 {
4595 for (;;)
4596 {
4597 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004599 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (pos == NULL)
4601 clearopbeep(cap->oap);
4602 n = 0;
4603 break;
4604 }
4605 c = gchar_cursor();
4606 if (c == '{' || c == '}')
4607 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004608 // Must have found end/start of class: use it.
4609 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if ((c == findc && norm) || (n == 1 && !norm))
4611 {
4612 new_pos = curwin->w_cursor;
4613 pos = &new_pos;
4614 n = 0;
4615 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004616 // if no match found at all, we started outside of the
4617 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 else if (new_pos.lnum == 0)
4619 {
4620 new_pos = curwin->w_cursor;
4621 pos = &new_pos;
4622 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 else if ((pos = findmatchlimit(cap->oap, findc,
4625 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4626 0)) == NULL)
4627 n = 0;
4628 else
4629 curwin->w_cursor = *pos;
4630 break;
4631 }
4632 }
4633 --n;
4634 }
4635 curwin->w_cursor = old_pos;
4636 if (pos == NULL && new_pos.lnum != 0)
4637 clearopbeep(cap->oap);
4638 }
4639 if (pos != NULL)
4640 {
4641 setpcmark();
4642 curwin->w_cursor = *pos;
4643 curwin->w_set_curswant = TRUE;
4644#ifdef FEAT_FOLDING
4645 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4646 && cap->oap->op_type == OP_NOP)
4647 foldOpenCursor();
4648#endif
4649 }
4650 }
4651
4652 /*
4653 * "[[", "[]", "]]" and "][": move to start or end of function
4654 */
4655 else if (cap->nchar == '[' || cap->nchar == ']')
4656 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004657 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 flag = '{';
4659 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 curwin->w_set_curswant = TRUE;
4663 /*
4664 * Imitate strange Vi behaviour: When using "]]" with an operator
4665 * we also stop at '}'.
4666 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004667 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 (cap->oap->op_type != OP_NOP
4669 && cap->arg == FORWARD && flag == '{')))
4670 clearopbeep(cap->oap);
4671 else
4672 {
4673 if (cap->oap->op_type == OP_NOP)
4674 beginline(BL_WHITE | BL_FIX);
4675#ifdef FEAT_FOLDING
4676 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4677 foldOpenCursor();
4678#endif
4679 }
4680 }
4681
4682 /*
4683 * "[p", "[P", "]P" and "]p": put with indent adjustment
4684 */
4685 else if (cap->nchar == 'p' || cap->nchar == 'P')
4686 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004687 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
4689
4690 /*
4691 * "['", "[`", "]'" and "]`": jump to next mark
4692 */
4693 else if (cap->nchar == '\'' || cap->nchar == '`')
4694 {
4695 pos = &curwin->w_cursor;
4696 for (n = cap->count1; n > 0; --n)
4697 {
4698 prev_pos = *pos;
4699 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4700 cap->nchar == '\'');
4701 if (pos == NULL)
4702 break;
4703 }
4704 if (pos == NULL)
4705 pos = &prev_pos;
4706 nv_cursormark(cap, cap->nchar == '\'', pos);
4707 }
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 /*
4710 * [ or ] followed by a middle mouse click: put selected text with
4711 * indent adjustment. Any other button just does as usual.
4712 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004713 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 (void)do_mouse(cap->oap, cap->nchar,
4716 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4717 cap->count1, PUT_FIXINDENT);
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720#ifdef FEAT_FOLDING
4721 /*
4722 * "[z" and "]z": move to start or end of open fold.
4723 */
4724 else if (cap->nchar == 'z')
4725 {
4726 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4727 cap->count1) == FAIL)
4728 clearopbeep(cap->oap);
4729 }
4730#endif
4731
4732#ifdef FEAT_DIFF
4733 /*
4734 * "[c" and "]c": move to next or previous diff-change.
4735 */
4736 else if (cap->nchar == 'c')
4737 {
4738 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4739 cap->count1) == FAIL)
4740 clearopbeep(cap->oap);
4741 }
4742#endif
4743
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004744#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004745 /*
4746 * "[s", "[S", "]s" and "]S": move to next spell error.
4747 */
4748 else if (cap->nchar == 's' || cap->nchar == 'S')
4749 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004750 setpcmark();
4751 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004752 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4753 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004754 {
4755 clearopbeep(cap->oap);
4756 break;
4757 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004758 else
4759 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004760# ifdef FEAT_FOLDING
4761 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4762 foldOpenCursor();
4763# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764 }
4765#endif
4766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004767 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 else
4769 clearopbeep(cap->oap);
4770}
4771
4772/*
4773 * Handle Normal mode "%" command.
4774 */
4775 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004776nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004779#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 linenr_T lnum = curwin->w_cursor.lnum;
4781#endif
4782
4783 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004784 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 if (cap->count0 > 100)
4787 clearopbeep(cap->oap);
4788 else
4789 {
4790 cap->oap->motion_type = MLINE;
4791 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004792 // Round up, so 'normal 100%' always jumps at the line line.
4793 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4794 // overflow on 32-bits, so use a formula with less accuracy
4795 // to avoid overflows.
4796 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4798 / 100L * cap->count0;
4799 else
4800 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4801 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004802 if (curwin->w_cursor.lnum < 1)
4803 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4805 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4806 beginline(BL_SOL | BL_FIX);
4807 }
4808 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004809 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 cap->oap->motion_type = MCHAR;
4812 cap->oap->use_reg_one = TRUE;
4813 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4814 clearopbeep(cap->oap);
4815 else
4816 {
4817 setpcmark();
4818 curwin->w_cursor = *pos;
4819 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823 }
4824#ifdef FEAT_FOLDING
4825 if (cap->oap->op_type == OP_NOP
4826 && lnum != curwin->w_cursor.lnum
4827 && (fdo_flags & FDO_PERCENT)
4828 && KeyTyped)
4829 foldOpenCursor();
4830#endif
4831}
4832
4833/*
4834 * Handle "(" and ")" commands.
4835 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4836 */
4837 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004838nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 cap->oap->motion_type = MCHAR;
4841 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004842 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004843 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 curwin->w_set_curswant = TRUE;
4845
4846 if (findsent(cap->arg, cap->count1) == FAIL)
4847 clearopbeep(cap->oap);
4848 else
4849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004851 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853#ifdef FEAT_FOLDING
4854 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4855 foldOpenCursor();
4856#endif
4857 }
4858}
4859
4860/*
4861 * "m" command: Mark a position.
4862 */
4863 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004864nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865{
4866 if (!checkclearop(cap->oap))
4867 {
4868 if (setmark(cap->nchar) == FAIL)
4869 clearopbeep(cap->oap);
4870 }
4871}
4872
4873/*
4874 * "{" and "}" commands.
4875 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4876 */
4877 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004878nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
4880 cap->oap->motion_type = MCHAR;
4881 cap->oap->inclusive = FALSE;
4882 cap->oap->use_reg_one = TRUE;
4883 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004884 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 clearopbeep(cap->oap);
4886 else
4887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889#ifdef FEAT_FOLDING
4890 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4891 foldOpenCursor();
4892#endif
4893 }
4894}
4895
4896/*
4897 * "u" command: Undo or make lower case.
4898 */
4899 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004900nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004902 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 cap->cmdchar = 'g';
4906 cap->nchar = 'u';
4907 nv_operator(cap);
4908 }
4909 else
4910 nv_kundo(cap);
4911}
4912
4913/*
4914 * <Undo> command.
4915 */
4916 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004917nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 if (!checkclearopq(cap->oap))
4920 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004921#ifdef FEAT_JOB_CHANNEL
4922 if (bt_prompt(curbuf))
4923 {
4924 clearopbeep(cap->oap);
4925 return;
4926 }
4927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 u_undo((int)cap->count1);
4929 curwin->w_set_curswant = TRUE;
4930 }
4931}
4932
4933/*
4934 * Handle the "r" command.
4935 */
4936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004937nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 char_u *ptr;
4940 int had_ctrl_v;
4941 long n;
4942
4943 if (checkclearop(cap->oap))
4944 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004945#ifdef FEAT_JOB_CHANNEL
4946 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4947 {
4948 clearopbeep(cap->oap);
4949 return;
4950 }
4951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (cap->nchar == Ctrl_V)
4955 {
4956 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004957 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004958 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (cap->nchar > DEL)
4960 had_ctrl_v = NUL;
4961 }
4962 else
4963 had_ctrl_v = NUL;
4964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004965 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004966 if (IS_SPECIAL(cap->nchar))
4967 {
4968 clearopbeep(cap->oap);
4969 return;
4970 }
4971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004972 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (VIsual_active)
4974 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004975 if (got_int)
4976 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004977 if (had_ctrl_v)
4978 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 // Use a special (negative) number to make a difference between a
4980 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004981 if (cap->nchar == CAR)
4982 cap->nchar = REPLACE_CR_NCHAR;
4983 else if (cap->nchar == NL)
4984 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 nv_operator(cap);
4987 return;
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004990 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (virtual_active())
4992 {
4993 if (u_save_cursor() == FAIL)
4994 return;
4995 if (gchar_cursor() == NUL)
4996 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004997 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 coladvance_force((colnr_T)(getviscol() + cap->count1));
4999 curwin->w_cursor.col -= cap->count1;
5000 }
5001 else if (gchar_cursor() == TAB)
5002 coladvance_force(getviscol());
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005005 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005007 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005008 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
5010 clearopbeep(cap->oap);
5011 return;
5012 }
5013
5014 /*
5015 * Replacing with a TAB is done by edit() when it is complicated because
5016 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5017 * Other characters are done below to avoid problems with things like
5018 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5019 */
5020 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5021 {
5022 stuffnumReadbuff(cap->count1);
5023 stuffcharReadbuff('R');
5024 stuffcharReadbuff('\t');
5025 stuffcharReadbuff(ESC);
5026 return;
5027 }
5028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (u_save_cursor() == FAIL)
5031 return;
5032
5033 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5034 {
5035 /*
5036 * Replace character(s) by a single newline.
5037 * Strange vi behaviour: Only one newline is inserted.
5038 * Delete the characters here.
5039 * Insert the newline with an insert command, takes care of
5040 * autoindent. The insert command depends on being on the last
5041 * character of a line or not.
5042 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 stuffcharReadbuff('\r');
5045 stuffcharReadbuff(ESC);
5046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 invoke_edit(cap, TRUE, 'r', FALSE);
5049 }
5050 else
5051 {
5052 prep_redo(cap->oap->regname, cap->count1,
5053 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5054
5055 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (has_mbyte)
5057 {
5058 int old_State = State;
5059
5060 if (cap->ncharC1 != 0)
5061 AppendCharToRedobuff(cap->ncharC1);
5062 if (cap->ncharC2 != 0)
5063 AppendCharToRedobuff(cap->ncharC2);
5064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005065 // This is slow, but it handles replacing a single-byte with a
5066 // multi-byte and the other way around. Also handles adding
5067 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 for (n = cap->count1; n > 0; --n)
5069 {
5070 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005071 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5072 {
5073 int c = ins_copychar(curwin->w_cursor.lnum
5074 + (cap->nchar == Ctrl_Y ? -1 : 1));
5075 if (c != NUL)
5076 ins_char(c);
5077 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005078 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005079 ++curwin->w_cursor.col;
5080 }
5081 else
5082 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 State = old_State;
5084 if (cap->ncharC1 != 0)
5085 ins_char(cap->ncharC1);
5086 if (cap->ncharC2 != 0)
5087 ins_char(cap->ncharC2);
5088 }
5089 }
5090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 /*
5093 * Replace the characters within one line.
5094 */
5095 for (n = cap->count1; n > 0; --n)
5096 {
5097 /*
5098 * Get ptr again, because u_save and/or showmatch() will have
5099 * released the line. At the same time we let know that the
5100 * line will be changed.
5101 */
5102 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005103 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5104 {
5105 int c = ins_copychar(curwin->w_cursor.lnum
5106 + (cap->nchar == Ctrl_Y ? -1 : 1));
5107 if (c != NUL)
5108 ptr[curwin->w_cursor.col] = c;
5109 }
5110 else
5111 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (p_sm && msg_silent == 0)
5113 showmatch(cap->nchar);
5114 ++curwin->w_cursor.col;
5115 }
5116#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005117 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005119 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
Bram Moolenaar009b2592004-10-24 19:18:58 +00005121 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005122 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005124 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126#endif
5127
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005128 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 changed_bytes(curwin->w_cursor.lnum,
5130 (colnr_T)(curwin->w_cursor.col - cap->count1));
5131 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005132 --curwin->w_cursor.col; // cursor on the last replaced char
5133 // if the character on the left of the current cursor is a multi-byte
5134 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (has_mbyte)
5136 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 curbuf->b_op_end = curwin->w_cursor;
5138 curwin->w_set_curswant = TRUE;
5139 set_last_insert(cap->nchar);
5140 }
5141}
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143/*
5144 * 'o': Exchange start and end of Visual area.
5145 * 'O': same, but in block mode exchange left and right corners.
5146 */
5147 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005148v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
5150 pos_T old_cursor;
5151 colnr_T left, right;
5152
5153 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5154 {
5155 old_cursor = curwin->w_cursor;
5156 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5157 curwin->w_cursor.lnum = VIsual.lnum;
5158 coladvance(left);
5159 VIsual = curwin->w_cursor;
5160
5161 curwin->w_cursor.lnum = old_cursor.lnum;
5162 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005163 // 'selection "exclusive" and cursor at right-bottom corner: move it
5164 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5166 ++curwin->w_curswant;
5167 coladvance(curwin->w_curswant);
5168 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005170 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 curwin->w_cursor.lnum = VIsual.lnum;
5173 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5174 ++right;
5175 coladvance(right);
5176 VIsual = curwin->w_cursor;
5177
5178 curwin->w_cursor.lnum = old_cursor.lnum;
5179 coladvance(left);
5180 curwin->w_curswant = left;
5181 }
5182 }
5183 else
5184 {
5185 old_cursor = curwin->w_cursor;
5186 curwin->w_cursor = VIsual;
5187 VIsual = old_cursor;
5188 curwin->w_set_curswant = TRUE;
5189 }
5190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192/*
5193 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5194 */
5195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005196nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005198 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {
5200 cap->cmdchar = 'c';
5201 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 VIsual_mode = 'V';
5204 nv_operator(cap);
5205 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005206 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005209 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else
5211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (virtual_active())
5213 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5215 }
5216 }
5217}
5218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219/*
5220 * "gr".
5221 */
5222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (VIsual_active)
5226 {
5227 cap->cmdchar = 'r';
5228 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005229 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005231 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005234 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 else
5236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005238 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 stuffcharReadbuff(cap->extra_char);
5240 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (virtual_active())
5242 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 invoke_edit(cap, TRUE, 'v', FALSE);
5244 }
5245 }
5246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248/*
5249 * Swap case for "~" command, when it does not work like an operator.
5250 */
5251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005252n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 long n;
5255 pos_T startpos;
5256 int did_change = 0;
5257#ifdef FEAT_NETBEANS_INTG
5258 pos_T pos;
5259 char_u *ptr;
5260 int count;
5261#endif
5262
5263 if (checkclearopq(cap->oap))
5264 return;
5265
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005266 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
5268 clearopbeep(cap->oap);
5269 return;
5270 }
5271
5272 prep_redo_cmd(cap);
5273
5274 if (u_save_cursor() == FAIL)
5275 return;
5276
5277 startpos = curwin->w_cursor;
5278#ifdef FEAT_NETBEANS_INTG
5279 pos = startpos;
5280#endif
5281 for (n = cap->count1; n > 0; --n)
5282 {
5283 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5284 inc_cursor();
5285 if (gchar_cursor() == NUL)
5286 {
5287 if (vim_strchr(p_ww, '~') != NULL
5288 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5289 {
5290#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005291 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 if (did_change)
5294 {
5295 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005296 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005297 netbeans_removed(curbuf, pos.lnum, pos.col,
5298 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005300 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302 pos.col = 0;
5303 pos.lnum++;
5304 }
5305#endif
5306 ++curwin->w_cursor.lnum;
5307 curwin->w_cursor.col = 0;
5308 if (n > 1)
5309 {
5310 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5311 break;
5312 u_clearline();
5313 }
5314 }
5315 else
5316 break;
5317 }
5318 }
5319#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005320 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 ptr = ml_get(pos.lnum);
5323 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005324 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5325 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327#endif
5328
5329
5330 check_cursor();
5331 curwin->w_set_curswant = TRUE;
5332 if (did_change)
5333 {
5334 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5335 0L);
5336 curbuf->b_op_start = startpos;
5337 curbuf->b_op_end = curwin->w_cursor;
5338 if (curbuf->b_op_end.col > 0)
5339 --curbuf->b_op_end.col;
5340 }
5341}
5342
5343/*
5344 * Move cursor to mark.
5345 */
5346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005347nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 if (check_mark(pos) == FAIL)
5350 clearop(cap->oap);
5351 else
5352 {
5353 if (cap->cmdchar == '\''
5354 || cap->cmdchar == '`'
5355 || cap->cmdchar == '['
5356 || cap->cmdchar == ']')
5357 setpcmark();
5358 curwin->w_cursor = *pos;
5359 if (flag)
5360 beginline(BL_WHITE | BL_FIX);
5361 else
5362 check_cursor();
5363 }
5364 cap->oap->motion_type = flag ? MLINE : MCHAR;
5365 if (cap->cmdchar == '`')
5366 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005367 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 curwin->w_set_curswant = TRUE;
5369}
5370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371/*
5372 * Handle commands that are operators in Visual mode.
5373 */
5374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005375v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005379 // Uppercase means linewise, except in block mode, then "D" deletes till
5380 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (isupper(cap->cmdchar))
5382 {
5383 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005384 {
5385 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5389 curwin->w_curswant = MAXCOL;
5390 }
5391 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5392 nv_operator(cap);
5393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395/*
5396 * "s" and "S" commands.
5397 */
5398 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005399nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005401#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005402 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005403 if (term_swap_diff() == OK)
5404 return;
5405#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005406#ifdef FEAT_JOB_CHANNEL
5407 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5408 {
5409 clearopbeep(cap->oap);
5410 return;
5411 }
5412#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005413 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
5415 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005416 {
5417 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 cap->cmdchar = 'c';
5421 nv_operator(cap);
5422 }
5423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 nv_optrans(cap);
5425}
5426
5427/*
5428 * Abbreviated commands.
5429 */
5430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005431nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 if (VIsual_active)
5438 v_visop(cap);
5439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 nv_optrans(cap);
5441}
5442
5443/*
5444 * Translate a command into another command.
5445 */
5446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005447nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5450 (char_u *)"d$", (char_u *)"c$",
5451 (char_u *)"cl", (char_u *)"cc",
5452 (char_u *)"yy", (char_u *)":s\r"};
5453 static char_u *str = (char_u *)"xXDCsSY&";
5454
5455 if (!checkclearopq(cap->oap))
5456 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005457 // In Vi "2D" doesn't delete the next line. Can't translate it
5458 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005459 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5460 {
5461 cap->oap->start = curwin->w_cursor;
5462 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005463#ifdef FEAT_EVAL
5464 set_op_var(OP_DELETE);
5465#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005466 cap->count1 = 1;
5467 nv_dollar(cap);
5468 finish_op = TRUE;
5469 ResetRedobuff();
5470 AppendCharToRedobuff('D');
5471 }
5472 else
5473 {
5474 if (cap->count0)
5475 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005476 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 }
5479 cap->opcount = 0;
5480}
5481
5482/*
5483 * "'" and "`" commands. Also for "g'" and "g`".
5484 * cap->arg is TRUE for "'" and "g'".
5485 */
5486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005487nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 pos_T *pos;
5490 int c;
5491#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005492 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005493 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494#endif
5495
5496 if (cap->cmdchar == 'g')
5497 c = cap->extra_char;
5498 else
5499 c = cap->nchar;
5500 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005501 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 if (cap->arg)
5504 {
5505 check_cursor_lnum();
5506 beginline(BL_WHITE | BL_FIX);
5507 }
5508 else
5509 check_cursor();
5510 }
5511 else
5512 nv_cursormark(cap, cap->arg, pos);
5513
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005514 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (!virtual_active())
5516 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005517 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518#ifdef FEAT_FOLDING
5519 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005520 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005521 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 && (fdo_flags & FDO_MARK)
5523 && old_KeyTyped)
5524 foldOpenCursor();
5525#endif
5526}
5527
5528/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005529 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 */
5531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005532nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534#ifdef FEAT_JUMPLIST
5535 pos_T *pos;
5536# ifdef FEAT_FOLDING
5537 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005538 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539# endif
5540
5541 if (!checkclearopq(cap->oap))
5542 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005543 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5544 {
5545 if (goto_tabpage_lastused() == FAIL)
5546 clearopbeep(cap->oap);
5547 return;
5548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (cap->cmdchar == 'g')
5550 pos = movechangelist((int)cap->count1);
5551 else
5552 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005553 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
5555 curwin->w_set_curswant = TRUE;
5556 check_cursor();
5557 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005558 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 nv_cursormark(cap, FALSE, pos);
5560 else if (cap->cmdchar == 'g')
5561 {
5562 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005563 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005565 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005567 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 else
5570 clearopbeep(cap->oap);
5571# ifdef FEAT_FOLDING
5572 if (cap->oap->op_type == OP_NOP
5573 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5574 && (fdo_flags & FDO_MARK)
5575 && old_KeyTyped)
5576 foldOpenCursor();
5577# endif
5578 }
5579#else
5580 clearopbeep(cap->oap);
5581#endif
5582}
5583
5584/*
5585 * Handle '"' command.
5586 */
5587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 if (checkclearop(cap->oap))
5591 return;
5592#ifdef FEAT_EVAL
5593 if (cap->nchar == '=')
5594 cap->nchar = get_expr_register();
5595#endif
5596 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5597 {
5598 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600#ifdef FEAT_EVAL
5601 set_reg_var(cap->oap->regname);
5602#endif
5603 }
5604 else
5605 clearopbeep(cap->oap);
5606}
5607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608/*
5609 * Handle "v", "V" and "CTRL-V" commands.
5610 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5611 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005612 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
5614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005615nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005617 if (cap->cmdchar == Ctrl_Q)
5618 cap->cmdchar = Ctrl_V;
5619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5621 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 if (cap->oap->op_type != OP_NOP)
5623 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005624 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005625 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 return;
5627 }
5628
5629 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 else // toggle char/block mode
5635 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 VIsual_mode = cap->cmdchar;
5637 showmode();
5638 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005644 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005646 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 VIsual = curwin->w_cursor;
5648
5649 VIsual_active = TRUE;
5650 VIsual_reselect = TRUE;
5651 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005652 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005655 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005656 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 /*
5658 * For V and ^V, we multiply the number of lines even if there
5659 * was only one -- webb
5660 */
5661 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5662 {
5663 curwin->w_cursor.lnum +=
5664 resel_VIsual_line_count * cap->count0 - 1;
5665 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5666 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5667 }
5668 VIsual_mode = resel_VIsual_mode;
5669 if (VIsual_mode == 'v')
5670 {
5671 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005672 {
5673 validate_virtcol();
5674 curwin->w_curswant = curwin->w_virtcol
5675 + resel_VIsual_vcol * cap->count0 - 1;
5676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005678 curwin->w_curswant = resel_VIsual_vcol;
5679 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005681 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
5683 curwin->w_curswant = MAXCOL;
5684 coladvance((colnr_T)MAXCOL);
5685 }
5686 else if (VIsual_mode == Ctrl_V)
5687 {
5688 validate_virtcol();
5689 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005690 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 coladvance(curwin->w_curswant);
5692 }
5693 else
5694 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 else
5698 {
5699 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005700 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 may_start_select('c');
5702 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005703 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005704 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005705 if (cap->count0 > 0 && --cap->count1 > 0)
5706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005708 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5709 nv_right(cap);
5710 else if (VIsual_mode == 'V')
5711 nv_down(cap);
5712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 }
5715}
5716
5717/*
5718 * Start selection for Shift-movement keys.
5719 */
5720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005721start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 may_start_select('k');
5725 n_start_visual_mode('v');
5726}
5727
5728/*
5729 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5730 */
5731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005732may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 VIsual_select = (stuff_empty() && typebuf_typed()
5735 && (vim_strchr(p_slm, c) != NULL));
5736}
5737
5738/*
5739 * Start Visual mode "c".
5740 * Should set VIsual_select before calling this.
5741 */
5742 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005743n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005745#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005746 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005747 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005748#endif
5749
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 VIsual_mode = c;
5751 VIsual_active = TRUE;
5752 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005753
5754 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005755 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005757 {
5758 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 VIsual = curwin->w_cursor;
5762
5763#ifdef FEAT_FOLDING
5764 foldAdjustVisual();
5765#endif
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005768#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005769 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005770 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005771#endif
5772
Bram Moolenaar09df3122006-01-23 22:23:09 +00005773 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005774 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005776 // Make sure the clipboard gets updated. Needed because start and
5777 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 clip_star.vmode = NUL;
5779#endif
5780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005781 // Only need to redraw this line, unless still need to redraw an old
5782 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (curwin->w_redr_type < INVERTED)
5784 {
5785 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5786 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5787 }
5788}
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791/*
5792 * CTRL-W: Window commands
5793 */
5794 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005795nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005797 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005799 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005800 cap->cmdchar = ':';
5801 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005802 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005803 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005804 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005805 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806}
5807
5808/*
5809 * CTRL-Z: Suspend
5810 */
5811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005812nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005816 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005817 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818}
5819
5820/*
5821 * Commands starting with "g".
5822 */
5823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005824nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 int i;
5829 int flag = FALSE;
5830
5831 switch (cap->nchar)
5832 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005833 case Ctrl_A:
5834 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#ifdef MEM_PROFILE
5836 /*
5837 * "g^A": dump log of used memory.
5838 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005839 if (!VIsual_active && cap->nchar == Ctrl_A)
5840 vim_mem_profile_dump();
5841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005843 /*
5844 * "g^A/g^X": sequentially increment visually selected region
5845 */
5846 if (VIsual_active)
5847 {
5848 cap->arg = TRUE;
5849 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005850 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005851 nv_addsub(cap);
5852 }
5853 else
5854 clearopbeep(oap);
5855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 /*
5858 * "gR": Enter virtual replace mode.
5859 */
5860 case 'R':
5861 cap->arg = TRUE;
5862 nv_Replace(cap);
5863 break;
5864
5865 case 'r':
5866 nv_vreplace(cap);
5867 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869 case '&':
5870 do_cmdline_cmd((char_u *)"%s//~/&");
5871 break;
5872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 /*
5874 * "gv": Reselect the previous Visual area. If Visual already active,
5875 * exchange previous and current Visual area.
5876 */
5877 case 'v':
5878 if (checkclearop(oap))
5879 break;
5880
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005881 if ( curbuf->b_visual.vi_start.lnum == 0
5882 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5883 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 beep_flush();
5885 else
5886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005887 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 if (VIsual_active)
5889 {
5890 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005891 VIsual_mode = curbuf->b_visual.vi_mode;
5892 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893# ifdef FEAT_EVAL
5894 curbuf->b_visual_mode_eval = i;
5895# endif
5896 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005897 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5898 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005900 tpos = curbuf->b_visual.vi_end;
5901 curbuf->b_visual.vi_end = curwin->w_cursor;
5902 curwin->w_cursor = curbuf->b_visual.vi_start;
5903 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 else
5906 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005907 VIsual_mode = curbuf->b_visual.vi_mode;
5908 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5909 tpos = curbuf->b_visual.vi_end;
5910 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
5912
5913 VIsual_active = TRUE;
5914 VIsual_reselect = TRUE;
5915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005916 // Set Visual to the start and w_cursor to the end of the Visual
5917 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 check_cursor();
5919 VIsual = curwin->w_cursor;
5920 curwin->w_cursor = tpos;
5921 check_cursor();
5922 update_topline();
5923 /*
5924 * When called from normal "g" command: start Select mode when
5925 * 'selectmode' contains "cmd". When called for K_SELECT, always
5926 * start Select mode.
5927 */
5928 if (cap->arg)
5929 VIsual_select = TRUE;
5930 else
5931 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005934 // Make sure the clipboard gets updated. Needed because start and
5935 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 clip_star.vmode = NUL;
5937#endif
5938 redraw_curbuf_later(INVERTED);
5939 showmode();
5940 }
5941 break;
5942 /*
5943 * "gV": Don't reselect the previous Visual area after a Select mode
5944 * mapping of menu.
5945 */
5946 case 'V':
5947 VIsual_reselect = FALSE;
5948 break;
5949
5950 /*
5951 * "gh": start Select mode.
5952 * "gH": start Select line mode.
5953 * "g^H": start Select block mode.
5954 */
5955 case K_BS:
5956 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005957 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 case 'h':
5959 case 'H':
5960 case Ctrl_H:
5961# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005962 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (cap->nchar == Ctrl_H)
5964 cap->cmdchar = Ctrl_V;
5965 else
5966# endif
5967 cap->cmdchar = cap->nchar + ('v' - 'h');
5968 cap->arg = TRUE;
5969 nv_visual(cap);
5970 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005972 // "gn", "gN" visually select next/previous search match
5973 // "gn" selects next match
5974 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005975 case 'N':
5976 case 'n':
5977 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005978 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 /*
5982 * "gj" and "gk" two new funny movement keys -- up and down
5983 * movement based on *screen* line rather than *file* line.
5984 */
5985 case 'j':
5986 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005987 // with 'nowrap' it works just like the normal "j" command.
5988 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {
5990 oap->motion_type = MLINE;
5991 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5992 }
5993 else
5994 i = nv_screengo(oap, FORWARD, cap->count1);
5995 if (i == FAIL)
5996 clearopbeep(oap);
5997 break;
5998
5999 case 'k':
6000 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006001 // with 'nowrap' it works just like the normal "k" command.
6002 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 {
6004 oap->motion_type = MLINE;
6005 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6006 }
6007 else
6008 i = nv_screengo(oap, BACKWARD, cap->count1);
6009 if (i == FAIL)
6010 clearopbeep(oap);
6011 break;
6012
6013 /*
6014 * "gJ": join two lines without inserting a space.
6015 */
6016 case 'J':
6017 nv_join(cap);
6018 break;
6019
6020 /*
6021 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6022 * "gm": middle of "g0" and "g$".
6023 */
6024 case '^':
6025 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006026 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027
6028 case '0':
6029 case 'm':
6030 case K_HOME:
6031 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 oap->motion_type = MCHAR;
6033 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006034 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006036 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 int width2 = width1 + curwin_col_off2();
6038
6039 validate_virtcol();
6040 i = 0;
6041 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6042 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6043 }
6044 else
6045 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006046 // Go to the middle of the screen line. When 'number' or
6047 // 'relativenumber' is on and lines are wrapping the middle can be more
6048 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006050 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 + ((curwin->w_p_wrap && i > 0)
6052 ? curwin_col_off2() : 0)) / 2;
6053 coladvance((colnr_T)i);
6054 if (flag)
6055 {
6056 do
6057 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006058 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006059 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061 curwin->w_set_curswant = TRUE;
6062 break;
6063
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006064 case 'M':
6065 {
6066 char_u *ptr = ml_get_curline();
6067
6068 oap->motion_type = MCHAR;
6069 oap->inclusive = FALSE;
6070 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006071 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006072 else
6073 i = (int)STRLEN(ptr);
6074 if (cap->count0 > 0 && cap->count0 <= 100)
6075 coladvance((colnr_T)(i * cap->count0 / 100));
6076 else
6077 coladvance((colnr_T)(i / 2));
6078 curwin->w_set_curswant = TRUE;
6079 }
6080 break;
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006083 // "g_": to the last non-blank character in the line or <count> lines
6084 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 cap->oap->motion_type = MCHAR;
6086 cap->oap->inclusive = TRUE;
6087 curwin->w_curswant = MAXCOL;
6088 if (cursor_down((long)(cap->count1 - 1),
6089 cap->oap->op_type == OP_NOP) == FAIL)
6090 clearopbeep(cap->oap);
6091 else
6092 {
6093 char_u *ptr = ml_get_curline();
6094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006095 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6097 --curwin->w_cursor.col;
6098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006099 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006101 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 --curwin->w_cursor.col;
6103 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006104 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106 break;
6107
6108 case '$':
6109 case K_END:
6110 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
6112 int col_off = curwin_col_off();
6113
6114 oap->motion_type = MCHAR;
6115 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006116 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006118 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (cap->count1 == 1)
6120 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006121 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 int width2 = width1 + curwin_col_off2();
6123
6124 validate_virtcol();
6125 i = width1 - 1;
6126 if (curwin->w_virtcol >= (colnr_T)width1)
6127 i += ((curwin->w_virtcol - width1) / width2 + 1)
6128 * width2;
6129 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006130
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006131 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006132 validate_virtcol();
6133 curwin->w_curswant = curwin->w_virtcol;
6134 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6136 {
6137 /*
6138 * Check for landing on a character that got split at
6139 * the end of the line. We do not want to advance to
6140 * the next screen line.
6141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (curwin->w_virtcol > (colnr_T)i)
6143 --curwin->w_cursor.col;
6144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
6146 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6147 clearopbeep(oap);
6148 }
6149 else
6150 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006151 if (cap->count1 > 1)
6152 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006153 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006154
Bram Moolenaar02631462017-09-22 15:20:32 +02006155 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006157
Bram Moolenaar74ede802021-05-29 19:18:01 +02006158 // if the character doesn't fit move one back
6159 if (curwin->w_cursor.col > 0
6160 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6161 {
6162 colnr_T vcol;
6163
6164 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6165 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6166 --curwin->w_cursor.col;
6167 }
6168
Bram Moolenaard5c82342019-07-27 18:44:57 +02006169 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006170 validate_virtcol();
6171 curwin->w_curswant = curwin->w_virtcol;
6172 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174 }
6175 break;
6176
6177 /*
6178 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6179 */
6180 case '*':
6181 case '#':
6182#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006183 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006185 case Ctrl_RSB: // :tag or :tselect for current identifier
6186 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 nv_ident(cap);
6188 break;
6189
6190 /*
6191 * ge and gE: go back to end of word
6192 */
6193 case 'e':
6194 case 'E':
6195 oap->motion_type = MCHAR;
6196 curwin->w_set_curswant = TRUE;
6197 oap->inclusive = TRUE;
6198 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6199 clearopbeep(oap);
6200 break;
6201
6202 /*
6203 * "g CTRL-G": display info about cursor position
6204 */
6205 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006206 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 break;
6208
6209 /*
6210 * "gi": start Insert at the last position.
6211 */
6212 case 'i':
6213 if (curbuf->b_last_insert.lnum != 0)
6214 {
6215 curwin->w_cursor = curbuf->b_last_insert;
6216 check_cursor_lnum();
6217 i = (int)STRLEN(ml_get_curline());
6218 if (curwin->w_cursor.col > (colnr_T)i)
6219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (virtual_active())
6221 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 curwin->w_cursor.col = i;
6223 }
6224 }
6225 cap->cmdchar = 'i';
6226 nv_edit(cap);
6227 break;
6228
6229 /*
6230 * "gI": Start insert in column 1.
6231 */
6232 case 'I':
6233 beginline(0);
6234 if (!checkclearopq(oap))
6235 invoke_edit(cap, FALSE, 'g', FALSE);
6236 break;
6237
6238#ifdef FEAT_SEARCHPATH
6239 /*
6240 * "gf": goto file, edit file under cursor
6241 * "]f" and "[f": can also be used.
6242 */
6243 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006244 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 nv_gotofile(cap);
6246 break;
6247#endif
6248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006249 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 case '\'':
6251 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006252 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 case '`':
6254 nv_gomark(cap);
6255 break;
6256
6257 /*
6258 * "gs": Goto sleep.
6259 */
6260 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006261 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 break;
6263
6264 /*
6265 * "ga": Display the ascii value of the character under the
6266 * cursor. It is displayed in decimal, hex, and octal. -- webb
6267 */
6268 case 'a':
6269 do_ascii(NULL);
6270 break;
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 /*
6273 * "g8": Display the bytes used for the UTF-8 character under the
6274 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006275 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 */
6277 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006278 if (cap->count0 == 8)
6279 utf_find_illegal();
6280 else
6281 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006284 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006285 case '<':
6286 show_sb_text();
6287 break;
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 /*
6290 * "gg": Goto the first line in file. With a count it goes to
6291 * that line number like for "G". -- webb
6292 */
6293 case 'g':
6294 cap->arg = FALSE;
6295 nv_goto(cap);
6296 break;
6297
6298 /*
6299 * Two-character operators:
6300 * "gq" Format text
6301 * "gw" Format text and keep cursor position
6302 * "g~" Toggle the case of the text.
6303 * "gu" Change text to lower case.
6304 * "gU" Change text to upper case.
6305 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006306 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
6308 case 'q':
6309 case 'w':
6310 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006311 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 case '~':
6313 case 'u':
6314 case 'U':
6315 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006316 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 nv_operator(cap);
6318 break;
6319
6320 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006321 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 * current function
6323 * "gD": idem, but in the current file.
6324 */
6325 case 'd':
6326 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006327 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 break;
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 /*
6331 * g<*Mouse> : <C-*mouse>
6332 */
6333 case K_MIDDLEMOUSE:
6334 case K_MIDDLEDRAG:
6335 case K_MIDDLERELEASE:
6336 case K_LEFTMOUSE:
6337 case K_LEFTDRAG:
6338 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006339 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 case K_RIGHTMOUSE:
6341 case K_RIGHTDRAG:
6342 case K_RIGHTRELEASE:
6343 case K_X1MOUSE:
6344 case K_X1DRAG:
6345 case K_X1RELEASE:
6346 case K_X2MOUSE:
6347 case K_X2DRAG:
6348 case K_X2RELEASE:
6349 mod_mask = MOD_MASK_CTRL;
6350 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
6353 case K_IGNORE:
6354 break;
6355
6356 /*
6357 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6358 */
6359 case 'p':
6360 case 'P':
6361 nv_put(cap);
6362 break;
6363
6364#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006365 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 case 'o':
6367 goto_byte(cap->count0);
6368 break;
6369#endif
6370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006371 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006373 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
6375 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006376 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 break;
6378 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006379
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (!checkclearopq(oap))
6381 do_exmode(TRUE);
6382 break;
6383
6384#ifdef FEAT_JUMPLIST
6385 case ',':
6386 nv_pcmark(cap);
6387 break;
6388
6389 case ';':
6390 cap->count1 = -cap->count1;
6391 nv_pcmark(cap);
6392 break;
6393#endif
6394
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006395 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006396 if (!checkclearop(oap))
6397 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006398 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006399 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006400 if (!checkclearop(oap))
6401 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006402 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006403
Bram Moolenaar62a23252020-08-09 14:04:42 +02006404 case TAB:
6405 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6406 clearopbeep(oap);
6407 break;
6408
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006409 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006410 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006411 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006412 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006413 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006414 break;
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 default:
6417 clearopbeep(oap);
6418 break;
6419 }
6420}
6421
6422/*
6423 * Handle "o" and "O" commands.
6424 */
6425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006426n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006428#ifdef FEAT_CONCEAL
6429 linenr_T oldline = curwin->w_cursor.lnum;
6430#endif
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (!checkclearopq(cap->oap))
6433 {
6434#ifdef FEAT_FOLDING
6435 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006436 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 (void)hasFolding(curwin->w_cursor.lnum,
6438 &curwin->w_cursor.lnum, NULL);
6439 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006440 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 (void)hasFolding(curwin->w_cursor.lnum,
6442 NULL, &curwin->w_cursor.lnum);
6443#endif
6444 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6445 (cap->cmdchar == 'O' ? 1 : 0)),
6446 (linenr_T)(curwin->w_cursor.lnum +
6447 (cap->cmdchar == 'o' ? 1 : 0))
6448 ) == OK
6449 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006450 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6451 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006453#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006454 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006455 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006456#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006457#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006458 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006460 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006461#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006462 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006463 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6464 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6466 }
6467 }
6468}
6469
6470/*
6471 * "." command: redo last change.
6472 */
6473 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006474nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 if (!checkclearopq(cap->oap))
6477 {
6478 /*
6479 * If "restart_edit" is TRUE, the last but one command is repeated
6480 * instead of the last command (inserting text). This is used for
6481 * CTRL-O <.> in insert mode.
6482 */
6483 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6484 clearopbeep(cap->oap);
6485 }
6486}
6487
6488/*
6489 * CTRL-R: undo undo
6490 */
6491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006492nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 if (!checkclearopq(cap->oap))
6495 {
6496 u_redo((int)cap->count1);
6497 curwin->w_set_curswant = TRUE;
6498 }
6499}
6500
6501/*
6502 * Handle "U" command.
6503 */
6504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006507 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006508 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006510 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 cap->cmdchar = 'g';
6512 cap->nchar = 'U';
6513 nv_operator(cap);
6514 }
6515 else if (!checkclearopq(cap->oap))
6516 {
6517 u_undoline();
6518 curwin->w_set_curswant = TRUE;
6519 }
6520}
6521
6522/*
6523 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6524 * single character.
6525 */
6526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006527nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006529 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006530 {
6531#ifdef FEAT_JOB_CHANNEL
6532 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6533 {
6534 clearopbeep(cap->oap);
6535 return;
6536 }
6537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 else
6541 nv_operator(cap);
6542}
6543
6544/*
6545 * Handle an operator command.
6546 * The actual work is done by do_pending_operator().
6547 */
6548 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006549nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 int op_type;
6552
6553 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006554#ifdef FEAT_JOB_CHANNEL
6555 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6556 {
6557 clearopbeep(cap->oap);
6558 return;
6559 }
6560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006562 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 nv_lineop(cap);
6564 else if (!checkclearop(cap->oap))
6565 {
6566 cap->oap->start = curwin->w_cursor;
6567 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006568#ifdef FEAT_EVAL
6569 set_op_var(op_type);
6570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572}
6573
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006574#ifdef FEAT_EVAL
6575/*
6576 * Set v:operator to the characters for "optype".
6577 */
6578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006579set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006580{
6581 char_u opchars[3];
6582
6583 if (optype == OP_NOP)
6584 set_vim_var_string(VV_OP, NULL, 0);
6585 else
6586 {
6587 opchars[0] = get_op_char(optype);
6588 opchars[1] = get_extra_op_char(optype);
6589 opchars[2] = NUL;
6590 set_vim_var_string(VV_OP, opchars, -1);
6591 }
6592}
6593#endif
6594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595/*
6596 * Handle linewise operator "dd", "yy", etc.
6597 *
6598 * "_" is is a strange motion command that helps make operators more logical.
6599 * It is actually implemented, but not documented in the real Vi. This motion
6600 * command actually refers to "the current line". Commands like "dd" and "yy"
6601 * are really an alternate form of "d_" and "y_". It does accept a count, so
6602 * "d3_" works to delete 3 lines.
6603 */
6604 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006605nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 cap->oap->motion_type = MLINE;
6608 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6609 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006610 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006611 && cap->oap->motion_force != 'v'
6612 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 || cap->oap->op_type == OP_LSHIFT
6614 || cap->oap->op_type == OP_RSHIFT)
6615 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006616 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 beginline(BL_WHITE | BL_FIX);
6618}
6619
6620/*
6621 * <Home> command.
6622 */
6623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006624nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006626 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006627 if (mod_mask & MOD_MASK_CTRL)
6628 nv_goto(cap);
6629 else
6630 {
6631 cap->count0 = 1;
6632 nv_pipe(cap);
6633 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006634 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6635 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636}
6637
6638/*
6639 * "|" command.
6640 */
6641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006642nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 cap->oap->motion_type = MCHAR;
6645 cap->oap->inclusive = FALSE;
6646 beginline(0);
6647 if (cap->count0 > 0)
6648 {
6649 coladvance((colnr_T)(cap->count0 - 1));
6650 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6651 }
6652 else
6653 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006654 // keep curswant at the column where we wanted to go, not where
6655 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 curwin->w_set_curswant = FALSE;
6657}
6658
6659/*
6660 * Handle back-word command "b" and "B".
6661 * cap->arg is 1 for "B"
6662 */
6663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006664nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 cap->oap->motion_type = MCHAR;
6667 cap->oap->inclusive = FALSE;
6668 curwin->w_set_curswant = TRUE;
6669 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6670 clearopbeep(cap->oap);
6671#ifdef FEAT_FOLDING
6672 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6673 foldOpenCursor();
6674#endif
6675}
6676
6677/*
6678 * Handle word motion commands "e", "E", "w" and "W".
6679 * cap->arg is TRUE for "E" and "W".
6680 */
6681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 int n;
6685 int word_end;
6686 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006687 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 /*
6690 * Set inclusive for the "E" and "e" command.
6691 */
6692 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6693 word_end = TRUE;
6694 else
6695 word_end = FALSE;
6696 cap->oap->inclusive = word_end;
6697
6698 /*
6699 * "cw" and "cW" are a special case.
6700 */
6701 if (!word_end && cap->oap->op_type == OP_CHANGE)
6702 {
6703 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006706 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 /*
6709 * Reproduce a funny Vi behaviour: "cw" on a blank only
6710 * changes one character, not all blanks until the start of
6711 * the next word. Only do this when the 'w' flag is included
6712 * in 'cpoptions'.
6713 */
6714 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6715 {
6716 cap->oap->inclusive = TRUE;
6717 cap->oap->motion_type = MCHAR;
6718 return;
6719 }
6720 }
6721 else
6722 {
6723 /*
6724 * This is a little strange. To match what the real Vi does,
6725 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6726 * that we are not on a space or a TAB. This seems impolite
6727 * at first, but it's really more what we mean when we say
6728 * 'cw'.
6729 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006730 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 * will change only one character! This is done by setting
6732 * flag.
6733 */
6734 cap->oap->inclusive = TRUE;
6735 word_end = TRUE;
6736 flag = TRUE;
6737 }
6738 }
6739 }
6740
6741 cap->oap->motion_type = MCHAR;
6742 curwin->w_set_curswant = TRUE;
6743 if (word_end)
6744 n = end_word(cap->count1, cap->arg, flag, FALSE);
6745 else
6746 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006748 // Don't leave the cursor on the NUL past the end of line. Unless we
6749 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006750 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006751 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
6753 if (n == FAIL && cap->oap->op_type == OP_NOP)
6754 clearopbeep(cap->oap);
6755 else
6756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758#ifdef FEAT_FOLDING
6759 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6760 foldOpenCursor();
6761#endif
6762 }
6763}
6764
6765/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006766 * Used after a movement command: If the cursor ends up on the NUL after the
6767 * end of the line, may move it back to the last character and make the motion
6768 * inclusive.
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006772{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006773 // The cursor cannot remain on the NUL when:
6774 // - the column is > 0
6775 // - not in Visual mode or 'selection' is "o"
6776 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006777 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006778 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006779 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006780 {
6781 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006782 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006783 if (has_mbyte)
6784 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006785 oap->inclusive = TRUE;
6786 }
6787}
6788
6789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 * "0" and "^" commands.
6791 * cap->arg is the argument for beginline().
6792 */
6793 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006794nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 cap->oap->motion_type = MCHAR;
6797 cap->oap->inclusive = FALSE;
6798 beginline(cap->arg);
6799#ifdef FEAT_FOLDING
6800 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6801 foldOpenCursor();
6802#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006803 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6804 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805}
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807/*
6808 * In exclusive Visual mode, may include the last character.
6809 */
6810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006811adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006814 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (has_mbyte)
6817 inc_cursor();
6818 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 ++curwin->w_cursor.col;
6820 cap->oap->inclusive = FALSE;
6821 }
6822}
6823
6824/*
6825 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6826 * Should check VIsual_mode before calling this.
6827 * Returns TRUE when backed up to the previous line.
6828 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006830unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 pos_T *pp;
6833
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006834 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006836 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 pp = &curwin->w_cursor;
6838 else
6839 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (pp->coladd > 0)
6841 --pp->coladd;
6842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 if (pp->col > 0)
6844 {
6845 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006846 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848 else if (pp->lnum > 1)
6849 {
6850 --pp->lnum;
6851 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6852 return TRUE;
6853 }
6854 }
6855 return FALSE;
6856}
6857
6858/*
6859 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6860 */
6861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 if (VIsual_active)
6865 VIsual_select = TRUE;
6866 else if (VIsual_reselect)
6867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 cap->arg = TRUE;
6870 nv_g_cmd(cap);
6871 }
6872}
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
6875/*
6876 * "G", "gg", CTRL-END, CTRL-HOME.
6877 * cap->arg is TRUE for "G".
6878 */
6879 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006880nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 linenr_T lnum;
6883
6884 if (cap->arg)
6885 lnum = curbuf->b_ml.ml_line_count;
6886 else
6887 lnum = 1L;
6888 cap->oap->motion_type = MLINE;
6889 setpcmark();
6890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006891 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (cap->count0 != 0)
6893 lnum = cap->count0;
6894 if (lnum < 1L)
6895 lnum = 1L;
6896 else if (lnum > curbuf->b_ml.ml_line_count)
6897 lnum = curbuf->b_ml.ml_line_count;
6898 curwin->w_cursor.lnum = lnum;
6899 beginline(BL_SOL | BL_FIX);
6900#ifdef FEAT_FOLDING
6901 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6902 foldOpenCursor();
6903#endif
6904}
6905
6906/*
6907 * CTRL-\ in Normal mode.
6908 */
6909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006910nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6913 {
6914 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006915 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 restart_edit = 0;
6918#ifdef FEAT_CMDWIN
6919 if (cmdwin_type != 0)
6920 cmdwin_result = Ctrl_C;
6921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (VIsual_active)
6923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006924 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 redraw_curbuf_later(INVERTED);
6926 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 if (cap->nchar == Ctrl_G && p_im)
6929 restart_edit = 'a';
6930 }
6931 else
6932 clearopbeep(cap->oap);
6933}
6934
6935/*
6936 * ESC in Normal mode: beep, but don't flush buffers.
6937 * Don't even beep if we are canceling a command.
6938 */
6939 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006940nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
6942 int no_reason;
6943
6944 no_reason = (cap->oap->op_type == OP_NOP
6945 && cap->opcount == 0
6946 && cap->count0 == 0
6947 && cap->oap->regname == 0
6948 && !p_im);
6949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006950 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 if (restart_edit == 0
6953#ifdef FEAT_CMDWIN
6954 && cmdwin_type == 0
6955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006958 {
6959 if (anyBufIsChanged())
6960 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6961 else
6962 msg(_("Type :qa and press <Enter> to exit Vim"));
6963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006965 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6966 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (!p_im)
6968 restart_edit = 0;
6969#ifdef FEAT_CMDWIN
6970 if (cmdwin_type != 0)
6971 {
6972 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006973 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 return;
6975 }
6976#endif
6977 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006978#ifdef FEAT_CMDWIN
6979 else if (cmdwin_type != 0 && ex_normal_busy)
6980 {
6981 // When :normal runs out of characters while in the command line window
6982 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6983 // loop.
6984 cmdwin_result = K_IGNORE;
6985 return;
6986 }
6987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (VIsual_active)
6990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 end_visual_mode(); // stop Visual
6992 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 curwin->w_set_curswant = TRUE;
6994 redraw_curbuf_later(INVERTED);
6995 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006996 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006997 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 clearop(cap->oap);
6999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7001 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007002 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 restart_edit = 'a';
7004}
7005
7006/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007007 * Move the cursor for the "A" command.
7008 */
7009 void
7010set_cursor_for_append_to_line(void)
7011{
7012 curwin->w_set_curswant = TRUE;
7013 if (ve_flags == VE_ALL)
7014 {
7015 int save_State = State;
7016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 // Pretend Insert mode here to allow the cursor on the
7018 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007019 State = INSERT;
7020 coladvance((colnr_T)MAXCOL);
7021 State = save_State;
7022 }
7023 else
7024 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7025}
7026
7027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007029 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 */
7031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007032nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007034 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7036 cap->cmdchar = 'i';
7037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007040 {
7041#ifdef FEAT_TERMINAL
7042 if (term_in_normal_mode())
7043 {
7044 end_visual_mode();
7045 clearop(cap->oap);
7046 term_enter_job_mode();
7047 return;
7048 }
7049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007053 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007054 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7055 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
7057#ifdef FEAT_TEXTOBJ
7058 nv_object(cap);
7059#else
7060 clearopbeep(cap->oap);
7061#endif
7062 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007063#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007064 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007065 {
7066 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007067 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007068 return;
7069 }
7070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 else if (!curbuf->b_p_ma && !p_im)
7072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007074 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007076 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007078 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007080 else if (cap->cmdchar == K_PS && VIsual_active)
7081 {
7082 pos_T old_pos = curwin->w_cursor;
7083 pos_T old_visual = VIsual;
7084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007085 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007086 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7087 {
7088 shift_delete_registers();
7089 cap->oap->regname = '1';
7090 }
7091 else
7092 cap->oap->regname = '-';
7093 cap->cmdchar = 'd';
7094 cap->nchar = NUL;
7095 nv_operator(cap);
7096 do_pending_operator(cap, 0, FALSE);
7097 cap->cmdchar = K_PS;
7098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 // When the last char in the line was deleted then append. Detect this
7100 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007101 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7102 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007103 inc_cursor();
7104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007106 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 else if (!checkclearopq(cap->oap))
7109 {
7110 switch (cap->cmdchar)
7111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007113 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 break;
7115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007116 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007117 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7118 beginline(BL_WHITE);
7119 else
7120 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 break;
7122
Bram Moolenaara1891842017-02-04 21:34:31 +01007123 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 // Bracketed paste works like "a"ppend, unless the cursor is in
7125 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007126 if (curwin->w_cursor.col == 0)
7127 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007128 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007129
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007130 case 'a': // "a"ppend is like "i"nsert on the next character.
7131 // increment coladd when in virtual space, increment the
7132 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (virtual_active()
7134 && (curwin->w_cursor.coladd > 0
7135 || *ml_get_cursor() == NUL
7136 || *ml_get_cursor() == TAB))
7137 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007138 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 inc_cursor();
7140 break;
7141 }
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7144 {
7145 int save_State = State;
7146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007147 // Pretend Insert mode here to allow the cursor on the
7148 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 State = INSERT;
7150 coladvance(getviscol());
7151 State = save_State;
7152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7155 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007156 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007157 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007158 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159}
7160
7161/*
7162 * Invoke edit() and take care of "restart_edit" and the return value.
7163 */
7164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007165invoke_edit(
7166 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007167 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007168 int cmd,
7169 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 int restart_edit_save = 0;
7172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007173 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7174 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7175 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (repl || !stuff_empty())
7177 restart_edit_save = restart_edit;
7178 else
7179 restart_edit_save = 0;
7180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 restart_edit = 0;
7183
7184 if (edit(cmd, startln, cap->count1))
7185 cap->retval |= CA_COMMAND_BUSY;
7186
7187 if (restart_edit == 0)
7188 restart_edit = restart_edit_save;
7189}
7190
7191#ifdef FEAT_TEXTOBJ
7192/*
7193 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7194 */
7195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007196nv_object(
7197 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 int flag;
7200 int include;
7201 char_u *mps_save;
7202
7203 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007204 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007206 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 mps_save = curbuf->b_p_mps;
7210 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7211
7212 switch (cap->nchar)
7213 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007214 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 flag = current_word(cap->oap, cap->count1, include, FALSE);
7216 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 flag = current_word(cap->oap, cap->count1, include, TRUE);
7219 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 case '(':
7222 case ')':
7223 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7224 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 case '{':
7227 case '}':
7228 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7229 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007230 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 case ']':
7232 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7233 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007234 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 case '>':
7236 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7237 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007238 case 't': // "at" = a tag block (xml and html)
7239 // Do not adjust oap->end in do_pending_operator()
7240 // otherwise there are different results for 'dit'
7241 // (note leading whitespace in last line):
7242 // 1) <b> 2) <b>
7243 // foobar foobar
7244 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007245 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007246 flag = current_tagblock(cap->oap, cap->count1, include);
7247 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007248 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 flag = current_par(cap->oap, cap->count1, include, 'p');
7250 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007251 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 flag = current_sent(cap->oap, cap->count1, include);
7253 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007254 case '"': // "a"" = a double quoted string
7255 case '\'': // "a'" = a single quoted string
7256 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007257 flag = current_quote(cap->oap, cap->count1, include,
7258 cap->nchar);
7259 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007260#if 0 // TODO
7261 case 'S': // "aS" = a section
7262 case 'f': // "af" = a filename
7263 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#endif
7265 default:
7266 flag = FAIL;
7267 break;
7268 }
7269
7270 curbuf->b_p_mps = mps_save;
7271 if (flag == FAIL)
7272 clearopbeep(cap->oap);
7273 adjust_cursor_col();
7274 curwin->w_set_curswant = TRUE;
7275}
7276#endif
7277
7278/*
7279 * "q" command: Start/stop recording.
7280 * "q:", "q/", "q?": edit command-line in command-line window.
7281 */
7282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007283nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 if (cap->oap->op_type == OP_FORMAT)
7286 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007287 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 cap->cmdchar = 'g';
7289 cap->nchar = 'q';
7290 nv_operator(cap);
7291 }
7292 else if (!checkclearop(cap->oap))
7293 {
7294#ifdef FEAT_CMDWIN
7295 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7296 {
7297 stuffcharReadbuff(cap->nchar);
7298 stuffcharReadbuff(K_CMDWIN);
7299 }
7300 else
7301#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 // (stop) recording into a named register, unless executing a
7303 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007304 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 clearopbeep(cap->oap);
7306 }
7307}
7308
7309/*
7310 * Handle the "@r" command.
7311 */
7312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007313nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 if (checkclearop(cap->oap))
7316 return;
7317#ifdef FEAT_EVAL
7318 if (cap->nchar == '=')
7319 {
7320 if (get_expr_register() == NUL)
7321 return;
7322 }
7323#endif
7324 while (cap->count1-- && !got_int)
7325 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007326 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 clearopbeep(cap->oap);
7329 break;
7330 }
7331 line_breakcheck();
7332 }
7333}
7334
7335/*
7336 * Handle the CTRL-U and CTRL-D commands.
7337 */
7338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007339nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7342 || (cap->cmdchar == Ctrl_D
7343 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7344 clearopbeep(cap->oap);
7345 else if (!checkclearop(cap->oap))
7346 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7347}
7348
7349/*
7350 * Handle "J" or "gJ" command.
7351 */
7352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007353nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007357 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
7359 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007360 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7362 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007365 if (cap->count0 <= 2)
7366 {
7367 clearopbeep(cap->oap);
7368 return;
7369 }
7370 cap->count0 = curbuf->b_ml.ml_line_count
7371 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007373
7374 prep_redo(cap->oap->regname, cap->count0,
7375 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7376 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
7378}
7379
7380/*
7381 * "P", "gP", "p" and "gp" commands.
7382 */
7383 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007384nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007386 nv_put_opt(cap, FALSE);
7387}
7388
7389/*
7390 * "P", "gP", "p" and "gp" commands.
7391 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7392 */
7393 static void
7394nv_put_opt(cmdarg_T *cap, int fix_indent)
7395{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int regname = 0;
7397 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007398 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007399 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 int dir;
7401 int flags = 0;
7402
7403 if (cap->oap->op_type != OP_NOP)
7404 {
7405#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007406 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7408 {
7409 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007410 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 }
7412 else
7413#endif
7414 clearopbeep(cap->oap);
7415 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007416#ifdef FEAT_JOB_CHANNEL
7417 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7418 {
7419 clearopbeep(cap->oap);
7420 }
7421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 else
7423 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007424 if (fix_indent)
7425 {
7426 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7427 ? FORWARD : BACKWARD;
7428 flags |= PUT_FIXINDENT;
7429 }
7430 else
7431 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007432 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7433 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 prep_redo_cmd(cap);
7435 if (cap->cmdchar == 'g')
7436 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007437 else if (cap->cmdchar == 'z')
7438 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 if (VIsual_active)
7441 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007442 // Putting in Visual mode: The put text replaces the selected
7443 // text. First delete the selected text, then put the new text.
7444 // Need to save and restore the registers that the delete
7445 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007446 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007448#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007450#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007451 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007452 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007453#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 )
7458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007459 // The delete is going to overwrite the register we want to
7460 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 reg1 = get_register(regname, TRUE);
7462 }
7463
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007464 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 cap->cmdchar = 'd';
7466 cap->nchar = NUL;
7467 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007468 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 nv_operator(cap);
7470 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007471 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007472 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007474 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 cap->oap->regname = regname;
7476
7477 if (reg1 != NULL)
7478 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007479 // Delete probably changed the register we want to put, save
7480 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 reg2 = get_register(regname, FALSE);
7482 put_register(regname, reg1);
7483 }
7484
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007485 // When deleted a linewise Visual area, put the register as
7486 // lines to avoid it joined with the next line. When deletion was
7487 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 if (VIsual_mode == 'V')
7489 flags |= PUT_LINE;
7490 else if (VIsual_mode == 'v')
7491 flags |= PUT_LINE_SPLIT;
7492 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7493 flags |= PUT_LINE_FORWARD;
7494 dir = BACKWARD;
7495 if ((VIsual_mode != 'V'
7496 && curwin->w_cursor.col < curbuf->b_op_start.col)
7497 || (VIsual_mode == 'V'
7498 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007499 // cursor is at the end of the line or end of file, put
7500 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007502 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007503 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007505 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007507 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 if (reg2 != NULL)
7509 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007510
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // What to reselect with "gv"? Selecting the just put text seems to
7512 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007513 if (was_visual)
7514 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007515 curbuf->b_visual.vi_start = curbuf->b_op_start;
7516 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007517 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007518 if (*p_sel == 'e')
7519 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007520 }
7521
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007522 // When all lines were selected and deleted do_put() leaves an empty
7523 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007524 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007525 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007526 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007527 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007528
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007529 // If the cursor was in that line, move it to the end of the last
7530 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007531 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7532 {
7533 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7534 coladvance((colnr_T)MAXCOL);
7535 }
7536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 auto_format(FALSE, TRUE);
7538 }
7539}
7540
7541/*
7542 * "o" and "O" commands.
7543 */
7544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007545nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007548 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7550 {
7551 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007552 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 }
7554 else
7555#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007556 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007558#ifdef FEAT_JOB_CHANNEL
7559 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007560 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 n_opencmd(cap);
7564}
7565
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566#ifdef FEAT_NETBEANS_INTG
7567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007568nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 netbeans_keycommand(cap->nchar);
7571}
7572#endif
7573
7574#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007576nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007578 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579}
7580#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007581
Bram Moolenaar3918c952005-03-15 22:34:55 +00007582/*
7583 * Trigger CursorHold event.
7584 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7585 * input buffer. "did_cursorhold" is set to avoid retriggering.
7586 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007588nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007589{
7590 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7591 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007592 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007593}